Skip to content

Commit

Permalink
Connect to test db and prepare test fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
hagopj13 committed Nov 1, 2019
1 parent aab7d9e commit e75f54a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
PORT=3000
MONGODB_URL=mongodb://127.0.0.1:27017/node-boilerplate
MONGODB_TEST_URL=mongodb://127.0.0.1:27017/node-boilerplate-test
JWT_SECRET=thisisasamplesecret
JWT_ACCESS_EXPIRATION_MINUTES=30
JWT_REFRESH_EXPIRATION_DAYS=30
2 changes: 1 addition & 1 deletion src/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dotenv.config({
module.exports = {
env: process.env.NODE_ENV,
port: process.env.PORT,
mongodbUrl: process.env.MONGODB_URL,
mongodbUrl: process.env.NODE_ENV === 'test' ? process.env.MONGODB_TEST_URL : process.env.MONGODB_URL,
jwt: {
secret: process.env.JWT_SECRET,
accessExpirationMinutes: parseInt(process.env.JWT_ACCESS_EXPIRATION_MINUTES, 10),
Expand Down
28 changes: 28 additions & 0 deletions tests/fixtures/token.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const moment = require('moment');
const config = require('../../src/config/config');
const Token = require('../../src/models/token.model');
const tokenService = require('../../src/services/token.service');
const { userOne, admin } = require('./user.fixture');

const accessTokenExpires = moment().add(config.jwt.accessExpirationMinutes, 'minutes');
const userOneAccessToken = tokenService.generateToken(userOne.id, accessTokenExpires);
const adminAccessToken = tokenService.generateToken(admin.id, accessTokenExpires);

const refreshTokenExpires = moment().add(config.jwt.refreshExpirationDays, 'days');
const userOneRefreshTokenDoc = {
token: tokenService.generateToken(userOne.id, refreshTokenExpires),
user: userOne._id,
type: 'refresh',
expires: refreshTokenExpires.toDate(),
};

const insertTokenDoc = async tokenDoc => {
await Token.create(tokenDoc);
};

module.exports = {
userOneAccessToken,
adminAccessToken,
userOneRefreshTokenDoc,
insertTokenDoc,
};
42 changes: 42 additions & 0 deletions tests/fixtures/user.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const User = require('../../src/models/user.model');

const password = 'password1';
const salt = bcrypt.genSaltSync(8);
const hashedPassword = bcrypt.hashSync(password, salt);

const userOne = {
_id: mongoose.Types.ObjectId(),
name: 'User One',
email: 'user1@example.com',
password,
role: 'user',
};

const userTwo = {
_id: mongoose.Types.ObjectId(),
name: 'User Two',
email: 'user2@example.com',
password,
role: 'user',
};

const admin = {
_id: mongoose.Types.ObjectId(),
name: 'Admin',
email: 'admin@example.com',
password,
role: 'admin',
};

const insertUsers = async users => {
await User.insertMany(users.map(user => ({ ...user, password: hashedPassword })));
};

module.exports = {
userOne,
userTwo,
admin,
insertUsers,
};
8 changes: 8 additions & 0 deletions tests/utils/clearDatabase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { User, Token } = require('../../src/models');

const clearDatabase = async () => {
await User.deleteMany();
await Token.deleteMany();
};

module.exports = clearDatabase;

0 comments on commit e75f54a

Please sign in to comment.