2023-08-14 15:11:53 +03:00
|
|
|
import {ActionsResponseType} from '../../src/api/actions';
|
|
|
|
import {ConfigResponseType} from '../../src/api/config';
|
|
|
|
import {CustomThemeSettingsResponseType} from '../../src/api/customThemeSettings';
|
|
|
|
import {InvitesResponseType} from '../../src/api/invites';
|
|
|
|
import {LabelsResponseType} from '../../src/api/labels';
|
|
|
|
import {NewslettersResponseType} from '../../src/api/newsletters';
|
|
|
|
import {OffersResponseType} from '../../src/api/offers';
|
2023-08-03 11:29:14 +03:00
|
|
|
import {Page} from '@playwright/test';
|
2023-08-14 15:11:53 +03:00
|
|
|
import {RolesResponseType} from '../../src/api/roles';
|
|
|
|
import {SettingsResponseType} from '../../src/api/settings';
|
|
|
|
import {SiteResponseType} from '../../src/api/site';
|
|
|
|
import {ThemesResponseType} from '../../src/api/themes';
|
|
|
|
import {TiersResponseType} from '../../src/api/tiers';
|
|
|
|
import {UsersResponseType} from '../../src/api/users';
|
2023-08-02 10:37:51 +03:00
|
|
|
import {readFileSync} from 'fs';
|
2023-06-06 02:29:28 +03:00
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
interface MockRequestConfig {
|
|
|
|
method: string;
|
|
|
|
path: string | RegExp;
|
|
|
|
response: unknown;
|
|
|
|
responseStatus?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface RequestRecord {
|
|
|
|
url?: string
|
|
|
|
body?: object | null
|
|
|
|
headers?: {[key: string]: string}
|
|
|
|
}
|
|
|
|
|
|
|
|
const siteFixture = JSON.parse(readFileSync(`${__dirname}/responses/site.json`).toString()) as SiteResponseType;
|
|
|
|
|
2023-06-26 06:12:46 +03:00
|
|
|
export const responseFixtures = {
|
|
|
|
settings: JSON.parse(readFileSync(`${__dirname}/responses/settings.json`).toString()) as SettingsResponseType,
|
2023-07-04 10:17:42 +03:00
|
|
|
config: JSON.parse(readFileSync(`${__dirname}/responses/config.json`).toString()) as ConfigResponseType,
|
2023-06-26 06:12:46 +03:00
|
|
|
users: JSON.parse(readFileSync(`${__dirname}/responses/users.json`).toString()) as UsersResponseType,
|
|
|
|
me: JSON.parse(readFileSync(`${__dirname}/responses/me.json`).toString()) as UsersResponseType,
|
|
|
|
roles: JSON.parse(readFileSync(`${__dirname}/responses/roles.json`).toString()) as RolesResponseType,
|
2023-08-03 11:29:14 +03:00
|
|
|
site: siteFixture,
|
2023-06-26 06:12:46 +03:00
|
|
|
invites: JSON.parse(readFileSync(`${__dirname}/responses/invites.json`).toString()) as InvitesResponseType,
|
2023-06-28 05:59:05 +03:00
|
|
|
customThemeSettings: JSON.parse(readFileSync(`${__dirname}/responses/custom_theme_settings.json`).toString()) as CustomThemeSettingsResponseType,
|
|
|
|
tiers: JSON.parse(readFileSync(`${__dirname}/responses/tiers.json`).toString()) as TiersResponseType,
|
|
|
|
labels: JSON.parse(readFileSync(`${__dirname}/responses/labels.json`).toString()) as LabelsResponseType,
|
|
|
|
offers: JSON.parse(readFileSync(`${__dirname}/responses/offers.json`).toString()) as OffersResponseType,
|
2023-08-03 11:29:14 +03:00
|
|
|
themes: JSON.parse(readFileSync(`${__dirname}/responses/themes.json`).toString()) as ThemesResponseType,
|
2023-08-07 14:44:30 +03:00
|
|
|
newsletters: JSON.parse(readFileSync(`${__dirname}/responses/newsletters.json`).toString()) as NewslettersResponseType,
|
2023-08-14 15:11:53 +03:00
|
|
|
actions: JSON.parse(readFileSync(`${__dirname}/responses/actions.json`).toString()) as ActionsResponseType,
|
2023-08-03 11:29:14 +03:00
|
|
|
latestPost: {posts: [{id: '1', url: `${siteFixture.site.url}/test-post/`}]}
|
2023-06-06 02:29:28 +03:00
|
|
|
};
|
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
export const globalDataRequests = {
|
|
|
|
browseSettings: {method: 'GET', path: /^\/settings\/\?group=/, response: responseFixtures.settings},
|
|
|
|
browseConfig: {method: 'GET', path: '/config/', response: responseFixtures.config},
|
|
|
|
browseSite: {method: 'GET', path: '/site/', response: responseFixtures.site},
|
2023-09-13 10:10:33 +03:00
|
|
|
browseMe: {method: 'GET', path: '/users/me/?include=roles', response: responseFixtures.me}
|
2023-06-23 11:16:01 +03:00
|
|
|
};
|
|
|
|
|
2023-08-24 00:04:27 +03:00
|
|
|
export const limitRequests = {
|
|
|
|
browseUsers: {method: 'GET', path: '/users/?limit=all&include=roles', response: responseFixtures.users},
|
|
|
|
browseInvites: {method: 'GET', path: '/invites/', response: responseFixtures.invites},
|
|
|
|
browseRoles: {method: 'GET', path: '/roles/?limit=all', response: responseFixtures.roles},
|
|
|
|
browseNewslettersLimit: {method: 'GET', path: '/newsletters/?filter=status%3Aactive&limit=all', response: responseFixtures.newsletters}
|
|
|
|
};
|
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
export async function mockApi<Requests extends Record<string, MockRequestConfig>>({page, requests}: {page: Page, requests: Requests}) {
|
|
|
|
const lastApiRequests: {[key in keyof Requests]?: RequestRecord} = {};
|
2023-06-28 05:59:05 +03:00
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
const namedRequests = Object.entries(requests).reduce(
|
|
|
|
(array, [key, value]) => array.concat({name: key, ...value}),
|
|
|
|
[] as Array<MockRequestConfig & {name: keyof Requests}>
|
|
|
|
);
|
2023-06-28 05:59:05 +03:00
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
await page.route(/\/ghost\/api\/admin\//, async (route) => {
|
|
|
|
const apiPath = route.request().url().replace(/^.*\/ghost\/api\/admin/, '');
|
2023-06-28 05:59:05 +03:00
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
const matchingMock = namedRequests.find((request) => {
|
|
|
|
if (request.method !== route.request().method()) {
|
|
|
|
return false;
|
2023-06-28 05:59:05 +03:00
|
|
|
}
|
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
if (typeof request.path === 'string') {
|
|
|
|
return request.path === apiPath;
|
2023-06-28 05:59:05 +03:00
|
|
|
}
|
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
return request.path.test(apiPath);
|
|
|
|
});
|
2023-07-14 16:04:34 +03:00
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
if (!matchingMock) {
|
|
|
|
return route.fulfill({
|
|
|
|
status: 418,
|
|
|
|
body: [
|
|
|
|
'No matching mock found. If this request is needed for the test, add it to your mockApi call',
|
|
|
|
'',
|
|
|
|
'Currently mocked:',
|
|
|
|
...namedRequests.map(({method, path}) => `${method} ${path}`)
|
|
|
|
].join('\n')
|
|
|
|
});
|
2023-07-14 16:04:34 +03:00
|
|
|
}
|
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
const requestBody = JSON.parse(route.request().postData() || 'null');
|
2023-06-28 05:59:05 +03:00
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
lastApiRequests[matchingMock.name] = {
|
|
|
|
body: requestBody,
|
|
|
|
url: route.request().url(),
|
|
|
|
headers: route.request().headers()
|
|
|
|
};
|
2023-06-28 05:59:05 +03:00
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
await route.fulfill({
|
|
|
|
status: matchingMock.responseStatus || 200,
|
|
|
|
body: typeof matchingMock.response === 'string' ? matchingMock.response : JSON.stringify(matchingMock.response)
|
|
|
|
});
|
2023-06-06 06:50:07 +03:00
|
|
|
});
|
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
return {lastApiRequests};
|
|
|
|
}
|
2023-06-12 02:04:19 +03:00
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
export function updatedSettingsResponse(newSettings: Array<{ key: string, value: string | boolean | null }>) {
|
|
|
|
return {
|
|
|
|
...responseFixtures.settings,
|
|
|
|
settings: responseFixtures.settings.settings.map((setting) => {
|
|
|
|
const newSetting = newSettings.find(({key}) => key === setting.key);
|
2023-06-28 05:59:05 +03:00
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
return {key: setting.key, value: newSetting?.value || setting.value};
|
|
|
|
})
|
|
|
|
};
|
2023-06-06 02:29:28 +03:00
|
|
|
}
|
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
export async function mockSitePreview({page, url, response}: {page: Page, url: string, response: string}) {
|
|
|
|
let lastRequest: {previewHeader?: string} = {};
|
2023-06-12 02:04:19 +03:00
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
await page.route(url, async (route) => {
|
|
|
|
if (route.request().method() !== 'POST') {
|
|
|
|
return route.continue();
|
|
|
|
}
|
2023-06-12 02:04:19 +03:00
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
if (!route.request().headers()['x-ghost-preview']) {
|
2023-06-06 02:29:28 +03:00
|
|
|
return route.continue();
|
|
|
|
}
|
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
lastRequest.previewHeader = route.request().headers()['x-ghost-preview'];
|
2023-06-06 02:29:28 +03:00
|
|
|
|
|
|
|
await route.fulfill({
|
2023-08-03 11:29:14 +03:00
|
|
|
status: 200,
|
|
|
|
body: response
|
2023-06-06 02:29:28 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-08-03 11:29:14 +03:00
|
|
|
return lastRequest;
|
2023-06-06 02:29:28 +03:00
|
|
|
}
|