Skip to content

Commit b7a7749

Browse files
committed
Prevent save data in apl when already exists
1 parent 6527b0e commit b7a7749

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/handlers/next/create-app-register-handler.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,34 @@ describe("create-app-register-handler", () => {
8888
expect(res._getData().success).toBe(false);
8989
});
9090

91+
it("Return 409 if APL already contains auth data", async () => {
92+
const { res, req } = createMocks({
93+
/**
94+
* Use body, instead of params, otherwise - for some reason - param is not accessible in mock request
95+
* Maybe this is a bug https://github.com/howardabrams/node-mocks-http/blob/master/lib/mockRequest.js
96+
*/
97+
body: {
98+
auth_token: "mock-auth-token",
99+
},
100+
headers: {
101+
host: "some-saleor-host.cloud",
102+
"x-forwarded-proto": "https",
103+
"saleor-api-url": "https://example.com/graphql/",
104+
"saleor-domain": "https://example.com/",
105+
},
106+
method: "POST",
107+
});
108+
109+
const handler = createAppRegisterHandler({
110+
apl: mockApl,
111+
});
112+
113+
await handler(req, res);
114+
115+
expect(mockApl.set).not.toHaveBeenCalled();
116+
expect(res._getStatusCode()).toBe(409);
117+
});
118+
91119
describe("Callback hooks", () => {
92120
it("Runs callback hooks - successful saving to APL scenario", async () => {
93121
const mockOnRequestStart = vi.fn();

src/handlers/next/create-app-register-handler.ts

+20
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,26 @@ export const createAppRegisterHandler = ({
240240
}
241241
}
242242

243+
try {
244+
const checkApl = await apl.get(saleorApiUrl);
245+
246+
if (checkApl !== undefined) {
247+
debug("APL already exists for this Saleor instance");
248+
249+
return new Response(
250+
createRegisterHandlerResponseBody(false, {
251+
code: "APL_ALREADY_EXISTS",
252+
message: "APL already exists for this Saleor instance",
253+
}),
254+
{
255+
status: 409,
256+
}
257+
);
258+
}
259+
} catch (error) {
260+
handleHookError(error);
261+
}
262+
243263
try {
244264
await apl.set(authData);
245265

0 commit comments

Comments
 (0)