2019-09-27 08:31:44 -04:00
|
|
|
const AuthenticationManager = require('../../../app/src/Features/Authentication/AuthenticationManager')
|
|
|
|
const UserHelper = require('./helpers/UserHelper')
|
2019-11-26 08:11:49 -05:00
|
|
|
const Features = require('../../../app/src/infrastructure/Features')
|
2021-03-31 08:20:55 -04:00
|
|
|
const { expect } = require('chai')
|
2019-09-27 08:31:44 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('UserHelper', function () {
|
2019-12-16 11:35:08 -05:00
|
|
|
// Disable all tests unless the public-registration feature is enabled
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(function () {
|
2019-12-16 11:35:08 -05:00
|
|
|
if (!Features.hasFeature('public-registration')) {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('UserHelper.createUser', function () {
|
|
|
|
describe('with no args', function () {
|
|
|
|
it('should create new user with default username and password', async function () {
|
2019-09-27 08:31:44 -04:00
|
|
|
const userHelper = await UserHelper.createUser()
|
|
|
|
userHelper.user.email.should.equal(userHelper.getDefaultEmail())
|
|
|
|
const authedUser = await AuthenticationManager.promises.authenticate(
|
|
|
|
{ _id: userHelper.user._id },
|
|
|
|
userHelper.getDefaultPassword()
|
|
|
|
)
|
|
|
|
expect(authedUser).to.not.be.null
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('with email', function () {
|
|
|
|
it('should create new user with provided email and default password', async function () {
|
2019-09-27 08:31:44 -04:00
|
|
|
const userHelper = await UserHelper.createUser({
|
|
|
|
email: 'foo@test.com'
|
|
|
|
})
|
|
|
|
userHelper.user.email.should.equal('foo@test.com')
|
|
|
|
const authedUser = await AuthenticationManager.promises.authenticate(
|
|
|
|
{ _id: userHelper.user._id },
|
|
|
|
userHelper.getDefaultPassword()
|
|
|
|
)
|
|
|
|
expect(authedUser).to.not.be.null
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('with password', function () {
|
|
|
|
it('should create new user with provided password and default email', async function () {
|
2019-09-27 08:31:44 -04:00
|
|
|
const userHelper = await UserHelper.createUser({
|
|
|
|
password: 'foofoofoo'
|
|
|
|
})
|
|
|
|
userHelper.user.email.should.equal(userHelper.getDefaultEmail())
|
|
|
|
const authedUser = await AuthenticationManager.promises.authenticate(
|
|
|
|
{ _id: userHelper.user._id },
|
|
|
|
'foofoofoo'
|
|
|
|
)
|
|
|
|
expect(authedUser).to.not.be.null
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('UserHelper.getUser', function () {
|
2019-09-27 08:31:44 -04:00
|
|
|
let user
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(async function () {
|
2019-09-27 08:31:44 -04:00
|
|
|
user = (await UserHelper.createUser()).user
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('with string id', function () {
|
|
|
|
it('should fetch user', async function () {
|
2019-09-27 08:31:44 -04:00
|
|
|
const userHelper = await UserHelper.getUser(user._id.toString())
|
|
|
|
userHelper.user.email.should.equal(user.email)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('with _id', function () {
|
|
|
|
it('should fetch user', async function () {
|
2019-09-27 08:31:44 -04:00
|
|
|
const userHelper = await UserHelper.getUser({ _id: user._id })
|
|
|
|
userHelper.user.email.should.equal(user.email)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('UserHelper.loginUser', function () {
|
2019-12-16 11:35:08 -05:00
|
|
|
let userHelper
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(async function () {
|
2019-12-16 11:35:08 -05:00
|
|
|
userHelper = await UserHelper.createUser()
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('with email and password', function () {
|
|
|
|
it('should login user', async function () {
|
2019-12-16 11:35:08 -05:00
|
|
|
const newUserHelper = await UserHelper.loginUser({
|
|
|
|
email: userHelper.getDefaultEmail(),
|
|
|
|
password: userHelper.getDefaultPassword()
|
|
|
|
})
|
|
|
|
newUserHelper.user.email.should.equal(userHelper.user.email)
|
2019-09-27 08:31:44 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('without email', function () {
|
|
|
|
it('should throw error', async function () {
|
2019-12-16 11:35:08 -05:00
|
|
|
await UserHelper.loginUser({
|
|
|
|
password: userHelper.getDefaultPassword()
|
|
|
|
}).should.be.rejectedWith('email and password required')
|
2019-09-27 08:31:44 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('without password', function () {
|
|
|
|
it('should throw error', async function () {
|
2019-12-16 11:35:08 -05:00
|
|
|
await UserHelper.loginUser({
|
|
|
|
email: userHelper.getDefaultEmail()
|
|
|
|
}).should.be.rejectedWith('email and password required')
|
2019-11-26 08:11:49 -05:00
|
|
|
})
|
2019-12-16 11:35:08 -05:00
|
|
|
})
|
2019-11-26 08:11:49 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('without email and password', function () {
|
|
|
|
it('should throw error', async function () {
|
2019-12-16 11:35:08 -05:00
|
|
|
await UserHelper.loginUser().should.be.rejectedWith(
|
|
|
|
'email and password required'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('UserHelper.registerUser', function () {
|
|
|
|
describe('with no args', function () {
|
|
|
|
it('should create new user with default username and password', async function () {
|
2019-09-27 08:31:44 -04:00
|
|
|
const userHelper = await UserHelper.registerUser()
|
|
|
|
userHelper.user.email.should.equal(userHelper.getDefaultEmail())
|
|
|
|
const authedUser = await AuthenticationManager.promises.authenticate(
|
|
|
|
{ _id: userHelper.user._id },
|
|
|
|
userHelper.getDefaultPassword()
|
|
|
|
)
|
|
|
|
expect(authedUser).to.not.be.null
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('with email', function () {
|
|
|
|
it('should create new user with provided email and default password', async function () {
|
2019-09-27 08:31:44 -04:00
|
|
|
const userHelper = await UserHelper.registerUser({
|
|
|
|
email: 'foo2@test.com'
|
|
|
|
})
|
|
|
|
userHelper.user.email.should.equal('foo2@test.com')
|
|
|
|
const authedUser = await AuthenticationManager.promises.authenticate(
|
|
|
|
{ _id: userHelper.user._id },
|
|
|
|
userHelper.getDefaultPassword()
|
|
|
|
)
|
|
|
|
expect(authedUser).to.not.be.null
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('with password', function () {
|
|
|
|
it('should create new user with provided password and default email', async function () {
|
2019-09-27 08:31:44 -04:00
|
|
|
const userHelper = await UserHelper.registerUser({
|
|
|
|
password: 'foofoofoo'
|
|
|
|
})
|
|
|
|
userHelper.user.email.should.equal(userHelper.getDefaultEmail())
|
|
|
|
const authedUser = await AuthenticationManager.promises.authenticate(
|
|
|
|
{ _id: userHelper.user._id },
|
|
|
|
'foofoofoo'
|
|
|
|
)
|
|
|
|
expect(authedUser).to.not.be.null
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('getCsrfToken', function () {
|
|
|
|
it('should fetch csrfToken', async function () {
|
2021-04-09 04:44:26 -04:00
|
|
|
const userHelper = new UserHelper()
|
|
|
|
await userHelper.getCsrfToken()
|
|
|
|
expect(userHelper.csrfToken).to.be.a.string
|
2019-12-16 11:35:08 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('after logout', function () {
|
2019-09-27 08:31:44 -04:00
|
|
|
let userHelper, oldCsrfToken
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(async function () {
|
2019-09-27 08:31:44 -04:00
|
|
|
userHelper = await UserHelper.registerUser()
|
|
|
|
oldCsrfToken = userHelper.csrfToken
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('refreshes csrf token after logout', async function () {
|
2019-09-27 08:31:44 -04:00
|
|
|
await userHelper.logout()
|
|
|
|
expect(userHelper._csrfToken).to.equal('')
|
|
|
|
await userHelper.getCsrfToken()
|
|
|
|
expect(userHelper._csrfToken).to.not.equal('')
|
|
|
|
expect(userHelper._csrfToken).to.not.equal(oldCsrfToken)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|