|
| 1 | +import axios from 'axios'; |
| 2 | +import {Browser, Page} from 'puppeteer'; |
| 3 | +import {afterAll, beforeAll, describe, expect, it} from 'vitest'; |
| 4 | +import {newTest, GotifyTest} from './setup'; |
| 5 | +import {DEX_PASSWORD, DexUser} from './dex'; |
| 6 | +import {waitForExists} from './utils'; |
| 7 | +import * as selector from './selector'; |
| 8 | +import * as auth from './authentication'; |
| 9 | + |
| 10 | +const linkUser: DexUser = {email: 'link@gotify.net', username: 'linkuser', userID: 'id-link'}; |
| 11 | +const dupUser1: DexUser = {email: 'dup1@gotify.net', username: 'dupuser', userID: 'id-dup-1'}; |
| 12 | +const dupUser2: DexUser = {email: 'dup2@gotify.net', username: 'dupuser', userID: 'id-dup-2'}; |
| 13 | + |
| 14 | +const createLocalUser = async (url: string, name: string, pass: string): Promise<void> => |
| 15 | + axios.post( |
| 16 | + `${url}/user`, |
| 17 | + {name, pass, admin: false}, |
| 18 | + {auth: {username: 'admin', password: 'admin'}} |
| 19 | + ); |
| 20 | + |
| 21 | +const loginWithOIDC = async (page: Page, user: DexUser): Promise<void> => { |
| 22 | + await waitForExists(page, selector.heading(), 'Login'); |
| 23 | + const href = await page.$eval('#oidc-login', (a) => (a as HTMLAnchorElement).href); |
| 24 | + await page.goto(href); |
| 25 | + |
| 26 | + await page.waitForSelector('#login'); |
| 27 | + await page.type('#login', user.email); |
| 28 | + await page.type('#password', DEX_PASSWORD); |
| 29 | + await page.click('#submit-login'); |
| 30 | +}; |
| 31 | + |
| 32 | +const expectLoggedIn = async (page: Page): Promise<void> => { |
| 33 | + await waitForExists(page, selector.heading(), 'All Messages'); |
| 34 | + await page.waitForSelector('#logout'); |
| 35 | +}; |
| 36 | + |
| 37 | +const oidcError = async (page: Page): Promise<string> => { |
| 38 | + await page.waitForFunction(() => document.location.pathname.includes('/auth/oidc/callback')); |
| 39 | + return page.evaluate(() => document.body.innerText); |
| 40 | +}; |
| 41 | + |
| 42 | +const clearSession = async (browser: Browser): Promise<void> => { |
| 43 | + await browser.deleteCookie(...(await browser.cookies())); |
| 44 | +}; |
| 45 | + |
| 46 | +describe('OIDC login of an existing local user without link-by-username', () => { |
| 47 | + let gotify: GotifyTest; |
| 48 | + let page: Page; |
| 49 | + beforeAll(async () => { |
| 50 | + gotify = await newTest('', { |
| 51 | + oidc: {autoRegister: true, linkByUsername: false, users: [linkUser]}, |
| 52 | + }); |
| 53 | + page = gotify.page; |
| 54 | + await createLocalUser(gotify.url, linkUser.username, 'localpass'); |
| 55 | + }); |
| 56 | + afterAll(async () => await gotify.close()); |
| 57 | + |
| 58 | + it('rejects the oidc login because linking is disabled', async () => { |
| 59 | + await loginWithOIDC(page, linkUser); |
| 60 | + expect(await oidcError(page)).toContain( |
| 61 | + `a local user with the username ${linkUser.username} already exists and linking by username is disabled` |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('still allows the local user to log in with a password', async () => { |
| 66 | + await page.goto(gotify.url); |
| 67 | + await auth.login(page, 'linkuser', 'localpass'); |
| 68 | + await auth.logout(page); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe('OIDC login of an existing local user with link-by-username', () => { |
| 73 | + let gotify: GotifyTest; |
| 74 | + let page: Page; |
| 75 | + beforeAll(async () => { |
| 76 | + gotify = await newTest('', { |
| 77 | + oidc: {autoRegister: true, linkByUsername: true, users: [linkUser]}, |
| 78 | + }); |
| 79 | + page = gotify.page; |
| 80 | + await createLocalUser(gotify.url, linkUser.username, 'localpass'); |
| 81 | + }); |
| 82 | + afterAll(async () => await gotify.close()); |
| 83 | + |
| 84 | + it('links the existing local user and logs in', async () => { |
| 85 | + await loginWithOIDC(page, linkUser); |
| 86 | + await expectLoggedIn(page); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe('OIDC login with two identities sharing the same username', () => { |
| 91 | + let gotify: GotifyTest; |
| 92 | + let page: Page; |
| 93 | + beforeAll(async () => { |
| 94 | + gotify = await newTest('', { |
| 95 | + oidc: {autoRegister: true, linkByUsername: true, users: [dupUser1, dupUser2]}, |
| 96 | + }); |
| 97 | + page = gotify.page; |
| 98 | + }); |
| 99 | + afterAll(async () => await gotify.close()); |
| 100 | + |
| 101 | + it('auto-registers the first identity', async () => { |
| 102 | + await loginWithOIDC(page, dupUser1); |
| 103 | + await expectLoggedIn(page); |
| 104 | + }); |
| 105 | + |
| 106 | + it('clears session', () => clearSession(gotify.browser)); |
| 107 | + it('rejects the second identity with same username', async () => { |
| 108 | + await page.goto(gotify.url); |
| 109 | + await loginWithOIDC(page, dupUser2); |
| 110 | + expect(await oidcError(page)).toContain( |
| 111 | + `the user ${dupUser2.username} is already bound to a different OIDC identity` |
| 112 | + ); |
| 113 | + }); |
| 114 | +}); |
0 commit comments