2023-06-06 02:29:28 +03:00
|
|
|
import {Page} from '@playwright/test';
|
|
|
|
import {readFileSync} from 'fs';
|
|
|
|
|
|
|
|
type LastApiRequest = {
|
|
|
|
url: null | string
|
|
|
|
body: null | any
|
|
|
|
};
|
|
|
|
|
|
|
|
const responseFixtures = {
|
|
|
|
settings: JSON.parse(readFileSync(`${__dirname}/responses/settings.json`).toString()),
|
2023-06-12 02:04:19 +03:00
|
|
|
site: JSON.parse(readFileSync(`${__dirname}/responses/site.json`).toString()),
|
|
|
|
custom_theme_settings: JSON.parse(readFileSync(`${__dirname}/responses/custom_theme_settings.json`).toString())
|
2023-06-06 02:29:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
interface Responses {
|
|
|
|
settings?: {
|
|
|
|
browse?: any
|
|
|
|
edit?: any
|
|
|
|
}
|
|
|
|
site?: {
|
|
|
|
browse?: any
|
|
|
|
}
|
2023-06-06 06:50:07 +03:00
|
|
|
images?: {
|
|
|
|
upload?: any
|
|
|
|
}
|
2023-06-12 02:04:19 +03:00
|
|
|
custom_theme_settings?: {
|
|
|
|
browse?: any
|
|
|
|
edit?: any
|
|
|
|
}
|
|
|
|
previewHtml: {
|
|
|
|
homepage?: string
|
|
|
|
}
|
2023-06-06 02:29:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function mockApi({page,responses}: {page: Page, responses?: Responses}) {
|
|
|
|
const lastApiRequest: LastApiRequest = {
|
|
|
|
url: null,
|
|
|
|
body: null
|
|
|
|
};
|
|
|
|
|
|
|
|
await mockApiResponse({
|
|
|
|
page,
|
2023-06-12 02:04:19 +03:00
|
|
|
path: /\/ghost\/api\/admin\/settings\//,
|
|
|
|
responses: {
|
|
|
|
GET: {body: responses?.settings?.browse ?? responseFixtures.settings},
|
|
|
|
PUT: {body: responses?.settings?.edit ?? responseFixtures.settings}
|
|
|
|
},
|
2023-06-06 02:29:28 +03:00
|
|
|
lastApiRequest
|
|
|
|
});
|
|
|
|
|
|
|
|
await mockApiResponse({
|
|
|
|
page,
|
|
|
|
path: /\/ghost\/api\/admin\/site\//,
|
2023-06-12 02:04:19 +03:00
|
|
|
responses: {
|
|
|
|
GET: {body: responses?.site?.browse ?? responseFixtures.site}
|
|
|
|
},
|
2023-06-06 02:29:28 +03:00
|
|
|
lastApiRequest
|
|
|
|
});
|
|
|
|
|
|
|
|
await mockApiResponse({
|
|
|
|
page,
|
2023-06-12 02:04:19 +03:00
|
|
|
path: /\/ghost\/api\/admin\/images\/upload\/$/,
|
|
|
|
responses: {
|
|
|
|
POST: {body: responses?.images?.upload ?? {images: [{url: 'http://example.com/image.png', ref: null}]}}
|
|
|
|
},
|
2023-06-06 02:29:28 +03:00
|
|
|
lastApiRequest
|
|
|
|
});
|
|
|
|
|
2023-06-06 06:50:07 +03:00
|
|
|
await mockApiResponse({
|
|
|
|
page,
|
2023-06-12 02:04:19 +03:00
|
|
|
path: /\/ghost\/api\/admin\/custom_theme_settings\/$/,
|
|
|
|
responses: {
|
|
|
|
GET: {body: responses?.custom_theme_settings?.browse ?? responseFixtures.custom_theme_settings},
|
|
|
|
PUT: {body: responses?.custom_theme_settings?.edit ?? responseFixtures.custom_theme_settings}
|
|
|
|
},
|
2023-06-06 06:50:07 +03:00
|
|
|
lastApiRequest
|
|
|
|
});
|
|
|
|
|
2023-06-12 02:04:19 +03:00
|
|
|
await page.route(responseFixtures.site.site.url, async (route) => {
|
|
|
|
if (!route.request().headers()['x-ghost-preview']) {
|
|
|
|
return route.continue();
|
|
|
|
}
|
|
|
|
|
|
|
|
await route.fulfill({
|
|
|
|
status: 200,
|
|
|
|
body: responses?.previewHtml?.homepage ?? '<html><head><style></style></head><body><div>test</div></body></html>'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-06-06 02:29:28 +03:00
|
|
|
return lastApiRequest;
|
|
|
|
}
|
|
|
|
|
2023-06-12 02:04:19 +03:00
|
|
|
interface MockResponse {
|
|
|
|
body: any
|
|
|
|
status?: number
|
|
|
|
}
|
|
|
|
|
|
|
|
async function mockApiResponse({page, path, lastApiRequest, responses}: { page: Page; path: string | RegExp; lastApiRequest: LastApiRequest, responses: { [method: string]: MockResponse } }) {
|
2023-06-06 02:29:28 +03:00
|
|
|
await page.route(path, async (route) => {
|
2023-06-12 02:04:19 +03:00
|
|
|
const response = responses[route.request().method()];
|
|
|
|
|
|
|
|
if (!response) {
|
2023-06-06 02:29:28 +03:00
|
|
|
return route.continue();
|
|
|
|
}
|
|
|
|
|
|
|
|
const requestBody = JSON.parse(route.request().postData() || 'null');
|
|
|
|
lastApiRequest.body = requestBody;
|
|
|
|
lastApiRequest.url = route.request().url();
|
|
|
|
|
|
|
|
await route.fulfill({
|
2023-06-12 02:04:19 +03:00
|
|
|
status: response.status || 200,
|
|
|
|
body: JSON.stringify(response.body)
|
2023-06-06 02:29:28 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-06 06:50:07 +03:00
|
|
|
export function updatedSettingsResponse(newSettings: Array<{ key: string, value: string | boolean | null }>) {
|
2023-06-06 02:29:28 +03:00
|
|
|
return {
|
|
|
|
...responseFixtures.settings,
|
|
|
|
settings: responseFixtures.settings.settings.map((setting) => {
|
|
|
|
const newSetting = newSettings.find(({key}) => key === setting.key);
|
|
|
|
|
|
|
|
return {key: setting.key, value: newSetting?.value || setting.value};
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|