From 969243c3e441faf228977d60babe081d4fcd0739 Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Sun, 11 Sep 2016 22:44:44 -0700 Subject: [PATCH] Added auth.login API --- server/api/__snapshots__/auth.test.js.snap | 47 ++++- server/api/auth.js | 24 +++ server/api/auth.test.js | 193 ++++++++++++++------- server/test/support.js | 1 + 4 files changed, 201 insertions(+), 64 deletions(-) diff --git a/server/api/__snapshots__/auth.test.js.snap b/server/api/__snapshots__/auth.test.js.snap index f921db9f49..c132cff7e6 100644 --- a/server/api/__snapshots__/auth.test.js.snap +++ b/server/api/__snapshots__/auth.test.js.snap @@ -1,27 +1,66 @@ -exports[`test should require params 1`] = ` +exports[`#auth.signup should require params 1`] = ` Object { "error": "name is required", "ok": false } `; -exports[`test should require unique email 1`] = ` +exports[`#auth.signup should require unique email 1`] = ` Object { "error": "User already exists with this email", "ok": false } `; -exports[`test should require unique username 1`] = ` +exports[`#auth.signup should require unique username 1`] = ` Object { "error": "User already exists with this username", "ok": false } `; -exports[`test should require valid email 1`] = ` +exports[`#auth.signup should require valid email 1`] = ` Object { "error": "email is invalid", "ok": false } `; + +exports[`#login should login with email 1`] = ` +Object { + "avatarUrl": "http://example.com/avatar.png", + "id": "86fde1d4-0050-428f-9f0b-0bf77f8bdf61", + "name": "User 1", + "username": "user1" +} +`; + +exports[`#login should login with username 1`] = ` +Object { + "avatarUrl": "http://example.com/avatar.png", + "id": "86fde1d4-0050-428f-9f0b-0bf77f8bdf61", + "name": "User 1", + "username": "user1" +} +`; + +exports[`#login should require either username or email 1`] = ` +Object { + "error": "username or email is required", + "ok": false +} +`; + +exports[`#login should require password 1`] = ` +Object { + "error": "password is required", + "ok": false +} +`; + +exports[`#login should validate password 1`] = ` +Object { + "error": "Invalid password", + "ok": false +} +`; diff --git a/server/api/auth.js b/server/api/auth.js index 281fbb02f9..5a7545b0fc 100644 --- a/server/api/auth.js +++ b/server/api/auth.js @@ -38,6 +38,30 @@ router.post('auth.signup', async (ctx) => { } }; }); +router.post('auth.login', async (ctx) => { + const { username, email, password } = ctx.request.body; + + ctx.assertPresent(password, 'password is required'); + + let user; + if (username) { + user = await User.findOne({ where: { username } }); + } else if (email) { + user = await User.findOne({ where: { email } }); + } else { + throw httpErrors.BadRequest('username or email is required'); + } + + if (!await user.verifyPassword(password)) { + throw httpErrors.BadRequest('Invalid password'); + } + + ctx.body = { data: { + user: await presentUser(ctx, user), + accessToken: user.getJwtToken(), + } }; +}); + router.post('auth.slack', async (ctx) => { const { code } = ctx.body; ctx.assertPresent(code, 'code is required'); diff --git a/server/api/auth.test.js b/server/api/auth.test.js index 03fa35b1d1..283a92328d 100644 --- a/server/api/auth.test.js +++ b/server/api/auth.test.js @@ -8,78 +8,151 @@ beforeEach(flushdb); afterAll(() => server.close()); afterAll(() => sequelize.close()); -it('should signup a new user', async () => { - const res = await server.post('/api/auth.signup', { - body: { - username: 'testuser', - name: 'Test User', - email: 'new.user@example.com', - password: 'test123!', - }, - }); - const body = await res.json(); +describe('#auth.signup', async () => { + it('should signup a new user', async () => { + const res = await server.post('/api/auth.signup', { + body: { + username: 'testuser', + name: 'Test User', + email: 'new.user@example.com', + password: 'test123!', + }, + }); + const body = await res.json(); - expect(res.status).toEqual(200); - expect(body.ok).toBe(true); - expect(body.data.user).toBeTruthy(); + expect(res.status).toEqual(200); + expect(body.ok).toBe(true); + expect(body.data.user).toBeTruthy(); + }); + + it('should require params', async () => { + const res = await server.post('/api/auth.signup', { + body: { + username: 'testuser', + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(400); + expect(body).toMatchSnapshot(); + }); + + + it('should require valid email', async () => { + const res = await server.post('/api/auth.signup', { + body: { + username: 'testuser', + name: 'Test User', + email: 'example.com', + password: 'test123!', + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(400); + expect(body).toMatchSnapshot(); + }); + + it('should require unique email', async () => { + await seed(); + const res = await server.post('/api/auth.signup', { + body: { + username: 'testuser', + name: 'Test User', + email: 'user1@example.com', + password: 'test123!', + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(400); + expect(body).toMatchSnapshot(); + }); + + it('should require unique username', async () => { + await seed(); + const res = await server.post('/api/auth.signup', { + body: { + username: 'user1', + name: 'Test User', + email: 'userone@example.com', + password: 'test123!', + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(400); + expect(body).toMatchSnapshot(); + }); }); -it('should require params', async () => { - const res = await server.post('/api/auth.signup', { - body: { - username: 'testuser', - }, +describe('#login', () => { + test('should login with email', async () => { + await seed(); + const res = await server.post('/api/auth.login', { + body: { + email: 'user1@example.com', + password: 'test123!', + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.ok).toBe(true); + expect(body.data.user).toMatchSnapshot(); }); - const body = await res.json(); - expect(res.status).toEqual(400); - expect(body).toMatchSnapshot(); -}); + test('should login with username', async () => { + await seed(); + const res = await server.post('/api/auth.login', { + body: { + username: 'user1', + password: 'test123!', + }, + }); + const body = await res.json(); - -it('should require valid email', async () => { - const res = await server.post('/api/auth.signup', { - body: { - username: 'testuser', - name: 'Test User', - email: 'example.com', - password: 'test123!', - }, + expect(res.status).toEqual(200); + expect(body.ok).toBe(true); + expect(body.data.user).toMatchSnapshot(); }); - const body = await res.json(); - expect(res.status).toEqual(400); - expect(body).toMatchSnapshot(); -}); + test('should validate password', async () => { + await seed(); + const res = await server.post('/api/auth.login', { + body: { + email: 'user1@example.com', + password: 'bad_password', + }, + }); + const body = await res.json(); -it('should require unique email', async () => { - await seed(); - const res = await server.post('/api/auth.signup', { - body: { - username: 'testuser', - name: 'Test User', - email: 'user1@example.com', - password: 'test123!', - }, + expect(res.status).toEqual(400); + expect(body).toMatchSnapshot(); }); - const body = await res.json(); - expect(res.status).toEqual(400); - expect(body).toMatchSnapshot(); -}); + test('should require either username or email', async () => { + const res = await server.post('/api/auth.login', { + body: { + password: 'test123!', + }, + }); + const body = await res.json(); -it('should require unique username', async () => { - await seed(); - const res = await server.post('/api/auth.signup', { - body: { - username: 'user1', - name: 'Test User', - email: 'userone@example.com', - password: 'test123!', - }, + expect(res.status).toEqual(400); + expect(body).toMatchSnapshot(); }); - const body = await res.json(); - expect(res.status).toEqual(400); - expect(body).toMatchSnapshot(); + test('should require password', async () => { + await seed(); + const res = await server.post('/api/auth.login', { + body: { + email: 'user1@example.com', + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(400); + expect(body).toMatchSnapshot(); + }); }); diff --git a/server/test/support.js b/server/test/support.js index 4a379484c4..c1646d62b1 100644 --- a/server/test/support.js +++ b/server/test/support.js @@ -16,6 +16,7 @@ const seed = async () => { email: 'user1@example.com', username: 'user1', name: 'User 1', + password: 'test123!', slackId: '123', slackData: { image_192: 'http://example.com/avatar.png',