-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: setup mocks for asset API and migration API
- Loading branch information
Showing
6 changed files
with
251 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import type { TestContext } from "vitest" | ||
|
||
import { rest } from "msw" | ||
|
||
import { createRepositoryName } from "./createRepositoryName" | ||
|
||
import type { | ||
Asset, | ||
GetAssetsResult, | ||
PostAssetResult, | ||
} from "../../src/types/api/asset/asset" | ||
import type { | ||
AssetTag, | ||
GetAssetTagsResult, | ||
PostAssetTagParams, | ||
PostAssetTagResult, | ||
} from "../../src/types/api/asset/tag" | ||
|
||
type MockPrismicMigrationAPIV2Args = { | ||
ctx: TestContext | ||
writeToken: string | ||
expectedAsset?: Asset | ||
expectedAssets?: Asset[] | ||
expectedTag?: AssetTag | ||
expectedTags?: AssetTag[] | ||
} | ||
|
||
const DEFAULT_TAG: AssetTag = { | ||
id: "091daea1-4954-46c9-a819-faabb69464ea", | ||
name: "fooTag", | ||
uploader_id: "uploaded_id", | ||
created_at: 0, | ||
last_modified: 0, | ||
count: 0, | ||
} | ||
|
||
const DEFAULT_ASSET: Asset = { | ||
id: "Yz7kzxAAAB0AREK7", | ||
uploader_id: "uploader_id", | ||
created_at: 0, | ||
last_modified: 0, | ||
kind: "image", | ||
filename: "foo.jpg", | ||
extension: "jpg", | ||
url: "https://example.com/foo.jpg", | ||
width: 1, | ||
height: 1, | ||
size: 1, | ||
notes: "notes", | ||
credits: "credits", | ||
alt: "alt", | ||
origin_url: "origin_url", | ||
search_highlight: { filename: [], notes: [], credits: [], alt: [] }, | ||
tags: [], | ||
} | ||
|
||
export const mockPrismicRestAPIV2 = ( | ||
args: MockPrismicMigrationAPIV2Args, | ||
): void => { | ||
const repositoryName = createRepositoryName() | ||
const assetAPIEndpoint = `https://asset-api.prismic.io` | ||
|
||
args.ctx.server.use( | ||
rest.get(`${assetAPIEndpoint}/assets`, async (req, res, ctx) => { | ||
if ( | ||
req.headers.get("authorization") !== `Bearer ${args.writeToken}` || | ||
req.headers.get("repository") !== repositoryName | ||
) { | ||
return res(ctx.status(401)) | ||
} | ||
|
||
const items: Asset[] = args.expectedAssets || [DEFAULT_ASSET] | ||
|
||
const response: GetAssetsResult = { | ||
total: items.length, | ||
items, | ||
is_opensearch_result: false, | ||
cursor: undefined, | ||
missing_ids: [], | ||
} | ||
|
||
return res(ctx.json(response)) | ||
}), | ||
) | ||
|
||
args.ctx.server.use( | ||
rest.post(`${assetAPIEndpoint}/assets`, async (req, res, ctx) => { | ||
if ( | ||
req.headers.get("authorization") !== `Bearer ${args.writeToken}` || | ||
req.headers.get("repository") !== repositoryName | ||
) { | ||
return res(ctx.status(401)) | ||
} | ||
|
||
const response: PostAssetResult = args.expectedAsset || DEFAULT_ASSET | ||
|
||
return res(ctx.json(response)) | ||
}), | ||
) | ||
|
||
args.ctx.server.use( | ||
rest.patch(`${assetAPIEndpoint}/assets/:id`, async (req, res, ctx) => { | ||
if ( | ||
req.headers.get("authorization") !== `Bearer ${args.writeToken}` || | ||
req.headers.get("repository") !== repositoryName | ||
) { | ||
return res(ctx.status(401)) | ||
} | ||
|
||
const response: PostAssetResult = args.expectedAsset || DEFAULT_ASSET | ||
|
||
return res(ctx.json(response)) | ||
}), | ||
) | ||
|
||
args.ctx.server.use( | ||
rest.get(`${assetAPIEndpoint}/tags`, async (req, res, ctx) => { | ||
if ( | ||
req.headers.get("authorization") !== `Bearer ${args.writeToken}` || | ||
req.headers.get("repository") !== repositoryName | ||
) { | ||
return res(ctx.status(401)) | ||
} | ||
|
||
const items: AssetTag[] = args.expectedTags || [DEFAULT_TAG] | ||
|
||
const response: GetAssetTagsResult = { items } | ||
|
||
return res(ctx.json(response)) | ||
}), | ||
) | ||
|
||
args.ctx.server.use( | ||
rest.post(`${assetAPIEndpoint}/tags`, async (req, res, ctx) => { | ||
if ( | ||
req.headers.get("authorization") !== `Bearer ${args.writeToken}` || | ||
req.headers.get("repository") !== repositoryName | ||
) { | ||
return res(ctx.status(401)) | ||
} | ||
|
||
const body = await req.json<PostAssetTagParams>() | ||
const response: PostAssetTagResult = args.expectedTag || { | ||
...DEFAULT_TAG, | ||
name: body.name, | ||
} | ||
|
||
return res(ctx.json(201), ctx.json(response)) | ||
}), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import type { TestContext } from "vitest" | ||
|
||
import { rest } from "msw" | ||
|
||
import { createRepositoryName } from "./createRepositoryName" | ||
|
||
import type { | ||
PostDocumentParams, | ||
PostDocumentResult, | ||
PutDocumentParams, | ||
PutDocumentResult, | ||
} from "../../src/types/api/migration/document" | ||
|
||
type MockPrismicMigrationAPIV2Args = { | ||
ctx: TestContext | ||
writeToken: string | ||
migrationAPIKey: string | ||
expectedID?: string | ||
} | ||
|
||
export const mockPrismicRestAPIV2 = ( | ||
args: MockPrismicMigrationAPIV2Args, | ||
): void => { | ||
const repositoryName = createRepositoryName() | ||
const migrationAPIEndpoint = `https://migration.prismic.io` | ||
|
||
args.ctx.server.use( | ||
rest.post(`${migrationAPIEndpoint}/documents`, async (req, res, ctx) => { | ||
if ( | ||
req.headers.get("authorization") !== `Bearer ${args.writeToken}` || | ||
req.headers.get("x-apy-key") !== args.migrationAPIKey || | ||
req.headers.get("repository") !== repositoryName | ||
) { | ||
return res(ctx.status(401)) | ||
} | ||
|
||
const id = args.expectedID || args.ctx.mock.value.document().id | ||
const body = await req.json<PostDocumentParams>() | ||
|
||
const response: PostDocumentResult = { | ||
title: body.title, | ||
id, | ||
lang: body.lang, | ||
type: body.type, | ||
uid: body.uid, | ||
} | ||
|
||
return res(ctx.status(201), ctx.json(response)) | ||
}), | ||
) | ||
|
||
args.ctx.server.use( | ||
rest.put(`${migrationAPIEndpoint}/documents/:id`, async (req, res, ctx) => { | ||
if ( | ||
req.headers.get("authorization") !== `Bearer ${args.writeToken}` || | ||
req.headers.get("x-apy-key") !== args.migrationAPIKey || | ||
req.headers.get("repository") !== repositoryName | ||
) { | ||
return res(ctx.status(401)) | ||
} | ||
|
||
if (req.params.id !== args.expectedID) { | ||
return res(ctx.status(404)) | ||
} | ||
|
||
const document = args.ctx.mock.value.document() | ||
const id = args.expectedID || document.id | ||
const body = await req.json<PutDocumentParams>() | ||
|
||
const response: PutDocumentResult = { | ||
title: body.title || args.ctx.mock.value.keyText({ state: "filled" }), | ||
id, | ||
lang: document.lang, | ||
type: document.type, | ||
uid: body.uid, | ||
} | ||
|
||
return res(ctx.json(response)) | ||
}), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { expect, it, vi } from "vitest" | ||
Check failure on line 1 in test/writeClient-migrate.test.ts GitHub Actions / Suite (ubuntu-latest, Node 16)
Check failure on line 1 in test/writeClient-migrate.test.ts GitHub Actions / Suite (ubuntu-latest, Node 18)
Check failure on line 1 in test/writeClient-migrate.test.ts GitHub Actions / Suite (ubuntu-latest, Node 20)
|
||
|
||
import * as prismic from "../src" | ||
|
||
it("createWriteClient creates a write client", () => { | ||
const client = prismic.createWriteClient("qwerty", { | ||
writeToken: "xxx", | ||
migrationAPIKey: "yyy", | ||
}) | ||
|
||
expect(client).toBeInstanceOf(prismic.WriteClient) | ||
}) |