Authentication tests: add more tests to email registration
This commit is contained in:
parent
683fe7262b
commit
0d0724c315
@ -94,7 +94,7 @@ describe('Register with username', () => {
|
|||||||
expect(user2).toBeNull();
|
expect(user2).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Can\'t register taken username', async () => {
|
test('Cannot register taken username', async () => {
|
||||||
// Check that there is no hordak in DB
|
// Check that there is no hordak in DB
|
||||||
expect(await User.select()
|
expect(await User.select()
|
||||||
.where('name', 'hordak')
|
.where('name', 'hordak')
|
||||||
@ -131,8 +131,8 @@ describe('Register with username', () => {
|
|||||||
csrf: res2.text,
|
csrf: res2.text,
|
||||||
auth_method: 'password',
|
auth_method: 'password',
|
||||||
identifier: 'hordak',
|
identifier: 'hordak',
|
||||||
password: 'horde_prime_will_rise',
|
password: 'horde_prime_will_rise_unless',
|
||||||
password_confirmation: 'horde_prime_will_rise',
|
password_confirmation: 'horde_prime_will_rise_unless',
|
||||||
terms: 'on',
|
terms: 'on',
|
||||||
})
|
})
|
||||||
.expect(400);
|
.expect(400);
|
||||||
@ -146,48 +146,172 @@ describe('Register with username', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Register with email (magic_link)', async () => {
|
describe('Register with email (magic_link)', () => {
|
||||||
const res = await agent.get('/csrf').expect(200);
|
test('General case', async () => {
|
||||||
const cookies = res.get('Set-Cookie');
|
const res = await agent.get('/csrf').expect(200);
|
||||||
const csrf = res.text;
|
const cookies = res.get('Set-Cookie');
|
||||||
|
const csrf = res.text;
|
||||||
|
|
||||||
expect(cookies).toBeDefined();
|
await agent.post('/auth/register')
|
||||||
await agent.post('/auth/register')
|
.set('Cookie', cookies)
|
||||||
.set('Cookie', cookies)
|
.send({
|
||||||
.send({
|
csrf: csrf,
|
||||||
csrf: csrf,
|
auth_method: 'magic_link',
|
||||||
auth_method: 'magic_link',
|
identifier: 'glimmer@example.org',
|
||||||
identifier: 'glimmer@example.org',
|
name: 'glimmer',
|
||||||
name: 'glimmer',
|
})
|
||||||
})
|
.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();
|
const mail: Record<string, unknown> | null = await popEmail();
|
||||||
expect(mail).not.toBeNull();
|
expect(mail).not.toBeNull();
|
||||||
|
|
||||||
const query = (mail?.text as string).split('/magic/link?')[1].split('\n')[0];
|
const query = (mail?.text as string).split('/magic/link?')[1].split('\n')[0];
|
||||||
expect(query).toBeDefined();
|
expect(query).toBeDefined();
|
||||||
|
|
||||||
await agent.get('/magic/link?' + query)
|
await agent.get('/magic/link?' + query)
|
||||||
.expect(200);
|
.expect(200);
|
||||||
await agent.get('/magic/lobby')
|
await agent.get('/magic/lobby')
|
||||||
.set('Cookie', cookies)
|
.set('Cookie', cookies)
|
||||||
.expect(302)
|
.expect(302)
|
||||||
.expect('Location', '/');
|
.expect('Location', '/');
|
||||||
|
|
||||||
// Verify saved user
|
// Verify saved user
|
||||||
const user = await User.select()
|
const user = await User.select()
|
||||||
.with('mainEmail')
|
.with('mainEmail')
|
||||||
.where('name', 'glimmer')
|
.where('name', 'glimmer')
|
||||||
.first();
|
.first();
|
||||||
|
|
||||||
expect(user).toBeDefined();
|
expect(user).toBeDefined();
|
||||||
|
|
||||||
const email = user?.mainEmail.getOrFail();
|
const email = user?.mainEmail.getOrFail();
|
||||||
expect(email).toBeDefined();
|
expect(email).toBeDefined();
|
||||||
expect(email?.email).toStrictEqual('glimmer@example.org');
|
expect(email?.email).toStrictEqual('glimmer@example.org');
|
||||||
|
|
||||||
expect(user?.as(UserNameComponent).name).toStrictEqual('glimmer');
|
expect(user?.as(UserNameComponent).name).toStrictEqual('glimmer');
|
||||||
await expect(user?.as(UserPasswordComponent).verifyPassword('')).resolves.toStrictEqual(false);
|
await expect(user?.as(UserPasswordComponent).verifyPassword('')).resolves.toStrictEqual(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Cannot register without specifying username', async () => {
|
||||||
|
const res = await agent.get('/csrf').expect(200);
|
||||||
|
const cookies = res.get('Set-Cookie');
|
||||||
|
const csrf = res.text;
|
||||||
|
|
||||||
|
await agent.post('/auth/register')
|
||||||
|
.set('Cookie', cookies)
|
||||||
|
.send({
|
||||||
|
csrf: csrf,
|
||||||
|
auth_method: 'magic_link',
|
||||||
|
identifier: 'glimmer@example.org',
|
||||||
|
})
|
||||||
|
.expect(400);
|
||||||
|
|
||||||
|
expect(await popEmail()).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Cannot register taken username', async () => {
|
||||||
|
const res = await agent.get('/csrf').expect(200);
|
||||||
|
const cookies = res.get('Set-Cookie');
|
||||||
|
const csrf = res.text;
|
||||||
|
|
||||||
|
await agent.post('/auth/register')
|
||||||
|
.set('Cookie', cookies)
|
||||||
|
.send({
|
||||||
|
csrf: csrf,
|
||||||
|
auth_method: 'magic_link',
|
||||||
|
identifier: 'angella@example.org',
|
||||||
|
name: 'angella',
|
||||||
|
})
|
||||||
|
.expect(302)
|
||||||
|
.expect('Location', '/magic/lobby?redirect_uri=%2Fcsrf');
|
||||||
|
|
||||||
|
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', '/');
|
||||||
|
|
||||||
|
// Verify saved user
|
||||||
|
const user = await User.select()
|
||||||
|
.with('mainEmail')
|
||||||
|
.where('name', 'glimmer')
|
||||||
|
.first();
|
||||||
|
|
||||||
|
expect(user).toBeDefined();
|
||||||
|
|
||||||
|
// Attempt register with another mail but same username
|
||||||
|
const res2 = await agent.get('/csrf').expect(200);
|
||||||
|
|
||||||
|
await agent.post('/auth/register')
|
||||||
|
.set('Cookie', res2.get('Set-Cookie'))
|
||||||
|
.send({
|
||||||
|
csrf: res2.text,
|
||||||
|
auth_method: 'magic_link',
|
||||||
|
identifier: 'angella_something_else@example.org',
|
||||||
|
name: 'angella',
|
||||||
|
})
|
||||||
|
.expect(400);
|
||||||
|
|
||||||
|
expect(await popEmail()).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Cannot register taken email', async () => {
|
||||||
|
const res = await agent.get('/csrf').expect(200);
|
||||||
|
const cookies = res.get('Set-Cookie');
|
||||||
|
const csrf = res.text;
|
||||||
|
|
||||||
|
await agent.post('/auth/register')
|
||||||
|
.set('Cookie', cookies)
|
||||||
|
.send({
|
||||||
|
csrf: csrf,
|
||||||
|
auth_method: 'magic_link',
|
||||||
|
identifier: 'bow@example.org',
|
||||||
|
name: 'bow',
|
||||||
|
})
|
||||||
|
.expect(302)
|
||||||
|
.expect('Location', '/magic/lobby?redirect_uri=%2Fcsrf');
|
||||||
|
|
||||||
|
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', '/');
|
||||||
|
|
||||||
|
// Verify saved user
|
||||||
|
const user = await User.select()
|
||||||
|
.with('mainEmail')
|
||||||
|
.where('name', 'glimmer')
|
||||||
|
.first();
|
||||||
|
|
||||||
|
expect(user).toBeDefined();
|
||||||
|
|
||||||
|
// Attempt register with another mail but same username
|
||||||
|
const res2 = await agent.get('/csrf').expect(200);
|
||||||
|
|
||||||
|
await agent.post('/auth/register')
|
||||||
|
.set('Cookie', res2.get('Set-Cookie'))
|
||||||
|
.send({
|
||||||
|
csrf: res2.text,
|
||||||
|
auth_method: 'magic_link',
|
||||||
|
identifier: 'bow@example.org',
|
||||||
|
name: 'bow2',
|
||||||
|
})
|
||||||
|
.expect(400);
|
||||||
|
|
||||||
|
expect(await popEmail()).toBeNull();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user