go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/common/api/auth_state/auth_state.test.ts (about) 1 // Copyright 2021 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 import { 16 ANONYMOUS_IDENTITY, 17 getAuthStateCache, 18 getAuthStateCacheSync, 19 msToExpire, 20 setAuthStateCache, 21 } from './auth_state'; 22 23 describe('auth_state', () => { 24 beforeEach(() => { 25 jest.useFakeTimers(); 26 }); 27 28 afterEach(() => { 29 jest.useRealTimers(); 30 }); 31 32 test('support accessing auth state synchronously', () => { 33 const state = { 34 accessToken: Math.random().toString(), 35 identity: `user: ${Math.random()}`, 36 accessTokenExpiry: Date.now() / 1000 + 1000, 37 }; 38 setAuthStateCache(state); 39 expect(getAuthStateCacheSync()).toEqual(state); 40 }); 41 42 test('support accessing auth state asynchronously', async () => { 43 const state = { 44 accessToken: Math.random().toString(), 45 identity: `user: ${Math.random()}`, 46 accessTokenExpiry: Date.now() / 1000 + 1000, 47 }; 48 setAuthStateCache(state); 49 expect(await getAuthStateCache()).toEqual(state); 50 }); 51 52 test('clear expired auth state when accessing synchronously', () => { 53 const state = { 54 accessToken: Math.random().toString(), 55 identity: `user: ${Math.random()}`, 56 accessTokenExpiry: Date.now() / 1000 - 1000, 57 }; 58 setAuthStateCache(state); 59 expect(getAuthStateCacheSync()).toBeNull(); 60 }); 61 62 test('clear expired auth state when accessing asynchronously', async () => { 63 const state = { 64 accessToken: Math.random().toString(), 65 identity: `user: ${Math.random()}`, 66 accessTokenExpiry: Date.now() / 1000 - 1000, 67 }; 68 setAuthStateCache(state); 69 expect(await getAuthStateCache()).toBeNull(); 70 }); 71 72 describe('msToExpire', () => { 73 test('when no tokens', () => { 74 const expireMs = msToExpire({ 75 identity: ANONYMOUS_IDENTITY, 76 }); 77 expect(expireMs).toBe(Infinity); 78 }); 79 80 test('when only access token', () => { 81 const expireMs = msToExpire({ 82 identity: `user: ${Math.random()}`, 83 accessToken: Math.random().toString(), 84 accessTokenExpiry: Date.now() / 1000 + 1234, 85 }); 86 expect(expireMs).toStrictEqual(1234000); 87 }); 88 89 test('when only id token', () => { 90 const expireMs = msToExpire({ 91 identity: `user: ${Math.random()}`, 92 idToken: Math.random().toString(), 93 idTokenExpiry: Date.now() / 1000 + 1234, 94 }); 95 expect(expireMs).toStrictEqual(1234000); 96 }); 97 98 test('old id token and new access token', () => { 99 const expireMs = msToExpire({ 100 identity: `user: ${Math.random()}`, 101 idToken: Math.random().toString(), 102 idTokenExpiry: Date.now() / 1000 + 1234, 103 accessToken: Math.random().toString(), 104 accessTokenExpiry: Date.now() / 1000 + 4567, 105 }); 106 expect(expireMs).toStrictEqual(1234000); 107 }); 108 109 test('old access token and new id token', () => { 110 const expireMs = msToExpire({ 111 identity: `user: ${Math.random()}`, 112 idToken: Math.random().toString(), 113 idTokenExpiry: Date.now() / 1000 + 4567, 114 accessToken: Math.random().toString(), 115 accessTokenExpiry: Date.now() / 1000 + 1234, 116 }); 117 expect(expireMs).toStrictEqual(1234000); 118 }); 119 }); 120 });