@@ -2,6 +2,7 @@ import { Static } from '@fastify/type-provider-typebox';
22import jwt from 'jsonwebtoken' ;
33
44import {
5+ createFetchMock ,
56 createSuperRequest ,
67 defaultUserId ,
78 devLogin ,
@@ -562,7 +563,98 @@ describe('/exam-environment/', () => {
562563 } ) ;
563564 } ) ;
564565
565- xdescribe ( 'POST /exam-environment/screenshot' , ( ) => { } ) ;
566+ describe ( 'POST /exam-environment/screenshot' , ( ) => {
567+ afterEach ( async ( ) => {
568+ await fastifyTestInstance . prisma . envExamAttempt . deleteMany ( ) ;
569+ } ) ;
570+
571+ it ( 'should return 400 if request is not multipart form data' , async ( ) => {
572+ const res = await superPost ( '/exam-environment/screenshot' ) . set (
573+ 'exam-environment-authorization-token' ,
574+ examEnvironmentAuthorizationToken
575+ ) ;
576+
577+ expect ( res . status ) . toBe ( 400 ) ;
578+ expect ( res . body ) . toStrictEqual ( {
579+ code : 'FCC_EINVAL_EXAM_ENVIRONMENT_SCREENSHOT' ,
580+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
581+ message : expect . any ( String )
582+ } ) ;
583+ } ) ;
584+
585+ it ( 'should return 400 if image is missing' , async ( ) => {
586+ const res = await superPost ( '/exam-environment/screenshot' )
587+ . set (
588+ 'exam-environment-authorization-token' ,
589+ examEnvironmentAuthorizationToken
590+ )
591+ . attach ( 'file' , '' ) ;
592+
593+ expect ( res . status ) . toBe ( 400 ) ;
594+ expect ( res . body ) . toStrictEqual ( {
595+ code : 'FCC_EINVAL_EXAM_ENVIRONMENT_SCREENSHOT' ,
596+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
597+ message : expect . any ( String )
598+ } ) ;
599+ } ) ;
600+
601+ it ( 'should return 404 if there is no ongoing exam attempt' , async ( ) => {
602+ const res = await superPost ( '/exam-environment/screenshot' )
603+ . set (
604+ 'exam-environment-authorization-token' ,
605+ examEnvironmentAuthorizationToken
606+ )
607+ . attach ( 'file' , Buffer . from ( [ ] ) ) ;
608+
609+ expect ( res . status ) . toBe ( 404 ) ;
610+ expect ( res . body ) . toStrictEqual ( {
611+ code : 'FCC_ERR_EXAM_ENVIRONMENT_EXAM_ATTEMPT' ,
612+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
613+ message : expect . any ( String )
614+ } ) ;
615+ } ) ;
616+
617+ it ( 'should return 400 if image is of wrong format' , async ( ) => {
618+ await fastifyTestInstance . prisma . envExamAttempt . create ( {
619+ data : mock . examAttempt
620+ } ) ;
621+
622+ const res = await superPost ( '/exam-environment/screenshot' )
623+ . set (
624+ 'exam-environment-authorization-token' ,
625+ examEnvironmentAuthorizationToken
626+ )
627+ . attach ( 'file' , Buffer . from ( [ ] ) ) ;
628+
629+ expect ( res . status ) . toBe ( 400 ) ;
630+ expect ( res . body ) . toStrictEqual ( {
631+ code : 'FCC_EINVAL_EXAM_ENVIRONMENT_SCREENSHOT' ,
632+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
633+ message : expect . any ( String )
634+ } ) ;
635+ } ) ;
636+
637+ it ( 'should return 200 if request is valid and send image to screenshot upload service' , async ( ) => {
638+ // Mock image upload service response
639+ const imageUploadRes = createFetchMock ( { ok : true } ) ;
640+ jest . spyOn ( globalThis , 'fetch' ) . mockImplementation ( imageUploadRes ) ;
641+
642+ await fastifyTestInstance . prisma . envExamAttempt . create ( {
643+ data : mock . examAttempt
644+ } ) ;
645+
646+ const res = await superPost ( '/exam-environment/screenshot' )
647+ . set (
648+ 'exam-environment-authorization-token' ,
649+ examEnvironmentAuthorizationToken
650+ )
651+ . attach ( 'file' , Buffer . from ( [ 0xff , 0xd8 , 0xff , 0xff ] ) ) ;
652+
653+ expect ( res . status ) . toBe ( 200 ) ;
654+ expect ( res . body ) . toStrictEqual ( { } ) ;
655+ expect ( globalThis . fetch ) . toHaveBeenCalled ( ) ;
656+ } ) ;
657+ } ) ;
566658
567659 describe ( 'GET /exam-environment/exams' , ( ) => {
568660 it ( 'should return 200' , async ( ) => {
0 commit comments