Added signup API endpoint
This commit is contained in:
27
server/api/__snapshots__/auth.test.js.snap
Normal file
27
server/api/__snapshots__/auth.test.js.snap
Normal file
@@ -0,0 +1,27 @@
|
||||
exports[`test should require params 1`] = `
|
||||
Object {
|
||||
"error": "name is required",
|
||||
"ok": false
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`test should require unique email 1`] = `
|
||||
Object {
|
||||
"error": "User already exists with this email",
|
||||
"ok": false
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`test should require unique username 1`] = `
|
||||
Object {
|
||||
"error": "User already exists with this username",
|
||||
"ok": false
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`test should require valid email 1`] = `
|
||||
Object {
|
||||
"error": "email is invalid",
|
||||
"ok": false
|
||||
}
|
||||
`;
|
||||
@@ -8,6 +8,36 @@ import { User, Team } from '../models';
|
||||
|
||||
const router = new Router();
|
||||
|
||||
router.post('auth.signup', async (ctx) => {
|
||||
const { username, name, email, password } = ctx.request.body;
|
||||
|
||||
ctx.assertPresent(username, 'name is required');
|
||||
ctx.assertPresent(name, 'name is required');
|
||||
ctx.assertPresent(email, 'email is required');
|
||||
ctx.assertEmail(email, 'email is invalid');
|
||||
ctx.assertPresent(password, 'password is required');
|
||||
|
||||
if (await User.findOne({ where: { email } })) {
|
||||
throw httpErrors.BadRequest('User already exists with this email');
|
||||
}
|
||||
|
||||
if (await User.findOne({ where: { username } })) {
|
||||
throw httpErrors.BadRequest('User already exists with this username');
|
||||
}
|
||||
|
||||
const user = await User.create({
|
||||
username,
|
||||
name,
|
||||
email,
|
||||
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');
|
||||
|
||||
85
server/api/auth.test.js
Normal file
85
server/api/auth.test.js
Normal file
@@ -0,0 +1,85 @@
|
||||
import TestServer from 'fetch-test-server';
|
||||
import app from '..';
|
||||
import { flushdb, sequelize, seed } from '../test/support';
|
||||
|
||||
const server = new TestServer(app.callback());
|
||||
|
||||
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();
|
||||
|
||||
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();
|
||||
});
|
||||
Reference in New Issue
Block a user