import { IExpectedSiop , IssuanceHelpers , SiopTokenValidator , TokenType , ValidationOptions } from '../lib' ; import ValidationQueue from '../lib/input_validation/ValidationQueue' ; import TestSetup from './TestSetup' ; describe ( 'SiopTokenValidator' , ( ) => { let setup ; beforeEach ( async ( ) => { setup = new TestSetup ( ) ; } ) ; afterEach ( ( ) => { setup . fetchMock . reset ( ) ; } ) ; it ( 'should test nonce and state' , async ( ) => { const [ request , options , siop ] = await IssuanceHelpers . createRequest ( setup , TokenType . siopIssuance , true ) ; const expected = siop . expected . filter ( ( token ) => token . type === TokenType . siopIssuance ) [ 0 ] ; let validator = new SiopTokenValidator ( setup . validatorOptions , expected ) ; let payload = { ... request . decodedToken , nonce : 'nonce' , state : 'state' } ; let siopRequest = await IssuanceHelpers . createSiopRequestWithPayload ( setup , payload , siop . didJwkPrivate ) ; let queue = new ValidationQueue ( ) ; queue . enqueueToken ( 'siop' , siopRequest ) ; let response = await validator . validate ( queue , queue . getNextToken ( ) ! ) ; expect ( response . result ) . toBeTruthy ( ) ; expected . nonce = 'nonce' ; expected . state = 'state' ; validator = new SiopTokenValidator ( setup . validatorOptions , expected ) ; siopRequest = await IssuanceHelpers . createSiopRequestWithPayload ( setup , payload , siop . didJwkPrivate ) ; queue = new ValidationQueue ( ) ; queue . enqueueToken ( 'siop' , siopRequest ) ; const queudToken = queue . getNextToken ( ) ; expect ( queudToken ?. isUnderValidation ) . toBeFalsy ( ) ; response = await validator . validate ( queue , queudToken ! ) ; expect ( response . result ) . toBeTruthy ( ) ; payload = { ... request . decodedToken , nonce : 'nonce' , state : 'xxx' } ; siopRequest = await IssuanceHelpers . createSiopRequestWithPayload ( setup , payload , siop . didJwkPrivate ) ; queue = new ValidationQueue ( ) ; queue . enqueueToken ( 'siop' , siopRequest ) ; response = await validator . validate ( queue , queue . getNextToken ( ) ! ) ; expect ( response . result ) . toBeFalsy ( ) ; expect ( response . detailedError ) . toEqual ( `Expected state 'state' does not match 'xxx'.` ) ; expect ( response . code ) . toEqual ( 'VCSDKSTVa02' ) ; payload = { ... request . decodedToken , nonce : 'xxx' , state : 'state' } ; siopRequest = await IssuanceHelpers . createSiopRequestWithPayload ( setup , payload , siop . didJwkPrivate ) ; queue = new ValidationQueue ( ) ; queue . enqueueToken ( 'siop' , siopRequest ) ; response = await validator . validate ( queue , queue . getNextToken ( ) ! ) ; expect ( response . result ) . toBeFalsy ( ) ; expect ( response . detailedError ) . toEqual ( `Expected nonce 'nonce' does not match 'xxx'.` ) ; expect ( response . code ) . toEqual ( 'VCSDKSTVa01' ) ; } ) ; } ) ;