11import { faker } from '@faker-js/faker' ;
22import { ChainId , EscrowUtils } from '@human-protocol/sdk' ;
3- import { ExecutionContext } from '@nestjs/common' ;
3+ import { ExecutionContext , HttpException } from '@nestjs/common' ;
44import { Reflector } from '@nestjs/core' ;
55import { Test , TestingModule } from '@nestjs/testing' ;
66
@@ -110,8 +110,9 @@ describe('SignatureAuthGuard', () => {
110110 reflector . get = jest . fn ( ) . mockReturnValue ( [ AuthSignatureRole . Worker ] ) ;
111111
112112 mockRequest . headers [ HEADER_SIGNATURE_KEY ] = 'validSignature' ;
113+ const assignmentId = faker . number . int ( ) ;
113114 mockRequest . body = {
114- assignment_id : '1' ,
115+ assignment_id : assignmentId ,
115116 } ;
116117 ( verifySignature as jest . Mock ) . mockReturnValue ( true ) ;
117118 assignmentRepository . findOneById . mockResolvedValue ( {
@@ -120,10 +121,10 @@ describe('SignatureAuthGuard', () => {
120121
121122 const result = await guard . canActivate ( context ) ;
122123 expect ( result ) . toBeTruthy ( ) ;
123- expect ( assignmentRepository . findOneById ) . toHaveBeenCalledWith ( '1' ) ;
124+ expect ( assignmentRepository . findOneById ) . toHaveBeenCalledWith ( assignmentId ) ;
124125 } ) ;
125126
126- it ( 'should throw AuthError if assignment is not found for Worker role ' , async ( ) => {
127+ it ( 'should throw BadRequest error if assignment id is not number ' , async ( ) => {
127128 reflector . get = jest . fn ( ) . mockReturnValue ( [ AuthSignatureRole . Worker ] ) ;
128129
129130 mockRequest . headers [ HEADER_SIGNATURE_KEY ] = 'validSignature' ;
@@ -133,6 +134,21 @@ describe('SignatureAuthGuard', () => {
133134 ( verifySignature as jest . Mock ) . mockReturnValue ( true ) ;
134135 assignmentRepository . findOneById . mockResolvedValue ( null ) ;
135136
137+ const resultPromise = guard . canActivate ( context ) ;
138+ await expect ( resultPromise ) . rejects . toBeInstanceOf ( HttpException ) ;
139+ await expect ( resultPromise ) . rejects . toThrow ( 'Invalid assignment id' ) ;
140+ } ) ;
141+
142+ it ( 'should throw AuthError if assignment is not found for Worker role' , async ( ) => {
143+ reflector . get = jest . fn ( ) . mockReturnValue ( [ AuthSignatureRole . Worker ] ) ;
144+
145+ mockRequest . headers [ HEADER_SIGNATURE_KEY ] = 'validSignature' ;
146+ mockRequest . body = {
147+ assignment_id : 1 ,
148+ } ;
149+ ( verifySignature as jest . Mock ) . mockReturnValue ( true ) ;
150+ assignmentRepository . findOneById . mockResolvedValue ( null ) ;
151+
136152 const resultPromise = guard . canActivate ( context ) ;
137153 await expect ( resultPromise ) . rejects . toBeInstanceOf ( ValidationError ) ;
138154 await expect ( resultPromise ) . rejects . toThrow ( ErrorAssignment . NotFound ) ;
0 commit comments