2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
2020-12-15 05:23:54 -05:00
|
|
|
node/handle-callback-err,
|
2019-05-29 05:21:06 -04:00
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
const { expect } = require('chai')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const assertCalledWith = sinon.assert.calledWith
|
|
|
|
const assertNotCalled = sinon.assert.notCalled
|
2020-09-23 04:49:26 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2019-05-29 05:21:06 -04:00
|
|
|
const modulePath =
|
|
|
|
'../../../../app/src/Features/UserMembership/UserMembershipViewModel'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2020-10-06 06:31:34 -04:00
|
|
|
const {
|
|
|
|
isObjectIdInstance,
|
|
|
|
normalizeQuery
|
|
|
|
} = require('../../../../app/src/Features/Helpers/Mongo')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('UserMembershipViewModel', function () {
|
|
|
|
beforeEach(function () {
|
2019-10-01 07:27:15 -04:00
|
|
|
this.UserGetter = { getUser: sinon.stub() }
|
2019-05-29 05:21:06 -04:00
|
|
|
this.UserMembershipViewModel = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2020-10-05 04:23:21 -04:00
|
|
|
mongodb: { ObjectId },
|
2020-10-06 06:31:34 -04:00
|
|
|
'../Helpers/Mongo': { isObjectIdInstance, normalizeQuery },
|
2019-05-29 05:21:06 -04:00
|
|
|
'../User/UserGetter': this.UserGetter
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.email = 'mock-email@bar.com'
|
|
|
|
this.user = {
|
|
|
|
_id: 'mock-user-id',
|
|
|
|
email: 'mock-email@baz.com',
|
2020-08-12 10:19:14 -04:00
|
|
|
first_name: 'Name',
|
|
|
|
lastLoggedIn: '2020-05-20T10:41:11.407Z'
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('build', function () {
|
|
|
|
it('build email', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
const viewModel = this.UserMembershipViewModel.build(this.email)
|
|
|
|
return expect(viewModel).to.deep.equal({
|
|
|
|
email: this.email,
|
|
|
|
invite: true,
|
2020-08-12 10:19:14 -04:00
|
|
|
last_logged_in_at: null,
|
2019-05-29 05:21:06 -04:00
|
|
|
first_name: null,
|
|
|
|
last_name: null,
|
|
|
|
_id: null
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('build user', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
const viewModel = this.UserMembershipViewModel.build(this.user)
|
|
|
|
expect(viewModel._id).to.equal(this.user._id)
|
|
|
|
expect(viewModel.email).to.equal(this.user.email)
|
2020-08-12 10:19:14 -04:00
|
|
|
expect(viewModel.last_logged_in_at).to.equal(this.user.lastLoggedIn)
|
2019-05-29 05:21:06 -04:00
|
|
|
return expect(viewModel.invite).to.equal(false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('build async', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return (this.UserMembershipViewModel.build = sinon.stub())
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('build email', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserMembershipViewModel.buildAsync(
|
|
|
|
this.email,
|
|
|
|
(error, viewModel) => {
|
|
|
|
assertCalledWith(this.UserMembershipViewModel.build, this.email)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('build user', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserMembershipViewModel.buildAsync(
|
|
|
|
this.user,
|
|
|
|
(error, viewModel) => {
|
|
|
|
assertCalledWith(this.UserMembershipViewModel.build, this.user)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('build user id', function (done) {
|
2019-10-01 07:27:15 -04:00
|
|
|
this.UserGetter.getUser.yields(null, this.user)
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserMembershipViewModel.buildAsync(
|
|
|
|
ObjectId(),
|
|
|
|
(error, viewModel) => {
|
2021-03-31 08:20:55 -04:00
|
|
|
expect(error).not.to.exist
|
2019-05-29 05:21:06 -04:00
|
|
|
assertNotCalled(this.UserMembershipViewModel.build)
|
|
|
|
expect(viewModel._id).to.equal(this.user._id)
|
|
|
|
expect(viewModel.email).to.equal(this.user.email)
|
|
|
|
expect(viewModel.first_name).to.equal(this.user.first_name)
|
|
|
|
expect(viewModel.invite).to.equal(false)
|
2021-03-31 08:20:55 -04:00
|
|
|
expect(viewModel.email).to.exist
|
2019-05-29 05:21:06 -04:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('build user id with error', function (done) {
|
2019-10-01 07:27:15 -04:00
|
|
|
this.UserGetter.getUser.yields(new Error('nope'))
|
2019-05-29 05:21:06 -04:00
|
|
|
const userId = ObjectId()
|
|
|
|
return this.UserMembershipViewModel.buildAsync(
|
|
|
|
userId,
|
|
|
|
(error, viewModel) => {
|
2021-03-31 08:20:55 -04:00
|
|
|
expect(error).not.to.exist
|
2019-05-29 05:21:06 -04:00
|
|
|
assertNotCalled(this.UserMembershipViewModel.build)
|
|
|
|
expect(viewModel._id).to.equal(userId.toString())
|
2021-03-31 08:20:55 -04:00
|
|
|
expect(viewModel.email).not.to.exist
|
2019-05-29 05:21:06 -04:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|