Skip to content

Commit 2202768

Browse files
committed
Adding Unit Test for auth_impl firebaseToken
1 parent c86cbfe commit 2202768

File tree

1 file changed

+140
-1
lines changed

1 file changed

+140
-1
lines changed

packages/auth/src/core/auth/auth_impl.test.ts

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ import { mockEndpointWithParams } from '../../../test/helpers/api/helper';
4646
import { Endpoint, RecaptchaClientType, RecaptchaVersion } from '../../api';
4747
import * as mockFetch from '../../../test/helpers/mock_fetch';
4848
import { AuthErrorCode } from '../errors';
49-
import { PasswordValidationStatus } from '../../model/public_types';
49+
import { FirebaseToken, PasswordValidationStatus } from '../../model/public_types';
5050
import { PasswordPolicyImpl } from './password_policy_impl';
51+
import { PersistenceUserManager } from '../persistence/persistence_user_manager';
5152

5253
use(sinonChai);
5354
use(chaiAsPromised);
@@ -150,6 +151,144 @@ describe('core/auth/auth_impl', () => {
150151
});
151152
});
152153

154+
describe('#updateFirebaseToken', () => {
155+
const token: FirebaseToken = {
156+
token: 'test-token',
157+
expirationTime: 123456789
158+
};
159+
160+
it('sets the field on the auth object', async () => {
161+
await auth._updateFirebaseToken(token);
162+
expect((auth as any).firebaseToken).to.eql(token);
163+
});
164+
165+
it('calls persistence._set with correct values', async () => {
166+
await auth._updateFirebaseToken(token);
167+
expect(persistenceStub._set).to.have.been.calledWith(
168+
'firebase:persistence-token:api-key:test-app', // key
169+
{
170+
token: token.token,
171+
expirationTime: token.expirationTime
172+
}
173+
);
174+
});
175+
176+
it('setting to null triggers persistence._remove', async () => {
177+
await auth._updateFirebaseToken(null);
178+
expect(persistenceStub._remove).to.have.been.calledWith(
179+
'firebase:persistence-token:api-key:test-app');
180+
});
181+
182+
it('orders async updates correctly', async () => {
183+
const tokens: FirebaseToken[] = Array.from({ length: 5 }, (_, i) => ({
184+
token: `token-${i}`,
185+
expirationTime: Date.now() + i
186+
}));
187+
188+
persistenceStub._set.callsFake(() => {
189+
return new Promise(resolve => {
190+
setTimeout(() => resolve(), 1);
191+
});
192+
});
193+
194+
await Promise.all(tokens.map(t => auth._updateFirebaseToken(t)));
195+
196+
for (let i = 0; i < tokens.length; i++) {
197+
expect(persistenceStub._set.getCall(i)).to.have.been.calledWith(
198+
'firebase:persistence-token:api-key:test-app',
199+
{
200+
token: tokens[i].token,
201+
expirationTime: tokens[i].expirationTime
202+
}
203+
);
204+
}
205+
});
206+
207+
it('throws if persistence._set fails', async () => {
208+
persistenceStub._set.rejects(new Error('fail'));
209+
await expect(auth._updateFirebaseToken(token)).to.be.rejectedWith('fail');
210+
});
211+
212+
it('throws if persistence._remove fails', async () => {
213+
persistenceStub._remove.rejects(new Error('remove fail'));
214+
await expect(auth._updateFirebaseToken(null)).to.be.rejectedWith('remove fail');
215+
});
216+
});
217+
218+
describe('#_initializeWithPersistence', () => {
219+
let mockToken: FirebaseToken;
220+
let persistenceManager: any;
221+
let subscription: any;
222+
let authImpl: AuthImpl;
223+
224+
beforeEach(() => {
225+
mockToken = {
226+
token: 'test-token',
227+
expirationTime: 123456789
228+
};
229+
230+
persistenceManager = {
231+
getFirebaseToken: sinon.stub().resolves(mockToken),
232+
getCurrentUser: sinon.stub().resolves(null),
233+
setCurrentUser: sinon.stub().resolves(),
234+
removeCurrentUser: sinon.stub().resolves(),
235+
getPersistence: sinon.stub().returns('LOCAL')
236+
};
237+
238+
subscription = {
239+
next: sinon.spy()
240+
};
241+
242+
sinon.stub(PersistenceUserManager, 'create').resolves(persistenceManager);
243+
244+
authImpl = new AuthImpl(
245+
FAKE_APP,
246+
FAKE_HEARTBEAT_CONTROLLER_PROVIDER,
247+
FAKE_APP_CHECK_CONTROLLER_PROVIDER,
248+
{
249+
apiKey: FAKE_APP.options.apiKey!,
250+
apiHost: DefaultConfig.API_HOST,
251+
apiScheme: DefaultConfig.API_SCHEME,
252+
tokenApiHost: DefaultConfig.TOKEN_API_HOST,
253+
clientPlatform: ClientPlatform.BROWSER,
254+
sdkClientVersion: 'v'
255+
}
256+
);
257+
258+
(authImpl as any).firebaseTokenSubscription = subscription;
259+
});
260+
261+
afterEach(() => {
262+
sinon.restore();
263+
});
264+
265+
it('should load the firebaseToken from persistence and set it', async () => {
266+
await authImpl._initializeWithPersistence([persistenceStub as PersistenceInternal]);
267+
268+
expect(persistenceManager.getFirebaseToken).to.have.been.called;
269+
expect((authImpl as any).firebaseToken).to.eql(mockToken);
270+
expect(subscription.next).to.have.been.calledWith(mockToken);
271+
});
272+
273+
it('should set firebaseToken to null if getFirebaseToken returns undefined', async () => {
274+
persistenceManager.getFirebaseToken.resolves(undefined);
275+
276+
await authImpl._initializeWithPersistence([persistenceStub as PersistenceInternal]);
277+
278+
expect((authImpl as any).firebaseToken).to.be.null;
279+
expect(subscription.next).to.have.been.calledWith(null);
280+
});
281+
282+
it('should set firebaseToken to null if getFirebaseToken returns null', async () => {
283+
persistenceManager.getFirebaseToken.resolves(null);
284+
285+
await authImpl._initializeWithPersistence([persistenceStub as PersistenceInternal]);
286+
287+
expect((authImpl as any).firebaseToken).to.be.null;
288+
expect(subscription.next).to.have.been.calledWith(null);
289+
});
290+
});
291+
153292
describe('#signOut', () => {
154293
it('sets currentUser to null, calls remove', async () => {
155294
await auth._updateCurrentUser(testUser(auth, 'test'));

0 commit comments

Comments
 (0)