diff --git a/.changeset/in-store-password-token.md b/.changeset/in-store-password-token.md new file mode 100644 index 00000000..c087edf3 --- /dev/null +++ b/.changeset/in-store-password-token.md @@ -0,0 +1,5 @@ +--- +"@labdigital/commercetools-mock": patch +--- + +Scope the in-store customer `password-token` endpoint to the store in the path, so it no longer issues a token for a customer with the same email in another store. diff --git a/src/repositories/customer/index.ts b/src/repositories/customer/index.ts index 887b1089..634711ea 100644 --- a/src/repositories/customer/index.ts +++ b/src/repositories/customer/index.ts @@ -168,12 +168,13 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> { async passwordResetToken( context: RepositoryContext, request: CustomerCreatePasswordResetToken, + storeWhere: string[] = [], ): Promise { const results = await this._storage.query( context.projectKey, this.getTypeId(), { - where: [`email="${request.email.toLocaleLowerCase()}"`], + where: [`email="${request.email.toLocaleLowerCase()}"`, ...storeWhere], }, ); if (results.count === 0) { diff --git a/src/services/customer.test.ts b/src/services/customer.test.ts index af1d66b6..8ccbd73c 100644 --- a/src/services/customer.test.ts +++ b/src/services/customer.test.ts @@ -1094,6 +1094,7 @@ describe("Customer email verification", () => { describe("Customer in-store endpoints", () => { const storeFactory = storeDraftFactory(ctMock); + const customerFactory = customerDraftFactory(ctMock); test("returns a 404 when the store in the path doesn't exist", async () => { const res = await ctMock.app.inject({ @@ -1159,4 +1160,53 @@ describe("Customer in-store endpoints", () => { { typeId: "store", key: "store-bal" }, ]); }); + + test("issues a password token for the customer in the path's store", async () => { + await storeFactory.create({ key: "store-a" }); + await storeFactory.create({ key: "store-b" }); + await storeFactory.create({ key: "store-c" }); + + // The other store's customer is created first, so an unscoped lookup + // would return it + await customerFactory.create({ + email: "shared@example.com", + stores: [{ typeId: "store", key: "store-a" }], + }); + const customerB = await customerFactory.create({ + email: "shared@example.com", + stores: [{ typeId: "store", key: "store-b" }], + }); + + const token = await ctMock.app.inject({ + method: "POST", + url: "/dummy/in-store/key=store-b/customers/password-token", + payload: { email: "shared@example.com" }, + }); + expect(token.statusCode, JSON.stringify(token.json())).toBe(200); + expect(token.json()).toEqual({ + id: expect.any(String), + createdAt: expect.any(String), + lastModifiedAt: expect.any(String), + customerId: customerB.id, + expiresAt: expect.any(String), + value: expect.any(String), + invalidateOlderTokens: false, + }); + + const missing = await ctMock.app.inject({ + method: "POST", + url: "/dummy/in-store/key=store-c/customers/password-token", + payload: { email: "shared@example.com" }, + }); + expect(missing.json()).toEqual({ + statusCode: 400, + message: "The Customer with ID 'shared@example.com' was not found.", + errors: [ + { + code: "ResourceNotFound", + message: "The Customer with ID 'shared@example.com' was not found.", + }, + ], + }); + }); }); diff --git a/src/services/customer.ts b/src/services/customer.ts index 7046b6c0..75cafdbd 100644 --- a/src/services/customer.ts +++ b/src/services/customer.ts @@ -78,6 +78,7 @@ export class CustomerService extends AbstractService { const customer = await this.repository.passwordResetToken( getRepositoryContext(request), request.body, + this.storeWhere(request), ); return reply.status(200).send(customer);