From 93c41ebd7e42dce843f3eb4afa80d508f4618cb5 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Fri, 22 Jan 2021 13:35:30 +0100 Subject: [PATCH] Authenticated.test: reorganize tests Closes #15 --- test/Authentication.test.ts | 329 ++++++++++++++++++------------------ 1 file changed, 168 insertions(+), 161 deletions(-) diff --git a/test/Authentication.test.ts b/test/Authentication.test.ts index 26308ed..b1d6692 100644 --- a/test/Authentication.test.ts +++ b/test/Authentication.test.ts @@ -809,6 +809,21 @@ describe('Change password', () => { describe('Manage email addresses', () => { + + async function testMainSecondaryState(main: string, secondary: string) { + const user = await User.select('main_email_id').where('name', 'katara').first(); + + const mainEmail = await UserEmail.select().where('email', main).first(); + expect(mainEmail).not.toBeNull(); + expect(user?.main_email_id).toBe(mainEmail?.id); + + const secondaryEmail = await UserEmail.select().where('email', secondary).first(); + expect(secondaryEmail).not.toBeNull(); + expect(user?.main_email_id).not.toBe(secondaryEmail?.id); + + return secondaryEmail; + } + let cookies: string[], csrf: string; test('Prepare user', async () => { const res = await agent.get('/csrf').expect(200); @@ -839,185 +854,177 @@ describe('Manage email addresses', () => { }).save(); }); - test('Add invalid email addresses', async () => { - await agent.post('/account/add-email') - .set('Cookie', cookies) - .send({ - csrf: csrf, - }) - .expect(400); - await agent.post('/account/add-email') - .set('Cookie', cookies) - .send({ - csrf: csrf, - email: '', - }) - .expect(400); - await agent.post('/account/add-email') - .set('Cookie', cookies) - .send({ - csrf: csrf, - email: 'katara@example.org', - }) - .expect(400); - - expect(await UserEmail.select().where('email', 'katara@example.org').count()).toBe(1); - }); - - test('Add valid email', async () => { - const expectedUserId = (await User.select('id').where('name', 'katara').first())?.id; - - for (const email of [ - 'katara2@example.org', - 'katara3@example.org', - 'katara4@example.org', - ]) { + describe('Add', () => { + test('Add invalid email addresses', async () => { await agent.post('/account/add-email') .set('Cookie', cookies) .send({ csrf: csrf, - email: email, + }) + .expect(400); + await agent.post('/account/add-email') + .set('Cookie', cookies) + .send({ + csrf: csrf, + email: '', + }) + .expect(400); + await agent.post('/account/add-email') + .set('Cookie', cookies) + .send({ + csrf: csrf, + email: 'katara@example.org', + }) + .expect(400); + + expect(await UserEmail.select().where('email', 'katara@example.org').count()).toBe(1); + }); + + test('Add valid email addresses', async () => { + const expectedUserId = (await User.select('id').where('name', 'katara').first())?.id; + + for (const email of [ + 'katara2@example.org', + 'katara3@example.org', + 'katara4@example.org', + ]) { + await agent.post('/account/add-email') + .set('Cookie', cookies) + .send({ + csrf: csrf, + email: email, + }) + .expect(302) + .expect('Location', '/magic/lobby?redirect_uri=%2Faccount%2F'); + + await followMagicLinkFromMail(agent, cookies, '/account/'); + + const userEmail = await UserEmail.select().where('email', email).first(); + expect(userEmail).not.toBeNull(); + expect(userEmail?.user_id).toBe(expectedUserId); + } + }); + }); + + describe('Set main', () => { + test('Set main email address as main email address', async () => { + await testMainSecondaryState('katara@example.org', 'katara3@example.org'); + + // Set secondary as main + await agent.post('/account/set-main-email') + .set('Cookie', cookies) + .send({ + csrf: csrf, + id: (await UserEmail.select().where('email', 'katara@example.org').first())?.id, + }) + .expect(400); + + await testMainSecondaryState('katara@example.org', 'katara3@example.org'); + }); + + test('Set secondary email address as main email address', async () => { + const beforeSecondaryEmail = await testMainSecondaryState('katara@example.org', 'katara3@example.org'); + + // Set secondary as main + await agent.post('/account/set-main-email') + .set('Cookie', cookies) + .send({ + csrf: csrf, + id: beforeSecondaryEmail?.id, }) .expect(302) - .expect('Location', '/magic/lobby?redirect_uri=%2Faccount%2F'); + .expect('Location', '/csrf'); // TODO: because of buggy RedirectBackComponent, change to /account once fixed. - await followMagicLinkFromMail(agent, cookies, '/account/'); + await testMainSecondaryState('katara3@example.org', 'katara@example.org'); + }); - const userEmail = await UserEmail.select().where('email', email).first(); - expect(userEmail).not.toBeNull(); - expect(userEmail?.user_id).toBe(expectedUserId); - } + test('Set non-owned address as main email address', async () => { + const beforeSecondaryEmail = await testMainSecondaryState('katara3@example.org', 'not_katara@example.org'); + + // Set secondary as main + await agent.post('/account/set-main-email') + .set('Cookie', cookies) + .send({ + csrf: csrf, + id: beforeSecondaryEmail?.id, + }) + .expect(403); + + await testMainSecondaryState('katara3@example.org', 'not_katara@example.org'); + }); + + test('Set non-existing address as main email address', async () => { + await testMainSecondaryState('katara3@example.org', 'katara4@example.org'); + + // Set secondary as main + await agent.post('/account/set-main-email') + .set('Cookie', cookies) + .send({ + csrf: csrf, + id: 999, + }) + .expect(404); + + await testMainSecondaryState('katara3@example.org', 'katara4@example.org'); + }); }); - async function testMainSecondaryState(main: string, secondary: string) { - const user = await User.select('main_email_id').where('name', 'katara').first(); + describe('Remove', () => { + test('Remove secondary email address', async () => { + expect(await UserEmail.select().where('email', 'katara2@example.org').count()).toBe(1); - const mainEmail = await UserEmail.select().where('email', main).first(); - expect(mainEmail).not.toBeNull(); - expect(user?.main_email_id).toBe(mainEmail?.id); + // Set secondary as main + await agent.post('/account/remove-email') + .set('Cookie', cookies) + .send({ + csrf: csrf, + id: (await UserEmail.select().where('email', 'katara2@example.org').first())?.id, + }) + .expect(302) + .expect('Location', '/csrf'); // TODO: because of buggy RedirectBackComponent, change to /account once fixed. - const secondaryEmail = await UserEmail.select().where('email', secondary).first(); - expect(secondaryEmail).not.toBeNull(); - expect(user?.main_email_id).not.toBe(secondaryEmail?.id); + expect(await UserEmail.select().where('email', 'katara2@example.org').count()).toBe(0); + }); - return secondaryEmail; - } + test('Remove non-owned email address', async () => { + expect(await UserEmail.select().where('email', 'not_katara@example.org').count()).toBe(1); - test('Set main email address as main email address', async () => { - await testMainSecondaryState('katara@example.org', 'katara3@example.org'); + // Set secondary as main + await agent.post('/account/remove-email') + .set('Cookie', cookies) + .send({ + csrf: csrf, + id: (await UserEmail.select().where('email', 'not_katara@example.org').first())?.id, + }) + .expect(403); - // Set secondary as main - await agent.post('/account/set-main-email') - .set('Cookie', cookies) - .send({ - csrf: csrf, - id: (await UserEmail.select().where('email', 'katara@example.org').first())?.id, - }) - .expect(400); + expect(await UserEmail.select().where('email', 'not_katara@example.org').count()).toBe(1); + }); - await testMainSecondaryState('katara@example.org', 'katara3@example.org'); - }); + test('Remove non-existing email address', async () => { + // Set secondary as main + await agent.post('/account/remove-email') + .set('Cookie', cookies) + .send({ + csrf: csrf, + id: 999, + }) + .expect(404); + }); - test('Set secondary email address as main email address', async () => { - const beforeSecondaryEmail = await testMainSecondaryState('katara@example.org', 'katara3@example.org'); + test('Remove main email address', async () => { + expect(await UserEmail.select().where('email', 'katara3@example.org').count()).toBe(1); - // Set secondary as main - await agent.post('/account/set-main-email') - .set('Cookie', cookies) - .send({ - csrf: csrf, - id: beforeSecondaryEmail?.id, - }) - .expect(302) - .expect('Location', '/csrf'); // TODO: because of buggy RedirectBackComponent, change to /account once fixed. + // Set secondary as main + await agent.post('/account/remove-email') + .set('Cookie', cookies) + .send({ + csrf: csrf, + id: (await UserEmail.select().where('email', 'katara3@example.org').first())?.id, + }) + .expect(400); - await testMainSecondaryState('katara3@example.org', 'katara@example.org'); - }); - - test('Set non-owned address as main email address', async () => { - const beforeSecondaryEmail = await testMainSecondaryState('katara3@example.org', 'not_katara@example.org'); - - // Set secondary as main - await agent.post('/account/set-main-email') - .set('Cookie', cookies) - .send({ - csrf: csrf, - id: beforeSecondaryEmail?.id, - }) - .expect(403); - - await testMainSecondaryState('katara3@example.org', 'not_katara@example.org'); - }); - - test('Set non-existing address as main email address', async () => { - await testMainSecondaryState('katara3@example.org', 'katara4@example.org'); - - // Set secondary as main - await agent.post('/account/set-main-email') - .set('Cookie', cookies) - .send({ - csrf: csrf, - id: 999, - }) - .expect(404); - - await testMainSecondaryState('katara3@example.org', 'katara4@example.org'); - }); - - test('Remove secondary email address', async () => { - expect(await UserEmail.select().where('email', 'katara2@example.org').count()).toBe(1); - - // Set secondary as main - await agent.post('/account/remove-email') - .set('Cookie', cookies) - .send({ - csrf: csrf, - id: (await UserEmail.select().where('email', 'katara2@example.org').first())?.id, - }) - .expect(302) - .expect('Location', '/csrf'); // TODO: because of buggy RedirectBackComponent, change to /account once fixed. - - expect(await UserEmail.select().where('email', 'katara2@example.org').count()).toBe(0); - }); - - test('Remove non-owned email address', async () => { - expect(await UserEmail.select().where('email', 'not_katara@example.org').count()).toBe(1); - - // Set secondary as main - await agent.post('/account/remove-email') - .set('Cookie', cookies) - .send({ - csrf: csrf, - id: (await UserEmail.select().where('email', 'not_katara@example.org').first())?.id, - }) - .expect(403); - - expect(await UserEmail.select().where('email', 'not_katara@example.org').count()).toBe(1); - }); - - test('Remove non-existing email address', async () => { - // Set secondary as main - await agent.post('/account/remove-email') - .set('Cookie', cookies) - .send({ - csrf: csrf, - id: 999, - }) - .expect(404); - }); - - test('Remove main email address', async () => { - expect(await UserEmail.select().where('email', 'katara3@example.org').count()).toBe(1); - - // Set secondary as main - await agent.post('/account/remove-email') - .set('Cookie', cookies) - .send({ - csrf: csrf, - id: (await UserEmail.select().where('email', 'katara3@example.org').first())?.id, - }) - .expect(400); - - expect(await UserEmail.select().where('email', 'katara3@example.org').count()).toBe(1); + expect(await UserEmail.select().where('email', 'katara3@example.org').count()).toBe(1); + }); }); });