Skip to content

Commit 7539e16

Browse files
committed
Fix tests & write some new ones
1 parent 35bda2c commit 7539e16

File tree

15 files changed

+116
-15
lines changed

15 files changed

+116
-15
lines changed

packages-exp/auth-exp/src/api/account_management/profile.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ describe('api/account_management/updateProfile', () => {
3333
const request = {
3434
idToken: 'my-token',
3535
36-
password: 'my-password'
36+
password: 'my-password',
37+
returnSecureToken: true,
3738
};
3839

3940
let auth: TestAuth;
@@ -48,7 +49,7 @@ describe('api/account_management/updateProfile', () => {
4849
it('should POST to the correct endpoint', async () => {
4950
const mock = mockEndpoint(Endpoint.SET_ACCOUNT_INFO, {
5051
displayName: 'my-name',
51-
52+
5253
});
5354

5455
const response = await updateProfile(auth, request);

packages-exp/auth-exp/src/api/account_management/profile.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface UpdateProfileRequest {
2323
idToken: string;
2424
displayName?: string | null;
2525
photoUrl?: string | null;
26+
returnSecureToken: boolean;
2627
}
2728

2829
export interface UpdateProfileResponse extends IdTokenResponse {

packages-exp/auth-exp/src/core/auth/auth_impl.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ export class AuthImpl implements Auth, _FirebaseService {
168168
// If the stored user (i.e. the old "currentUser") has a redirectId that
169169
// matches the redirect user, then we want to initially sign in with the
170170
// new user object from result.
171+
// TODO(samgho): More thoroughly test all of this
171172
if (
172173
(!redirectUserEventId || redirectUserEventId === storedUserEventId) &&
173174
result?.user

packages-exp/auth-exp/src/core/strategies/credential.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ import { AUTH_ERROR_FACTORY, AuthErrorCode } from '../errors';
4242
import {
4343
linkWithCredential,
4444
reauthenticateWithCredential,
45-
signInWithCredential
45+
signInWithCredential,
46+
_signInWithCredential
4647
} from './credential';
4748

4849
use(chaiAsPromised);
@@ -111,6 +112,15 @@ describe('core/strategies/credential', () => {
111112
expect(auth.currentUser).to.eq(user);
112113
});
113114

115+
it('does not update the current user if bypass is true', async () => {
116+
stub(authCredential, '_getIdTokenResponse').returns(
117+
Promise.resolve(idTokenResponse)
118+
);
119+
const { user } = await _signInWithCredential(auth, authCredential, true);
120+
expect(auth.currentUser).to.be.null;
121+
expect(user).not.to.be.null;
122+
});
123+
114124
it('should handle MFA', async () => {
115125
const serverResponse: IdTokenMfaResponse = {
116126
localId: 'uid',

packages-exp/auth-exp/src/core/user/account_info.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ describe('core/user/profile', () => {
7373
expect(ep.calls[0].request).to.eql({
7474
idToken: 'access-token',
7575
displayName: 'displayname',
76-
photoUrl: 'photo'
76+
photoUrl: 'photo',
77+
returnSecureToken: true,
7778
});
7879
});
7980

@@ -118,7 +119,8 @@ describe('core/user/profile', () => {
118119
await updateEmail(user, '[email protected]');
119120
expect(set.calls[0].request).to.eql({
120121
idToken: 'access-token',
121-
122+
123+
returnSecureToken: true,
122124
});
123125

124126
expect(user.uid).to.eq('new-uid-to-prove-refresh-got-called');
@@ -135,7 +137,8 @@ describe('core/user/profile', () => {
135137
await updatePassword(user, 'pass');
136138
expect(set.calls[0].request).to.eql({
137139
idToken: 'access-token',
138-
password: 'pass'
140+
password: 'pass',
141+
returnSecureToken: true,
139142
});
140143

141144
expect(user.uid).to.eq('new-uid-to-prove-refresh-got-called');

packages-exp/auth-exp/src/core/user/account_info.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export async function updateProfile(
4949

5050
const userInternal = user as User;
5151
const idToken = await user.getIdToken();
52-
const profileRequest = { idToken, displayName, photoUrl };
52+
const profileRequest = { idToken, displayName, photoUrl, returnSecureToken: true };
5353
const response = await _logoutIfInvalidated(
5454
userInternal,
5555
apiUpdateProfile(userInternal.auth, profileRequest)
@@ -121,7 +121,7 @@ async function updateEmailOrPassword(
121121
): Promise<void> {
122122
const { auth } = user;
123123
const idToken = await user.getIdToken();
124-
const request: UpdateEmailPasswordRequest = { idToken };
124+
const request: UpdateEmailPasswordRequest = { idToken, returnSecureToken: true };
125125

126126
if (email) {
127127
request.email = email;

packages-exp/auth-exp/src/core/user/invalidation.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ describe('src/core/user/invalidation', () => {
6363
expect(auth.currentUser).to.be.null;
6464
});
6565

66+
it('does not log out if bypass auth state is true', async () => {
67+
const error = makeError(AuthErrorCode.USER_DISABLED);
68+
try { await _logoutIfInvalidated(user, Promise.reject(error), true); } catch {}
69+
expect(auth.currentUser).to.eq(user);
70+
});
71+
6672
it('logs out the user if the error is token_expired', async () => {
6773
const error = makeError(AuthErrorCode.TOKEN_EXPIRED);
6874
await expect(

packages-exp/auth-exp/src/platform_browser/auth.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ import { _getInstance } from '../core/util/instantiator';
4343
import { _getClientVersion, ClientPlatform } from '../core/util/version';
4444
import { Auth } from '../model/auth';
4545
import { browserPopupRedirectResolver } from './popup_redirect';
46+
import { PopupRedirectResolver } from '../model/popup_redirect';
47+
import { UserCredentialImpl } from '../core/user/user_credential_impl';
48+
import { User } from '../model/user';
4649

4750
use(sinonChai);
4851
use(chaiAsPromised);
@@ -102,13 +105,15 @@ describe('core/auth/initializeAuth', () => {
102105
let createManagerStub: sinon.SinonSpy;
103106
let reloadStub: sinon.SinonStub;
104107
let oldAuth: Auth;
108+
let completeRedirectFnStub: sinon.SinonStub;
105109

106110
beforeEach(async () => {
107111
oldAuth = await testAuth();
108112
createManagerStub = sinon.spy(PersistenceUserManager, 'create');
109113
reloadStub = sinon
110114
.stub(reload, '_reloadWithoutSaving')
111115
.returns(Promise.resolve());
116+
completeRedirectFnStub = sinon.stub(_getInstance<PopupRedirectResolver>(browserPopupRedirectResolver), '_completeRedirectFn').returns(Promise.resolve(null));
112117
});
113118

114119
async function initAndWait(
@@ -173,6 +178,23 @@ describe('core/auth/initializeAuth', () => {
173178
expect(reload._reloadWithoutSaving).not.to.have.been.called;
174179
});
175180

181+
it('signs in the redirect user if found', async () => {
182+
let user: User|null = null;
183+
completeRedirectFnStub.callsFake((auth: Auth) => {
184+
user = testUser(auth, 'uid', '[email protected]');
185+
return Promise.resolve(new UserCredentialImpl({
186+
operationType: externs.OperationType.SIGN_IN,
187+
user,
188+
providerId: null
189+
}));
190+
191+
});
192+
193+
const auth = await initAndWait([inMemoryPersistence], browserPopupRedirectResolver);
194+
expect(user).not.to.be.null;
195+
expect(auth.currentUser).to.eq(user);
196+
});
197+
176198
it('reloads non-redirect users', async () => {
177199
sinon
178200
.stub(_getInstance<Persistence>(inMemoryPersistence), '_get')

packages-exp/auth-exp/src/platform_browser/popup_redirect.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import { _setWindowLocation } from './auth_window';
4040
import { _openIframe } from './iframe/iframe';
4141
import { browserSessionPersistence } from './persistence/session_storage';
4242
import { _open, AuthPopup } from './util/popup';
43-
import { getRedirectResult } from './strategies/redirect';
43+
import {_getRedirectResult } from './strategies/redirect';
4444

4545
/**
4646
* URL for Authentication widget which will initiate the OAuth handshake
@@ -200,7 +200,7 @@ class BrowserPopupRedirectResolver implements PopupRedirectResolver {
200200
return this.originValidationPromises[key];
201201
}
202202

203-
_completeRedirectFn = getRedirectResult;
203+
_completeRedirectFn = _getRedirectResult;
204204
}
205205

206206
/**

packages-exp/auth-exp/src/platform_browser/strategies/abstract_popup_redirect_operation.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ describe('src/core/strategies/abstract_popup_redirect_operation', () => {
185185
sessionId: BASE_AUTH_EVENT.sessionId!,
186186
tenantId: BASE_AUTH_EVENT.tenantId || undefined,
187187
postBody: BASE_AUTH_EVENT.postBody || undefined,
188-
user: undefined
188+
user: undefined,
189+
bypassAuthState: false,
189190
};
190191
}
191192

@@ -236,6 +237,25 @@ describe('src/core/strategies/abstract_popup_redirect_operation', () => {
236237
await operation.execute();
237238
expect(idp._reauth).to.have.been.calledWith(expectedIdpTaskParams());
238239
});
240+
241+
it('includes the bypassAuthState parameter', async () => {
242+
operation = new WrapperOperation(
243+
auth,
244+
AuthEventType.REAUTH_VIA_REDIRECT,
245+
resolver,
246+
undefined,
247+
/** bypassAuthState */ true
248+
);
249+
250+
const type = AuthEventType.REAUTH_VIA_REDIRECT;
251+
updateFilter(type);
252+
finishPromise(authEvent({ type }));
253+
await operation.execute();
254+
expect(idp._reauth).to.have.been.calledWith({
255+
...expectedIdpTaskParams(),
256+
bypassAuthState: true,
257+
});
258+
});
239259
});
240260
});
241261
});

packages-exp/auth-exp/src/platform_browser/strategies/phone.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,8 @@ describe('core/strategies/phone', () => {
424424
users: [{ uid: 'uid' }]
425425
});
426426
signInMock = mockEndpoint(Endpoint.SIGN_IN_WITH_PHONE_NUMBER, {
427-
idToken: 'new-access-token'
427+
idToken: 'new-access-token',
428+
refreshToken: 'refresh-token',
428429
});
429430
credential = PhoneAuthCredential._fromVerification(
430431
'session-info',

packages-exp/auth-exp/src/platform_browser/strategies/redirect.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import {
5151
getRedirectResult,
5252
linkWithRedirect,
5353
reauthenticateWithRedirect,
54-
signInWithRedirect
54+
signInWithRedirect, _getRedirectResult
5555
} from './redirect';
5656
import { FirebaseError } from '@firebase/util';
5757

@@ -426,5 +426,29 @@ describe('src/core/strategies/redirect', () => {
426426
expect(auth.persistenceLayer.lastObjectSet?._redirectEventId).to.be
427427
.undefined;
428428
});
429+
430+
it('does not mutate authstate if bypassAuthState is true', async () => {
431+
await reInitAuthWithRedirectUser(MATCHING_EVENT_ID);
432+
const redirectPersistence: Persistence = _getInstance(
433+
RedirectPersistence
434+
);
435+
sinon.spy(redirectPersistence, '_remove');
436+
437+
const cred = new UserCredentialImpl({
438+
user: auth._currentUser!,
439+
providerId: externs.ProviderId.GOOGLE,
440+
operationType: externs.OperationType.LINK
441+
});
442+
idpStubs._link.returns(Promise.resolve(cred));
443+
const promise = _getRedirectResult(auth, resolver, true);
444+
iframeEvent({
445+
type: AuthEventType.LINK_VIA_REDIRECT
446+
});
447+
expect(await promise).to.eq(cred);
448+
expect(redirectPersistence._remove).not.to.have.been.called;
449+
expect(auth._currentUser?._redirectEventId).not.to.be.undefined;
450+
expect(auth.persistenceLayer.lastObjectSet?._redirectEventId).not.to.be
451+
.undefined;
452+
});
429453
});
430454
});

packages-exp/auth-exp/src/platform_browser/strategies/redirect.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,16 @@ export async function linkWithRedirect(
228228
* @public
229229
*/
230230
export async function getRedirectResult(
231+
auth: externs.Auth,
232+
resolver?: externs.PopupRedirectResolver,
233+
): Promise<externs.UserCredential | null> {
234+
return _getRedirectResult(auth, resolver, false);
235+
}
236+
237+
export async function _getRedirectResult(
231238
auth: externs.Auth,
232239
resolverExtern?: externs.PopupRedirectResolver,
233-
bypassAuthState = false
240+
bypassAuthState = false,
234241
): Promise<externs.UserCredential | null> {
235242
const authInternal = _castAuth(auth);
236243
const resolver = _withDefaultResolver(authInternal, resolverExtern);

packages-exp/auth-exp/test/helpers/mock_popup_redirect_resolver.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,9 @@ export function makeMockPopupRedirectResolver(
4949
}
5050

5151
_redirectPersistence?: Persistence;
52+
53+
async _completeRedirectFn(): Promise<void> {
54+
55+
}
5256
};
5357
}

packages-exp/auth-exp/test/integration/flows/phone.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ describe('Integration test: phone auth', () => {
107107

108108
await unlink(user, ProviderId.PHONE);
109109
expect(auth.currentUser!.uid).to.eq(anonId);
110-
expect(auth.currentUser!.isAnonymous).to.be.true;
110+
// Is anonymous stays false even after unlinking
111+
expect(auth.currentUser!.isAnonymous).to.be.false;
111112
expect(auth.currentUser!.phoneNumber).to.be.null;
112113
});
113114

0 commit comments

Comments
 (0)