|
| 1 | +import request from 'supertest'; |
| 2 | +import { admin, app, closeApplication } from './authTestApp'; |
| 3 | + |
| 4 | +type LogoutHook = (params: { adminUser: any, adminforth: any, extra: any, tr: any }) => Promise<void>; |
| 5 | + |
| 6 | +const hooks = admin.config.auth.beforeLogout as LogoutHook[]; |
| 7 | + |
| 8 | +const logout = (cookie?: string) => { |
| 9 | + const req = request(app).post('/adminapi/v1/logout'); |
| 10 | + return (cookie ? req.set("Cookie", cookie) : req).send({}); |
| 11 | +}; |
| 12 | + |
| 13 | +const loginCookie = async (): Promise<string> => { |
| 14 | + const res = await request(app).post('/adminapi/v1/login').send({ username: 'adminforth', password: 'adminforth' }); |
| 15 | + return res.headers['set-cookie'][0].split(';')[0]; |
| 16 | +}; |
| 17 | + |
| 18 | +afterAll(async () => { |
| 19 | + await closeApplication(); |
| 20 | +}); |
| 21 | + |
| 22 | +afterEach(() => { |
| 23 | + hooks.length = 0; |
| 24 | +}); |
| 25 | + |
| 26 | +describe('auth.beforeLogout', () => { |
| 27 | + it('receives logged in user, request data and translate function', async () => { |
| 28 | + const calls: { username: string, headers: Record<string, string>, translated: string }[] = []; |
| 29 | + hooks.push(async ({ adminUser, extra, tr }) => { |
| 30 | + calls.push({ |
| 31 | + username: adminUser.username, |
| 32 | + headers: extra.headers, |
| 33 | + translated: await tr('Invalid username or password', 'errors'), |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + const res = await logout(await loginCookie()).set('x-some-header', 'value'); |
| 38 | + |
| 39 | + expect(res.status).toEqual(200); |
| 40 | + expect(res.body).toEqual({ ok: true }); |
| 41 | + expect(calls).toHaveLength(1); |
| 42 | + expect(calls[0].username).toEqual('adminforth'); |
| 43 | + expect(calls[0].headers['x-some-header']).toEqual('value'); |
| 44 | + expect(calls[0].translated).toEqual('Invalid username or password'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('is called before auth cookie is removed and does not block logout', async () => { |
| 48 | + const called: string[] = []; |
| 49 | + hooks.push( |
| 50 | + async () => { called.push('first'); }, |
| 51 | + async () => { called.push('second'); }, |
| 52 | + ); |
| 53 | + |
| 54 | + const res = await logout(await loginCookie()); |
| 55 | + |
| 56 | + expect(res.status).toEqual(200); |
| 57 | + expect(called).toEqual(['first', 'second']); |
| 58 | + expect(res.headers['set-cookie'][0]).toContain('adminforth_'); |
| 59 | + expect(res.headers['set-cookie'][0]).toContain('Expires=Thu, 01 Jan 1970 00:00:00 GMT'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('passes null user when session is missing or invalid', async () => { |
| 63 | + const users: (any | null)[] = []; |
| 64 | + hooks.push(async ({ adminUser }) => { users.push(adminUser); }); |
| 65 | + |
| 66 | + const withoutCookie = await logout(); |
| 67 | + const withBrokenCookie = await logout(`adminforth_${admin.config.customization.brandNameSlug}_jwt=not-a-jwt`); |
| 68 | + |
| 69 | + expect(withoutCookie.status).toEqual(200); |
| 70 | + expect(withBrokenCookie.status).toEqual(200); |
| 71 | + expect(users).toEqual([null, null]); |
| 72 | + }); |
| 73 | +}); |
0 commit comments