|
| 1 | +import { TestEnvironment } from '__test__/support/environment/TestEnvironment'; |
| 2 | +import { updateIdentityModel } from '__test__/support/helpers/setup'; |
| 3 | +import { SubscriptionModel } from 'src/core/models/SubscriptionModel'; |
| 4 | +import { db } from 'src/shared/database/client'; |
| 5 | +import Log from 'src/shared/libraries/Log'; |
| 6 | +import * as userDirector from '../../onesignal/userDirector'; |
| 7 | +import LoginManager from './LoginManager'; |
| 8 | + |
| 9 | +const createUserOnServerSpy = vi.spyOn(userDirector, 'createUserOnServer'); |
| 10 | + |
| 11 | +describe('LoginManager', () => { |
| 12 | + beforeEach(() => { |
| 13 | + TestEnvironment.initialize(); |
| 14 | + }); |
| 15 | + |
| 16 | + test('login: skips when externalId unchanged and logs debug', async () => { |
| 17 | + const debugSpy = vi |
| 18 | + .spyOn(Log, '_debug') |
| 19 | + .mockImplementation(() => undefined); |
| 20 | + await updateIdentityModel('external_id', 'same-id'); |
| 21 | + |
| 22 | + await LoginManager.login('same-id'); |
| 23 | + expect(debugSpy).toHaveBeenCalledWith( |
| 24 | + 'Login: External ID already set, skipping login', |
| 25 | + ); |
| 26 | + }); |
| 27 | + |
| 28 | + test('login: stores token when provided and enqueues operations', async () => { |
| 29 | + const dbSpy = vi.spyOn(db, 'put'); |
| 30 | + // mock push subscription exists so transfer op enqueues |
| 31 | + const createPushSub = () => ({ |
| 32 | + id: 'push-sub-id', |
| 33 | + }); |
| 34 | + vi.spyOn( |
| 35 | + OneSignal._coreDirector, |
| 36 | + '_getPushSubscriptionModel', |
| 37 | + ).mockResolvedValue(createPushSub() as SubscriptionModel); |
| 38 | + const enqueueSpy = vi.spyOn( |
| 39 | + OneSignal._coreDirector._operationRepo, |
| 40 | + '_enqueue', |
| 41 | + ); |
| 42 | + const enqueueAndWaitSpy = vi |
| 43 | + .spyOn(OneSignal._coreDirector._operationRepo, '_enqueueAndWait') |
| 44 | + .mockResolvedValue(undefined); |
| 45 | + |
| 46 | + await LoginManager.login('new-external-id', 'jwt-token-123'); |
| 47 | + expect(dbSpy).toHaveBeenCalledWith('Ids', { |
| 48 | + id: 'jwt-token-123', |
| 49 | + type: 'jwtToken', |
| 50 | + }); |
| 51 | + expect(enqueueSpy).toHaveBeenCalled(); |
| 52 | + expect(enqueueAndWaitSpy).toHaveBeenCalled(); |
| 53 | + }); |
| 54 | + |
| 55 | + test('logout: no external id logs debug and returns', async () => { |
| 56 | + const debugSpy = vi |
| 57 | + .spyOn(Log, '_debug') |
| 58 | + .mockImplementation(() => undefined); |
| 59 | + await updateIdentityModel('external_id', undefined); |
| 60 | + await LoginManager.logout(); |
| 61 | + expect(debugSpy).toHaveBeenCalledWith( |
| 62 | + 'Logout: User is not logged in, skipping logout', |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + test('logout: with external id resets models and creates anonymous user', async () => { |
| 67 | + await updateIdentityModel('external_id', 'abc'); |
| 68 | + await LoginManager.logout(); |
| 69 | + expect(createUserOnServerSpy).toHaveBeenCalled(); |
| 70 | + }); |
| 71 | +}); |
0 commit comments