import { getCookieValue, setCookieValue, logout, refreshAccessToken } from './authUtils'; import { AuthenticationConfiguration } from '../types'; // Mock the global fetch function global.fetch = jest.fn(); describe('auth-utils', () => { beforeEach(() => { // Clear all cookies before each test document.cookie.split(';').forEach((cookie) => { const eqPos = cookie.indexOf('='); const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie; document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/`; }); }); describe('getCookieValue', () => { it('should return the value of the cookie if it exists', () => { document.cookie = 'testCookie=testValue'; expect(getCookieValue('testCookie')).toBe('testValue'); }); it('should return null if the cookie does not exist', () => { expect(getCookieValue('nonExistentCookie')).toBeNull(); }); it('should return null if an error occurs when reading cookies', () => { const originalCookieDescriptor = Object.getOwnPropertyDescriptor(Document.prototype, 'cookie'); Object.defineProperty(document, 'cookie', { get: () => { throw new Error('Cookie read error'); }, configurable: true, }); expect(getCookieValue('anyCookie')).toBeNull(); if (originalCookieDescriptor) { Object.defineProperty(document, 'cookie', originalCookieDescriptor); } }); }); describe('setCookieValue', () => { it('should set the cookie with the given name and value', () => { setCookieValue('testCookie', 'testValue'); expect(document.cookie).toContain('testCookie=testValue'); }); }); describe('logout', () => { it('should clear authToken and refreshToken cookies', () => { document.cookie = 'authToken=testAuthToken'; document.cookie = 'refreshToken=testRefreshToken'; logout(); expect(getCookieValue('authToken')).toBeNull(); expect(getCookieValue('refreshToken')).toBeNull(); }); }); describe('refreshAccessToken', () => { const authConfig: AuthenticationConfiguration = { authUrl: 'https://auth.example.com', clientId: 'example-client-id', redirectUrl: 'https://example.com/callback', scope: 'offline_access', }; it('should throw an error if authConfig is not provided', async () => { await expect(refreshAccessToken()).rejects.toThrow('No auth configuration available'); }); it('should throw an error if refreshToken is not available', async () => { await expect(refreshAccessToken(authConfig)).rejects.toThrow('You are not logged in'); }); it('should refresh the access token and set the authToken cookie', async () => { document.cookie = 'refreshToken=testRefreshToken'; // Mock fetch response (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: true, json: async () => ({ access_token: 'newAccessToken' }), }); const newToken = await refreshAccessToken(authConfig); expect(newToken).toBe('newAccessToken'); expect(getCookieValue('authToken')).toBe('newAccessToken'); }); it('should call logout and throw an error if the fetch response is not ok', async () => { document.cookie = 'refreshToken=testRefreshToken'; // Mock fetch response (global.fetch as jest.Mock).mockResolvedValueOnce({ ok: false, }); await expect(refreshAccessToken(authConfig)).rejects.toThrow('Failed to refresh token'); expect(getCookieValue('authToken')).toBeNull(); expect(getCookieValue('refreshToken')).toBeNull(); }); }); });