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