Authenticated.test: reorganize tests

Closes #15
This commit is contained in:
Alice Gaudon 2021-01-22 13:35:30 +01:00
parent 49168b5391
commit 93c41ebd7e

View File

@ -809,6 +809,21 @@ describe('Change password', () => {
describe('Manage email addresses', () => { 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; let cookies: string[], csrf: string;
test('Prepare user', async () => { test('Prepare user', async () => {
const res = await agent.get('/csrf').expect(200); const res = await agent.get('/csrf').expect(200);
@ -839,185 +854,177 @@ describe('Manage email addresses', () => {
}).save(); }).save();
}); });
test('Add invalid email addresses', async () => { describe('Add', () => {
await agent.post('/account/add-email') test('Add invalid email addresses', async () => {
.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',
]) {
await agent.post('/account/add-email') await agent.post('/account/add-email')
.set('Cookie', cookies) .set('Cookie', cookies)
.send({ .send({
csrf: csrf, 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(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(); test('Set non-owned address as main email address', async () => {
expect(userEmail).not.toBeNull(); const beforeSecondaryEmail = await testMainSecondaryState('katara3@example.org', 'not_katara@example.org');
expect(userEmail?.user_id).toBe(expectedUserId);
} // 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) { describe('Remove', () => {
const user = await User.select('main_email_id').where('name', 'katara').first(); 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(); // Set secondary as main
expect(mainEmail).not.toBeNull(); await agent.post('/account/remove-email')
expect(user?.main_email_id).toBe(mainEmail?.id); .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(await UserEmail.select().where('email', 'katara2@example.org').count()).toBe(0);
expect(secondaryEmail).not.toBeNull(); });
expect(user?.main_email_id).not.toBe(secondaryEmail?.id);
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 () => { // Set secondary as main
await testMainSecondaryState('katara@example.org', 'katara3@example.org'); 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 expect(await UserEmail.select().where('email', 'not_katara@example.org').count()).toBe(1);
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('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 () => { test('Remove main email address', async () => {
const beforeSecondaryEmail = await testMainSecondaryState('katara@example.org', 'katara3@example.org'); expect(await UserEmail.select().where('email', 'katara3@example.org').count()).toBe(1);
// Set secondary as main // Set secondary as main
await agent.post('/account/set-main-email') await agent.post('/account/remove-email')
.set('Cookie', cookies) .set('Cookie', cookies)
.send({ .send({
csrf: csrf, csrf: csrf,
id: beforeSecondaryEmail?.id, id: (await UserEmail.select().where('email', 'katara3@example.org').first())?.id,
}) })
.expect(302) .expect(400);
.expect('Location', '/csrf'); // TODO: because of buggy RedirectBackComponent, change to /account once fixed.
await testMainSecondaryState('katara3@example.org', 'katara@example.org'); expect(await UserEmail.select().where('email', 'katara3@example.org').count()).toBe(1);
}); });
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);
}); });
}); });