diff --git a/test/Authentication.test.ts b/test/Authentication.test.ts index b9cadb1..430bcab 100644 --- a/test/Authentication.test.ts +++ b/test/Authentication.test.ts @@ -34,6 +34,21 @@ useApp(async (addr, port) => { }(addr, port); }); +async function followMagicLinkFromMail(cookies: string[]): Promise { + const mail: Record | null = await popEmail(); + expect(mail).not.toBeNull(); + + const query = (mail?.text as string).split('/magic/link?')[1].split('\n')[0]; + expect(query).toBeDefined(); + + await agent.get('/magic/link?' + query) + .expect(200); + await agent.get('/magic/lobby') + .set('Cookie', cookies) + .expect(302) + .expect('Location', '/'); +} + let agent: supertest.SuperTest; beforeAll(() => { @@ -163,18 +178,7 @@ describe('Register with email (magic_link)', () => { .expect(302) .expect('Location', '/magic/lobby?redirect_uri=%2Fcsrf'); - const mail: Record | null = await popEmail(); - expect(mail).not.toBeNull(); - - const query = (mail?.text as string).split('/magic/link?')[1].split('\n')[0]; - expect(query).toBeDefined(); - - await agent.get('/magic/link?' + query) - .expect(200); - await agent.get('/magic/lobby') - .set('Cookie', cookies) - .expect(302) - .expect('Location', '/'); + await followMagicLinkFromMail(cookies); // Verify saved user const user = await User.select() @@ -225,18 +229,7 @@ describe('Register with email (magic_link)', () => { .expect(302) .expect('Location', '/magic/lobby?redirect_uri=%2Fcsrf'); - const mail: Record | null = await popEmail(); - expect(mail).not.toBeNull(); - - const query = (mail?.text as string).split('/magic/link?')[1].split('\n')[0]; - expect(query).toBeDefined(); - - await agent.get('/magic/link?' + query) - .expect(200); - await agent.get('/magic/lobby') - .set('Cookie', cookies) - .expect(302) - .expect('Location', '/'); + await followMagicLinkFromMail(cookies); // Verify saved user const user = await User.select() @@ -278,18 +271,7 @@ describe('Register with email (magic_link)', () => { .expect(302) .expect('Location', '/magic/lobby?redirect_uri=%2Fcsrf'); - const mail: Record | null = await popEmail(); - expect(mail).not.toBeNull(); - - const query = (mail?.text as string).split('/magic/link?')[1].split('\n')[0]; - expect(query).toBeDefined(); - - await agent.get('/magic/link?' + query) - .expect(200); - await agent.get('/magic/lobby') - .set('Cookie', cookies) - .expect(302) - .expect('Location', '/'); + await followMagicLinkFromMail(cookies); // Verify saved user const user = await User.select()