@labdigital/commercetools-mock 2.64.0 → 2.65.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@labdigital/commercetools-mock",
3
- "version": "2.64.0",
3
+ "version": "2.65.0",
4
4
  "license": "MIT",
5
5
  "author": "Michael van Tellingen",
6
6
  "type": "module",
@@ -23,6 +23,7 @@ import {
23
23
  createEmailVerifyToken,
24
24
  createPasswordResetToken,
25
25
  hashPassword,
26
+ validateEmailVerifyToken,
26
27
  validatePasswordResetToken,
27
28
  } from "#src/lib/password.ts";
28
29
  import type { ResourceMap, ShallowWritable, Writable } from "#src/types.ts";
@@ -231,7 +232,7 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> {
231
232
  return customer;
232
233
  }
233
234
 
234
- verifyEmailToken(context: RepositoryContext, id: string): CustomerToken {
235
+ emailToken(context: RepositoryContext, id: string): CustomerToken {
235
236
  const results = this._storage.query(context.projectKey, this.getTypeId(), {
236
237
  where: [`id="${id.toLocaleLowerCase()}"`],
237
238
  });
@@ -257,6 +258,39 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> {
257
258
  };
258
259
  }
259
260
 
261
+ emailTokenConfirm(
262
+ context: RepositoryContext,
263
+ request: { tokenValue: string },
264
+ ) {
265
+ const customerId = validateEmailVerifyToken(request.tokenValue);
266
+ if (!customerId) {
267
+ throw new CommercetoolsError<ResourceNotFoundError>({
268
+ code: "ResourceNotFound",
269
+ message: `The Customer with ID 'Token(${request.tokenValue})' was not found.`,
270
+ });
271
+ }
272
+
273
+ const customer = this._storage.get(
274
+ context.projectKey,
275
+ "customer",
276
+ customerId,
277
+ ) as Writable<Customer> | undefined;
278
+
279
+ if (!customer) {
280
+ throw new CommercetoolsError<ResourceNotFoundError>({
281
+ code: "ResourceNotFound",
282
+ message: `The Customer with ID 'Token(${request.tokenValue})' was not found.`,
283
+ });
284
+ }
285
+
286
+ customer.isEmailVerified = true;
287
+ customer.version += 1;
288
+
289
+ // Update storage
290
+ this._storage.add(context.projectKey, "customer", customer);
291
+ return customer;
292
+ }
293
+
260
294
  private storeReferenceToStoreKeyReference(
261
295
  draftStores: StoreResourceIdentifier[],
262
296
  projectKey: string,
@@ -925,3 +925,53 @@ describe("Customer Password Reset", () => {
925
925
  });
926
926
  });
927
927
  });
928
+
929
+ describe("Customer email verification", () => {
930
+ test("creates an email token", async () => {
931
+ const customer = await customerDraftFactory(ctMock).create();
932
+
933
+ const response = await supertest(ctMock.app)
934
+ .post(`/dummy/customers/email-token`)
935
+ .send({
936
+ id: customer.id,
937
+ ttlMinutes: 60,
938
+ });
939
+
940
+ expect(response.status, JSON.stringify(response.body)).toBe(200);
941
+ expect(response.body).toMatchObject({
942
+ customerId: customer.id,
943
+ invalidateOlderTokens: false,
944
+ id: expect.any(String),
945
+ value: expect.any(String),
946
+ });
947
+
948
+ const dateTime = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
949
+ expect(response.body.createdAt).toMatch(dateTime);
950
+ expect(response.body.lastModifiedAt).toMatch(dateTime);
951
+ expect(response.body.expiresAt).toMatch(dateTime);
952
+ });
953
+
954
+ test("validates an email token", async () => {
955
+ const customer = await customerDraftFactory(ctMock).create({
956
+ isEmailVerified: false,
957
+ });
958
+
959
+ const tokenResponse = await supertest(ctMock.app)
960
+ .post(`/dummy/customers/email-token`)
961
+ .send({
962
+ id: customer.id,
963
+ ttlMinutes: 60,
964
+ });
965
+
966
+ const response = await supertest(ctMock.app)
967
+ .post(`/dummy/customers/email/confirm`)
968
+ .send({
969
+ id: customer.id,
970
+ tokenValue: tokenResponse.body.value,
971
+ });
972
+
973
+ expect(response.status, JSON.stringify(response.body)).toBe(200);
974
+ expect(response.body.id).toEqual(customer.id);
975
+ expect(response.body.isEmailVerified).toEqual(true);
976
+ });
977
+ });
@@ -19,7 +19,8 @@ export class CustomerService extends AbstractService {
19
19
  extraRoutes(parent: Router) {
20
20
  parent.post("/password-token", this.passwordResetToken.bind(this));
21
21
  parent.post("/password/reset", this.passwordReset.bind(this));
22
- parent.post("/email-token", this.confirmEmailToken.bind(this));
22
+ parent.post("/email-token", this.emailToken.bind(this));
23
+ parent.post("/email/confirm", this.emailTokenConfirm.bind(this));
23
24
  }
24
25
 
25
26
  post(request: Request, response: Response) {
@@ -54,12 +55,18 @@ export class CustomerService extends AbstractService {
54
55
  response.status(200).send(customer);
55
56
  }
56
57
 
57
- confirmEmailToken(request: Request, response: Response) {
58
+ emailToken(request: Request, response: Response) {
58
59
  const id = request.body.id;
59
- const token = this.repository.verifyEmailToken(
60
+ const token = this.repository.emailToken(getRepositoryContext(request), id);
61
+ response.status(200).send(token);
62
+ }
63
+
64
+ emailTokenConfirm(request: Request, response: Response) {
65
+ const customer = this.repository.emailTokenConfirm(
60
66
  getRepositoryContext(request),
61
- id,
67
+ request.body,
62
68
  );
63
- response.status(200).send(token);
69
+
70
+ response.status(200).send(customer);
64
71
  }
65
72
  }