Authentication tests: refactor magic link following from mail

This commit is contained in:
Alice Gaudon 2020-11-15 14:21:11 +01:00
parent 0d0724c315
commit ef51d128f1

View File

@ -34,6 +34,21 @@ useApp(async (addr, port) => {
}(addr, port); }(addr, port);
}); });
async function followMagicLinkFromMail(cookies: string[]): Promise<void> {
const mail: Record<string, unknown> | 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<supertest.Test>; let agent: supertest.SuperTest<supertest.Test>;
beforeAll(() => { beforeAll(() => {
@ -163,18 +178,7 @@ describe('Register with email (magic_link)', () => {
.expect(302) .expect(302)
.expect('Location', '/magic/lobby?redirect_uri=%2Fcsrf'); .expect('Location', '/magic/lobby?redirect_uri=%2Fcsrf');
const mail: Record<string, unknown> | null = await popEmail(); await followMagicLinkFromMail(cookies);
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', '/');
// Verify saved user // Verify saved user
const user = await User.select() const user = await User.select()
@ -225,18 +229,7 @@ describe('Register with email (magic_link)', () => {
.expect(302) .expect(302)
.expect('Location', '/magic/lobby?redirect_uri=%2Fcsrf'); .expect('Location', '/magic/lobby?redirect_uri=%2Fcsrf');
const mail: Record<string, unknown> | null = await popEmail(); await followMagicLinkFromMail(cookies);
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', '/');
// Verify saved user // Verify saved user
const user = await User.select() const user = await User.select()
@ -278,18 +271,7 @@ describe('Register with email (magic_link)', () => {
.expect(302) .expect(302)
.expect('Location', '/magic/lobby?redirect_uri=%2Fcsrf'); .expect('Location', '/magic/lobby?redirect_uri=%2Fcsrf');
const mail: Record<string, unknown> | null = await popEmail(); await followMagicLinkFromMail(cookies);
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', '/');
// Verify saved user // Verify saved user
const user = await User.select() const user = await User.select()