@@ -79,7 +79,14 @@ async function buildApp(): Promise<FastifyInstance> {
7979 app . decorateRequest ( 'jwtVerify' , function ( ) {
8080 return mockJwtVerify ( ) ;
8181 } ) ;
82-
82+ app . decorate ( 'authenticate' , async function ( request , reply ) {
83+ try {
84+ const payload = await request . jwtVerify ( ) ;
85+ if ( payload ) { request . user = payload as typeof request . user ; }
86+ } catch {
87+ return reply . status ( 401 ) . send ( { error : 'Unauthorized' } ) ;
88+ }
89+ } ) ;
8390 // Register with the same prefix used in production (app.ts) so that
8491 // tests exercise routes at their real paths — /api/events, /api/events/:slug, etc.
8592 await app . register ( eventRoutes , { prefix : '/api/events' } ) ;
@@ -253,14 +260,15 @@ describe('Events API', () => {
253260 it ( '200 — returns event info with attendee count' , async ( ) => {
254261 prismaMock . event . findUnique . mockResolvedValue ( {
255262 ...MOCK_EVENT ,
263+ organizer : { username : 'johndoe' , displayName : 'John Doe' } ,
256264 _count : { attendees : 42 } ,
257265 } ) ;
258266
259267 const res = await app . inject ( {
260268 method : 'GET' ,
261269 url : '/api/events/devcard-conf-2025' ,
262270 } ) ;
263-
271+ console . log ( JSON . stringify ( res . json ( ) , null , 2 ) ) ;
264272 expect ( res . statusCode ) . toBe ( 200 ) ;
265273 const body = res . json ( ) ;
266274 expect ( body . slug ) . toBe ( 'devcard-conf-2025' ) ;
@@ -277,7 +285,7 @@ describe('Events API', () => {
277285 method : 'GET' ,
278286 url : '/api/events/ghost-event' ,
279287 } ) ;
280-
288+
281289 expect ( res . statusCode ) . toBe ( 404 ) ;
282290 expect ( res . json ( ) ) . toMatchObject ( { error : 'Event not found' } ) ;
283291 } ) ;
@@ -287,6 +295,7 @@ describe('Events API', () => {
287295 mockJwtVerify . mockRejectedValue ( new Error ( 'Should not be called' ) ) ;
288296 prismaMock . event . findUnique . mockResolvedValue ( {
289297 ...MOCK_EVENT ,
298+ organizer : { username : 'johndoe' , displayName : 'John Doe' } ,
290299 _count : { attendees : 0 } ,
291300 } ) ;
292301
@@ -505,6 +514,7 @@ describe('Events API', () => {
505514 prismaMock . event . findUnique . mockResolvedValue ( {
506515 ...MOCK_EVENT ,
507516 attendees : attendeeRows ,
517+ _count : { attendees : 2 } ,
508518 } ) ;
509519
510520 const res = await app . inject ( {
@@ -533,6 +543,7 @@ describe('Events API', () => {
533543 prismaMock . event . findUnique . mockResolvedValue ( {
534544 ...MOCK_EVENT ,
535545 attendees : [ makeAttendeeRow ( MOCK_OTHER_USER_PROFILE ) ] ,
546+ _count : { attendees : 1 } ,
536547 } ) ;
537548
538549 const res = await app . inject ( {
@@ -555,6 +566,7 @@ describe('Events API', () => {
555566 prismaMock . event . findUnique . mockResolvedValue ( {
556567 ...MOCK_EVENT ,
557568 attendees : [ ] ,
569+ _count : { attendees : 0 } ,
558570 } ) ;
559571
560572 const res = await app . inject ( {
@@ -571,6 +583,7 @@ describe('Events API', () => {
571583 prismaMock . event . findUnique . mockResolvedValue ( {
572584 ...MOCK_EVENT ,
573585 attendees : [ ] ,
586+ _count : { attendees : 0 } ,
574587 } ) ;
575588
576589 const res = await app . inject ( {
@@ -587,6 +600,7 @@ describe('Events API', () => {
587600 prismaMock . event . findUnique . mockResolvedValue ( {
588601 ...MOCK_EVENT ,
589602 attendees : [ ] ,
603+ _count : { attendees : 0 } ,
590604 } ) ;
591605
592606 const res = await app . inject ( {
@@ -604,6 +618,7 @@ describe('Events API', () => {
604618 prismaMock . event . findUnique . mockResolvedValue ( {
605619 ...MOCK_EVENT ,
606620 attendees : [ makeAttendeeRow ( MOCK_USER_PROFILE ) ] ,
621+ _count : { attendees : 1 } ,
607622 } ) ;
608623
609624 const res = await app . inject ( {
@@ -693,4 +708,4 @@ describe('Events API', () => {
693708 expect ( slug ) . not . toMatch ( / - - / ) ;
694709 } ) ;
695710 } ) ;
696- } ) ;
711+ } ) ;
0 commit comments