Skip to content

Commit 37a7cd6

Browse files
committed
add missing tests
1 parent 9f5bb48 commit 37a7cd6

File tree

1 file changed

+91
-1
lines changed

1 file changed

+91
-1
lines changed

src/handlers/actions/register-action-handler.test.ts

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,36 @@ describe("RegisterActionHandler", () => {
6161
const result = await handler.handleAction({ apl: mockApl });
6262

6363
expect(result.status).toBe(400);
64-
expect(result.body).toBe("Missing auth token.");
64+
});
65+
66+
it("should return 400 when request body is not an object", async () => {
67+
vi.spyOn(adapter, "getBody").mockResolvedValue("not-an-object" as unknown as object);
68+
69+
const handler = new RegisterActionHandler(adapter);
70+
const result = await handler.handleAction({ apl: mockApl });
71+
72+
expect(result.status).toBe(400);
73+
expect(result.body).toBe("Invalid request json.");
74+
});
75+
76+
it("should return 400 when request body is null", async () => {
77+
vi.spyOn(adapter, "getBody").mockResolvedValue(null);
78+
79+
const handler = new RegisterActionHandler(adapter);
80+
const result = await handler.handleAction({ apl: mockApl });
81+
82+
expect(result.status).toBe(400);
83+
expect(result.body).toBe("Invalid request json.");
84+
});
85+
86+
it("should return 400 when auth_token is not a string", async () => {
87+
vi.spyOn(adapter, "getBody").mockResolvedValue({ auth_token: 123 });
88+
89+
const handler = new RegisterActionHandler(adapter);
90+
const result = await handler.handleAction({ apl: mockApl });
91+
92+
expect(result.status).toBe(400);
93+
expect(result.body).toBe("Invalid request json.");
6594
});
6695

6796
it("should validate allowed Saleor URLs", async () => {
@@ -269,12 +298,32 @@ describe("RegisterActionHandler", () => {
269298
adapter.request,
270299
expect.objectContaining({
271300
authData: mockAuthData,
301+
rawBody: { auth_token: mockAuthData.token },
272302
respondWithError: expect.any(Function),
273303
}),
274304
);
275305
expect(mockApl.set).toHaveBeenCalledWith(mockAuthData);
276306
});
277307

308+
it("should pass rawBody with additionalData to onRequestVerified", async () => {
309+
const bodyWithAdditionalData = {
310+
auth_token: mockAuthData.token,
311+
additionalData: { env: "production", region: "us-east-1" },
312+
};
313+
vi.spyOn(adapter, "getBody").mockResolvedValue(bodyWithAdditionalData);
314+
const onRequestVerified = vi.fn();
315+
316+
const handler = new RegisterActionHandler(adapter);
317+
await handler.handleAction({ apl: mockApl, onRequestVerified });
318+
319+
expect(onRequestVerified).toHaveBeenCalledWith(
320+
adapter.request,
321+
expect.objectContaining({
322+
rawBody: bodyWithAdditionalData,
323+
}),
324+
);
325+
});
326+
278327
it("should map and return error when onRequestVerified calls respondWithError", async () => {
279328
const errorMessage = "Verification failed";
280329
const onRequestVerified = vi.fn().mockImplementation((_req, { respondWithError }) => {
@@ -334,11 +383,31 @@ describe("RegisterActionHandler", () => {
334383
adapter.request,
335384
expect.objectContaining({
336385
authData: mockAuthData,
386+
rawBody: { auth_token: mockAuthData.token },
337387
respondWithError: expect.any(Function),
338388
}),
339389
);
340390
});
341391

392+
it("should pass rawBody with additionalData to onAuthAplSaved", async () => {
393+
const bodyWithAdditionalData = {
394+
auth_token: mockAuthData.token,
395+
additionalData: { customField: "value" },
396+
};
397+
vi.spyOn(adapter, "getBody").mockResolvedValue(bodyWithAdditionalData);
398+
const onAuthAplSaved = vi.fn();
399+
400+
const handler = new RegisterActionHandler(adapter);
401+
await handler.handleAction({ apl: mockApl, onAuthAplSaved });
402+
403+
expect(onAuthAplSaved).toHaveBeenCalledWith(
404+
adapter.request,
405+
expect.objectContaining({
406+
rawBody: bodyWithAdditionalData,
407+
}),
408+
);
409+
});
410+
342411
it("should map and return error when onAuthAplSaved calls respondWithError", async () => {
343412
const errorMessage = "Post-save validation failed";
344413
const onAuthAplSaved = vi.fn().mockImplementation((_req, { respondWithError }) => {
@@ -399,13 +468,34 @@ describe("RegisterActionHandler", () => {
399468
expect.objectContaining({
400469
authData: mockAuthData,
401470
error: aplError,
471+
rawBody: { auth_token: mockAuthData.token },
402472
respondWithError: expect.any(Function),
403473
}),
404474
);
405475
const body = result.body as RegisterHandlerResponseBody;
406476
expect(body.success).toBe(false);
407477
});
408478

479+
it("should pass rawBody with additionalData to onAplSetFailed", async () => {
480+
const bodyWithAdditionalData = {
481+
auth_token: mockAuthData.token,
482+
additionalData: { debug: true },
483+
};
484+
vi.spyOn(adapter, "getBody").mockResolvedValue(bodyWithAdditionalData);
485+
const onAplSetFailed = vi.fn();
486+
mockApl.set.mockRejectedValue(new Error("APL save error"));
487+
488+
const handler = new RegisterActionHandler(adapter);
489+
await handler.handleAction({ apl: mockApl, onAplSetFailed });
490+
491+
expect(onAplSetFailed).toHaveBeenCalledWith(
492+
adapter.request,
493+
expect.objectContaining({
494+
rawBody: bodyWithAdditionalData,
495+
}),
496+
);
497+
});
498+
409499
it("should map and return error when onAplSetFailed calls respondWithError", async () => {
410500
const errorMessage = "Custom error handling";
411501
const onAplSetFailed = vi.fn().mockImplementation((_req, { respondWithError }) => {

0 commit comments

Comments
 (0)