Skip to content

Commit 8da3a8a

Browse files
authored
PAV-55: Adicionar credentials: include nas chamadas fetch do jobsService (#143)
## Descrição Adiciona `credentials: "include"` em todas as chamadas `fetch()` do `jobsService.ts` para que o cookie de sessão (`vagas_session`) seja enviado nas requests. Sem essa opção, todas as rotas protegidas retornavam 401 mesmo com o usuário autenticado. Outros serviços do projeto (como `authService.ts` e o `AuthContext` com axios) já usavam essa configuração. ## Linear link https://linear.app/tatame/issue/PAV-55/adicionar-credentials-include-nas-chamadas-fetch-do-jobsservice ## Como foi testado Backend: 305/305 passando | Frontend: 143/148 (5 falhando — falhas pré-existentes nos testes de `jobsService`, não introduzidas por esta mudança) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
2 parents ec2e49d + 3e8e30f commit 8da3a8a

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

frontend/src/services/jobsService.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ function readMessage(payload: unknown): string | undefined {
5858
}
5959

6060
export async function fetchJobFiles(): Promise<JobFile[]> {
61-
const response = await fetch(buildApiUrl("/api/jobs/files"));
61+
const response = await fetch(buildApiUrl("/api/jobs/files"), {
62+
credentials: "include",
63+
});
6264
const payload = (await readPayload(response)) as { files?: unknown } & Record<
6365
string,
6466
unknown
@@ -86,7 +88,9 @@ export async function fetchJobFiles(): Promise<JobFile[]> {
8688

8789
export async function fetchJobsByFile(fileName: string): Promise<JobsResponse> {
8890
const suffix = fileName ? `?file=${encodeURIComponent(fileName)}` : "";
89-
const response = await fetch(buildApiUrl(`/api/jobs${suffix}`));
91+
const response = await fetch(buildApiUrl(`/api/jobs${suffix}`), {
92+
credentials: "include",
93+
});
9094
const payload = (await readPayload(response)) as Record<string, unknown>;
9195

9296
if (!response.ok) {
@@ -106,7 +110,9 @@ export async function fetchJobsByFile(fileName: string): Promise<JobsResponse> {
106110
}
107111

108112
export async function fetchKeywords(): Promise<string[]> {
109-
const response = await fetch(buildApiUrl("/api/keywords"));
113+
const response = await fetch(buildApiUrl("/api/keywords"), {
114+
credentials: "include",
115+
});
110116
const payload = (await readPayload(response)) as {
111117
keywords?: unknown;
112118
} & Record<string, unknown>;
@@ -125,6 +131,7 @@ export async function saveKeywords(keywords: string[]): Promise<void> {
125131
"Content-Type": "application/json",
126132
},
127133
body: JSON.stringify({ keywords }),
134+
credentials: "include",
128135
});
129136
const payload = (await readPayload(response)) as Record<string, unknown>;
130137

@@ -136,6 +143,7 @@ export async function saveKeywords(keywords: string[]): Promise<void> {
136143
export async function runScraperRequest(): Promise<void> {
137144
const response = await fetch(buildApiUrl("/api/jobs/search"), {
138145
method: "POST",
146+
credentials: "include",
139147
});
140148
const payload = (await readPayload(response)) as Record<string, unknown>;
141149

frontend/tests/unit/utils/jobsService.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe("jobsService", () => {
2323
vi.stubGlobal("fetch", fetchMock);
2424

2525
await fetchJobFiles();
26-
expect(fetchMock).toHaveBeenCalledWith("https://jobsglobalscraper.ddns.net/api/jobs/files");
26+
expect(fetchMock).toHaveBeenCalledWith("https://jobsglobalscraper.ddns.net/api/jobs/files", { credentials: "include" });
2727
});
2828

2929
it("filtra entradas invalidas ao listar arquivos", async () => {
@@ -68,7 +68,7 @@ describe("jobsService", () => {
6868
const response = await fetchJobsByFile("vagas.xlsx");
6969
expect(response.total).toBe(1);
7070
expect(response.file).toBe("vagas.xlsx");
71-
expect(fetchMock).toHaveBeenCalledWith("/api/jobs?file=vagas.xlsx");
71+
expect(fetchMock).toHaveBeenCalledWith("/api/jobs?file=vagas.xlsx", { credentials: "include" });
7272
});
7373

7474
it("usa endpoint sem sufixo quando fileName vazio", async () => {
@@ -81,7 +81,7 @@ describe("jobsService", () => {
8181
);
8282

8383
await fetchJobsByFile("");
84-
expect(fetch).toHaveBeenCalledWith("/api/jobs");
84+
expect(fetch).toHaveBeenCalledWith("/api/jobs", { credentials: "include" });
8585
});
8686

8787
it("normaliza payload invalido ao buscar jobs", async () => {
@@ -92,7 +92,7 @@ describe("jobsService", () => {
9292

9393
const response = await fetchJobsByFile("vagas com espaco.xlsx");
9494
expect(response).toEqual({ jobs: [], file: "", modifiedAt: null, total: 0 });
95-
expect(fetch).toHaveBeenCalledWith("/api/jobs?file=vagas%20com%20espaco.xlsx");
95+
expect(fetch).toHaveBeenCalledWith("/api/jobs?file=vagas%20com%20espaco.xlsx", { credentials: "include" });
9696
});
9797

9898
it("lanca erro com mensagem da API ao buscar jobs", async () => {
@@ -173,6 +173,7 @@ describe("jobsService", () => {
173173
method: "POST",
174174
headers: { "Content-Type": "application/json" },
175175
body: JSON.stringify({ keywords: ["node", "vitest"] }),
176+
credentials: "include",
176177
});
177178
});
178179

0 commit comments

Comments
 (0)