|
| 1 | +import { RichieContextFactory as mockRichieContextFactory } from 'utils/test/factories/richie'; |
| 2 | +import API from './keycloak'; |
| 3 | + |
| 4 | +const mockKeycloakInit = jest.fn().mockResolvedValue(true); |
| 5 | +const mockKeycloakLogout = jest.fn().mockResolvedValue(undefined); |
| 6 | +const mockKeycloakLogin = jest.fn().mockResolvedValue(undefined); |
| 7 | +const mockKeycloakLoadUserProfile = jest.fn(); |
| 8 | + |
| 9 | +jest.mock('keycloak-js', () => { |
| 10 | + return jest.fn().mockImplementation(() => ({ |
| 11 | + init: mockKeycloakInit, |
| 12 | + logout: mockKeycloakLogout, |
| 13 | + login: mockKeycloakLogin, |
| 14 | + loadUserProfile: mockKeycloakLoadUserProfile, |
| 15 | + })); |
| 16 | +}); |
| 17 | + |
| 18 | +jest.mock('utils/indirection/window', () => ({ |
| 19 | + location: { |
| 20 | + origin: 'https://richie.test', |
| 21 | + pathname: '/courses/test-course/', |
| 22 | + }, |
| 23 | +})); |
| 24 | + |
| 25 | +jest.mock('utils/context', () => ({ |
| 26 | + __esModule: true, |
| 27 | + default: mockRichieContextFactory({ |
| 28 | + authentication: { |
| 29 | + backend: 'keycloak', |
| 30 | + endpoint: 'https://keycloak.test/auth', |
| 31 | + client_id: 'richie-client', |
| 32 | + realm: 'richie-realm', |
| 33 | + auth_url: 'https://keycloak.test/auth/realms/richie-realm/protocol/openid-connect/auth', |
| 34 | + }, |
| 35 | + }).one(), |
| 36 | +})); |
| 37 | + |
| 38 | +describe('Keycloak API', () => { |
| 39 | + const authConfig = { |
| 40 | + backend: 'keycloak', |
| 41 | + endpoint: 'https://keycloak.test/auth', |
| 42 | + client_id: 'richie-client', |
| 43 | + realm: 'richie-realm', |
| 44 | + auth_url: 'https://keycloak.test/auth/realms/richie-realm/protocol/openid-connect/auth', |
| 45 | + registration_url: 'https://keycloak.test/auth/realms/richie-realm/protocol/openid-connect/registrations', |
| 46 | + }; |
| 47 | + |
| 48 | + let keycloakApi: ReturnType<typeof API>; |
| 49 | + |
| 50 | + beforeEach(() => { |
| 51 | + jest.clearAllMocks(); |
| 52 | + keycloakApi = API(authConfig); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('user.me', () => { |
| 56 | + it('returns null when loadUserProfile fails', async () => { |
| 57 | + mockKeycloakLoadUserProfile.mockRejectedValueOnce(new Error('Not authenticated')); |
| 58 | + const response = await keycloakApi.user.me(); |
| 59 | + expect(response).toBeNull(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('returns user when loadUserProfile succeeds', async () => { |
| 63 | + mockKeycloakLoadUserProfile.mockResolvedValueOnce({ |
| 64 | + firstName: 'John', |
| 65 | + lastName: 'Doe', |
| 66 | + email: 'johndoe@example.com', |
| 67 | + }); |
| 68 | + |
| 69 | + const response = await keycloakApi.user.me(); |
| 70 | + expect(response).toEqual({ |
| 71 | + username: 'John Doe', |
| 72 | + email: 'johndoe@example.com', |
| 73 | + }); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('user.login', () => { |
| 78 | + it('calls keycloak.login with correct redirect URI', async () => { |
| 79 | + await keycloakApi.user.login(); |
| 80 | + |
| 81 | + expect(mockKeycloakLogin).toHaveBeenCalledWith({ |
| 82 | + redirectUri: 'https://richie.test/courses/test-course/', |
| 83 | + }); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('user.register', () => { |
| 88 | + it('calls keycloak.login with register action', async () => { |
| 89 | + await keycloakApi.user.register(); |
| 90 | + |
| 91 | + expect(mockKeycloakLogin).toHaveBeenCalledWith({ |
| 92 | + redirectUri: 'https://richie.test/courses/test-course/', |
| 93 | + action: 'REGISTER', |
| 94 | + }); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('user.logout', () => { |
| 99 | + it('calls keycloak.logout with correct redirect URI', async () => { |
| 100 | + await keycloakApi.user.logout(); |
| 101 | + |
| 102 | + expect(mockKeycloakLogout).toHaveBeenCalledWith({ |
| 103 | + redirectUri: 'https://richie.test/courses/test-course/', |
| 104 | + }); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe('Keycloak initialization', () => { |
| 109 | + it('initializes keycloak with correct configuration', () => { |
| 110 | + const Keycloak = require('keycloak-js'); |
| 111 | + |
| 112 | + expect(Keycloak).toHaveBeenCalledWith({ |
| 113 | + url: 'https://keycloak.test/auth', |
| 114 | + realm: 'richie-realm', |
| 115 | + clientId: 'richie-client', |
| 116 | + }); |
| 117 | + |
| 118 | + expect(mockKeycloakInit).toHaveBeenCalledWith({ |
| 119 | + checkLoginIframe: false, |
| 120 | + flow: 'implicit', |
| 121 | + token: undefined, |
| 122 | + }); |
| 123 | + }); |
| 124 | + }); |
| 125 | +}); |
0 commit comments