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.
Connect to test db and prepare test fixtures
- Loading branch information
Showing
5 changed files
with
80 additions
and
1 deletion.
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
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 |
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,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, | ||
}; |
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,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, | ||
}; |
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,8 @@ | ||
const { User, Token } = require('../../src/models'); | ||
|
||
const clearDatabase = async () => { | ||
await User.deleteMany(); | ||
await Token.deleteMany(); | ||
}; | ||
|
||
module.exports = clearDatabase; |