| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- /* eslint-disable
- max-len,
- no-return-assign,
- no-unused-vars,
- */
- // 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 sinon = require('sinon')
- const assertCalledWith = sinon.assert.calledWith
- const assertNotCalled = sinon.assert.notCalled
- const chai = require('chai')
- const should = chai.should()
- const { assert } = chai
- const { expect } = require('chai')
- const modulePath =
- '../../../../app/src/Features/UserMembership/UserMembershipController.js'
- const SandboxedModule = require('sandboxed-module')
- const MockRequest = require('../helpers/MockRequest')
- const MockResponse = require('../helpers/MockResponse')
- const EntityConfigs = require('../../../../app/src/Features/UserMembership/UserMembershipEntityConfigs')
- const Errors = require('../../../../app/src/Features/Errors/Errors')
- describe('UserMembershipController', function() {
- beforeEach(function() {
- this.req = new MockRequest()
- this.req.params.id = 'mock-entity-id'
- this.user = { _id: 'mock-user-id' }
- this.newUser = { _id: 'mock-new-user-id', email: 'new-user-email@foo.bar' }
- this.subscription = {
- _id: 'mock-subscription-id',
- fetchV1Data: callback => callback(null, this.subscription)
- }
- this.institution = {
- _id: 'mock-institution-id',
- v1Id: 123,
- fetchV1Data: callback => {
- const institution = Object.assign({}, this.institution)
- institution.name = 'Test Institution Name'
- return callback(null, institution)
- }
- }
- this.users = [
- {
- _id: 'mock-member-id-1',
- email: 'mock-email-1@foo.com',
- last_logged_in_at: '2020-08-09T12:43:11.467Z'
- },
- {
- _id: 'mock-member-id-2',
- email: 'mock-email-2@foo.com',
- last_logged_in_at: '2020-05-20T10:41:11.407Z'
- }
- ]
- this.AuthenticationController = {
- getSessionUser: sinon.stub().returns(this.user),
- getLoggedInUserId: sinon.stub().returns(this.user._id)
- }
- this.UserMembershipHandler = {
- getEntity: sinon.stub().yields(null, this.subscription),
- createEntity: sinon.stub().yields(null, this.institution),
- getUsers: sinon.stub().yields(null, this.users),
- addUser: sinon.stub().yields(null, this.newUser),
- removeUser: sinon.stub().yields(null)
- }
- return (this.UserMembershipController = SandboxedModule.require(
- modulePath,
- {
- globals: {
- console: console
- },
- requires: {
- '../Authentication/AuthenticationController': this
- .AuthenticationController,
- './UserMembershipHandler': this.UserMembershipHandler,
- 'logger-sharelatex': {
- log() {},
- err() {}
- }
- }
- }
- ))
- })
- describe('index', function() {
- beforeEach(function() {
- this.req.entity = this.subscription
- return (this.req.entityConfig = EntityConfigs.group)
- })
- it('get users', function(done) {
- return this.UserMembershipController.index(this.req, {
- render: () => {
- sinon.assert.calledWithMatch(
- this.UserMembershipHandler.getUsers,
- this.subscription,
- { modelName: 'Subscription' }
- )
- return done()
- }
- })
- })
- it('render group view', function(done) {
- return this.UserMembershipController.index(this.req, {
- render: (viewPath, viewParams) => {
- expect(viewPath).to.equal('user_membership/index')
- expect(viewParams.users).to.deep.equal(this.users)
- expect(viewParams.groupSize).to.equal(this.subscription.membersLimit)
- expect(viewParams.translations.title).to.equal('group_account')
- expect(viewParams.paths.addMember).to.equal(
- `/manage/groups/${this.subscription._id}/invites`
- )
- return done()
- }
- })
- })
- it('render group managers view', function(done) {
- this.req.entityConfig = EntityConfigs.groupManagers
- return this.UserMembershipController.index(this.req, {
- render: (viewPath, viewParams) => {
- expect(viewPath).to.equal('user_membership/index')
- expect(viewParams.groupSize).to.equal(undefined)
- expect(viewParams.translations.title).to.equal('group_account')
- expect(viewParams.translations.subtitle).to.equal(
- 'managers_management'
- )
- expect(viewParams.paths.exportMembers).to.be.undefined
- return done()
- }
- })
- })
- it('render institution view', function(done) {
- this.req.entity = this.institution
- this.req.entityConfig = EntityConfigs.institution
- return this.UserMembershipController.index(this.req, {
- render: (viewPath, viewParams) => {
- expect(viewPath).to.equal('user_membership/index')
- expect(viewParams.name).to.equal('Test Institution Name')
- expect(viewParams.groupSize).to.equal(undefined)
- expect(viewParams.translations.title).to.equal('institution_account')
- expect(viewParams.paths.exportMembers).to.be.undefined
- return done()
- }
- })
- })
- })
- describe('add', function() {
- beforeEach(function() {
- this.req.body.email = this.newUser.email
- this.req.entity = this.subscription
- return (this.req.entityConfig = EntityConfigs.groupManagers)
- })
- it('add user', function(done) {
- return this.UserMembershipController.add(this.req, {
- json: () => {
- sinon.assert.calledWithMatch(
- this.UserMembershipHandler.addUser,
- this.subscription,
- { modelName: 'Subscription' },
- this.newUser.email
- )
- return done()
- }
- })
- })
- it('return user object', function(done) {
- return this.UserMembershipController.add(this.req, {
- json: payload => {
- payload.user.should.equal(this.newUser)
- return done()
- }
- })
- })
- it('handle readOnly entity', function(done) {
- this.req.entityConfig = EntityConfigs.group
- return this.UserMembershipController.add(this.req, null, error => {
- expect(error).to.exist
- expect(error).to.be.an.instanceof(Errors.NotFoundError)
- return done()
- })
- })
- it('handle user already added', function(done) {
- this.UserMembershipHandler.addUser.yields({ alreadyAdded: true })
- return this.UserMembershipController.add(this.req, {
- status: () => ({
- json: payload => {
- expect(payload.error.code).to.equal('user_already_added')
- return done()
- }
- })
- })
- })
- it('handle user not found', function(done) {
- this.UserMembershipHandler.addUser.yields({ userNotFound: true })
- return this.UserMembershipController.add(this.req, {
- status: () => ({
- json: payload => {
- expect(payload.error.code).to.equal('user_not_found')
- return done()
- }
- })
- })
- })
- it('handle invalid email', function(done) {
- this.req.body.email = 'not_valid_email'
- return this.UserMembershipController.add(this.req, {
- status: () => ({
- json: payload => {
- expect(payload.error.code).to.equal('invalid_email')
- return done()
- }
- })
- })
- })
- })
- describe('remove', function() {
- beforeEach(function() {
- this.req.params.userId = this.newUser._id
- this.req.entity = this.subscription
- return (this.req.entityConfig = EntityConfigs.groupManagers)
- })
- it('remove user', function(done) {
- return this.UserMembershipController.remove(this.req, {
- sendStatus: () => {
- sinon.assert.calledWithMatch(
- this.UserMembershipHandler.removeUser,
- this.subscription,
- { modelName: 'Subscription' },
- this.newUser._id
- )
- return done()
- }
- })
- })
- it('handle readOnly entity', function(done) {
- this.req.entityConfig = EntityConfigs.group
- return this.UserMembershipController.remove(this.req, null, error => {
- expect(error).to.exist
- expect(error).to.be.an.instanceof(Errors.NotFoundError)
- return done()
- })
- })
- it('prevent self removal', function(done) {
- this.req.params.userId = this.user._id
- return this.UserMembershipController.remove(this.req, {
- status: () => ({
- json: payload => {
- expect(payload.error.code).to.equal('managers_cannot_remove_self')
- return done()
- }
- })
- })
- })
- it('prevent admin removal', function(done) {
- this.UserMembershipHandler.removeUser.yields({ isAdmin: true })
- return this.UserMembershipController.remove(this.req, {
- status: () => ({
- json: payload => {
- expect(payload.error.code).to.equal('managers_cannot_remove_admin')
- return done()
- }
- })
- })
- })
- })
- describe('exportCsv', function() {
- beforeEach(function() {
- this.req.entity = this.subscription
- this.req.entityConfig = EntityConfigs.groupManagers
- this.res = new MockResponse()
- this.res.contentType = sinon.stub()
- this.res.header = sinon.stub()
- this.res.send = sinon.stub()
- return this.UserMembershipController.exportCsv(this.req, this.res)
- })
- it('get users', function() {
- return sinon.assert.calledWithMatch(
- this.UserMembershipHandler.getUsers,
- this.subscription,
- { modelName: 'Subscription' }
- )
- })
- it('should set the correct content type on the request', function() {
- return assertCalledWith(this.res.contentType, 'text/csv')
- })
- it('should name the exported csv file', function() {
- return assertCalledWith(
- this.res.header,
- 'Content-Disposition',
- 'attachment; filename=Group.csv'
- )
- })
- it('should export the correct csv', function() {
- return assertCalledWith(
- this.res.send,
- '"email","last_logged_in_at"\n"mock-email-1@foo.com","2020-08-09T12:43:11.467Z"\n"mock-email-2@foo.com","2020-05-20T10:41:11.407Z"'
- )
- })
- })
- describe('new', function() {
- beforeEach(function() {
- this.req.params.name = 'publisher'
- return (this.req.params.id = 'abc')
- })
- it('renders view', function(done) {
- return this.UserMembershipController.new(this.req, {
- render: (viewPath, data) => {
- expect(data.entityName).to.eq('publisher')
- expect(data.entityId).to.eq('abc')
- return done()
- }
- })
- })
- })
- describe('create', function() {
- beforeEach(function() {
- this.req.params.name = 'institution'
- this.req.entityConfig = EntityConfigs.institution
- return (this.req.params.id = 123)
- })
- it('creates institution', function(done) {
- return this.UserMembershipController.create(this.req, {
- redirect: path => {
- expect(path).to.eq(EntityConfigs.institution.pathsFor(123).index)
- sinon.assert.calledWithMatch(
- this.UserMembershipHandler.createEntity,
- 123,
- { modelName: 'Institution' }
- )
- return done()
- }
- })
- })
- })
- })
|