|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import { JwtTokenCache, MfaOtpRequestCache } from './cache'; |
| 6 | +import { |
| 7 | + clearMfaAndJwtCacheOnInvalidJwt, |
| 8 | + isInvalidJwtError, |
| 9 | +} from './mfa-guard-utils'; |
| 10 | + |
| 11 | +const defaultSessionToken = 'you-get-a-session-token'; |
| 12 | +const jwt = 'and-you-get-a-jwt'; |
| 13 | +const scope = 'test'; |
| 14 | + |
| 15 | +jest.mock('./cache', () => { |
| 16 | + const actual = jest.requireActual('./cache'); |
| 17 | + return { |
| 18 | + __esModule: true, |
| 19 | + ...actual, |
| 20 | + sessionToken: jest.fn(() => defaultSessionToken), |
| 21 | + }; |
| 22 | +}); |
| 23 | + |
| 24 | +describe('mfa-guard-utils', () => { |
| 25 | + let sessionTokenSpy: jest.SpyInstance; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + sessionTokenSpy = jest.mocked(require('./cache').sessionToken); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + sessionTokenSpy.mockReturnValue(defaultSessionToken); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('isInvalidJwtError', () => { |
| 36 | + it('should return true if the error is an invalid JWT error', () => { |
| 37 | + expect(isInvalidJwtError({ code: 401, errno: 110 })).toBe(true); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should return false if the error is not an invalid JWT error', () => { |
| 41 | + expect(isInvalidJwtError({ code: 401, errno: 100 })).toBe(false); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should return false if the error is not an object', () => { |
| 45 | + expect(isInvalidJwtError('not-an-object')).toBe(false); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should return false if the error is not an object with code and errno properties', () => { |
| 49 | + expect(isInvalidJwtError({ code: 401 })).toBe(false); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('clearMfaAndJwtCacheOnInvalidJwt', () => { |
| 54 | + let removeJwtSpy: jest.SpyInstance; |
| 55 | + let removeOtpSpy: jest.SpyInstance; |
| 56 | + |
| 57 | + beforeEach(() => { |
| 58 | + removeJwtSpy = jest.spyOn(JwtTokenCache, 'removeToken'); |
| 59 | + removeOtpSpy = jest.spyOn(MfaOtpRequestCache, 'remove'); |
| 60 | + }); |
| 61 | + |
| 62 | + afterEach(() => { |
| 63 | + removeJwtSpy.mockReset(); |
| 64 | + removeOtpSpy.mockReset(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should clear the MFA and JWT cache if the error is an invalid JWT error', () => { |
| 68 | + const e = { code: 401, errno: 110 }; |
| 69 | + |
| 70 | + clearMfaAndJwtCacheOnInvalidJwt(e, scope); |
| 71 | + |
| 72 | + expect(removeOtpSpy).toHaveBeenCalledWith(defaultSessionToken, scope); |
| 73 | + expect(removeJwtSpy).toHaveBeenCalledWith(defaultSessionToken, scope); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should not clear the MFA and JWT cache if the error is not an invalid JWT error', () => { |
| 77 | + MfaOtpRequestCache.set(defaultSessionToken, scope); |
| 78 | + JwtTokenCache.setToken(defaultSessionToken, scope, jwt); |
| 79 | + const e = { code: 401, errno: 100 }; |
| 80 | + |
| 81 | + clearMfaAndJwtCacheOnInvalidJwt(e, scope); |
| 82 | + |
| 83 | + expect(MfaOtpRequestCache.get(defaultSessionToken, scope)).toBeDefined(); |
| 84 | + expect(JwtTokenCache.getToken(defaultSessionToken, scope)).toBeDefined(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should not clear the MFA and JWT cache if the session token is not set', () => { |
| 88 | + MfaOtpRequestCache.set(defaultSessionToken, scope); |
| 89 | + JwtTokenCache.setToken(defaultSessionToken, scope, jwt); |
| 90 | + |
| 91 | + // Override sessionToken to return undefined |
| 92 | + sessionTokenSpy.mockReturnValue(undefined); |
| 93 | + |
| 94 | + const e = { code: 401, errno: 110 }; |
| 95 | + |
| 96 | + clearMfaAndJwtCacheOnInvalidJwt(e, scope); |
| 97 | + |
| 98 | + expect(removeOtpSpy).not.toHaveBeenCalled(); |
| 99 | + expect(removeJwtSpy).not.toHaveBeenCalled(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should not clear the MFA and JWT cache if the session token is null', () => { |
| 103 | + MfaOtpRequestCache.set(defaultSessionToken, scope); |
| 104 | + JwtTokenCache.setToken(defaultSessionToken, scope, jwt); |
| 105 | + |
| 106 | + // Override sessionToken to return null |
| 107 | + sessionTokenSpy.mockReturnValue(null); |
| 108 | + |
| 109 | + const e = { code: 401, errno: 110 }; |
| 110 | + |
| 111 | + clearMfaAndJwtCacheOnInvalidJwt(e, scope); |
| 112 | + |
| 113 | + expect(removeOtpSpy).not.toHaveBeenCalled(); |
| 114 | + expect(removeJwtSpy).not.toHaveBeenCalled(); |
| 115 | + }); |
| 116 | + |
| 117 | + it('does not throw if token does not exist by scope', () => { |
| 118 | + // Set a token in cache with a different scope |
| 119 | + JwtTokenCache.setToken(defaultSessionToken, 'email', jwt); |
| 120 | + |
| 121 | + const e = { code: 401, errno: 110 }; |
| 122 | + |
| 123 | + expect(() => clearMfaAndJwtCacheOnInvalidJwt(e, scope)).not.toThrow(); |
| 124 | + |
| 125 | + expect(removeOtpSpy).toHaveBeenCalledWith(defaultSessionToken, scope); |
| 126 | + expect(removeJwtSpy).toHaveBeenCalledWith(defaultSessionToken, scope); |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments