fix(medusa): throw proper error when product doesn't exist w/ key header (#5745)

**What**
- ensure that an error is raised properly if the requested product doesn't exist and a publishable-key header is set

**Why**
- previously endpoints would hang if the product didn't exist and requests would time out


closes #5724
This commit is contained in:
Philip Korsholm
2023-11-28 10:24:50 +00:00
committed by GitHub
parent 870d686136
commit e4bfa6c88a
4 changed files with 92 additions and 24 deletions

View File

@@ -385,13 +385,11 @@ describe("Publishable API keys", () => {
})
await dbConnection.manager.query(
`INSERT INTO
publishable_api_key_sales_channel
(publishable_key_id, sales_channel_id)
VALUES
('${pubKeyId}', '${salesChannel1.id}'),
('${pubKeyId}', '${salesChannel2.id}'),
('${pubKeyId}', '${salesChannel3.id}');`
`INSERT INTO publishable_api_key_sales_channel
(publishable_key_id, sales_channel_id)
VALUES ('${pubKeyId}', '${salesChannel1.id}'),
('${pubKeyId}', '${salesChannel2.id}'),
('${pubKeyId}', '${salesChannel3.id}');`
)
})
@@ -468,12 +466,10 @@ describe("Publishable API keys", () => {
})
await dbConnection.manager.query(
`INSERT INTO
publishable_api_key_sales_channel
`INSERT INTO publishable_api_key_sales_channel
(publishable_key_id, sales_channel_id)
VALUES
('${pubKeyId}', '${salesChannel1.id}'),
('${pubKeyId}', '${salesChannel2.id}');`
VALUES ('${pubKeyId}', '${salesChannel1.id}'),
('${pubKeyId}', '${salesChannel2.id}');`
)
})
@@ -856,6 +852,62 @@ describe("Publishable API keys", () => {
expect(response.status).toEqual(400)
})
it("should return 404 when the requested variant doesn't exist", async () => {
const api = useApi()
await api.post(
`/admin/publishable-api-keys/${pubKeyId}/sales-channels/batch`,
{
sales_channel_ids: [{ id: salesChannel1.id }],
},
adminHeaders
)
const response = await api
.get(`/store/variants/does-not-exist`, {
headers: {
"x-medusa-access-token": "test_token",
"x-publishable-api-key": pubKeyId,
},
})
.catch((err) => {
return err.response
})
expect(response.status).toEqual(404)
expect(response.data.message).toEqual(
"Variant with id: does-not-exist was not found"
)
})
it("should return 404 when the requested product doesn't exist", async () => {
const api = useApi()
await api.post(
`/admin/publishable-api-keys/${pubKeyId}/sales-channels/batch`,
{
sales_channel_ids: [{ id: salesChannel1.id }],
},
adminHeaders
)
const response = await api
.get(`/store/products/does-not-exist`, {
headers: {
"x-medusa-access-token": "test_token",
"x-publishable-api-key": pubKeyId,
},
})
.catch((err) => {
return err.response
})
expect(response.status).toEqual(404)
expect(response.data.message).toEqual(
"Product with id: does-not-exist was not found"
)
})
it("correctly returns a product if passed PK has no associated SCs", async () => {
const api = useApi()