|
17 | 17 |
|
18 | 18 | import { expect } from 'chai';
|
19 | 19 |
|
20 |
| -import { ProviderId, UserProfile } from '@firebase/auth-types-exp'; |
| 20 | +import { OperationType, ProviderId, UserProfile } from '@firebase/auth-types-exp'; |
21 | 21 |
|
22 | 22 | import { IdTokenResponse, IdTokenResponseKind } from '../../model/id_token';
|
23 |
| -import { _fromIdTokenResponse } from './additional_user_info'; |
| 23 | +import { _fromIdTokenResponse, getAdditionalUserInfo } from './additional_user_info'; |
24 | 24 | import { base64Encode } from '@firebase/util';
|
| 25 | +import { UserCredentialImpl } from './user_credential_impl'; |
| 26 | +import { Auth } from '../../model/auth'; |
| 27 | +import { User, UserCredential } from '../../model/user'; |
| 28 | +import { testAuth, testUser } from '../../../test/helpers/mock_auth'; |
25 | 29 |
|
26 | 30 | describe('core/user/additional_user_info', () => {
|
| 31 | + const userProfileWithLogin: UserProfile = { |
| 32 | + login: 'scott', |
| 33 | + friends: [], |
| 34 | + netWorth: 5.0 |
| 35 | + }; |
| 36 | + const rawUserInfoWithLogin = JSON.stringify(userProfileWithLogin); |
| 37 | + const userProfileNoLogin: UserProfile = { sample: 'data' }; |
| 38 | + const rawUserInfoNoLogin = JSON.stringify(userProfileNoLogin); |
27 | 39 | describe('_fromIdTokenResponse', () => {
|
28 |
| - const userProfileWithLogin: UserProfile = { |
29 |
| - login: 'scott', |
30 |
| - friends: [], |
31 |
| - netWorth: 5.0 |
32 |
| - }; |
33 |
| - const rawUserInfoWithLogin = JSON.stringify(userProfileWithLogin); |
34 |
| - const userProfileNoLogin: UserProfile = { sample: 'data' }; |
35 |
| - const rawUserInfoNoLogin = JSON.stringify(userProfileNoLogin); |
36 |
| - |
37 | 40 | describe('parses federated IDP response tokens', () => {
|
38 | 41 | it('for FacebookAdditionalUserInfo', () => {
|
39 | 42 | const idResponse = idTokenResponse({
|
@@ -211,6 +214,74 @@ describe('core/user/additional_user_info', () => {
|
211 | 214 | });
|
212 | 215 | });
|
213 | 216 | });
|
| 217 | + |
| 218 | + describe('getAdditionalUserInfo()', () => { |
| 219 | + let auth: Auth; |
| 220 | + let user: User; |
| 221 | + let cred: UserCredential; |
| 222 | + beforeEach(async () => { |
| 223 | + auth = await testAuth(); |
| 224 | + user = testUser(auth, 'uid'); |
| 225 | + cred = new UserCredentialImpl({ |
| 226 | + user, |
| 227 | + providerId: null, |
| 228 | + operationType: OperationType.SIGN_IN, |
| 229 | + }); |
| 230 | + }); |
| 231 | + |
| 232 | + it('calls through to _fromIdTokenResponse', () => { |
| 233 | + cred._tokenResponse = idTokenResponse({ |
| 234 | + providerId: ProviderId.ANONYMOUS, |
| 235 | + rawUserInfo: rawUserInfoWithLogin |
| 236 | + }); |
| 237 | + const { |
| 238 | + isNewUser, |
| 239 | + providerId, |
| 240 | + username, |
| 241 | + profile |
| 242 | + } = getAdditionalUserInfo(cred)!; |
| 243 | + expect(isNewUser).to.be.false; |
| 244 | + expect(providerId).to.be.null; |
| 245 | + expect(username).to.be.undefined; |
| 246 | + expect(profile).to.eq(profile); |
| 247 | + }); |
| 248 | + |
| 249 | + it('calls through to _fromIdTokenResponse preserving isNewUser', () => { |
| 250 | + cred._tokenResponse = idTokenResponse({ |
| 251 | + providerId: ProviderId.ANONYMOUS, |
| 252 | + rawUserInfo: rawUserInfoWithLogin, |
| 253 | + isNewUser: true |
| 254 | + }); |
| 255 | + const { |
| 256 | + isNewUser, |
| 257 | + providerId, |
| 258 | + username, |
| 259 | + profile |
| 260 | + } = getAdditionalUserInfo(cred)!; |
| 261 | + expect(isNewUser).to.be.true; |
| 262 | + expect(providerId).to.be.null; |
| 263 | + expect(username).to.be.undefined; |
| 264 | + expect(profile).to.eq(profile); |
| 265 | + }); |
| 266 | + |
| 267 | + it('returns bespoke info if existing anonymous user', () => { |
| 268 | + // Note that _tokenResponse is not set on cred |
| 269 | + (user as unknown as Record<string, unknown>).isAnonymous = true; |
| 270 | + const { |
| 271 | + isNewUser, |
| 272 | + providerId, |
| 273 | + profile |
| 274 | + } = getAdditionalUserInfo(cred)!; |
| 275 | + expect(isNewUser).to.be.false; |
| 276 | + expect(providerId).to.be.null; |
| 277 | + expect(profile).to.eq(profile); |
| 278 | + }); |
| 279 | + |
| 280 | + it('returns null if not anonymous', () => { |
| 281 | + // Note that _tokenResponse is not set on cred |
| 282 | + expect(getAdditionalUserInfo(cred)).to.be.null; |
| 283 | + }); |
| 284 | + }); |
214 | 285 | });
|
215 | 286 |
|
216 | 287 | function idTokenResponse(partial: Partial<IdTokenResponse>): IdTokenResponse {
|
|
0 commit comments