forked from hagopj13/node-express-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add create user test and refactor other tests
- Loading branch information
Showing
3 changed files
with
151 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
const request = require('supertest'); | ||
const faker = require('faker'); | ||
const httpStatus = require('http-status'); | ||
const app = require('../../src/app'); | ||
const setupDatabase = require('../utils/setupDatabase'); | ||
const { User } = require('../../src/models'); | ||
const { userOne, admin, insertUsers } = require('../fixtures/user.fixture'); | ||
const { userOneAccessToken, adminAccessToken } = require('../fixtures/token.fixture'); | ||
|
||
setupDatabase(); | ||
|
||
describe('User route', () => { | ||
describe('POST /v1/users', () => { | ||
let newUser; | ||
|
||
beforeEach(() => { | ||
newUser = { | ||
name: faker.name.findName(), | ||
email: faker.internet.email().toLowerCase(), | ||
password: 'password1', | ||
role: 'user', | ||
}; | ||
}); | ||
|
||
test('should return 201 and successfully create new user if data is ok', async () => { | ||
await insertUsers([admin]); | ||
|
||
const res = await request(app) | ||
.post('/v1/users') | ||
.set('Authorization', `Bearer ${adminAccessToken}`) | ||
.send(newUser) | ||
.expect(httpStatus.CREATED); | ||
|
||
expect(res.body).not.toHaveProperty('password'); | ||
expect(res.body).toEqual({ id: expect.anything(), name: newUser.name, email: newUser.email, role: newUser.role }); | ||
|
||
const dbUser = await User.findById(res.body.id); | ||
expect(dbUser).toBeDefined(); | ||
expect(dbUser.password).not.toBe(newUser.password); | ||
expect(dbUser).toMatchObject({ name: newUser.name, email: newUser.email, role: newUser.role }); | ||
}); | ||
|
||
test('should be able to create an admin as well', async () => { | ||
await insertUsers([admin]); | ||
newUser.role = 'admin'; | ||
|
||
const res = await request(app) | ||
.post('/v1/users') | ||
.set('Authorization', `Bearer ${adminAccessToken}`) | ||
.send(newUser) | ||
.expect(httpStatus.CREATED); | ||
|
||
expect(res.body.role).toBe('admin'); | ||
|
||
const dbUser = await User.findById(res.body.id); | ||
expect(dbUser.role).toBe('admin'); | ||
}); | ||
|
||
test('should return 401 error is access token is missing', async () => { | ||
await request(app) | ||
.post('/v1/users') | ||
.send(newUser) | ||
.expect(httpStatus.UNAUTHORIZED); | ||
}); | ||
|
||
test('should return 403 error if logged in user is not admin', async () => { | ||
await insertUsers([userOne]); | ||
|
||
await request(app) | ||
.post('/v1/users') | ||
.set('Authorization', `Bearer ${userOneAccessToken}`) | ||
.send(newUser) | ||
.expect(httpStatus.FORBIDDEN); | ||
}); | ||
|
||
test('should return 400 error if email is invalid', async () => { | ||
await insertUsers([admin]); | ||
newUser.email = 'invalidEmail'; | ||
|
||
await request(app) | ||
.post('/v1/users') | ||
.set('Authorization', `Bearer ${adminAccessToken}`) | ||
.send(newUser) | ||
.expect(httpStatus.BAD_REQUEST); | ||
}); | ||
|
||
test('should return 400 error if email is already used', async () => { | ||
await insertUsers([admin, userOne]); | ||
newUser.email = userOne.email; | ||
|
||
await request(app) | ||
.post('/v1/users') | ||
.set('Authorization', `Bearer ${adminAccessToken}`) | ||
.send(newUser) | ||
.expect(httpStatus.BAD_REQUEST); | ||
}); | ||
|
||
test('should return 400 error if password is less than 8 characters', async () => { | ||
await insertUsers([admin]); | ||
newUser.password = 'passwo1'; | ||
|
||
await request(app) | ||
.post('/v1/users') | ||
.set('Authorization', `Bearer ${adminAccessToken}`) | ||
.send(newUser) | ||
.expect(httpStatus.BAD_REQUEST); | ||
}); | ||
|
||
test('should return 400 error if password does not contain both letters and numbers', async () => { | ||
await insertUsers([admin]); | ||
newUser.password = 'password'; | ||
|
||
await request(app) | ||
.post('/v1/users') | ||
.set('Authorization', `Bearer ${adminAccessToken}`) | ||
.send(newUser) | ||
.expect(httpStatus.BAD_REQUEST); | ||
|
||
newUser.password = '1111111'; | ||
|
||
await request(app) | ||
.post('/v1/users') | ||
.set('Authorization', `Bearer ${adminAccessToken}`) | ||
.send(newUser) | ||
.expect(httpStatus.BAD_REQUEST); | ||
}); | ||
|
||
test('should return 400 error if role is neither user nor admin', async () => { | ||
await insertUsers([admin]); | ||
newUser.role = 'invalid'; | ||
|
||
await request(app) | ||
.post('/v1/users') | ||
.set('Authorization', `Bearer ${adminAccessToken}`) | ||
.send(newUser) | ||
.expect(httpStatus.BAD_REQUEST); | ||
}); | ||
}); | ||
}); |