@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.
@@ -1,4 +1,4 @@
1
- import { Credentials } from "../types";
1
+ import { SetupConfiguration, UrlConfig } from "../types";
2
2
 
3
3
  import {
4
4
  AuthenticationClient,
@@ -6,6 +6,7 @@ import {
6
6
  } from "../clients/authenticationApi";
7
7
  import { createCoreApiClient } from "../clients/coreApi";
8
8
  import { createCustomTypesApiClient } from "../clients/customTypesApi";
9
+ import { ManageV2Client } from "../clients/manageV2";
9
10
  import { WroomClient } from "../clients/wroom";
10
11
  import { EnvVariableManager } from "../utils/envVariableManager";
11
12
  import { logHttpResponse, logger } from "../utils/log";
@@ -18,30 +19,29 @@ import { RepositoryConfig, RepositoryManager } from "./repository";
18
19
  * Factory method to create a RepositoriesManager to manage repositories test
19
20
  * data.
20
21
  *
21
- * @param urlConfig - If provided as an object:
22
+ * @param configuration - Global object containing the different configurations
23
+ * required to setup the RepositoriesManager.
22
24
  *
23
- * - {@link UrlConfig.baseURL}: The Prismic base URL.
24
- * - {@link UrlConfig.customTypesApi}: The base URL for the Custom Types API.
25
- * - {@link UrlConfig.authenticationApi}: The base URL for the Authentication API.
26
- *
27
- * If provided as a string: It is treated as the base URL for Prismic. Other
28
- * URLs (Custom Types API, Auth API) will be derived from it.
29
- * @param auth - The authentication credentials
25
+ * - {@link configuration.urlConfig}: Configuration around the API's URLs management.
26
+ * - {@link configuration.authConfig}: Configuration around the authentication of a user.
27
+ * - {@link configuration.manageV2Config}: Optional Configuration used to make calls to ManageV2.
30
28
  *
31
29
  * @returns An instance of {@link RepositoriesManager} to manage test data.
32
30
  */
33
31
  export const createRepositoriesManager = (
34
- urlConfig: UrlConfig | string,
35
- auth: Credentials,
32
+ configuration: SetupConfiguration,
36
33
  ): RepositoriesManager => {
37
- return new RepositoriesManager(urlConfig, auth);
34
+ return new RepositoriesManager(configuration);
38
35
  };
39
36
 
40
37
  /** Utility object to manage Prismic test data */
41
38
  export class RepositoriesManager {
42
- private readonly urls: UrlConfig;
39
+ private readonly config: Omit<SetupConfiguration, "urlConfig"> & {
40
+ urlConfig: UrlConfig;
41
+ };
43
42
  private readonly wroomClient: WroomClient;
44
43
  private readonly authClient: AuthenticationClient;
44
+ private readonly manageV2Client: ManageV2Client;
45
45
  /**
46
46
  * helper to keep track of repositories across independent test phases (setup,
47
47
  * teardown)
@@ -51,22 +51,31 @@ export class RepositoriesManager {
51
51
  );
52
52
 
53
53
  /**
54
- * @param urlConfig - The base URL of the Wroom app.
54
+ * @param configuration - Global object containing the different configuration
55
+ * required to setup the different clients.
55
56
  * @param credentials - Authentication credentials (email and password)
56
57
  */
57
- constructor(
58
- urlConfig: UrlConfig | string,
59
- private readonly credentials: Credentials,
60
- ) {
61
- if (typeof urlConfig === "string") {
58
+ constructor(configuration: SetupConfiguration) {
59
+ this.config = {
60
+ ...configuration,
62
61
  // When a string is provided, treat it as the 'base' property
63
- urlConfig = this.inferUrls(urlConfig);
64
- }
65
- this.urls = urlConfig;
66
- this.wroomClient = new WroomClient(this.urls.baseURL, this.credentials);
62
+ urlConfig:
63
+ typeof configuration.urlConfig === "string"
64
+ ? this.inferUrls(configuration.urlConfig)
65
+ : configuration.urlConfig,
66
+ };
67
+
68
+ this.wroomClient = new WroomClient(
69
+ this.config.urlConfig.baseURL,
70
+ this.config.authConfig,
71
+ );
67
72
  this.authClient = createAuthenticationApiClient(
68
- this.urls.authenticationApi,
69
- this.credentials,
73
+ this.config.urlConfig.authenticationApi,
74
+ this.config.authConfig,
75
+ );
76
+ this.manageV2Client = new ManageV2Client(
77
+ this.config.urlConfig.baseURL,
78
+ this.config.manageV2Config,
70
79
  );
71
80
  }
72
81
 
@@ -149,7 +158,7 @@ export class RepositoriesManager {
149
158
  const post = async () =>
150
159
  this.wroomClient.post(repository, "app/settings/delete", {
151
160
  confirm: repository,
152
- password: this.credentials.password,
161
+ password: this.config.authConfig.password,
153
162
  });
154
163
  const response = await post();
155
164
  if (response.status !== 200) {
@@ -183,27 +192,20 @@ export class RepositoriesManager {
183
192
  */
184
193
  getRepositoryManager(name: string): RepositoryManager {
185
194
  const customTypeClient = createCustomTypesApiClient(
186
- this.urls.customTypesApi,
195
+ this.config.urlConfig.customTypesApi,
187
196
  name,
188
197
  this.authClient,
189
198
  );
190
- const coreApiUrl = getRepositoryUrl(this.urls.baseURL, name);
199
+ const coreApiUrl = getRepositoryUrl(this.config.urlConfig.baseURL, name);
191
200
  const coreApiClient = createCoreApiClient(coreApiUrl, this.authClient);
192
201
 
193
202
  return new RepositoryManager(
194
203
  name,
195
204
  coreApiClient,
205
+ this.authClient,
196
206
  this.wroomClient,
197
207
  customTypeClient,
208
+ this.manageV2Client,
198
209
  );
199
210
  }
200
211
  }
201
-
202
- export type UrlConfig = {
203
- /** Prismic base url */
204
- baseURL: string;
205
- /** Custom types api base url */
206
- customTypesApi: string;
207
- /** Auth service api base url */
208
- authenticationApi: string;
209
- };
@@ -5,8 +5,10 @@ import {
5
5
  SharedSlice,
6
6
  } from "@prismicio/types-internal/lib/customtypes";
7
7
 
8
+ import type { AuthenticationClient } from "../clients/authenticationApi";
8
9
  import type { CoreClient } from "../clients/coreApi";
9
10
  import { CustomTypesClient } from "../clients/customTypesApi";
11
+ import { ManageV2Client } from "../clients/manageV2";
10
12
  import { WroomClient } from "../clients/wroom";
11
13
  import { logHttpResponse, logger } from "../utils/log";
12
14
  import { getRepositoryUrl } from "../utils/urls";
@@ -16,8 +18,10 @@ export class RepositoryManager {
16
18
  constructor(
17
19
  readonly name: string,
18
20
  private readonly coreApiClient: CoreClient,
21
+ private readonly authApiClient: AuthenticationClient,
19
22
  private readonly wroomClient: WroomClient,
20
23
  private readonly customTypesApiClient: CustomTypesClient,
24
+ private readonly manageV2Client: ManageV2Client,
21
25
  ) {}
22
26
 
23
27
  async configure(config: RepositoryConfig): Promise<void> {
@@ -65,6 +69,10 @@ export class RepositoryManager {
65
69
  if (config.preview) {
66
70
  await this.createPreview(config.preview);
67
71
  }
72
+
73
+ if (config.rolePerLocal) {
74
+ await this.toggleRolePerLocal(true);
75
+ }
68
76
  }
69
77
  /** @returns the repository base url, like https://my-repo.prismic.io */
70
78
  getBaseURL(): string {
@@ -318,6 +326,143 @@ export class RepositoryManager {
318
326
  // only return the generated token since there is no need to access more
319
327
  return response.data.wroom_auths[0].token;
320
328
  }
329
+
330
+ /** Create a machine2machine token */
331
+ async createMachine2MachineToken(): Promise<string> {
332
+ const profiler = logger.startTimer();
333
+
334
+ const token = await this.authApiClient.getMachine2MachineToken(this.name);
335
+
336
+ profiler.done({
337
+ message: `machine2machine token created`,
338
+ });
339
+
340
+ return token;
341
+ }
342
+
343
+ /**
344
+ * Toggle the Role per local feature
345
+ *
346
+ * @param enabled - The feature new status
347
+ */
348
+ async toggleRolePerLocal(enabled: boolean): Promise<void> {
349
+ const profiler = logger.startTimer();
350
+
351
+ const response = await this.manageV2Client.toggleRolePerLocal({
352
+ repository: this.name,
353
+ enabled: enabled,
354
+ });
355
+
356
+ this.failIfNot200(
357
+ response,
358
+ `Could not ${enabled ? "enable" : "disable"} Role per local for ${
359
+ this.name
360
+ }`,
361
+ );
362
+ profiler.done({
363
+ message: `Role per local ${enabled ? "enabled" : "disabled"}`,
364
+ });
365
+ }
366
+
367
+ /**
368
+ * Change the Repository Plan
369
+ *
370
+ * @param {string} newPlanId - The Id of the new Plan to apply
371
+ */
372
+ async changePlan(newPlanId: string): Promise<void> {
373
+ const profiler = logger.startTimer();
374
+
375
+ const response = await this.manageV2Client.changePlan({
376
+ repository: this.name,
377
+ newPlanId: newPlanId,
378
+ bypassManualBilling: true, // For now the library does not support Stripe features
379
+ });
380
+
381
+ this.failIfNot200(response, "Could not change the Repository Plan");
382
+ profiler.done({
383
+ message: `Repository Plan changed to ${newPlanId}`,
384
+ });
385
+ }
386
+
387
+ /**
388
+ * Add a new user to the repository
389
+ *
390
+ * @param {string} email - The email of the user
391
+ */
392
+ async addUser(email: string): Promise<void> {
393
+ const profiler = logger.startTimer();
394
+
395
+ const response = await this.manageV2Client.addUserToRepository({
396
+ repository: this.name,
397
+ email: email,
398
+ });
399
+
400
+ this.failIfNot200(response, "Could not add a new user to the repository");
401
+ profiler.done({
402
+ message: `User ${email} was added to the repository`,
403
+ });
404
+ }
405
+
406
+ /**
407
+ * Remove a user from the repository
408
+ *
409
+ * @param {string} email - The email of the user
410
+ */
411
+ async removeUser(email: string): Promise<void> {
412
+ const profiler = logger.startTimer();
413
+
414
+ const response = await this.manageV2Client.removeUserFromRepository({
415
+ repository: this.name,
416
+ email: email,
417
+ });
418
+
419
+ this.failIfNot200(
420
+ response,
421
+ `Could not remove the user ${email} from the repository`,
422
+ );
423
+ profiler.done({
424
+ message: `User ${email} was removed from the repository`,
425
+ });
426
+ }
427
+
428
+ /**
429
+ * Update the role of a user in a repository
430
+ *
431
+ * @param {string} email - The email of the user
432
+ * @param {string | Record<string, string>} role - The new Role of the user,
433
+ * either a string for basic workflow or an object such as `{ "lang_id":
434
+ * "Writer" }`
435
+ *
436
+ * Example of roles are
437
+ *
438
+ * - Administrator
439
+ * - Writer
440
+ * - Contributor
441
+ * - Manager (publisher)
442
+ */
443
+ async updateUserRole(
444
+ email: string,
445
+ role: string | Record<string, string>,
446
+ ): Promise<void> {
447
+ const profiler = logger.startTimer();
448
+
449
+ const response =
450
+ typeof role === "string"
451
+ ? await this.wroomClient.updateRole(this.name, email, role)
452
+ : await this.wroomClient.updateRolePerLocal(this.name, email, role);
453
+
454
+ this.failIfNot200(
455
+ response,
456
+ `Could not update the ${
457
+ typeof role === "string" ? "role" : "rolePerLocal"
458
+ } of the user ${email}`,
459
+ );
460
+ profiler.done({
461
+ message: `Updated User's ${
462
+ typeof role === "string" ? "role" : "rolePerLocal"
463
+ } for user ${email}`,
464
+ });
465
+ }
321
466
  }
322
467
 
323
468
  export type RepositoryConfig = {
@@ -327,6 +472,7 @@ export type RepositoryConfig = {
327
472
  slices?: SharedSlice[];
328
473
  customTypes?: CustomType[];
329
474
  preview?: Preview;
475
+ rolePerLocal?: boolean;
330
476
  };
331
477
 
332
478
  export type Preview = {
package/src/types.ts CHANGED
@@ -1 +1,27 @@
1
- export type Credentials = { email: string; password: string };
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
+
10
+ export type AuthConfig = {
11
+ email: string;
12
+ password: string;
13
+ };
14
+
15
+ export type ManageV2Config = {
16
+ secret: string;
17
+ audience: string;
18
+ };
19
+
20
+ // Global configuration Object
21
+ export type SetupConfiguration = {
22
+ // If provided as a string: It is treated as the base URL for Prismic. Other
23
+ // URLs (Custom Types API, Auth API) will be derived from it.
24
+ urlConfig: UrlConfig | string;
25
+ authConfig: AuthConfig;
26
+ manageV2Config?: ManageV2Config;
27
+ };