@@ -22,6 +22,7 @@ import { OperationType } from '../../model/enums';
2222import { TEST_ID_TOKEN_RESPONSE } from '../../../test/helpers/id_token_response' ;
2323import { testUser , testAuth } from '../../../test/helpers/mock_auth' ;
2424import { TaggedWithTokenResponse } from '../../model/id_token' ;
25+ import { SAMLAuthCredential } from '../credentials/saml' ;
2526import { AuthErrorCode } from '../errors' ;
2627import { UserCredentialImpl } from '../user/user_credential_impl' ;
2728import { _createError } from '../util/assert' ;
@@ -45,6 +46,17 @@ describe('core/providers/saml', () => {
4546 expect ( cred . signInMethod ) . to . eq ( 'saml.provider' ) ;
4647 } ) ;
4748
49+ it ( 'generates SAML provider' , ( ) => {
50+ const provider = new SAMLAuthProvider ( 'saml.provider' ) ;
51+ expect ( provider . providerId ) . to . eq ( 'saml.provider' ) ;
52+ } ) ;
53+
54+ it ( 'returns error for invalid SAML provdier' , ( ) => {
55+ expect ( ( ) => {
56+ new SAMLAuthProvider ( 'provider' ) ;
57+ } ) . throw ( / a u t h \/ a r g u m e n t - e r r o r / ) ;
58+ } ) ;
59+
4860 it ( 'credentialFromResult returns null if provider ID not specified' , async ( ) => {
4961 const auth = await testAuth ( ) ;
5062 const userCred = new UserCredentialImpl ( {
@@ -73,4 +85,78 @@ describe('core/providers/saml', () => {
7385 expect ( cred . providerId ) . to . eq ( 'saml.provider' ) ;
7486 expect ( cred . signInMethod ) . to . eq ( 'saml.provider' ) ;
7587 } ) ;
88+
89+ it ( 'credentialFromJSON returns SAML credential from valid object' , ( ) => {
90+ const json = {
91+ providerId : 'saml.provider' ,
92+ signInMethod : 'saml.provider' ,
93+ pendingToken : 'fake-pending-token'
94+ } ;
95+
96+ const credential = SAMLAuthProvider . credentialFromJSON ( json ) ;
97+ expect ( credential . providerId ) . to . eq ( 'saml.provider' ) ;
98+ expect ( credential . signInMethod ) . to . eq ( 'saml.provider' ) ;
99+ expect ( ( credential as any ) . pendingToken ) . to . eq ( 'fake-pending-token' ) ;
100+ } ) ;
101+
102+ it ( 'returns null when _tokenResponse is missing (undefined)' , ( ) => {
103+ const error = _createError ( AuthErrorCode . NEED_CONFIRMATION , {
104+ appName : 'test-app'
105+ } ) ;
106+
107+ error . customData = { } ; // _tokenResponse missing
108+ const credential = SAMLAuthProvider . credentialFromError ( error ) ;
109+ expect ( credential ) . to . be . null ;
110+ } ) ;
111+
112+ it ( 'returns null when _tokenResponse is missing oauthAccessToken key' , ( ) => {
113+ const error = _createError ( AuthErrorCode . NEED_CONFIRMATION , {
114+ appName : 'foo'
115+ } ) ;
116+ error . customData = {
117+ _tokenResponse : {
118+ // intentionally missing oauthAccessToken
119+ idToken : 'some-id-token' ,
120+ oauthAccessToken : null
121+ }
122+ } ;
123+
124+ const cred = SAMLAuthProvider . credentialFromError ( error ) ;
125+ expect ( cred ) . to . be . null ;
126+ } ) ;
127+
128+ it ( 'returns null if _create throws internally' , ( ) => {
129+ const originalCreate = ( SAMLAuthCredential as any ) . _create ;
130+
131+ ( SAMLAuthCredential as any ) . _create = ( ) => {
132+ throw new Error ( 'Simulated error' ) ;
133+ } ;
134+
135+ const error = _createError ( AuthErrorCode . NEED_CONFIRMATION , {
136+ appName : 'test-app'
137+ } ) ;
138+
139+ error . customData = {
140+ _tokenResponse : {
141+ pendingToken : 'valid-token' ,
142+ providerId : 'saml.my-provider'
143+ }
144+ } ;
145+
146+ const cred = SAMLAuthProvider . credentialFromError ( error ) ;
147+ expect ( cred ) . to . be . null ;
148+
149+ ( SAMLAuthCredential as any ) . _create = originalCreate ;
150+ } ) ;
151+
152+ it ( 'returns null when customData is undefined (falls back to empty object)' , ( ) => {
153+ const error = _createError ( AuthErrorCode . NEED_CONFIRMATION , {
154+ appName : 'test-app'
155+ } ) ;
156+
157+ delete ( error as any ) . customData ;
158+
159+ const credential = SAMLAuthProvider . credentialFromError ( error ) ;
160+ expect ( credential ) . to . be . null ;
161+ } ) ;
76162} ) ;
0 commit comments