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 success tests for register endpoint
- Loading branch information
Showing
11 changed files
with
151 additions
and
47 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
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,52 @@ | ||
test('default test', () => { | ||
expect(1 + 2).toBe(3); | ||
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'); | ||
|
||
setupDatabase(); | ||
|
||
describe('Auth route', () => { | ||
describe('POST /v1/auth/register', () => { | ||
test('should successfully register user and return 201, if request data is ok', async () => { | ||
const newUser = { | ||
name: faker.name.findName(), | ||
email: faker.internet.email().toLowerCase(), | ||
password: 'password1', | ||
}; | ||
|
||
const res = await request(app) | ||
.post('/v1/auth/register') | ||
.send(newUser) | ||
.expect(httpStatus.CREATED); | ||
|
||
const resUser = res.body.user; | ||
expect(resUser).not.toHaveProperty('password'); | ||
expect(resUser).toEqual({ id: expect.anything(), name: newUser.name, email: newUser.email, role: 'user' }); | ||
|
||
const dbUser = await User.findById(resUser.id); | ||
expect(dbUser).toBeDefined(); | ||
expect(dbUser.password).not.toBe(newUser.password); | ||
expect(dbUser).toMatchObject({ name: newUser.name, email: newUser.email, role: 'user' }); | ||
}); | ||
|
||
test('should return access and refresh tokens, if request data is ok', async () => { | ||
const newUser = { | ||
name: faker.name.findName(), | ||
email: faker.internet.email().toLowerCase(), | ||
password: 'password1', | ||
}; | ||
|
||
const res = await request(app) | ||
.post('/v1/auth/register') | ||
.send(newUser) | ||
.expect(httpStatus.CREATED); | ||
|
||
expect(res.body.tokens).toEqual({ | ||
access: { token: expect.anything(), expires: expect.anything() }, | ||
refresh: { token: expect.anything(), expires: expect.anything() }, | ||
}); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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,22 @@ | ||
const mongoose = require('mongoose'); | ||
const config = require('../../src/config/config'); | ||
|
||
const clearDatabase = async () => { | ||
await Promise.all(Object.values(mongoose.connection.collections).map(async collection => collection.deleteMany())); | ||
}; | ||
|
||
const setupDatabase = () => { | ||
beforeAll(async () => { | ||
await mongoose.connect(config.mongoose.url, config.mongoose.options); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await clearDatabase(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await mongoose.disconnect(); | ||
}); | ||
}; | ||
|
||
module.exports = setupDatabase; |
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