@prismicio/e2e-tests-utils 1.1.0 → 1.1.2-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/clients/authenticationApi.cjs +18 -1
- package/dist/clients/authenticationApi.cjs.map +1 -1
- package/dist/clients/authenticationApi.d.ts +3 -2
- package/dist/clients/authenticationApi.js +19 -2
- package/dist/clients/authenticationApi.js.map +1 -1
- package/dist/clients/manageV2.cjs +145 -0
- package/dist/clients/manageV2.cjs.map +1 -0
- package/dist/clients/manageV2.d.ts +91 -0
- package/dist/clients/manageV2.js +145 -0
- package/dist/clients/manageV2.js.map +1 -0
- package/dist/clients/wroom.cjs +26 -0
- package/dist/clients/wroom.cjs.map +1 -1
- package/dist/clients/wroom.d.ts +22 -2
- package/dist/clients/wroom.js +26 -0
- package/dist/clients/wroom.js.map +1 -1
- package/dist/managers/repositories.cjs +20 -17
- package/dist/managers/repositories.cjs.map +1 -1
- package/dist/managers/repositories.d.ts +12 -22
- package/dist/managers/repositories.js +20 -17
- package/dist/managers/repositories.js.map +1 -1
- package/dist/managers/repository.cjs +106 -1
- package/dist/managers/repository.cjs.map +1 -1
- package/dist/managers/repository.d.ts +48 -1
- package/dist/managers/repository.js +106 -1
- package/dist/managers/repository.js.map +1 -1
- package/dist/types.d.ts +18 -1
- package/package.json +3 -1
- package/src/clients/authenticationApi.ts +30 -3
- package/src/clients/manageV2.ts +178 -0
- package/src/clients/wroom.ts +40 -2
- package/src/managers/repositories.ts +39 -37
- package/src/managers/repository.ts +146 -0
- package/src/types.ts +27 -1
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import { CustomType, SharedSlice } from "@prismicio/types-internal/lib/customtypes";
|
|
2
|
+
import type { AuthenticationClient } from "../clients/authenticationApi";
|
|
2
3
|
import type { CoreClient } from "../clients/coreApi";
|
|
3
4
|
import { CustomTypesClient } from "../clients/customTypesApi";
|
|
5
|
+
import { ManageV2Client } from "../clients/manageV2";
|
|
4
6
|
import { WroomClient } from "../clients/wroom";
|
|
5
7
|
/** Utility to manage test data for a Prismic repository */
|
|
6
8
|
export declare class RepositoryManager {
|
|
7
9
|
readonly name: string;
|
|
8
10
|
private readonly coreApiClient;
|
|
11
|
+
private readonly authApiClient;
|
|
9
12
|
private readonly wroomClient;
|
|
10
13
|
private readonly customTypesApiClient;
|
|
11
|
-
|
|
14
|
+
private readonly manageV2Client;
|
|
15
|
+
constructor(name: string, coreApiClient: CoreClient, authApiClient: AuthenticationClient, wroomClient: WroomClient, customTypesApiClient: CustomTypesClient, manageV2Client: ManageV2Client);
|
|
12
16
|
configure(config: RepositoryConfig): Promise<void>;
|
|
13
17
|
/** @returns the repository base url, like https://my-repo.prismic.io */
|
|
14
18
|
getBaseURL(): string;
|
|
@@ -95,6 +99,48 @@ export declare class RepositoryManager {
|
|
|
95
99
|
* @param appName - mandatory authorized application name
|
|
96
100
|
*/
|
|
97
101
|
createPermanentAccessToken(appName: string): Promise<string>;
|
|
102
|
+
/** Create a machine2machine token */
|
|
103
|
+
createMachine2MachineToken(): Promise<string>;
|
|
104
|
+
/**
|
|
105
|
+
* Toggle the Role per local feature
|
|
106
|
+
*
|
|
107
|
+
* @param enabled - The feature new status
|
|
108
|
+
*/
|
|
109
|
+
toggleRolePerLocal(enabled: boolean): Promise<void>;
|
|
110
|
+
/**
|
|
111
|
+
* Change the Repository Plan
|
|
112
|
+
*
|
|
113
|
+
* @param {string} newPlanId - The Id of the new Plan to apply
|
|
114
|
+
*/
|
|
115
|
+
changePlan(newPlanId: string): Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
* Add a new user to the repository
|
|
118
|
+
*
|
|
119
|
+
* @param {string} email - The email of the user
|
|
120
|
+
*/
|
|
121
|
+
addUser(email: string): Promise<void>;
|
|
122
|
+
/**
|
|
123
|
+
* Remove a user from the repository
|
|
124
|
+
*
|
|
125
|
+
* @param {string} email - The email of the user
|
|
126
|
+
*/
|
|
127
|
+
removeUser(email: string): Promise<void>;
|
|
128
|
+
/**
|
|
129
|
+
* Update the role of a user in a repository
|
|
130
|
+
*
|
|
131
|
+
* @param {string} email - The email of the user
|
|
132
|
+
* @param {string | Record<string, string>} role - The new Role of the user,
|
|
133
|
+
* either a string for basic workflow or an object such as `{ "lang_id":
|
|
134
|
+
* "Writer" }`
|
|
135
|
+
*
|
|
136
|
+
* Example of roles are
|
|
137
|
+
*
|
|
138
|
+
* - Administrator
|
|
139
|
+
* - Writer
|
|
140
|
+
* - Contributor
|
|
141
|
+
* - Manager (publisher)
|
|
142
|
+
*/
|
|
143
|
+
updateUserRole(email: string, role: string | Record<string, string>): Promise<void>;
|
|
98
144
|
}
|
|
99
145
|
export type RepositoryConfig = {
|
|
100
146
|
customLocale?: CustomLocale;
|
|
@@ -103,6 +149,7 @@ export type RepositoryConfig = {
|
|
|
103
149
|
slices?: SharedSlice[];
|
|
104
150
|
customTypes?: CustomType[];
|
|
105
151
|
preview?: Preview;
|
|
152
|
+
rolePerLocal?: boolean;
|
|
106
153
|
};
|
|
107
154
|
export type Preview = {
|
|
108
155
|
name: string;
|
|
@@ -7,15 +7,19 @@ var __publicField = (obj, key, value) => {
|
|
|
7
7
|
import { logger, logHttpResponse } from "../utils/log.js";
|
|
8
8
|
import { getRepositoryUrl } from "../utils/urls.js";
|
|
9
9
|
class RepositoryManager {
|
|
10
|
-
constructor(name, coreApiClient, wroomClient, customTypesApiClient) {
|
|
10
|
+
constructor(name, coreApiClient, authApiClient, wroomClient, customTypesApiClient, manageV2Client) {
|
|
11
11
|
__publicField(this, "name");
|
|
12
12
|
__publicField(this, "coreApiClient");
|
|
13
|
+
__publicField(this, "authApiClient");
|
|
13
14
|
__publicField(this, "wroomClient");
|
|
14
15
|
__publicField(this, "customTypesApiClient");
|
|
16
|
+
__publicField(this, "manageV2Client");
|
|
15
17
|
this.name = name;
|
|
16
18
|
this.coreApiClient = coreApiClient;
|
|
19
|
+
this.authApiClient = authApiClient;
|
|
17
20
|
this.wroomClient = wroomClient;
|
|
18
21
|
this.customTypesApiClient = customTypesApiClient;
|
|
22
|
+
this.manageV2Client = manageV2Client;
|
|
19
23
|
}
|
|
20
24
|
async configure(config) {
|
|
21
25
|
const hasLangConfig = config.defaultLocale || config.locales || config.customLocale;
|
|
@@ -53,6 +57,9 @@ class RepositoryManager {
|
|
|
53
57
|
if (config.preview) {
|
|
54
58
|
await this.createPreview(config.preview);
|
|
55
59
|
}
|
|
60
|
+
if (config.rolePerLocal) {
|
|
61
|
+
await this.toggleRolePerLocal(true);
|
|
62
|
+
}
|
|
56
63
|
}
|
|
57
64
|
/** @returns the repository base url, like https://my-repo.prismic.io */
|
|
58
65
|
getBaseURL() {
|
|
@@ -228,6 +235,104 @@ class RepositoryManager {
|
|
|
228
235
|
});
|
|
229
236
|
return response.data.wroom_auths[0].token;
|
|
230
237
|
}
|
|
238
|
+
/** Create a machine2machine token */
|
|
239
|
+
async createMachine2MachineToken() {
|
|
240
|
+
const profiler = logger.startTimer();
|
|
241
|
+
const token = await this.authApiClient.getMachine2MachineToken(this.name);
|
|
242
|
+
profiler.done({
|
|
243
|
+
message: `machine2machine token created`
|
|
244
|
+
});
|
|
245
|
+
return token;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Toggle the Role per local feature
|
|
249
|
+
*
|
|
250
|
+
* @param enabled - The feature new status
|
|
251
|
+
*/
|
|
252
|
+
async toggleRolePerLocal(enabled) {
|
|
253
|
+
const profiler = logger.startTimer();
|
|
254
|
+
const response = await this.manageV2Client.toggleRolePerLocal({
|
|
255
|
+
repository: this.name,
|
|
256
|
+
enabled
|
|
257
|
+
});
|
|
258
|
+
this.failIfNot200(response, `Could not ${enabled ? "enable" : "disable"} Role per local for ${this.name}`);
|
|
259
|
+
profiler.done({
|
|
260
|
+
message: `Role per local ${enabled ? "enabled" : "disabled"}`
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Change the Repository Plan
|
|
265
|
+
*
|
|
266
|
+
* @param {string} newPlanId - The Id of the new Plan to apply
|
|
267
|
+
*/
|
|
268
|
+
async changePlan(newPlanId) {
|
|
269
|
+
const profiler = logger.startTimer();
|
|
270
|
+
const response = await this.manageV2Client.changePlan({
|
|
271
|
+
repository: this.name,
|
|
272
|
+
newPlanId,
|
|
273
|
+
bypassManualBilling: true
|
|
274
|
+
// For now the library does not support Stripe features
|
|
275
|
+
});
|
|
276
|
+
this.failIfNot200(response, "Could not change the Repository Plan");
|
|
277
|
+
profiler.done({
|
|
278
|
+
message: `Repository Plan changed to ${newPlanId}`
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Add a new user to the repository
|
|
283
|
+
*
|
|
284
|
+
* @param {string} email - The email of the user
|
|
285
|
+
*/
|
|
286
|
+
async addUser(email) {
|
|
287
|
+
const profiler = logger.startTimer();
|
|
288
|
+
const response = await this.manageV2Client.addUserToRepository({
|
|
289
|
+
repository: this.name,
|
|
290
|
+
email
|
|
291
|
+
});
|
|
292
|
+
this.failIfNot200(response, "Could not add a new user to the repository");
|
|
293
|
+
profiler.done({
|
|
294
|
+
message: `User ${email} was added to the repository`
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Remove a user from the repository
|
|
299
|
+
*
|
|
300
|
+
* @param {string} email - The email of the user
|
|
301
|
+
*/
|
|
302
|
+
async removeUser(email) {
|
|
303
|
+
const profiler = logger.startTimer();
|
|
304
|
+
const response = await this.manageV2Client.removeUserFromRepository({
|
|
305
|
+
repository: this.name,
|
|
306
|
+
email
|
|
307
|
+
});
|
|
308
|
+
this.failIfNot200(response, `Could not remove the user ${email} from the repository`);
|
|
309
|
+
profiler.done({
|
|
310
|
+
message: `User ${email} was removed from the repository`
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Update the role of a user in a repository
|
|
315
|
+
*
|
|
316
|
+
* @param {string} email - The email of the user
|
|
317
|
+
* @param {string | Record<string, string>} role - The new Role of the user,
|
|
318
|
+
* either a string for basic workflow or an object such as `{ "lang_id":
|
|
319
|
+
* "Writer" }`
|
|
320
|
+
*
|
|
321
|
+
* Example of roles are
|
|
322
|
+
*
|
|
323
|
+
* - Administrator
|
|
324
|
+
* - Writer
|
|
325
|
+
* - Contributor
|
|
326
|
+
* - Manager (publisher)
|
|
327
|
+
*/
|
|
328
|
+
async updateUserRole(email, role) {
|
|
329
|
+
const profiler = logger.startTimer();
|
|
330
|
+
const response = typeof role === "string" ? await this.wroomClient.updateRole(this.name, email, role) : await this.wroomClient.updateRolePerLocal(this.name, email, role);
|
|
331
|
+
this.failIfNot200(response, `Could not update the ${typeof role === "string" ? "role" : "rolePerLocal"} of the user ${email}`);
|
|
332
|
+
profiler.done({
|
|
333
|
+
message: `Updated User's ${typeof role === "string" ? "role" : "rolePerLocal"} for user ${email}`
|
|
334
|
+
});
|
|
335
|
+
}
|
|
231
336
|
}
|
|
232
337
|
export {
|
|
233
338
|
RepositoryManager
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"repository.js","sources":["../../../src/managers/repository.ts"],"sourcesContent":["import { AxiosResponse } from \"axios\";\n\nimport {\n\tCustomType,\n\tSharedSlice,\n} from \"@prismicio/types-internal/lib/customtypes\";\n\nimport type { CoreClient } from \"../clients/coreApi\";\nimport { CustomTypesClient } from \"../clients/customTypesApi\";\nimport { WroomClient } from \"../clients/wroom\";\nimport { logHttpResponse, logger } from \"../utils/log\";\nimport { getRepositoryUrl } from \"../utils/urls\";\n\n/** Utility to manage test data for a Prismic repository */\nexport class RepositoryManager {\n\tconstructor(\n\t\treadonly name: string,\n\t\tprivate readonly coreApiClient: CoreClient,\n\t\tprivate readonly wroomClient: WroomClient,\n\t\tprivate readonly customTypesApiClient: CustomTypesClient,\n\t) {}\n\n\tasync configure(config: RepositoryConfig): Promise<void> {\n\t\tconst hasLangConfig =\n\t\t\tconfig.defaultLocale || config.locales || config.customLocale;\n\t\tif (hasLangConfig) {\n\t\t\tconst languages = await this.coreApiClient.getLanguages();\n\n\t\t\t// default locale must be set first\n\t\t\tif (config.defaultLocale) {\n\t\t\t\tconst master = languages.find(({ is_master }) => is_master === true);\n\t\t\t\tif (!master) {\n\t\t\t\t\tawait this.setDefaultLocale(config.defaultLocale);\n\t\t\t\t} else if (master.id !== config.defaultLocale) {\n\t\t\t\t\tlogger.warn(\n\t\t\t\t\t\t`Master locale is already set to '${master.id}', cannot update it to '${config.defaultLocale}'`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (config.locales) {\n\t\t\t\tconst localesToAdd = config.locales.filter(\n\t\t\t\t\t(id) => !languages.some((language) => language.id === id),\n\t\t\t\t);\n\t\t\t\tif (localesToAdd.length > 0) {\n\t\t\t\t\tawait this.createLocales(localesToAdd);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (config.customLocale) {\n\t\t\t\tif (!languages.find(({ id }) => id === config.customLocale?.lang.id)) {\n\t\t\t\t\tawait this.createCustomLocale(config.customLocale);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (config.slices) {\n\t\t\tawait this.createSlices(config.slices);\n\t\t}\n\n\t\tif (config.customTypes) {\n\t\t\tawait this.createCustomTypes(config.customTypes);\n\t\t}\n\n\t\tif (config.preview) {\n\t\t\tawait this.createPreview(config.preview);\n\t\t}\n\t}\n\t/** @returns the repository base url, like https://my-repo.prismic.io */\n\tgetBaseURL(): string {\n\t\treturn getRepositoryUrl(this.wroomClient.getBaseURL(), this.name);\n\t}\n\n\t/**\n\t * Set additional standard locales for a repository in Wroom, does not fail if\n\t * the locales already exist.\n\t *\n\t * @param locales - An array of all locales to be added to the repository.\n\t *\n\t * @throws Error if setting the locales fails.\n\t */\n\tasync createLocales(locales: string[]): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t\"app/settings/multilanguages\",\n\t\t\t{ languages: locales },\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 &&\n\t\t\t\t(data as string).includes(\"already existing languages\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not add locales ${locales.join(\", \")}`);\n\t\t}\n\t\tprofiler.done({ message: `locales '${locales}' created` });\n\t}\n\n\t/**\n\t * Set additional custom locale for a repository in Wroom, does not fail if\n\t * the locales already exist.\n\t *\n\t * @param locale - The locale to be added to the repository.\n\t *\n\t * @throws Error if setting the locale fails.\n\t */\n\tasync createCustomLocale(locale: CustomLocale): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t\"app/settings/multilanguages/custom\",\n\t\t\tlocale,\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 && JSON.stringify(data).includes(\"code is already used\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not create custom locale ${locale}`);\n\t\t}\n\t\tprofiler.done({\n\t\t\tmessage: `custom locale ${JSON.stringify(locale)} created`,\n\t\t});\n\t}\n\n\tprivate failIfNot200(response: AxiosResponse, msg: string) {\n\t\tif (response.status !== 200) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(msg);\n\t\t}\n\t}\n\n\t/**\n\t * Delete a locale from a repository.\n\t *\n\t * @param locale - locale to remove from the repository configuration.\n\t *\n\t * @throws Error if deleting the locale fails.\n\t */\n\tasync deleteLocale(locale: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`app/settings/multilanguages/${locale}/delete`,\n\t\t);\n\t\tthis.failIfNot200(response, `Could not delete locale ${locale}`);\n\t\tprofiler.done({ message: `locale '${locale}' deleted` });\n\t}\n\n\t/**\n\t * Set the default (master) locale for a repository in Wroom, the locale needs\n\t * to exist for the repository.\n\t *\n\t * @param locale - The locale to be set as the default.\n\t *\n\t * @throws Error if setting the default locale fails.\n\t */\n\tasync setDefaultLocale(locale: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`/app/settings/multilanguages/${locale}/createMasterLang`,\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 &&\n\t\t\t\t// returns 400 if master lang is already set\n\t\t\t\tJSON.stringify(data).includes(\"Unable to set as master language\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not set default locale to ${locale}`);\n\t\t}\n\t\tprofiler.done({ message: `default locale set to '${locale}'` });\n\t}\n\n\t/**\n\t * Creates a release.\n\t *\n\t * @param label - The label for the release.\n\t *\n\t * @returns A Promise that resolves when the release is created.\n\t */\n\tasync createRelease(label: string): Promise<{ id: string }> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(this.name, \"app/releases\", {\n\t\t\tlabel,\n\t\t});\n\t\tthis.failIfNot200(response, `Could not create release ${label}`);\n\t\tprofiler.done({ message: `release '${label}' created` });\n\n\t\treturn response.data;\n\t}\n\n\t/**\n\t * Deletes a repository release.\n\t *\n\t * @param repository - The name of the repository.\n\t * @param id - The ID of the release to be deleted.\n\t */\n\tasync deleteRelease(id: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`app/releases/${id}/delete`,\n\t\t\t{},\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not delete release with id ${id}`);\n\t\tprofiler.done({ message: `release '${id}' deleted` });\n\t}\n\n\t/**\n\t * Creates a preview for a repository.\n\t *\n\t * @param data - The data for creating the preview.\n\t */\n\tasync createPreview(data: Preview): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`previews/new`,\n\t\t\tdata,\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not create preview ${data.name}`);\n\t\tprofiler.done({ message: `preview '${data.name}' created` });\n\n\t\treturn response.data;\n\t}\n\n\t/**\n\t * Deletes a preview from a repository.\n\t *\n\t * @param id - The ID of the preview to be deleted.\n\t */\n\tasync deletePreview(id: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`previews/delete/${id}`,\n\t\t\t{},\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not delete preview with id ${id}`);\n\t\tprofiler.done({ message: `preview '${id}' deleted` });\n\t}\n\n\t/**\n\t * Create Custom Types using the Custom types api.\n\t *\n\t * @param customTypes -\n\t */\n\tasync createCustomTypes(customTypes: CustomType[]): Promise<void> {\n\t\tawait this.customTypesApiClient.createCustomTypes(customTypes);\n\t}\n\n\t/**\n\t * Create slices using the Custom types api.\n\t *\n\t * @param slices -\n\t */\n\tasync createSlices(slices: SharedSlice[]): Promise<void> {\n\t\tawait this.customTypesApiClient.createSlices(slices);\n\t}\n\n\t/**\n\t * Create a permanent access token along with an application name\n\t *\n\t * @param appName - mandatory authorized application name\n\t */\n\tasync createPermanentAccessToken(appName: string): Promise<string> {\n\t\tconst profiler = logger.startTimer();\n\t\ttype AuthorizedAppResponse = {\n\t\t\tname: string;\n\t\t\twroom_auths: { token: string }[];\n\t\t};\n\t\tconst existingApps: AxiosResponse<AuthorizedAppResponse[]> =\n\t\t\tawait this.wroomClient.get(this.name, \"settings/security/contentapi\");\n\n\t\tthis.failIfNot200(\n\t\t\texistingApps,\n\t\t\t`Could not get status of existing applications to generate a permanent token`,\n\t\t);\n\t\tconst existingApp = existingApps.data.find(({ name, wroom_auths }) => {\n\t\t\treturn name === appName && wroom_auths.length > 0;\n\t\t});\n\t\tif (existingApp) {\n\t\t\treturn existingApp.wroom_auths[0].token;\n\t\t}\n\t\tconst response: AxiosResponse<AuthorizedAppResponse> =\n\t\t\tawait this.wroomClient.post(this.name, \"settings/security/oauthapp\", {\n\t\t\t\tapp_name: appName,\n\t\t\t});\n\n\t\tthis.failIfNot200(\n\t\t\tresponse,\n\t\t\t`Could not create permanent access token for app name ${appName}`,\n\t\t);\n\t\tprofiler.done({\n\t\t\tmessage: `permanent access token for app name '${appName}' created`,\n\t\t});\n\n\t\t// only return the generated token since there is no need to access more\n\t\treturn response.data.wroom_auths[0].token;\n\t}\n}\n\nexport type RepositoryConfig = {\n\tcustomLocale?: CustomLocale;\n\tlocales?: string[];\n\tdefaultLocale?: string;\n\tslices?: SharedSlice[];\n\tcustomTypes?: CustomType[];\n\tpreview?: Preview;\n};\n\nexport type Preview = {\n\tname: string;\n\twebsiteURL: string;\n\tresolverPath: string;\n};\n\nexport type CustomLocale = {\n\tlang: {\n\t\tlabel: string;\n\t\tid: string;\n\t\tuseStandardAnalyzer?: boolean;\n\t};\n\tregion: {\n\t\tlabel: string;\n\t\tid: string;\n\t};\n};\n"],"names":[],"mappings":";;;;;;;;MAca,kBAAiB;AAAA,EAC7B,YACU,MACQ,eACA,aACA,sBAAuC;AAH/C;AACQ;AACA;AACA;AAHR,SAAI,OAAJ;AACQ,SAAa,gBAAb;AACA,SAAW,cAAX;AACA,SAAoB,uBAApB;AAAA,EACf;AAAA,EAEH,MAAM,UAAU,QAAwB;AACvC,UAAM,gBACL,OAAO,iBAAiB,OAAO,WAAW,OAAO;AAClD,QAAI,eAAe;AAClB,YAAM,YAAY,MAAM,KAAK,cAAc,aAAY;AAGvD,UAAI,OAAO,eAAe;AACnB,cAAA,SAAS,UAAU,KAAK,CAAC,EAAE,gBAAgB,cAAc,IAAI;AACnE,YAAI,CAAC,QAAQ;AACN,gBAAA,KAAK,iBAAiB,OAAO,aAAa;AAAA,QACtC,WAAA,OAAO,OAAO,OAAO,eAAe;AAC9C,iBAAO,KACN,oCAAoC,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,UAAU,oCAAoC,EAAE,EAAE;AACpE,aAAS,KAAK,EAAE,SAAS,YAAY,EAAE,aAAa;AAAA,EACrD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,kBAAkB,aAAyB;AAC1C,UAAA,KAAK,qBAAqB,kBAAkB,WAAW;AAAA,EAC9D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,aAAa,QAAqB;AACjC,UAAA,KAAK,qBAAqB,aAAa,MAAM;AAAA,EACpD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,2BAA2B,SAAe;AACzC,UAAA,WAAW,OAAO;AAKxB,UAAM,eACL,MAAM,KAAK,YAAY,IAAI,KAAK,MAAM,8BAA8B;AAEhE,SAAA,aACJ,cACA,6EAA6E;AAExE,UAAA,cAAc,aAAa,KAAK,KAAK,CAAC,EAAE,MAAM,kBAAiB;AAC7D,aAAA,SAAS,WAAW,YAAY,SAAS;AAAA,IAAA,CAChD;AACD,QAAI,aAAa;AACT,aAAA,YAAY,YAAY,CAAC,EAAE;AAAA,IAClC;AACD,UAAM,WACL,MAAM,KAAK,YAAY,KAAK,KAAK,MAAM,8BAA8B;AAAA,MACpE,UAAU;AAAA,IAAA,CACV;AAEF,SAAK,aACJ,UACA,wDAAwD,OAAO,EAAE;AAElE,aAAS,KAAK;AAAA,MACb,SAAS,wCAAwC,OAAO;AAAA,IAAA,CACxD;AAGD,WAAO,SAAS,KAAK,YAAY,CAAC,EAAE;AAAA,EACrC;AACA;"}
|
|
1
|
+
{"version":3,"file":"repository.js","sources":["../../../src/managers/repository.ts"],"sourcesContent":["import { AxiosResponse } from \"axios\";\n\nimport {\n\tCustomType,\n\tSharedSlice,\n} from \"@prismicio/types-internal/lib/customtypes\";\n\nimport type { AuthenticationClient } from \"../clients/authenticationApi\";\nimport type { CoreClient } from \"../clients/coreApi\";\nimport { CustomTypesClient } from \"../clients/customTypesApi\";\nimport { ManageV2Client } from \"../clients/manageV2\";\nimport { WroomClient } from \"../clients/wroom\";\nimport { logHttpResponse, logger } from \"../utils/log\";\nimport { getRepositoryUrl } from \"../utils/urls\";\n\n/** Utility to manage test data for a Prismic repository */\nexport class RepositoryManager {\n\tconstructor(\n\t\treadonly name: string,\n\t\tprivate readonly coreApiClient: CoreClient,\n\t\tprivate readonly authApiClient: AuthenticationClient,\n\t\tprivate readonly wroomClient: WroomClient,\n\t\tprivate readonly customTypesApiClient: CustomTypesClient,\n\t\tprivate readonly manageV2Client: ManageV2Client,\n\t) {}\n\n\tasync configure(config: RepositoryConfig): Promise<void> {\n\t\tconst hasLangConfig =\n\t\t\tconfig.defaultLocale || config.locales || config.customLocale;\n\t\tif (hasLangConfig) {\n\t\t\tconst languages = await this.coreApiClient.getLanguages();\n\n\t\t\t// default locale must be set first\n\t\t\tif (config.defaultLocale) {\n\t\t\t\tconst master = languages.find(({ is_master }) => is_master === true);\n\t\t\t\tif (!master) {\n\t\t\t\t\tawait this.setDefaultLocale(config.defaultLocale);\n\t\t\t\t} else if (master.id !== config.defaultLocale) {\n\t\t\t\t\tlogger.warn(\n\t\t\t\t\t\t`Master locale is already set to '${master.id}', cannot update it to '${config.defaultLocale}'`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (config.locales) {\n\t\t\t\tconst localesToAdd = config.locales.filter(\n\t\t\t\t\t(id) => !languages.some((language) => language.id === id),\n\t\t\t\t);\n\t\t\t\tif (localesToAdd.length > 0) {\n\t\t\t\t\tawait this.createLocales(localesToAdd);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (config.customLocale) {\n\t\t\t\tif (!languages.find(({ id }) => id === config.customLocale?.lang.id)) {\n\t\t\t\t\tawait this.createCustomLocale(config.customLocale);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (config.slices) {\n\t\t\tawait this.createSlices(config.slices);\n\t\t}\n\n\t\tif (config.customTypes) {\n\t\t\tawait this.createCustomTypes(config.customTypes);\n\t\t}\n\n\t\tif (config.preview) {\n\t\t\tawait this.createPreview(config.preview);\n\t\t}\n\n\t\tif (config.rolePerLocal) {\n\t\t\tawait this.toggleRolePerLocal(true);\n\t\t}\n\t}\n\t/** @returns the repository base url, like https://my-repo.prismic.io */\n\tgetBaseURL(): string {\n\t\treturn getRepositoryUrl(this.wroomClient.getBaseURL(), this.name);\n\t}\n\n\t/**\n\t * Set additional standard locales for a repository in Wroom, does not fail if\n\t * the locales already exist.\n\t *\n\t * @param locales - An array of all locales to be added to the repository.\n\t *\n\t * @throws Error if setting the locales fails.\n\t */\n\tasync createLocales(locales: string[]): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t\"app/settings/multilanguages\",\n\t\t\t{ languages: locales },\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 &&\n\t\t\t\t(data as string).includes(\"already existing languages\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not add locales ${locales.join(\", \")}`);\n\t\t}\n\t\tprofiler.done({ message: `locales '${locales}' created` });\n\t}\n\n\t/**\n\t * Set additional custom locale for a repository in Wroom, does not fail if\n\t * the locales already exist.\n\t *\n\t * @param locale - The locale to be added to the repository.\n\t *\n\t * @throws Error if setting the locale fails.\n\t */\n\tasync createCustomLocale(locale: CustomLocale): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t\"app/settings/multilanguages/custom\",\n\t\t\tlocale,\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 && JSON.stringify(data).includes(\"code is already used\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not create custom locale ${locale}`);\n\t\t}\n\t\tprofiler.done({\n\t\t\tmessage: `custom locale ${JSON.stringify(locale)} created`,\n\t\t});\n\t}\n\n\tprivate failIfNot200(response: AxiosResponse, msg: string) {\n\t\tif (response.status !== 200) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(msg);\n\t\t}\n\t}\n\n\t/**\n\t * Delete a locale from a repository.\n\t *\n\t * @param locale - locale to remove from the repository configuration.\n\t *\n\t * @throws Error if deleting the locale fails.\n\t */\n\tasync deleteLocale(locale: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`app/settings/multilanguages/${locale}/delete`,\n\t\t);\n\t\tthis.failIfNot200(response, `Could not delete locale ${locale}`);\n\t\tprofiler.done({ message: `locale '${locale}' deleted` });\n\t}\n\n\t/**\n\t * Set the default (master) locale for a repository in Wroom, the locale needs\n\t * to exist for the repository.\n\t *\n\t * @param locale - The locale to be set as the default.\n\t *\n\t * @throws Error if setting the default locale fails.\n\t */\n\tasync setDefaultLocale(locale: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`/app/settings/multilanguages/${locale}/createMasterLang`,\n\t\t);\n\n\t\tconst { data, status } = response;\n\t\tconst success =\n\t\t\tstatus === 200 ||\n\t\t\t(status === 400 &&\n\t\t\t\t// returns 400 if master lang is already set\n\t\t\t\tJSON.stringify(data).includes(\"Unable to set as master language\"));\n\t\tif (!success) {\n\t\t\tlogHttpResponse(response);\n\t\t\tthrow new Error(`Could not set default locale to ${locale}`);\n\t\t}\n\t\tprofiler.done({ message: `default locale set to '${locale}'` });\n\t}\n\n\t/**\n\t * Creates a release.\n\t *\n\t * @param label - The label for the release.\n\t *\n\t * @returns A Promise that resolves when the release is created.\n\t */\n\tasync createRelease(label: string): Promise<{ id: string }> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(this.name, \"app/releases\", {\n\t\t\tlabel,\n\t\t});\n\t\tthis.failIfNot200(response, `Could not create release ${label}`);\n\t\tprofiler.done({ message: `release '${label}' created` });\n\n\t\treturn response.data;\n\t}\n\n\t/**\n\t * Deletes a repository release.\n\t *\n\t * @param repository - The name of the repository.\n\t * @param id - The ID of the release to be deleted.\n\t */\n\tasync deleteRelease(id: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`app/releases/${id}/delete`,\n\t\t\t{},\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not delete release with id ${id}`);\n\t\tprofiler.done({ message: `release '${id}' deleted` });\n\t}\n\n\t/**\n\t * Creates a preview for a repository.\n\t *\n\t * @param data - The data for creating the preview.\n\t */\n\tasync createPreview(data: Preview): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`previews/new`,\n\t\t\tdata,\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not create preview ${data.name}`);\n\t\tprofiler.done({ message: `preview '${data.name}' created` });\n\n\t\treturn response.data;\n\t}\n\n\t/**\n\t * Deletes a preview from a repository.\n\t *\n\t * @param id - The ID of the preview to be deleted.\n\t */\n\tasync deletePreview(id: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.wroomClient.post(\n\t\t\tthis.name,\n\t\t\t`previews/delete/${id}`,\n\t\t\t{},\n\t\t);\n\n\t\tthis.failIfNot200(response, `Could not delete preview with id ${id}`);\n\t\tprofiler.done({ message: `preview '${id}' deleted` });\n\t}\n\n\t/**\n\t * Create Custom Types using the Custom types api.\n\t *\n\t * @param customTypes -\n\t */\n\tasync createCustomTypes(customTypes: CustomType[]): Promise<void> {\n\t\tawait this.customTypesApiClient.createCustomTypes(customTypes);\n\t}\n\n\t/**\n\t * Create slices using the Custom types api.\n\t *\n\t * @param slices -\n\t */\n\tasync createSlices(slices: SharedSlice[]): Promise<void> {\n\t\tawait this.customTypesApiClient.createSlices(slices);\n\t}\n\n\t/**\n\t * Create a permanent access token along with an application name\n\t *\n\t * @param appName - mandatory authorized application name\n\t */\n\tasync createPermanentAccessToken(appName: string): Promise<string> {\n\t\tconst profiler = logger.startTimer();\n\t\ttype AuthorizedAppResponse = {\n\t\t\tname: string;\n\t\t\twroom_auths: { token: string }[];\n\t\t};\n\t\tconst existingApps: AxiosResponse<AuthorizedAppResponse[]> =\n\t\t\tawait this.wroomClient.get(this.name, \"settings/security/contentapi\");\n\n\t\tthis.failIfNot200(\n\t\t\texistingApps,\n\t\t\t`Could not get status of existing applications to generate a permanent token`,\n\t\t);\n\t\tconst existingApp = existingApps.data.find(({ name, wroom_auths }) => {\n\t\t\treturn name === appName && wroom_auths.length > 0;\n\t\t});\n\t\tif (existingApp) {\n\t\t\treturn existingApp.wroom_auths[0].token;\n\t\t}\n\t\tconst response: AxiosResponse<AuthorizedAppResponse> =\n\t\t\tawait this.wroomClient.post(this.name, \"settings/security/oauthapp\", {\n\t\t\t\tapp_name: appName,\n\t\t\t});\n\n\t\tthis.failIfNot200(\n\t\t\tresponse,\n\t\t\t`Could not create permanent access token for app name ${appName}`,\n\t\t);\n\t\tprofiler.done({\n\t\t\tmessage: `permanent access token for app name '${appName}' created`,\n\t\t});\n\n\t\t// only return the generated token since there is no need to access more\n\t\treturn response.data.wroom_auths[0].token;\n\t}\n\n\t/** Create a machine2machine token */\n\tasync createMachine2MachineToken(): Promise<string> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst token = await this.authApiClient.getMachine2MachineToken(this.name);\n\n\t\tprofiler.done({\n\t\t\tmessage: `machine2machine token created`,\n\t\t});\n\n\t\treturn token;\n\t}\n\n\t/**\n\t * Toggle the Role per local feature\n\t *\n\t * @param enabled - The feature new status\n\t */\n\tasync toggleRolePerLocal(enabled: boolean): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.manageV2Client.toggleRolePerLocal({\n\t\t\trepository: this.name,\n\t\t\tenabled: enabled,\n\t\t});\n\n\t\tthis.failIfNot200(\n\t\t\tresponse,\n\t\t\t`Could not ${enabled ? \"enable\" : \"disable\"} Role per local for ${\n\t\t\t\tthis.name\n\t\t\t}`,\n\t\t);\n\t\tprofiler.done({\n\t\t\tmessage: `Role per local ${enabled ? \"enabled\" : \"disabled\"}`,\n\t\t});\n\t}\n\n\t/**\n\t * Change the Repository Plan\n\t *\n\t * @param {string} newPlanId - The Id of the new Plan to apply\n\t */\n\tasync changePlan(newPlanId: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.manageV2Client.changePlan({\n\t\t\trepository: this.name,\n\t\t\tnewPlanId: newPlanId,\n\t\t\tbypassManualBilling: true, // For now the library does not support Stripe features\n\t\t});\n\n\t\tthis.failIfNot200(response, \"Could not change the Repository Plan\");\n\t\tprofiler.done({\n\t\t\tmessage: `Repository Plan changed to ${newPlanId}`,\n\t\t});\n\t}\n\n\t/**\n\t * Add a new user to the repository\n\t *\n\t * @param {string} email - The email of the user\n\t */\n\tasync addUser(email: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.manageV2Client.addUserToRepository({\n\t\t\trepository: this.name,\n\t\t\temail: email,\n\t\t});\n\n\t\tthis.failIfNot200(response, \"Could not add a new user to the repository\");\n\t\tprofiler.done({\n\t\t\tmessage: `User ${email} was added to the repository`,\n\t\t});\n\t}\n\n\t/**\n\t * Remove a user from the repository\n\t *\n\t * @param {string} email - The email of the user\n\t */\n\tasync removeUser(email: string): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response = await this.manageV2Client.removeUserFromRepository({\n\t\t\trepository: this.name,\n\t\t\temail: email,\n\t\t});\n\n\t\tthis.failIfNot200(\n\t\t\tresponse,\n\t\t\t`Could not remove the user ${email} from the repository`,\n\t\t);\n\t\tprofiler.done({\n\t\t\tmessage: `User ${email} was removed from the repository`,\n\t\t});\n\t}\n\n\t/**\n\t * Update the role of a user in a repository\n\t *\n\t * @param {string} email - The email of the user\n\t * @param {string | Record<string, string>} role - The new Role of the user,\n\t * either a string for basic workflow or an object such as `{ \"lang_id\":\n\t * \"Writer\" }`\n\t *\n\t * Example of roles are\n\t *\n\t * - Administrator\n\t * - Writer\n\t * - Contributor\n\t * - Manager (publisher)\n\t */\n\tasync updateUserRole(\n\t\temail: string,\n\t\trole: string | Record<string, string>,\n\t): Promise<void> {\n\t\tconst profiler = logger.startTimer();\n\n\t\tconst response =\n\t\t\ttypeof role === \"string\"\n\t\t\t\t? await this.wroomClient.updateRole(this.name, email, role)\n\t\t\t\t: await this.wroomClient.updateRolePerLocal(this.name, email, role);\n\n\t\tthis.failIfNot200(\n\t\t\tresponse,\n\t\t\t`Could not update the ${\n\t\t\t\ttypeof role === \"string\" ? \"role\" : \"rolePerLocal\"\n\t\t\t} of the user ${email}`,\n\t\t);\n\t\tprofiler.done({\n\t\t\tmessage: `Updated User's ${\n\t\t\t\ttypeof role === \"string\" ? \"role\" : \"rolePerLocal\"\n\t\t\t} for user ${email}`,\n\t\t});\n\t}\n}\n\nexport type RepositoryConfig = {\n\tcustomLocale?: CustomLocale;\n\tlocales?: string[];\n\tdefaultLocale?: string;\n\tslices?: SharedSlice[];\n\tcustomTypes?: CustomType[];\n\tpreview?: Preview;\n\trolePerLocal?: boolean;\n};\n\nexport type Preview = {\n\tname: string;\n\twebsiteURL: string;\n\tresolverPath: string;\n};\n\nexport type CustomLocale = {\n\tlang: {\n\t\tlabel: string;\n\t\tid: string;\n\t\tuseStandardAnalyzer?: boolean;\n\t};\n\tregion: {\n\t\tlabel: string;\n\t\tid: string;\n\t};\n};\n"],"names":[],"mappings":";;;;;;;;MAgBa,kBAAiB;AAAA,EAC7B,YACU,MACQ,eACA,eACA,aACA,sBACA,gBAA8B;AALtC;AACQ;AACA;AACA;AACA;AACA;AALR,SAAI,OAAJ;AACQ,SAAa,gBAAb;AACA,SAAa,gBAAb;AACA,SAAW,cAAX;AACA,SAAoB,u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Aa,UAAU,4CAA4C;AACxE,aAAS,KAAK;AAAA,MACb,SAAS,QAAQ,KAAK;AAAA,IAAA,CACtB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,WAAW,OAAa;AACvB,UAAA,WAAW,OAAO;AAExB,UAAM,WAAW,MAAM,KAAK,eAAe,yBAAyB;AAAA,MACnE,YAAY,KAAK;AAAA,MACjB;AAAA,IAAA,CACA;AAED,SAAK,aACJ,UACA,6BAA6B,KAAK,sBAAsB;AAEzD,aAAS,KAAK;AAAA,MACb,SAAS,QAAQ,KAAK;AAAA,IAAA,CACtB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBA,MAAM,eACL,OACA,MAAqC;AAE/B,UAAA,WAAW,OAAO;AAElB,UAAA,WACL,OAAO,SAAS,WACb,MAAM,KAAK,YAAY,WAAW,KAAK,MAAM,OAAO,IAAI,IACxD,MAAM,KAAK,YAAY,mBAAmB,KAAK,MAAM,OAAO,IAAI;AAE/D,SAAA,aACJ,UACA,wBACC,OAAO,SAAS,WAAW,SAAS,cACrC,gBAAgB,KAAK,EAAE;AAExB,aAAS,KAAK;AAAA,MACb,SAAS,kBACR,OAAO,SAAS,WAAW,SAAS,cACrC,aAAa,KAAK;AAAA,IAAA,CAClB;AAAA,EACF;AACA;"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,21 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export type UrlConfig = {
|
|
2
|
+
/** Prismic base url */
|
|
3
|
+
baseURL: string;
|
|
4
|
+
/** Custom types api base url */
|
|
5
|
+
customTypesApi: string;
|
|
6
|
+
/** Auth service api base url */
|
|
7
|
+
authenticationApi: string;
|
|
8
|
+
};
|
|
9
|
+
export type AuthConfig = {
|
|
2
10
|
email: string;
|
|
3
11
|
password: string;
|
|
4
12
|
};
|
|
13
|
+
export type ManageV2Config = {
|
|
14
|
+
secret: string;
|
|
15
|
+
audience: string;
|
|
16
|
+
};
|
|
17
|
+
export type SetupConfiguration = {
|
|
18
|
+
urlConfig: UrlConfig | string;
|
|
19
|
+
authConfig: AuthConfig;
|
|
20
|
+
manageV2Config?: ManageV2Config;
|
|
21
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prismicio/e2e-tests-utils",
|
|
3
|
-
"version": "1.1.0",
|
|
3
|
+
"version": "1.1.2-alpha.0",
|
|
4
4
|
"description": "A collection of utilities for to manage Prismic test data",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -54,12 +54,14 @@
|
|
|
54
54
|
"dependencies": {
|
|
55
55
|
"@prismicio/types-internal": "^2.3.1",
|
|
56
56
|
"axios": "^1.6.2",
|
|
57
|
+
"jsonwebtoken": "^9.0.2",
|
|
57
58
|
"lodash.isequal": "^4.5.0",
|
|
58
59
|
"winston": "^3.11.0"
|
|
59
60
|
},
|
|
60
61
|
"devDependencies": {
|
|
61
62
|
"@size-limit/preset-small-lib": "^11.0.1",
|
|
62
63
|
"@trivago/prettier-plugin-sort-imports": "^4.2.0",
|
|
64
|
+
"@types/jsonwebtoken": "^9.0.6",
|
|
63
65
|
"@types/lodash.isequal": "^4.5.8",
|
|
64
66
|
"@types/node": "^20.9.2",
|
|
65
67
|
"@typescript-eslint/eslint-plugin": "^6.8.0",
|
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import axios, { AxiosInstance } from "axios";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { AuthConfig } from "../types";
|
|
4
4
|
|
|
5
5
|
import { logHttpResponse, logger } from "../utils/log";
|
|
6
6
|
|
|
7
7
|
export type AuthenticationClient = {
|
|
8
8
|
getToken: () => Promise<string>;
|
|
9
|
+
getMachine2MachineToken: (repository: string) => Promise<string>;
|
|
9
10
|
};
|
|
10
11
|
|
|
11
12
|
/** Client for interacting with the authentication service to manage tokens */
|
|
12
13
|
export const createAuthenticationApiClient = (
|
|
13
14
|
baseURL: string,
|
|
14
|
-
auth:
|
|
15
|
+
auth: AuthConfig,
|
|
15
16
|
): AuthenticationClient => {
|
|
16
17
|
let authToken: string;
|
|
17
18
|
|
|
@@ -39,7 +40,7 @@ export const createAuthenticationApiClient = (
|
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
/** Return an api user token. Creates one if needed. */
|
|
42
|
-
async function getToken() {
|
|
43
|
+
async function getToken(): Promise<string> {
|
|
43
44
|
if (!authToken) {
|
|
44
45
|
authToken = await login();
|
|
45
46
|
}
|
|
@@ -47,7 +48,33 @@ export const createAuthenticationApiClient = (
|
|
|
47
48
|
return authToken;
|
|
48
49
|
}
|
|
49
50
|
|
|
51
|
+
/** Return an api user token. Creates one if needed. */
|
|
52
|
+
async function getMachine2MachineToken(repository: string): Promise<string> {
|
|
53
|
+
if (!authToken) {
|
|
54
|
+
authToken = await login();
|
|
55
|
+
}
|
|
56
|
+
const result = await client.get<{ token: string; timestamp: number }>(
|
|
57
|
+
"machine2machine",
|
|
58
|
+
{
|
|
59
|
+
headers: {
|
|
60
|
+
Authorization: `Bearer ${authToken}`,
|
|
61
|
+
repository,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
if (![200].includes(result.status) || !result.data.token) {
|
|
67
|
+
logHttpResponse(result);
|
|
68
|
+
throw new Error(
|
|
69
|
+
"Machine2Machine token generation failed, no token received.",
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return result.data.token;
|
|
74
|
+
}
|
|
75
|
+
|
|
50
76
|
return {
|
|
51
77
|
getToken,
|
|
78
|
+
getMachine2MachineToken,
|
|
52
79
|
};
|
|
53
80
|
};
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import axios, { AxiosInstance, AxiosResponse } from "axios";
|
|
2
|
+
import jwt from "jsonwebtoken";
|
|
3
|
+
|
|
4
|
+
import { ManageV2Config } from "../types";
|
|
5
|
+
|
|
6
|
+
import { extractCookie } from "../utils/cookies";
|
|
7
|
+
|
|
8
|
+
const ManageV2StaticAuthor = "prismic-e2e-tests-utils";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Client for interacting with ManageV2 routes to perform operations on
|
|
12
|
+
* repositories.
|
|
13
|
+
*/
|
|
14
|
+
export class ManageV2Client {
|
|
15
|
+
readonly client: AxiosInstance; // Not private to have it available on tests
|
|
16
|
+
private token: string | null = null;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param baseURL - The base URL of the Wroom app. ex: https://prismic.io
|
|
20
|
+
* @param config - ManageV2 configuration variable to call ManageV2.
|
|
21
|
+
*/
|
|
22
|
+
constructor(baseUrl: string, config?: ManageV2Config | undefined) {
|
|
23
|
+
this.token = config ? this.generateToken(config) : null;
|
|
24
|
+
|
|
25
|
+
this.client = axios.create({
|
|
26
|
+
baseURL: `${baseUrl}/manageroutes/`,
|
|
27
|
+
withCredentials: true,
|
|
28
|
+
validateStatus: () => true,
|
|
29
|
+
headers: {
|
|
30
|
+
"User-Agent": "prismic-cli/prismic-e2e-tests-utils",
|
|
31
|
+
Authorization: `Bearer ${this.token}`,
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// cookies are not forwarded automatically in a non-browser env
|
|
36
|
+
this.client.interceptors.response.use((response) => {
|
|
37
|
+
const cookies = response.headers["set-cookie"];
|
|
38
|
+
if (cookies && extractCookie(cookies, "SESSION")) {
|
|
39
|
+
this.client.defaults.headers["Cookie"] = cookies;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return response;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The function generates a JWT token using the provided configuration
|
|
48
|
+
* parameters.
|
|
49
|
+
*
|
|
50
|
+
* @param {ManageV2Config} config - The `config` parameter in the
|
|
51
|
+
* `generateToken` function is of type `ManageV2Config`. It contains the
|
|
52
|
+
* following properties:
|
|
53
|
+
*
|
|
54
|
+
* @returns A JSON Web Token (JWT) is being returned by the `generateToken`
|
|
55
|
+
* function. The token is signed using the provided `config.secret` with the
|
|
56
|
+
* HS256 algorithm and includes the specified audience and issuer values.
|
|
57
|
+
*/
|
|
58
|
+
private generateToken(config: ManageV2Config): string {
|
|
59
|
+
return jwt.sign({}, config.secret, {
|
|
60
|
+
algorithm: "HS256",
|
|
61
|
+
audience: config.audience,
|
|
62
|
+
issuer: "prismic.io",
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The function `assertTokenExist` checks if a token exists before executing a
|
|
68
|
+
* given function.
|
|
69
|
+
*
|
|
70
|
+
* @param f - The `assertTokenExist` function takes a function `f` as a
|
|
71
|
+
* parameter. This function `f` should accept a parameter of type
|
|
72
|
+
* `Parameters` and return a Promise that resolves to an AxiosResponse. The
|
|
73
|
+
* `assertTokenExist` function ensures that a token is not null before
|
|
74
|
+
* calling the provided
|
|
75
|
+
*
|
|
76
|
+
* @returns A function is being returned that takes a parameter of type
|
|
77
|
+
* Parameters and checks if the token is null. If the token is null, an
|
|
78
|
+
* error is thrown. Otherwise, the function f is called with the provided
|
|
79
|
+
* parameters.
|
|
80
|
+
*/
|
|
81
|
+
private assertTokenExist<Parameters>(
|
|
82
|
+
f: (_: Parameters) => Promise<AxiosResponse>,
|
|
83
|
+
) {
|
|
84
|
+
return (params: Parameters) => {
|
|
85
|
+
if (this.token === null) {
|
|
86
|
+
throw new Error(
|
|
87
|
+
"Undefined configuration for ManageV2 while trying to use it's routes",
|
|
88
|
+
);
|
|
89
|
+
} else {
|
|
90
|
+
return f(params);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
getBaseURL(): string {
|
|
96
|
+
return this.client.getUri();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The function `toggleRolePerLocal` enable or disable the role per local
|
|
101
|
+
* feature
|
|
102
|
+
*
|
|
103
|
+
* @param repository - The Repository name
|
|
104
|
+
* @param enabled - The feature new status
|
|
105
|
+
*/
|
|
106
|
+
toggleRolePerLocal = this.assertTokenExist(
|
|
107
|
+
(params: { repository: string; enabled: boolean }) => {
|
|
108
|
+
return this.client.post("/repository/toggleRolesPerLocale", {
|
|
109
|
+
domain: params.repository,
|
|
110
|
+
enabled: params.enabled,
|
|
111
|
+
author: ManageV2StaticAuthor,
|
|
112
|
+
});
|
|
113
|
+
},
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The function `changePlan` changes the plan of a repository the database
|
|
118
|
+
*
|
|
119
|
+
* @param repository - The Repository name
|
|
120
|
+
* @param newPlanId - The new planId
|
|
121
|
+
* @param bypassManualBilling - Beware that using this will not verify that
|
|
122
|
+
* the database and Stripe are in sync
|
|
123
|
+
*/
|
|
124
|
+
changePlan = this.assertTokenExist(
|
|
125
|
+
({
|
|
126
|
+
repository,
|
|
127
|
+
newPlanId,
|
|
128
|
+
bypassManualBilling = false,
|
|
129
|
+
}: {
|
|
130
|
+
repository: string;
|
|
131
|
+
newPlanId: string;
|
|
132
|
+
bypassManualBilling?: boolean;
|
|
133
|
+
}) => {
|
|
134
|
+
return this.client.post(
|
|
135
|
+
`/repository/changePlan?bypassManualBilling=${bypassManualBilling}`,
|
|
136
|
+
{
|
|
137
|
+
domain: repository,
|
|
138
|
+
newPlan: newPlanId,
|
|
139
|
+
author: ManageV2StaticAuthor,
|
|
140
|
+
},
|
|
141
|
+
);
|
|
142
|
+
},
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The function `addUserToRepository` adds a user corresponding to the email
|
|
147
|
+
* to the given repository
|
|
148
|
+
*
|
|
149
|
+
* @param repository - The Repository name
|
|
150
|
+
* @param email - The user email
|
|
151
|
+
*/
|
|
152
|
+
addUserToRepository = this.assertTokenExist(
|
|
153
|
+
({ repository, email }: { repository: string; email: string }) => {
|
|
154
|
+
return this.client.post("/repository/addUser", {
|
|
155
|
+
domain: repository,
|
|
156
|
+
email: email,
|
|
157
|
+
author: ManageV2StaticAuthor,
|
|
158
|
+
});
|
|
159
|
+
},
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The function `removeUserFromRepository` removes a user corresponding to the
|
|
164
|
+
* email from the given repository
|
|
165
|
+
*
|
|
166
|
+
* @param repository - The Repository name
|
|
167
|
+
* @param email - The user email
|
|
168
|
+
*/
|
|
169
|
+
removeUserFromRepository = this.assertTokenExist(
|
|
170
|
+
({ repository, email }: { repository: string; email: string }) => {
|
|
171
|
+
return this.client.post("/repository/removeUser", {
|
|
172
|
+
domain: repository,
|
|
173
|
+
email: email,
|
|
174
|
+
author: ManageV2StaticAuthor,
|
|
175
|
+
});
|
|
176
|
+
},
|
|
177
|
+
);
|
|
178
|
+
}
|
package/src/clients/wroom.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import axios, { AxiosInstance, AxiosResponse } from "axios";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { AuthConfig } from "../types";
|
|
4
4
|
|
|
5
5
|
import { extractCookie } from "../utils/cookies";
|
|
6
6
|
import { logHttpResponse, logger } from "../utils/log";
|
|
@@ -20,7 +20,7 @@ export class WroomClient {
|
|
|
20
20
|
*/
|
|
21
21
|
constructor(
|
|
22
22
|
baseURL: string,
|
|
23
|
-
private readonly auth:
|
|
23
|
+
private readonly auth: AuthConfig,
|
|
24
24
|
) {
|
|
25
25
|
this.client = axios.create({
|
|
26
26
|
baseURL,
|
|
@@ -84,6 +84,44 @@ export class WroomClient {
|
|
|
84
84
|
return response;
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
+
/**
|
|
88
|
+
* Update the role of a user in the repository
|
|
89
|
+
*
|
|
90
|
+
* @param {string} repository - The repository name
|
|
91
|
+
* @param {string} email - The email of the user
|
|
92
|
+
* @param {string} role - The new role to be given
|
|
93
|
+
*
|
|
94
|
+
* @returns The Axios Reponse
|
|
95
|
+
*/
|
|
96
|
+
async updateRole(
|
|
97
|
+
repository: string,
|
|
98
|
+
email: string,
|
|
99
|
+
role: string,
|
|
100
|
+
): Promise<AxiosResponse> {
|
|
101
|
+
const path = `/app/settings/users/profiles?email=${email}&profile=${role}`;
|
|
102
|
+
|
|
103
|
+
return this.post(repository, path);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Update the role of a user in the repository
|
|
108
|
+
*
|
|
109
|
+
* @param {string} repository - The repository name
|
|
110
|
+
* @param {string} email - The email of the user
|
|
111
|
+
* @param {string} rolePerLocal - The role per local object
|
|
112
|
+
*
|
|
113
|
+
* @returns The Axios Reponse
|
|
114
|
+
*/
|
|
115
|
+
async updateRolePerLocal(
|
|
116
|
+
repository: string,
|
|
117
|
+
email: string,
|
|
118
|
+
rolePerLocal: Record<string, string>,
|
|
119
|
+
): Promise<AxiosResponse> {
|
|
120
|
+
const path = `/app/settings/users/rolesperlocale?email=${email}`;
|
|
121
|
+
|
|
122
|
+
return this.post(repository, path, rolePerLocal);
|
|
123
|
+
}
|
|
124
|
+
|
|
87
125
|
/**
|
|
88
126
|
* Authenticate Wroom to call Wroom unofficial endpoints.
|
|
89
127
|
*
|