import { ChainableElement , ScopeContext , ServiceProviderBase , UserClaims , } from '../../common/types' ; import { getClaims } from '../helpers' ; export default class ServiceProviderPage { fcButtonSelector : string ; logoutButtonSelector : string ; originUrl : string ; constructor ( args ) { const { selectors : { fcButton , logoutButton } , url , } = args ; this . fcButtonSelector = fcButton ; this . logoutButtonSelector = logoutButton ; this . originUrl = url ; } get fcButton ( ) : ChainableElement { return cy . get ( this . fcButtonSelector ) ; } get logoutButton ( ) : ChainableElement { return cy . get ( this . logoutButtonSelector ) ; } checkIsVisible ( ) { cy . url ( ) . should ( 'include' , this . originUrl ) ; } checkIsUserConnected ( isConnected = true ) { if ( isConnected ) { this . logoutButton . should ( 'be.visible' ) ; } else { this . logoutButton . should ( 'not.exist' ) ; } } setMockAuthorizeHttpMethod ( formMethod ) { cy . get ( '#httpMethod' ) . select ( formMethod ) ; } setMockRequestedScope ( scopeContext ? ) { if ( scopeContext ) { cy . get ( 'input[name="scope"]' ) . clear ( ) ; const { scopes = [ ] } = scopeContext ; const scopeValue = scopes . join ( ' ' ) ; if ( scopeValue ) { cy . get ( 'input[name="scope"]' ) . type ( scopeValue ) ; } } } setMockRequestedAcr ( acrValue ) { cy . get ( 'input[name="acr_values"]' ) . clear ( ) . type ( acrValue ) ; } setMockRequestedAmr ( isRequested ) { if ( isRequested ) { cy . get ( '#claim_amr' ) . check ( ) ; } else { cy . get ( '#claim_amr' ) . uncheck ( ) ; } } checkMockInformationAccess ( requestedScope , userClaims ) { const expectedClaims = getClaims ( requestedScope ) ; cy . get ( '#json-output' ) . invoke ( 'text' ) . then ( ( text ) => { const responseBody = JSON . parse ( text . trim ( ) ) ; const mandatoryData = { aud : /^\w+$/ , exp : /^\d+/ , iat : /^\d+/ , iss : /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w .-]*)*\/?$/ , sub : /^[0-9a-f]{64}v1$/ , } ; Object . keys ( mandatoryData ) . forEach ( ( key ) => expect ( responseBody [ key ] ) . to . match ( mandatoryData [ key ] , `${ key } should be present.` ) ) ; expectedClaims . filter ( ( claimName ) => claimName !== 'sub' ) . forEach ( ( claimName ) => { expect ( responseBody [ claimName ] ) . to . deep . equal ( userClaims [ claimName ] , `The claim ${ claimName } should be ${ JSON . stringify ( userClaims [ claimName ] ) }` ) ; } ) ; const extraClaimsName = Object . keys ( responseBody ) . filter ( ( key ) => ! mandatoryData [ key ] && ! expectedClaims . includes ( key ) ) ; expect ( extraClaimsName ) . to . deep . equal ( [ ] , 'No extra claims should be sent.' ) ; } ) ; } checkMockAcrValue ( acrValue ) { cy . get ( '[id="info-acr"] strong' ) . contains ( acrValue ) ; } checkMockAmrValue ( amrValue ) { cy . get ( '[id="info-amr"] strong' ) . contains ( amrValue ) ; } checkMockErrorCallback ( ) { const errorCallbackURL = `${ this . originUrl }/error` ; cy . url ( ) . should ( 'include' , errorCallbackURL ) ; } checkMockErrorCode ( errorCode ) { const encodedError = encodeURIComponent ( errorCode ) ; cy . url ( ) . should ( 'match' , new RegExp ( `(?<=[&|?])error=${ encodedError }(?=&|$)` ) ) ; } checkMockErrorDescription ( errorDescription ) { const encodedDescription = encodeURIComponent ( errorDescription ) ; cy . url ( ) . should ( 'match' , new RegExp ( `(?<=[&|?])error_description=${ encodedDescription }(?=&|$)` ) ) ; } }