2020-07-15 06:46:32 -04:00
|
|
|
const Path = require('path')
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
|
|
|
|
const MODULE_PATH = Path.join(
|
|
|
|
__dirname,
|
|
|
|
'../../../app/src/UserActivateController.js'
|
|
|
|
)
|
|
|
|
const VIEW_PATH = Path.join(__dirname, '../../../app/views/user/activate')
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('UserActivateController', function () {
|
|
|
|
beforeEach(function () {
|
2020-07-15 06:46:32 -04:00
|
|
|
this.user = {
|
|
|
|
_id: (this.user_id = 'kwjewkl'),
|
|
|
|
features: {},
|
2021-04-27 03:52:58 -04:00
|
|
|
email: 'joe@example.com',
|
2020-07-15 06:46:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.UserGetter = { getUser: sinon.stub() }
|
|
|
|
this.ErrorController = { notFound: sinon.stub() }
|
|
|
|
this.UserActivateController = SandboxedModule.require(MODULE_PATH, {
|
|
|
|
requires: {
|
|
|
|
'../../../../app/src/Features/User/UserGetter': this.UserGetter,
|
|
|
|
'../../../../app/src/Features/Errors/ErrorController': this
|
2021-04-27 03:52:58 -04:00
|
|
|
.ErrorController,
|
|
|
|
},
|
2020-07-15 06:46:32 -04:00
|
|
|
})
|
|
|
|
this.req = {
|
|
|
|
query: {},
|
|
|
|
session: {
|
2021-04-27 03:52:58 -04:00
|
|
|
user: this.user,
|
|
|
|
},
|
2020-07-15 06:46:32 -04:00
|
|
|
}
|
|
|
|
this.res = {}
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('activateAccountPage', function () {
|
|
|
|
beforeEach(function () {
|
2020-07-15 06:46:32 -04:00
|
|
|
this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
|
|
|
|
this.req.query.user_id = this.user_id
|
|
|
|
this.req.query.token = this.token = 'mock-token-123'
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should 404 without a user_id', function (done) {
|
2020-07-15 06:46:32 -04:00
|
|
|
delete this.req.query.user_id
|
|
|
|
this.ErrorController.notFound = () => done()
|
|
|
|
this.UserActivateController.activateAccountPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should 404 without a token', function (done) {
|
2020-07-15 06:46:32 -04:00
|
|
|
delete this.req.query.token
|
|
|
|
this.ErrorController.notFound = () => done()
|
|
|
|
this.UserActivateController.activateAccountPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should 404 without a valid user_id', function (done) {
|
2020-07-15 06:46:32 -04:00
|
|
|
this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
|
|
|
|
this.ErrorController.notFound = () => done()
|
|
|
|
this.UserActivateController.activateAccountPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should 403 for complex user_id', function (done) {
|
2020-07-15 06:46:32 -04:00
|
|
|
this.ErrorController.forbidden = () => done()
|
|
|
|
this.req.query.user_id = { first_name: 'X' }
|
|
|
|
this.UserActivateController.activateAccountPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should redirect activated users to login', function (done) {
|
2020-07-15 06:46:32 -04:00
|
|
|
this.user.loginCount = 1
|
|
|
|
this.res.redirect = url => {
|
|
|
|
this.UserGetter.getUser.calledWith(this.user_id).should.equal(true)
|
|
|
|
url.should.equal('/login')
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
this.UserActivateController.activateAccountPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('render the activation page if the user has not logged in before', function (done) {
|
2020-07-15 06:46:32 -04:00
|
|
|
this.user.loginCount = 0
|
|
|
|
this.res.render = (page, opts) => {
|
|
|
|
page.should.equal(VIEW_PATH)
|
|
|
|
opts.email.should.equal(this.user.email)
|
|
|
|
opts.token.should.equal(this.token)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
this.UserActivateController.activateAccountPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|