Skip to content

Commit 8107d16

Browse files
committed
feat: update error responses to include code and message structure for authentication
1 parent e7e7b56 commit 8107d16

4 files changed

Lines changed: 32 additions & 11 deletions

File tree

backend/tests/integration/middleware/authGuards.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ describe("Integration - Auth Guards (requireAuth)", () => {
3333
async ({ method, path }) => {
3434
const res = await request(app)[method](path).expect(401);
3535

36-
expect(res.body).toEqual({ message: "Não autenticado." });
36+
expect(res.body).toEqual({
37+
code: "UNAUTHORIZED",
38+
message: "Não autenticado.",
39+
});
3740
},
3841
);
3942

@@ -43,7 +46,10 @@ describe("Integration - Auth Guards (requireAuth)", () => {
4346
.send({ email: "invalido", password: "x" });
4447

4548
expect(res.status).not.toBe(401);
46-
expect(res.body).not.toEqual({ message: "Não autenticado." });
49+
expect(res.body).not.toEqual({
50+
code: "UNAUTHORIZED",
51+
message: "Não autenticado.",
52+
});
4753
});
4854

4955
it("permite /health sem autenticação", async () => {

backend/tests/integration/routes/auth.routes.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,10 @@ describe("Integration - Auth Routes", () => {
591591

592592
const res = await request(app).get(`${BASE}/me`).expect(401);
593593

594-
expect(res.body).toHaveProperty("error", "Não autenticado");
594+
expect(res.body).toEqual({
595+
code: "UNAUTHORIZED",
596+
message: "Não autenticado.",
597+
});
595598
expect(session.destroy).toHaveBeenCalled();
596599
});
597600
});
@@ -636,7 +639,10 @@ describe("Integration - Auth Routes", () => {
636639

637640
const meRes = await request(app).get(`${BASE}/me`).expect(401);
638641

639-
expect(meRes.body).toHaveProperty("error", "Não autenticado");
642+
expect(meRes.body).toEqual({
643+
code: "UNAUTHORIZED",
644+
message: "Não autenticado.",
645+
});
640646
});
641647
});
642648
});

backend/tests/integration/routes/savedJobs.routes.test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ describe("Integration - SavedJobs Routes", () => {
120120

121121
const res = await request(app).get(BASE).expect(401);
122122

123-
expect(res.body).toEqual({ message: "Não autenticado." });
123+
expect(res.body).toEqual({
124+
code: "UNAUTHORIZED",
125+
message: "Não autenticado.",
126+
});
124127
});
125128

126129
it("retorna 500 quando getAll lança erro", async () => {
@@ -159,7 +162,10 @@ describe("Integration - SavedJobs Routes", () => {
159162

160163
const res = await request(app).get(`${BASE}/job-outro-user`).expect(404);
161164

162-
expect(res.body).toHaveProperty("error", "Vaga não encontrada");
165+
expect(res.body).toEqual({
166+
code: "NOT_FOUND",
167+
message: "Vaga não encontrada",
168+
});
163169
expect(mockSavedJobsService.getById).toHaveBeenCalledWith(
164170
"user_abc",
165171
"job-outro-user",
@@ -174,9 +180,6 @@ describe("Integration - SavedJobs Routes", () => {
174180
await request(app).get(`${BASE}/job-1`).expect(401);
175181
});
176182
});
177-
178-
// ── POST / ────────────────────────────────────────────────────────────────
179-
180183
describe("POST /", () => {
181184
it("cria vaga e retorna 201", async () => {
182185
const res = await request(app).post(BASE).send(createPayload).expect(201);

backend/tests/unit/middleware/requireAuth.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ describe("requireAuth", () => {
1919
requireAuth(req, res, next);
2020

2121
expect(status).toHaveBeenCalledWith(401);
22-
expect(json).toHaveBeenCalledWith({ message: "Não autenticado." });
22+
expect(json).toHaveBeenCalledWith({
23+
code: "UNAUTHORIZED",
24+
message: "Não autenticado.",
25+
});
2326
expect(next).not.toHaveBeenCalled();
2427
});
2528

@@ -29,7 +32,10 @@ describe("requireAuth", () => {
2932
requireAuth(req, res, next);
3033

3134
expect(status).toHaveBeenCalledWith(401);
32-
expect(json).toHaveBeenCalledWith({ message: "Não autenticado." });
35+
expect(json).toHaveBeenCalledWith({
36+
code: "UNAUTHORIZED",
37+
message: "Não autenticado.",
38+
});
3339
expect(next).not.toHaveBeenCalled();
3440
});
3541

0 commit comments

Comments
 (0)