Skip to content

Commit eadfb06

Browse files
committed
update tests
1 parent 8e77482 commit eadfb06

2 files changed

Lines changed: 61 additions & 16 deletions

File tree

packages/passport/sdk/src/authManager.test.ts

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Overlay from './overlay';
66
import { PassportError, PassportErrorType } from './errors/passportError';
77
import { PassportConfiguration } from './config';
88
import { mockUser, mockUserImx, mockUserZkEvm } from './test/mocks';
9-
import { isTokenExpired } from './utils/token';
9+
import { isAccessTokenExpiredOrExpiring } from './utils/token';
1010
import { isUserZkEvm, PassportModuleConfiguration } from './types';
1111

1212
jest.mock('jwt-decode');
@@ -352,7 +352,7 @@ describe('AuthManager', () => {
352352
describe('when getUser returns a user', () => {
353353
it('should return the user', async () => {
354354
mockGetUser.mockReturnValue(mockOidcUser);
355-
(isTokenExpired as jest.Mock).mockReturnValue(false);
355+
(isAccessTokenExpiredOrExpiring as jest.Mock).mockReturnValue(false);
356356

357357
const result = await authManager.getUserOrLogin();
358358

@@ -364,7 +364,7 @@ describe('AuthManager', () => {
364364
it('calls attempts to sign in the user using signinPopup', async () => {
365365
mockGetUser.mockRejectedValue(new Error(mockErrorMsg));
366366
mockSigninPopup.mockReturnValue(mockOidcUser);
367-
(isTokenExpired as jest.Mock).mockReturnValue(false);
367+
(isAccessTokenExpiredOrExpiring as jest.Mock).mockReturnValue(false);
368368

369369
const result = await authManager.getUserOrLogin();
370370

@@ -510,16 +510,61 @@ describe('AuthManager', () => {
510510
describe('getUser', () => {
511511
it('should retrieve the user from the userManager and return the domain model', async () => {
512512
mockGetUser.mockReturnValue(mockOidcUser);
513-
(isTokenExpired as jest.Mock).mockReturnValue(false);
513+
(isAccessTokenExpiredOrExpiring as jest.Mock).mockReturnValue(false);
514514

515515
const result = await authManager.getUser();
516516

517517
expect(result).toEqual(mockUser);
518518
});
519519

520+
it('should return null when user has no idToken and isAccessTokenExpiredOrExpiring returns true', async () => {
521+
const userWithoutIdToken = { ...mockOidcUser, id_token: undefined };
522+
mockGetUser.mockReturnValue(userWithoutIdToken);
523+
(isAccessTokenExpiredOrExpiring as jest.Mock).mockReturnValue(true);
524+
525+
const result = await authManager.getUser();
526+
527+
expect(result).toBeNull();
528+
expect(isAccessTokenExpiredOrExpiring).toHaveBeenCalledWith(userWithoutIdToken);
529+
});
530+
531+
it('should refresh token when expires_in is 30 seconds or less', async () => {
532+
const expiringUser = { ...mockOidcUser, expires_in: 25 };
533+
mockGetUser.mockReturnValue(expiringUser);
534+
(isAccessTokenExpiredOrExpiring as jest.Mock).mockReturnValue(true);
535+
mockSigninSilent.mockResolvedValue(mockOidcUser);
536+
537+
const result = await authManager.getUser();
538+
539+
expect(mockSigninSilent).toBeCalledTimes(1);
540+
expect(result).toEqual(mockUser);
541+
});
542+
543+
it('should handle user with null expires_in', async () => {
544+
const userWithNullExpiresIn = { ...mockOidcUser, expires_in: null };
545+
mockGetUser.mockReturnValue(userWithNullExpiresIn);
546+
(isAccessTokenExpiredOrExpiring as jest.Mock).mockReturnValue(false);
547+
548+
const result = await authManager.getUser();
549+
550+
expect(mockSigninSilent).not.toHaveBeenCalled();
551+
expect(result).toEqual(mockUser);
552+
});
553+
554+
it('should return user directly when token is not expired or expiring', async () => {
555+
const freshUser = { ...mockOidcUser, expires_in: 3600 }; // 1 hour
556+
mockGetUser.mockReturnValue(freshUser);
557+
(isAccessTokenExpiredOrExpiring as jest.Mock).mockReturnValue(false);
558+
559+
const result = await authManager.getUser();
560+
561+
expect(mockSigninSilent).not.toHaveBeenCalled();
562+
expect(result).toEqual(mockUser);
563+
});
564+
520565
it('should call signinSilent and returns user when user token is expired with the refresh token', async () => {
521566
mockGetUser.mockReturnValue(mockOidcExpiredUser);
522-
(isTokenExpired as jest.Mock).mockReturnValue(true);
567+
(isAccessTokenExpiredOrExpiring as jest.Mock).mockReturnValue(true);
523568
mockSigninSilent.mockResolvedValue(mockOidcUser);
524569

525570
const result = await authManager.getUser();
@@ -530,7 +575,7 @@ describe('AuthManager', () => {
530575

531576
it('should reject with an error when signinSilent throws a string', async () => {
532577
mockGetUser.mockReturnValue(mockOidcExpiredUser);
533-
(isTokenExpired as jest.Mock).mockReturnValue(true);
578+
(isAccessTokenExpiredOrExpiring as jest.Mock).mockReturnValue(true);
534579
mockSigninSilent.mockRejectedValue('oops');
535580

536581
await expect(() => authManager.getUser()).rejects.toThrow(
@@ -543,7 +588,7 @@ describe('AuthManager', () => {
543588

544589
it('should return null when the user token is expired without refresh token', async () => {
545590
mockGetUser.mockReturnValue(mockOidcExpiredNoRefreshTokenUser);
546-
(isTokenExpired as jest.Mock).mockReturnValue(true);
591+
(isAccessTokenExpiredOrExpiring as jest.Mock).mockReturnValue(true);
547592

548593
const result = await authManager.getUser();
549594

@@ -553,7 +598,7 @@ describe('AuthManager', () => {
553598

554599
it('should return null when the user token is expired with the refresh token, but signinSilent returns null', async () => {
555600
mockGetUser.mockReturnValue(mockOidcExpiredUser);
556-
(isTokenExpired as jest.Mock).mockReturnValue(true);
601+
(isAccessTokenExpiredOrExpiring as jest.Mock).mockReturnValue(true);
557602
mockSigninSilent.mockResolvedValue(null);
558603
const result = await authManager.getUser();
559604

@@ -584,7 +629,7 @@ describe('AuthManager', () => {
584629
describe('when the user is expired', () => {
585630
it('should only call refresh the token once', async () => {
586631
mockGetUser.mockReturnValue(mockOidcExpiredUser);
587-
(isTokenExpired as jest.Mock).mockReturnValue(true);
632+
(isAccessTokenExpiredOrExpiring as jest.Mock).mockReturnValue(true);
588633
mockSigninSilent.mockReturnValue(mockOidcUser);
589634

590635
await Promise.allSettled([
@@ -600,7 +645,7 @@ describe('AuthManager', () => {
600645
describe('when the user does not meet the type assertion', () => {
601646
it('should return null', async () => {
602647
mockGetUser.mockReturnValue(mockOidcUser);
603-
(isTokenExpired as jest.Mock).mockReturnValue(false);
648+
(isAccessTokenExpiredOrExpiring as jest.Mock).mockReturnValue(false);
604649

605650
const result = await authManager.getUser(isUserZkEvm);
606651

@@ -617,7 +662,7 @@ describe('AuthManager', () => {
617662
zkevm_user_admin_address: mockUserZkEvm.zkEvm.userAdminAddress,
618663
},
619664
});
620-
(isTokenExpired as jest.Mock).mockReturnValue(false);
665+
(isAccessTokenExpiredOrExpiring as jest.Mock).mockReturnValue(false);
621666

622667
const result = await authManager.getUser(isUserZkEvm);
623668

packages/passport/sdk/src/utils/token.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import encode from 'jwt-encode';
22
import {
33
User as OidcUser,
44
} from 'oidc-client-ts';
5-
import { isIdTokenExpired, isTokenExpired } from './token';
5+
import { isIdTokenExpired, isAccessTokenExpiredOrExpiring } from './token';
66

77
const now = Math.floor(Date.now() / 1000);
88
const oneHourLater = now + 3600;
@@ -31,28 +31,28 @@ describe('isIdTokenExpired', () => {
3131
});
3232
});
3333

34-
describe('isTokenExpired', () => {
34+
describe('isAccessTokenExpiredOrExpiring', () => {
3535
it('should return true if expired is true', () => {
3636
const user = {
3737
id_token: mockValidIdToken,
3838
expired: true,
3939
} as unknown as OidcUser;
40-
expect(isTokenExpired(user)).toBe(true);
40+
expect(isAccessTokenExpiredOrExpiring(user)).toBe(true);
4141
});
4242

4343
it('should return false if idToken is valid', () => {
4444
const user = {
4545
id_token: mockValidIdToken,
4646
expired: false,
4747
} as unknown as OidcUser;
48-
expect(isTokenExpired(user)).toBe(false);
48+
expect(isAccessTokenExpiredOrExpiring(user)).toBe(false);
4949
});
5050

5151
it('should return true idToken is expired', () => {
5252
const user = {
5353
id_token: mockExpiredIdToken,
5454
expired: false,
5555
} as unknown as OidcUser;
56-
expect(isTokenExpired(user)).toBe(true);
56+
expect(isAccessTokenExpiredOrExpiring(user)).toBe(true);
5757
});
5858
});

0 commit comments

Comments
 (0)