@@ -26,8 +26,15 @@ import { AuthErrorCode } from '../errors';
26
26
import { UserCredentialImpl } from '../user/user_credential_impl' ;
27
27
import { _createError } from '../util/assert' ;
28
28
import { OAuthProvider } from './oauth' ;
29
+ import { OAuthCredential } from '../credentials/oauth' ;
29
30
30
31
describe ( 'core/providers/oauth' , ( ) => {
32
+ const callMethod = ( tokenResponse : any ) : OAuthCredential => {
33
+ return ( OAuthProvider as any ) . oauthCredentialFromTaggedObject ( {
34
+ _tokenResponse : tokenResponse
35
+ } ) ;
36
+ } ;
37
+
31
38
it ( 'generates the correct type of oauth credential' , ( ) => {
32
39
const cred = new OAuthProvider ( 'google.com' ) . credential ( {
33
40
idToken : 'id-token' ,
@@ -125,4 +132,96 @@ describe('core/providers/oauth', () => {
125
132
expect ( cred . signInMethod ) . to . eq ( 'foo.test' ) ;
126
133
expect ( ( cred . toJSON ( ) as { nonce : string } ) . nonce ) . to . eq ( 'i-am-a-nonce' ) ;
127
134
} ) ;
135
+
136
+ it ( 'creates OAuthCredential from valid object input' , ( ) => {
137
+ const input = {
138
+ providerId : 'google.com' ,
139
+ signInMethod : 'google.com' ,
140
+ idToken : 'id-token' ,
141
+ accessToken : 'access-token'
142
+ } ;
143
+
144
+ const credential = OAuthProvider . credentialFromJSON ( input ) ;
145
+ expect ( credential ) . to . be . instanceOf ( OAuthCredential ) ;
146
+ expect ( credential . providerId ) . to . equal ( 'google.com' ) ;
147
+ expect ( credential . signInMethod ) . to . equal ( 'google.com' ) ;
148
+ expect ( credential . idToken ) . to . equal ( 'id-token' ) ;
149
+ expect ( credential . accessToken ) . to . equal ( 'access-token' ) ;
150
+ } ) ;
151
+
152
+ it ( 'creates OAuthCredential from valid JSON string input' , ( ) => {
153
+ const input = JSON . stringify ( {
154
+ providerId : 'providerid' ,
155
+ signInMethod : 'providerid' ,
156
+ accessToken : 'access-token'
157
+ } ) ;
158
+
159
+ const credential = OAuthProvider . credentialFromJSON ( input ) ;
160
+ expect ( credential ) . to . be . instanceOf ( OAuthCredential ) ;
161
+ expect ( credential . providerId ) . to . equal ( 'providerid' ) ;
162
+ expect ( credential . signInMethod ) . to . equal ( 'providerid' ) ;
163
+ expect ( credential . accessToken ) . to . equal ( 'access-token' ) ;
164
+ } ) ;
165
+
166
+ it ( 'throws an error if providerId or signInMethod is missing' , ( ) => {
167
+ const input = {
168
+ idToken : 'missing-provider-id'
169
+ } ;
170
+
171
+ expect ( ( ) => {
172
+ OAuthProvider . credentialFromJSON ( input ) ;
173
+ } ) . to . throw ( AuthErrorCode . ARGUMENT_ERROR ) ;
174
+ } ) ;
175
+
176
+ it ( 'throws an error if JSON string is invalid' , ( ) => {
177
+ const invalidJson = '{ not valid json }' ;
178
+
179
+ expect ( ( ) => {
180
+ OAuthProvider . credentialFromJSON ( invalidJson ) ;
181
+ } ) . to . throw ( SyntaxError ) ;
182
+ } ) ;
183
+
184
+ it ( 'returns null if tokenResponse is missing' , ( ) => {
185
+ const result = callMethod ( undefined ) ;
186
+ expect ( result ) . to . be . null ;
187
+ } ) ;
188
+
189
+ it ( 'returns null if all tokens (idToken, accessToken, tokenSecret, pendingToken) are missing' , ( ) => {
190
+ const result = callMethod ( {
191
+ providerId : 'google.com'
192
+ // all token fields missing
193
+ } ) ;
194
+ expect ( result ) . to . be . null ;
195
+ } ) ;
196
+
197
+ it ( 'returns null if providerId is missing' , ( ) => {
198
+ const result = callMethod ( {
199
+ oauthIdToken : 'id-token' ,
200
+ oauthAccessToken : 'access-token'
201
+ // providerId is missing
202
+ } ) ;
203
+ expect ( result ) . to . be . null ;
204
+ } ) ;
205
+
206
+ it ( 'returns null if OAuthProvider._credential throws' , ( ) => {
207
+ const proto = OAuthProvider . prototype as any ;
208
+ const original = proto . _credential ;
209
+
210
+ // Temporarily replace _credential to throw an error
211
+ proto . _credential = ( ) => {
212
+ throw new Error ( 'Simulated error' ) ;
213
+ } ;
214
+
215
+ const result = ( OAuthProvider as any ) . oauthCredentialFromTaggedObject ( {
216
+ _tokenResponse : {
217
+ providerId : 'google.com' ,
218
+ oauthIdToken : 'id-token'
219
+ }
220
+ } ) ;
221
+
222
+ expect ( result ) . to . be . null ;
223
+
224
+ // Restore original method
225
+ proto . _credential = original ;
226
+ } ) ;
128
227
} ) ;
0 commit comments