@@ -117,7 +117,9 @@ describe("Integration - SavedJobs Routes", () => {
117117 userId : undefined ,
118118 } as any ) ;
119119
120- await request ( app ) . get ( BASE ) . expect ( 401 ) ;
120+ const res = await request ( app ) . get ( BASE ) . expect ( 401 ) ;
121+
122+ expect ( res . body ) . toEqual ( { message : "Não autenticado." } ) ;
121123 } ) ;
122124
123125 it ( "retorna 500 quando getAll lança erro" , async ( ) => {
@@ -151,6 +153,18 @@ describe("Integration - SavedJobs Routes", () => {
151153 await request ( app ) . get ( `${ BASE } /inexistente` ) . expect ( 404 ) ;
152154 } ) ;
153155
156+ it ( "retorna 404 quando vaga pertence a outro usuário (ownership)" , async ( ) => {
157+ mockSavedJobsService . getById . mockResolvedValueOnce ( undefined ) ;
158+
159+ const res = await request ( app ) . get ( `${ BASE } /job-outro-user` ) . expect ( 404 ) ;
160+
161+ expect ( res . body ) . toHaveProperty ( "error" , "Vaga não encontrada" ) ;
162+ expect ( mockSavedJobsService . getById ) . toHaveBeenCalledWith (
163+ "user_abc" ,
164+ "job-outro-user" ,
165+ ) ;
166+ } ) ;
167+
154168 it ( "retorna 401 quando sessão não tem userId" , async ( ) => {
155169 vi . mocked ( getIronSession ) . mockResolvedValueOnce ( {
156170 userId : undefined ,
@@ -228,6 +242,78 @@ describe("Integration - SavedJobs Routes", () => {
228242 } ) ;
229243 } ) ;
230244
245+ // ── PATCH /:id — transições de status (eventos de candidatura) ─────────────
246+
247+ describe ( "PATCH /:id — transições de status" , ( ) => {
248+ it . each ( [
249+ "applied" ,
250+ "interviewing" ,
251+ "rejected" ,
252+ "accepted" ,
253+ ] as const ) ( "atualiza status para %s" , async ( status ) => {
254+ mockSavedJobsService . update . mockResolvedValueOnce ( {
255+ ...fixtureJob ,
256+ status,
257+ } ) ;
258+
259+ const res = await request ( app )
260+ . patch ( `${ BASE } /job-1` )
261+ . send ( { status } )
262+ . expect ( 200 ) ;
263+
264+ expect ( res . body . status ) . toBe ( status ) ;
265+ expect ( mockSavedJobsService . update ) . toHaveBeenCalledWith (
266+ "user_abc" ,
267+ "job-1" ,
268+ expect . objectContaining ( { status } ) ,
269+ ) ;
270+ } ) ;
271+
272+ it ( "persiste appliedAt ao registrar candidatura enviada" , async ( ) => {
273+ const appliedAt = "2024-06-15T10:00:00.000Z" ;
274+ mockSavedJobsService . update . mockResolvedValueOnce ( {
275+ ...fixtureJob ,
276+ status : "applied" ,
277+ appliedAt,
278+ } ) ;
279+
280+ const res = await request ( app )
281+ . patch ( `${ BASE } /job-1` )
282+ . send ( { status : "applied" , appliedAt } )
283+ . expect ( 200 ) ;
284+
285+ expect ( res . body . status ) . toBe ( "applied" ) ;
286+ expect ( mockSavedJobsService . update ) . toHaveBeenCalledWith (
287+ "user_abc" ,
288+ "job-1" ,
289+ expect . objectContaining ( {
290+ status : "applied" ,
291+ appliedAt : expect . any ( Date ) ,
292+ } ) ,
293+ ) ;
294+ } ) ;
295+
296+ it ( "simula pipeline completo saved → applied → interviewing → accepted" , async ( ) => {
297+ const pipeline = [ "applied" , "interviewing" , "accepted" ] as const ;
298+
299+ for ( const status of pipeline ) {
300+ mockSavedJobsService . update . mockResolvedValueOnce ( {
301+ ...fixtureJob ,
302+ status,
303+ } ) ;
304+
305+ const res = await request ( app )
306+ . patch ( `${ BASE } /job-1` )
307+ . send ( { status } )
308+ . expect ( 200 ) ;
309+
310+ expect ( res . body . status ) . toBe ( status ) ;
311+ }
312+
313+ expect ( mockSavedJobsService . update ) . toHaveBeenCalledTimes ( pipeline . length ) ;
314+ } ) ;
315+ } ) ;
316+
231317 // ── PATCH /:id ────────────────────────────────────────────────────────────
232318
233319 describe ( "PATCH /:id" , ( ) => {
@@ -324,5 +410,14 @@ describe("Integration - SavedJobs Routes", () => {
324410
325411 await request ( app ) . delete ( `${ BASE } /job-1` ) . expect ( 500 ) ;
326412 } ) ;
413+
414+ it ( "retorna 204 mesmo quando vaga não pertence ao usuário (ownership silencioso)" , async ( ) => {
415+ await request ( app ) . delete ( `${ BASE } /job-outro-user` ) . expect ( 204 ) ;
416+
417+ expect ( mockSavedJobsService . delete ) . toHaveBeenCalledWith (
418+ "user_abc" ,
419+ "job-outro-user" ,
420+ ) ;
421+ } ) ;
327422 } ) ;
328423} ) ;
0 commit comments