import 'fast-text-encoding' import { MessageChannel } from 'worker_threads' import unfetch from 'unfetch' import { verify } from '../../src/jwt' import * as utils from '../../src/utils' import * as scope from '../../src/scope' // @ts-ignore import { TEST_CODE_CHALLENGE } from '../constants' import { loginWithPopupFn, loginWithRedirectFn, setupFn } from './helpers' jest.mock('unfetch') jest.mock('es-cookie') jest.mock('../../src/jwt') jest.mock('../../src/worker/token.worker') const mockWindow = global const mockFetch = (mockWindow.fetch = unfetch) const mockVerify = verify jest .spyOn(utils, 'bufferToBase64UrlEncoded') .mockReturnValue(TEST_CODE_CHALLENGE) jest.spyOn(utils, 'runPopup') const setup = setupFn(mockVerify) const loginWithRedirect = loginWithRedirectFn(mockWindow, mockFetch) const loginWithPopup = loginWithPopupFn(mockWindow, mockFetch) describe('AuthcClient', () => { const oldWindowLocation = window.location beforeEach(() => { // https://www.benmvp.com/blog/mocking-window-location-methods-jest-jsdom/ delete window.location window.location = Object.defineProperties( {}, { ...Object.getOwnPropertyDescriptors(oldWindowLocation), assign: { configurable: true, value: jest.fn(), }, }, ) as Location // -- mockWindow.open = jest.fn() mockWindow.addEventListener = jest.fn() mockWindow.crypto = { subtle: { digest: () => 'foo', }, getRandomValues() { return '123' }, } mockWindow.MessageChannel = MessageChannel mockWindow.Worker = {} jest.spyOn(scope, 'getUniqueScopes') sessionStorage.clear() }) afterEach(() => { mockFetch.mockReset() jest.clearAllMocks() window.location = oldWindowLocation }) describe('isAuthenticated', () => { describe('loginWithRedirect', () => { it('returns true if there is a user', async () => { const authc = setup() await loginWithRedirect(authc) const result = await authc.isAuthenticated() expect(result).toBe(true) }) it('returns false if error was returned', async () => { const authc = setup() try { await loginWithRedirect(authc, undefined, { authorize: { error: 'some-error', }, }) } catch { } const result = await authc.isAuthenticated() expect(result).toBe(false) }) it('returns false if token call fails', async () => { const authc = setup() try { await loginWithRedirect(authc, undefined, { token: { success: false }, }) } catch { } const result = await authc.isAuthenticated() expect(result).toBe(false) }) }) describe('loginWithPopup', () => { it('returns true if there is a user', async () => { const authc = setup() await loginWithPopup(authc) const result = await authc.isAuthenticated() expect(result).toBe(true) }) }) it('returns false if code not part of URL', async () => { const authc = setup() try { await loginWithPopup(authc, undefined, undefined, { authorize: { response: { error: 'some error', }, }, }) } catch { } const result = await authc.isAuthenticated() expect(result).toBe(false) }) it('returns false if there is no user', async () => { const authc = setup() const result = await authc.isAuthenticated() expect(result).toBe(false) }) }) })