Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/in-store-password-token.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 2 additions & 1 deletion src/repositories/customer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,13 @@ export class CustomerRepository extends AbstractResourceRepository<"customer"> {
async passwordResetToken(
context: RepositoryContext,
request: CustomerCreatePasswordResetToken,
storeWhere: string[] = [],
): Promise<CustomerToken> {
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) {
Expand Down
50 changes: 50 additions & 0 deletions src/services/customer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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.",
},
],
});
});
});
1 change: 1 addition & 0 deletions src/services/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading