Skip to content
Merged
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
43 changes: 40 additions & 3 deletions backend/tests/interviewExperienceController.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,51 @@ describe("createInterviewExperience", () => {
expect(res.status).toHaveBeenCalledWith(201);
});

it("isolates anonymous submissions by clientKey so different visitors with the same key both persist", async () => {
it("returns the existing submission when the same authenticated user retries the same idempotency key", async () => {
InterviewExperience.findOne = vi.fn().mockResolvedValue(sampleDoc);
InterviewExperience.create = vi.fn();

const userA = { _id: "507f1f77bcf86cd799439011" };
const req = makeReq(
{
company: "Google",
role: "SDE-2",
summary: "Summary",
idempotencyKey: "submit-key-abc12345",
},
{},
{},
userA,
);
const res = makeRes();

await createInterviewExperience(req, res);

expect(InterviewExperience.findOne).toHaveBeenCalledWith({
idempotencyKey: "submit-key-abc12345",
userId: "507f1f77bcf86cd799439011",
});
expect(InterviewExperience.create).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith(
expect.objectContaining({
success: true,
experience: expect.objectContaining({
id: "507f1f77bcf86cd799439011",
}),
}),
);
});

it("does not deduplicate an anonymous request against an authenticated submission with the same idempotency key", async () => {
InterviewExperience.findOne = vi.fn().mockResolvedValue(null);
InterviewExperience.create = vi.fn().mockResolvedValue(sampleDoc);

const req = makeReq({
company: "Google",
role: "SDE-2",
summary: "Summary",
clientKey: "22222222-2222-4222-8222-222222222222",
clientKey: "11111111-1111-4111-8111-111111111111",
idempotencyKey: "shared-key-abc12345",
});
const res = makeRes();
Expand All @@ -223,9 +259,10 @@ describe("createInterviewExperience", () => {

expect(InterviewExperience.findOne).toHaveBeenCalledWith({
idempotencyKey: "shared-key-abc12345",
clientKey: "22222222-2222-4222-8222-222222222222",
clientKey: "11111111-1111-4111-8111-111111111111",
});
expect(InterviewExperience.create).toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(201);
});
});

Expand Down
Loading