@prismicio/e2e-tests-utils 2.0.0-alpha.0 → 2.0.0-alpha.10

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.
Files changed (43) hide show
  1. package/dist/clients/assetApi.cjs +48 -0
  2. package/dist/clients/assetApi.cjs.map +1 -0
  3. package/dist/clients/assetApi.d.ts +54 -0
  4. package/dist/clients/assetApi.js +48 -0
  5. package/dist/clients/assetApi.js.map +1 -0
  6. package/dist/clients/authenticationApi.cjs +3 -10
  7. package/dist/clients/authenticationApi.cjs.map +1 -1
  8. package/dist/clients/authenticationApi.js +3 -10
  9. package/dist/clients/authenticationApi.js.map +1 -1
  10. package/dist/clients/manageV2.cjs +19 -6
  11. package/dist/clients/manageV2.cjs.map +1 -1
  12. package/dist/clients/manageV2.d.ts +15 -5
  13. package/dist/clients/manageV2.js +19 -6
  14. package/dist/clients/manageV2.js.map +1 -1
  15. package/dist/clients/wroom.cjs +10 -16
  16. package/dist/clients/wroom.cjs.map +1 -1
  17. package/dist/clients/wroom.d.ts +6 -6
  18. package/dist/clients/wroom.js +10 -16
  19. package/dist/clients/wroom.js.map +1 -1
  20. package/dist/managers/repositories.cjs +10 -3
  21. package/dist/managers/repositories.cjs.map +1 -1
  22. package/dist/managers/repositories.js +10 -3
  23. package/dist/managers/repositories.js.map +1 -1
  24. package/dist/managers/repository.cjs +65 -9
  25. package/dist/managers/repository.cjs.map +1 -1
  26. package/dist/managers/repository.d.ts +41 -9
  27. package/dist/managers/repository.js +65 -9
  28. package/dist/managers/repository.js.map +1 -1
  29. package/dist/types.d.ts +3 -0
  30. package/dist/utils/authentication.cjs +38 -0
  31. package/dist/utils/authentication.cjs.map +1 -0
  32. package/dist/utils/authentication.d.ts +4 -0
  33. package/dist/utils/authentication.js +38 -0
  34. package/dist/utils/authentication.js.map +1 -0
  35. package/package.json +3 -2
  36. package/src/clients/assetApi.ts +76 -0
  37. package/src/clients/authenticationApi.ts +7 -10
  38. package/src/clients/manageV2.ts +22 -6
  39. package/src/clients/wroom.ts +14 -17
  40. package/src/managers/repositories.ts +17 -1
  41. package/src/managers/repository.ts +102 -8
  42. package/src/types.ts +3 -0
  43. package/src/utils/authentication.ts +58 -0
@@ -35,7 +35,7 @@ export class ManageV2Client {
35
35
  // cookies are not forwarded automatically in a non-browser env
36
36
  this.client.interceptors.response.use((response) => {
37
37
  const cookies = response.headers["set-cookie"];
38
- if (cookies && extractCookie(cookies, "SESSION")) {
38
+ if (cookies && extractCookie(cookies, "prismic-auth")) {
39
39
  this.client.defaults.headers["Cookie"] = cookies;
40
40
  }
41
41
 
@@ -47,9 +47,8 @@ export class ManageV2Client {
47
47
  * The function generates a JWT token using the provided configuration
48
48
  * parameters.
49
49
  *
50
- * @param {ManageV2Config} config - The `config` parameter in the
51
- * `generateToken` function is of type `ManageV2Config`. It contains the
52
- * following properties:
50
+ * @param config - The `config` parameter in the `generateToken` function is
51
+ * of type `ManageV2Config`. It contains the following properties:
53
52
  *
54
53
  * @returns A JSON Web Token (JWT) is being returned by the `generateToken`
55
54
  * function. The token is signed using the provided `config.secret` with the
@@ -97,8 +96,8 @@ export class ManageV2Client {
97
96
  }
98
97
 
99
98
  /**
100
- * The function `toggleRolePerLocal` enable or disable the role per local
101
- * feature
99
+ * The function `toggleRolePerLocal` enables or disables the RolesPerLocale
100
+ * workflow
102
101
  *
103
102
  * @param repository - The Repository name
104
103
  * @param enabled - The feature new status
@@ -113,6 +112,23 @@ export class ManageV2Client {
113
112
  },
114
113
  );
115
114
 
115
+ /**
116
+ * The function `toggleCustomRoles` enables or disables the CustomRoles
117
+ * workflow
118
+ *
119
+ * @param repository - The Repository name
120
+ * @param enabled - The feature new status
121
+ */
122
+ toggleCustomRoles = this.assertTokenExist(
123
+ (params: { repository: string; enabled: boolean }) => {
124
+ return this.client.post("/repository/toggleCustomRoles", {
125
+ domain: params.repository,
126
+ enabled: params.enabled,
127
+ author: ManageV2StaticAuthor,
128
+ });
129
+ },
130
+ );
131
+
116
132
  /**
117
133
  * The function `changePlan` changes the plan of a repository the database
118
134
  *
@@ -2,8 +2,8 @@ import axios, { AxiosInstance, AxiosResponse } from "axios";
2
2
 
3
3
  import { AuthConfig } from "../types";
4
4
 
5
+ import { login } from "../utils/authentication";
5
6
  import { extractCookie } from "../utils/cookies";
6
- import { logHttpResponse, logger } from "../utils/log";
7
7
  import { getRepositoryUrl } from "../utils/urls";
8
8
 
9
9
  /**
@@ -97,9 +97,9 @@ export class WroomClient {
97
97
  /**
98
98
  * Update the role of a user in the repository
99
99
  *
100
- * @param {string} repository - The repository name
101
- * @param {string} email - The email of the user
102
- * @param {string} role - The new role to be given
100
+ * @param repository - The repository name
101
+ * @param email - The email of the user
102
+ * @param role - The new role to be given
103
103
  *
104
104
  * @returns The Axios Reponse
105
105
  */
@@ -117,9 +117,9 @@ export class WroomClient {
117
117
  /**
118
118
  * Update the role of a user in the repository
119
119
  *
120
- * @param {string} repository - The repository name
121
- * @param {string} email - The email of the user
122
- * @param {string} rolePerLocal - The role per local object
120
+ * @param repository - The repository name
121
+ * @param email - The email of the user
122
+ * @param rolePerLocal - The role per local object
123
123
  *
124
124
  * @returns The Axios Reponse
125
125
  */
@@ -140,17 +140,14 @@ export class WroomClient {
140
140
  * @throws Error if the login to Wroom fails.
141
141
  */
142
142
  private async login(): Promise<void> {
143
- const profiler = logger.startTimer();
144
- const response = await this.client.post<string>("/authentication/signin", {
145
- email: this.auth.email,
146
- password: this.auth.password,
147
- });
148
-
149
- if (response.status !== 200) {
150
- logHttpResponse(response);
151
- throw new Error("Could not login to Prismic, check your credentials");
143
+ const { cookies } = await login(
144
+ this.getBaseURL(),
145
+ this.auth.email,
146
+ this.auth.password,
147
+ );
148
+ if (cookies && extractCookie(cookies, "prismic-auth")) {
149
+ this.client.defaults.headers["Cookie"] = cookies;
152
150
  }
153
151
  this.loggedIn = true;
154
- profiler.done({ message: "logged in to Prismic" });
155
152
  }
156
153
  }
@@ -1,5 +1,6 @@
1
1
  import { SetupConfiguration, UrlConfig } from "../types";
2
2
 
3
+ import { AssetApiClient } from "../clients/assetApi";
3
4
  import { AuthenticationApiClient } from "../clients/authenticationApi";
4
5
  import { CoreApiClient } from "../clients/coreApi";
5
6
  import { CustomTypesApiClient } from "../clients/customTypesApi";
@@ -77,6 +78,15 @@ export class RepositoriesManager {
77
78
  this.config.urlConfig.baseURL,
78
79
  this.config.manageV2Config,
79
80
  );
81
+
82
+ // This logic specific to staging environment allow us to target a specific version of the custom types API.
83
+ // This is only works for unify-exp at the moment.
84
+ // Having this logic here avoids duplicating it across different CI workflows.
85
+ this.config.urlConfig.customTypesApi =
86
+ configuration.environment === "stage" &&
87
+ configuration.cluster === "unify-exp"
88
+ ? `https://customtypes.wroom.io/${configuration.cluster}/`
89
+ : this.config.urlConfig.customTypesApi;
80
90
  }
81
91
 
82
92
  /**
@@ -92,6 +102,7 @@ export class RepositoriesManager {
92
102
  authenticationApi: `${protocol}://auth.${url.hostname}`,
93
103
  customTypesApi: `${protocol}://customtypes.${url.hostname}`,
94
104
  migrationApi: `${protocol}://migration.${url.hostname}`,
105
+ assetApi: `${protocol}://asset-api.${url.hostname}`,
95
106
  };
96
107
  }
97
108
 
@@ -126,7 +137,7 @@ export class RepositoriesManager {
126
137
  "authentication/newrepository",
127
138
  {
128
139
  domain: repositoryName,
129
- framework: "vue",
140
+ framework: "next",
130
141
  plan: "personal",
131
142
  isAnnual: "false",
132
143
  role: "developer",
@@ -212,6 +223,10 @@ export class RepositoriesManager {
212
223
  repository,
213
224
  })
214
225
  : undefined;
226
+ const assetApiClient = new AssetApiClient(urlConfig.assetApi, {
227
+ authToken,
228
+ repository,
229
+ });
215
230
 
216
231
  return new RepositoryManager(
217
232
  repository,
@@ -220,6 +235,7 @@ export class RepositoriesManager {
220
235
  this.wroomClient,
221
236
  customTypeClient,
222
237
  migrationApiClient,
238
+ assetApiClient,
223
239
  this.manageV2Client,
224
240
  );
225
241
  }
@@ -6,6 +6,7 @@ import {
6
6
  SharedSlice,
7
7
  } from "@prismicio/types-internal/lib/customtypes";
8
8
 
9
+ import { AssetApiClient } from "../clients/assetApi";
9
10
  import { AuthenticationApiClient } from "../clients/authenticationApi";
10
11
  import { ContentApiClient } from "../clients/contentApi";
11
12
  import {
@@ -29,6 +30,7 @@ export class RepositoryManager {
29
30
  private readonly wroomClient: WroomClient,
30
31
  private readonly customTypesApiClient: CustomTypesApiClient,
31
32
  private readonly migrationApiClient: MigrationApiClient | undefined,
33
+ private readonly assetApiClient: AssetApiClient,
32
34
  private readonly manageV2Client: ManageV2Client,
33
35
  ) {}
34
36
 
@@ -85,6 +87,10 @@ export class RepositoryManager {
85
87
  if (config.rolePerLocal) {
86
88
  await this.toggleRolePerLocal(true);
87
89
  }
90
+
91
+ if (config.customRoles) {
92
+ await this.toggleCustomRoles(true);
93
+ }
88
94
  }
89
95
 
90
96
  /** @returns the repository base url, like https://my-repo.prismic.io */
@@ -121,6 +127,14 @@ export class RepositoryManager {
121
127
  return this.migrationApiClient;
122
128
  }
123
129
 
130
+ /**
131
+ * @returns an instance of the Asset api client, see
132
+ * https://prismic.io/docs/asset-api-technical-reference
133
+ */
134
+ getAssetApiClient(): AssetApiClient {
135
+ return this.assetApiClient;
136
+ }
137
+
124
138
  /** @returns the Wroom commit hash deployed on the current repository */
125
139
  async getDeployedVersion(): Promise<string | undefined> {
126
140
  const response = await this.wroomClient.get(
@@ -330,6 +344,62 @@ export class RepositoryManager {
330
344
  profiler.done({ message: `preview '${id}' deleted` });
331
345
  }
332
346
 
347
+ /**
348
+ * Create a webhook.
349
+ *
350
+ * @returns A Promise that resolves with the webhook id.
351
+ */
352
+ async createWebhook(
353
+ name: string,
354
+ url: string,
355
+ secret: string,
356
+ triggers: {
357
+ documentsPublished?: boolean;
358
+ documentsUnpublished?: boolean;
359
+ releasesCreated?: boolean;
360
+ releasesUpdated?: boolean;
361
+ tagsCreated?: boolean;
362
+ tagsDeleted?: boolean;
363
+ },
364
+ active: boolean,
365
+ ): Promise<string> {
366
+ const profiler = logger.startTimer();
367
+
368
+ const response = await this.wroomClient.post(
369
+ this.name,
370
+ "app/settings/webhooks/create",
371
+ {
372
+ name,
373
+ url,
374
+ secret,
375
+ active: active ? "on" : "off",
376
+ ...triggers,
377
+ },
378
+ );
379
+ this.failIfNot200(response, `Could not create webhook ${name}`);
380
+ profiler.done({ message: `webhook '${name}' created` });
381
+
382
+ return response.data.created;
383
+ }
384
+
385
+ /**
386
+ * Deletes a webhook.
387
+ *
388
+ * @param id - The webhook ID.
389
+ */
390
+ async deleteWebhook(id: string): Promise<void> {
391
+ const profiler = logger.startTimer();
392
+
393
+ const response = await this.wroomClient.post(
394
+ this.name,
395
+ `app/settings/webhooks/${id}/delete`,
396
+ {},
397
+ );
398
+
399
+ this.failIfNot200(response, `Could not delete webhook with id ${id}`);
400
+ profiler.done({ message: `webhook '${id}' deleted` });
401
+ }
402
+
333
403
  /**
334
404
  * Create Custom Types using the Custom types api.
335
405
  *
@@ -431,7 +501,7 @@ export class RepositoryManager {
431
501
  }
432
502
 
433
503
  /**
434
- * Toggle the Role per local feature
504
+ * Toggle the Role per Locale workflow
435
505
  *
436
506
  * @param enabled - The feature new status
437
507
  */
@@ -454,10 +524,34 @@ export class RepositoryManager {
454
524
  });
455
525
  }
456
526
 
527
+ /**
528
+ * Toggle the Custom Roles workflow
529
+ *
530
+ * @param enabled - The feature new status
531
+ */
532
+ async toggleCustomRoles(enabled: boolean): Promise<void> {
533
+ const profiler = logger.startTimer();
534
+
535
+ const response = await this.manageV2Client.toggleCustomRoles({
536
+ repository: this.name,
537
+ enabled: enabled,
538
+ });
539
+
540
+ this.failIfNot200(
541
+ response,
542
+ `Could not ${enabled ? "enable" : "disable"} Custom Roles for ${
543
+ this.name
544
+ }`,
545
+ );
546
+ profiler.done({
547
+ message: `Custom Roles ${enabled ? "enabled" : "disabled"}`,
548
+ });
549
+ }
550
+
457
551
  /**
458
552
  * Change the Repository Plan
459
553
  *
460
- * @param {string} newPlanId - The Id of the new Plan to apply
554
+ * @param newPlanId - The Id of the new Plan to apply
461
555
  */
462
556
  async changePlan(newPlanId: string): Promise<void> {
463
557
  const profiler = logger.startTimer();
@@ -477,7 +571,7 @@ export class RepositoryManager {
477
571
  /**
478
572
  * Add a new user to the repository
479
573
  *
480
- * @param {string} email - The email of the user
574
+ * @param email - The email of the user
481
575
  */
482
576
  async addUser(email: string): Promise<void> {
483
577
  const profiler = logger.startTimer();
@@ -504,7 +598,7 @@ export class RepositoryManager {
504
598
  /**
505
599
  * Remove a user from the repository
506
600
  *
507
- * @param {string} email - The email of the user
601
+ * @param email - The email of the user
508
602
  */
509
603
  async removeUser(email: string): Promise<void> {
510
604
  const profiler = logger.startTimer();
@@ -526,10 +620,9 @@ export class RepositoryManager {
526
620
  /**
527
621
  * Update the role of a user in a repository
528
622
  *
529
- * @param {string} email - The email of the user
530
- * @param {string | Record<string, string>} role - The new Role of the user,
531
- * either a string for basic workflow or an object such as `{ "lang_id":
532
- * "Writer" }`
623
+ * @param email - The email of the user
624
+ * @param role - The new Role of the user, either a string for basic workflow
625
+ * or an object such as `{ "lang_id": "Writer" }`
533
626
  *
534
627
  * Example of roles are
535
628
  *
@@ -633,6 +726,7 @@ export type RepositoryConfig = {
633
726
  customTypes?: CustomType[];
634
727
  preview?: Preview;
635
728
  rolePerLocal?: boolean;
729
+ customRoles?: boolean;
636
730
  };
637
731
 
638
732
  export type Preview = {
package/src/types.ts CHANGED
@@ -7,6 +7,8 @@ export type UrlConfig = {
7
7
  authenticationApi: string;
8
8
  /** Prismic migration api base url */
9
9
  migrationApi?: string;
10
+ /** Asset api base url */
11
+ assetApi: string;
10
12
  };
11
13
 
12
14
  export type AuthConfig = {
@@ -27,4 +29,5 @@ export type SetupConfiguration = {
27
29
  authConfig: AuthConfig;
28
30
  manageV2Config?: ManageV2Config;
29
31
  cluster?: string;
32
+ environment?: string;
30
33
  };
@@ -0,0 +1,58 @@
1
+ import axios from "axios";
2
+
3
+ import { logHttpResponse, logger } from "./log";
4
+
5
+ export async function login(
6
+ baseUrl: string,
7
+ email: string,
8
+ password: string,
9
+ ): Promise<{ token: string; cookies: string[] | undefined }> {
10
+ const profiler = logger.startTimer();
11
+
12
+ const payload = {
13
+ email,
14
+ password,
15
+ };
16
+
17
+ const url = transformUrl(baseUrl, "auth.internal");
18
+
19
+ const response = await axios.post<{ token: string }>(
20
+ `${url}/login-e2e`,
21
+ payload,
22
+ {
23
+ headers: {
24
+ "User-Agent": "prismic-cli/prismic-e2e-tests-utils",
25
+ Authorization:
26
+ "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3NDE3ODQ5NDEsImF1ZCI6ImF1dGgwLWFjdGlvbnMiLCJpc3MiOiJwcmlzbWljLmlvIn0.1UPoVMXJSTWtsyNzK5XYftK_mW0ScVuXwCK69nsyf5c",
27
+ },
28
+ },
29
+ );
30
+
31
+ if (response.status !== 200) {
32
+ logHttpResponse(response);
33
+ throw new Error("Could not obtain OAuth token, check your credentials");
34
+ }
35
+
36
+ profiler.done({ message: "logged in via OAuth" });
37
+ const token = response.data.token;
38
+
39
+ return { token, cookies: response.headers["set-cookie"] };
40
+ }
41
+
42
+ function transformUrl(baseUrl: string, subdomainPrefix: string): string {
43
+ try {
44
+ const url = new URL(baseUrl);
45
+ const domainParts = url.hostname.split(".");
46
+
47
+ // Ensure we retain only the main domain (e.g., "platform-wroom.com")
48
+ const mainDomain = domainParts.slice(-2).join("."); // Keeps last two parts of domain
49
+
50
+ // Construct the new hostname with the custom prefix
51
+ const transformedHostname = `${subdomainPrefix}.${mainDomain}`;
52
+
53
+ // Construct and return the full URL with the new hostname
54
+ return `${url.protocol}//${transformedHostname}`;
55
+ } catch (error) {
56
+ throw new Error(`Invalid URL: ${baseUrl}`);
57
+ }
58
+ }