import { validateDto } from '@fc/common' ; import { DiscoveryIdpAdapterMongoDTO , IdentityProviderAdapterMongoDTO , MetadataIdpAdapterMongoDTO , } from './identity-provider-adapter-mongo.dto' ; const DTO_VALIDATION_OPTIONS = { forbidNonWhitelisted : true , } ; describe ( 'Identity Provider (Data Transfer Object)' , ( ) => { const metaDataIdPAdapterMongoMock = { active : true , clientID : 'clientID' , client_secret : '7vhnwzo1yUVOJT9GJ91gD5oid56effu1' , discovery : false , display : false , eidas : 2 , endSessionURL : 'https://corev2.docker.dev-franceconnect.fr/api/v2/session/end' , featureHandlers : { } , id_token_encrypted_response_alg : 'RSA-OAEP' , id_token_encrypted_response_enc : 'A256GCM' , id_token_signed_response_alg : 'RS512' , image : 'provider1.png' , name : 'provider1' , post_logout_redirect_uris : [ 'https://corev2.docker.dev-franceconnect.fr/api/v2/logout-from-provider' , ] , redirect_uris : [ 'https://corev2.docker.dev-franceconnect.fr/api/v2/oidc-callback/fip1v2' , ] , response_types : [ 'code' ] , revocation_endpoint_auth_method : 'client_secret_post' , title : 'provider 1' , token_endpoint_auth_method : 'client_secret_post' , uid : 'uid' , url : 'https://corev2.docker.dev-franceconnect.fr' , userinfo_encrypted_response_alg : 'RSA-OAEP' , userinfo_encrypted_response_enc : 'A256GCM' , userinfo_signed_response_alg : 'RS512' , } ; const discoveryIdpAdapterMongoMock = { ... metaDataIdPAdapterMongoMock , discovery : true , discoveryUrl : 'https://corev2.docker.dev-franceconnect.fr/well_known_url' , } ; const identityProviderAdapterMongoMock = { ... discoveryIdpAdapterMongoMock , authzURL : 'https://corev2.docker.dev-franceconnect.fr/api/v2/authorize' , discovery : false , jwksURL : 'https://corev2.docker.dev-franceconnect.fr/api/v2/jwksURL' , tokenURL : 'https://corev2.docker.dev-franceconnect.fr/api/v2/token' , userInfoURL : 'https://corev2.docker.dev-franceconnect.fr/api/v2/userinfo' , } ; describe ( 'should validate identity provider' , ( ) => { it ( 'with only meta data' , async ( ) => { const result = await validateDto ( metaDataIdPAdapterMongoMock , MetadataIdpAdapterMongoDTO , DTO_VALIDATION_OPTIONS ) ; expect ( result ) . toEqual ( [ ] ) ; } ) ; it ( 'with discovery' , async ( ) => { const result = await validateDto ( discoveryIdpAdapterMongoMock , DiscoveryIdpAdapterMongoDTO , DTO_VALIDATION_OPTIONS ) ; expect ( result ) . toEqual ( [ ] ) ; } ) ; it ( 'with all data' , async ( ) => { const result = await validateDto ( identityProviderAdapterMongoMock , IdentityProviderAdapterMongoDTO , DTO_VALIDATION_OPTIONS ) ; expect ( result ) . toEqual ( [ ] ) ; } ) ; it ( 'with wrong conditions to get jwksURL' , async ( ) => { const dto = Object . assign ( identityProviderAdapterMongoMock , { discovery : true , id_token_signed_response_alg : 'HS512' , userinfo_signed_response_alg : 'HS512' , } ) ; delete dto . jwksURL ; const result = await validateDto ( dto , IdentityProviderAdapterMongoDTO , DTO_VALIDATION_OPTIONS ) ; expect ( result ) . toEqual ( [ ] ) ; } ) ; } ) ; describe ( 'should not validate' , ( ) => { it ( 'identity provider with missing discovery property' , async ( ) => { const result = await validateDto ( metaDataIdPAdapterMongoMock , DiscoveryIdpAdapterMongoDTO , DTO_VALIDATION_OPTIONS ) ; expect ( result . length ) . toBeGreaterThanOrEqual ( 1 ) ; expect ( result [ 0 ] . property ) . toEqual ( 'discoveryUrl' ) ; } ) ; it ( 'identity provider with missing openid properties' , async ( ) => { const result = await validateDto ( discoveryIdpAdapterMongoMock , IdentityProviderAdapterMongoDTO , DTO_VALIDATION_OPTIONS ) ; expect ( result . length ) . toBeGreaterThanOrEqual ( 1 ) ; expect ( result [ 0 ] . property ) . toBe ( 'authzURL' ) ; } ) ; } ) ; } ) ;