UserMembershipControllerTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon')
  14. const assertCalledWith = sinon.assert.calledWith
  15. const assertNotCalled = sinon.assert.notCalled
  16. const { assert, expect } = require('chai')
  17. const modulePath =
  18. '../../../../app/src/Features/UserMembership/UserMembershipController.js'
  19. const SandboxedModule = require('sandboxed-module')
  20. const MockRequest = require('../helpers/MockRequest')
  21. const MockResponse = require('../helpers/MockResponse')
  22. const EntityConfigs = require('../../../../app/src/Features/UserMembership/UserMembershipEntityConfigs')
  23. const Errors = require('../../../../app/src/Features/Errors/Errors')
  24. describe('UserMembershipController', function () {
  25. beforeEach(function () {
  26. this.req = new MockRequest()
  27. this.req.params.id = 'mock-entity-id'
  28. this.user = { _id: 'mock-user-id' }
  29. this.newUser = { _id: 'mock-new-user-id', email: 'new-user-email@foo.bar' }
  30. this.subscription = {
  31. _id: 'mock-subscription-id',
  32. fetchV1Data: callback => callback(null, this.subscription),
  33. }
  34. this.institution = {
  35. _id: 'mock-institution-id',
  36. v1Id: 123,
  37. fetchV1Data: callback => {
  38. const institution = Object.assign({}, this.institution)
  39. institution.name = 'Test Institution Name'
  40. return callback(null, institution)
  41. },
  42. }
  43. this.users = [
  44. {
  45. _id: 'mock-member-id-1',
  46. email: 'mock-email-1@foo.com',
  47. last_logged_in_at: '2020-08-09T12:43:11.467Z',
  48. },
  49. {
  50. _id: 'mock-member-id-2',
  51. email: 'mock-email-2@foo.com',
  52. last_logged_in_at: '2020-05-20T10:41:11.407Z',
  53. },
  54. ]
  55. this.AuthenticationController = {
  56. getSessionUser: sinon.stub().returns(this.user),
  57. getLoggedInUserId: sinon.stub().returns(this.user._id),
  58. }
  59. this.UserMembershipHandler = {
  60. getEntity: sinon.stub().yields(null, this.subscription),
  61. createEntity: sinon.stub().yields(null, this.institution),
  62. getUsers: sinon.stub().yields(null, this.users),
  63. addUser: sinon.stub().yields(null, this.newUser),
  64. removeUser: sinon.stub().yields(null),
  65. }
  66. return (this.UserMembershipController = SandboxedModule.require(
  67. modulePath,
  68. {
  69. requires: {
  70. '../Authentication/AuthenticationController': this
  71. .AuthenticationController,
  72. './UserMembershipHandler': this.UserMembershipHandler,
  73. },
  74. }
  75. ))
  76. })
  77. describe('index', function () {
  78. beforeEach(function () {
  79. this.req.entity = this.subscription
  80. return (this.req.entityConfig = EntityConfigs.group)
  81. })
  82. it('get users', function (done) {
  83. return this.UserMembershipController.index(this.req, {
  84. render: () => {
  85. sinon.assert.calledWithMatch(
  86. this.UserMembershipHandler.getUsers,
  87. this.subscription,
  88. { modelName: 'Subscription' }
  89. )
  90. return done()
  91. },
  92. })
  93. })
  94. it('render group view', function (done) {
  95. return this.UserMembershipController.index(this.req, {
  96. render: (viewPath, viewParams) => {
  97. expect(viewPath).to.equal('user_membership/index')
  98. expect(viewParams.users).to.deep.equal(this.users)
  99. expect(viewParams.groupSize).to.equal(this.subscription.membersLimit)
  100. expect(viewParams.translations.title).to.equal('group_account')
  101. expect(viewParams.paths.addMember).to.equal(
  102. `/manage/groups/${this.subscription._id}/invites`
  103. )
  104. return done()
  105. },
  106. })
  107. })
  108. it('render group managers view', function (done) {
  109. this.req.entityConfig = EntityConfigs.groupManagers
  110. return this.UserMembershipController.index(this.req, {
  111. render: (viewPath, viewParams) => {
  112. expect(viewPath).to.equal('user_membership/index')
  113. expect(viewParams.groupSize).to.equal(undefined)
  114. expect(viewParams.translations.title).to.equal('group_account')
  115. expect(viewParams.translations.subtitle).to.equal(
  116. 'managers_management'
  117. )
  118. expect(viewParams.paths.exportMembers).to.be.undefined
  119. return done()
  120. },
  121. })
  122. })
  123. it('render institution view', function (done) {
  124. this.req.entity = this.institution
  125. this.req.entityConfig = EntityConfigs.institution
  126. return this.UserMembershipController.index(this.req, {
  127. render: (viewPath, viewParams) => {
  128. expect(viewPath).to.equal('user_membership/index')
  129. expect(viewParams.name).to.equal('Test Institution Name')
  130. expect(viewParams.groupSize).to.equal(undefined)
  131. expect(viewParams.translations.title).to.equal('institution_account')
  132. expect(viewParams.paths.exportMembers).to.be.undefined
  133. return done()
  134. },
  135. })
  136. })
  137. })
  138. describe('add', function () {
  139. beforeEach(function () {
  140. this.req.body.email = this.newUser.email
  141. this.req.entity = this.subscription
  142. return (this.req.entityConfig = EntityConfigs.groupManagers)
  143. })
  144. it('add user', function (done) {
  145. return this.UserMembershipController.add(this.req, {
  146. json: () => {
  147. sinon.assert.calledWithMatch(
  148. this.UserMembershipHandler.addUser,
  149. this.subscription,
  150. { modelName: 'Subscription' },
  151. this.newUser.email
  152. )
  153. return done()
  154. },
  155. })
  156. })
  157. it('return user object', function (done) {
  158. return this.UserMembershipController.add(this.req, {
  159. json: payload => {
  160. payload.user.should.equal(this.newUser)
  161. return done()
  162. },
  163. })
  164. })
  165. it('handle readOnly entity', function (done) {
  166. this.req.entityConfig = EntityConfigs.group
  167. return this.UserMembershipController.add(this.req, null, error => {
  168. expect(error).to.exist
  169. expect(error).to.be.an.instanceof(Errors.NotFoundError)
  170. return done()
  171. })
  172. })
  173. it('handle user already added', function (done) {
  174. this.UserMembershipHandler.addUser.yields({ alreadyAdded: true })
  175. return this.UserMembershipController.add(this.req, {
  176. status: () => ({
  177. json: payload => {
  178. expect(payload.error.code).to.equal('user_already_added')
  179. return done()
  180. },
  181. }),
  182. })
  183. })
  184. it('handle user not found', function (done) {
  185. this.UserMembershipHandler.addUser.yields({ userNotFound: true })
  186. return this.UserMembershipController.add(this.req, {
  187. status: () => ({
  188. json: payload => {
  189. expect(payload.error.code).to.equal('user_not_found')
  190. return done()
  191. },
  192. }),
  193. })
  194. })
  195. it('handle invalid email', function (done) {
  196. this.req.body.email = 'not_valid_email'
  197. return this.UserMembershipController.add(this.req, {
  198. status: () => ({
  199. json: payload => {
  200. expect(payload.error.code).to.equal('invalid_email')
  201. return done()
  202. },
  203. }),
  204. })
  205. })
  206. })
  207. describe('remove', function () {
  208. beforeEach(function () {
  209. this.req.params.userId = this.newUser._id
  210. this.req.entity = this.subscription
  211. return (this.req.entityConfig = EntityConfigs.groupManagers)
  212. })
  213. it('remove user', function (done) {
  214. return this.UserMembershipController.remove(this.req, {
  215. sendStatus: () => {
  216. sinon.assert.calledWithMatch(
  217. this.UserMembershipHandler.removeUser,
  218. this.subscription,
  219. { modelName: 'Subscription' },
  220. this.newUser._id
  221. )
  222. return done()
  223. },
  224. })
  225. })
  226. it('handle readOnly entity', function (done) {
  227. this.req.entityConfig = EntityConfigs.group
  228. return this.UserMembershipController.remove(this.req, null, error => {
  229. expect(error).to.exist
  230. expect(error).to.be.an.instanceof(Errors.NotFoundError)
  231. return done()
  232. })
  233. })
  234. it('prevent self removal', function (done) {
  235. this.req.params.userId = this.user._id
  236. return this.UserMembershipController.remove(this.req, {
  237. status: () => ({
  238. json: payload => {
  239. expect(payload.error.code).to.equal('managers_cannot_remove_self')
  240. return done()
  241. },
  242. }),
  243. })
  244. })
  245. it('prevent admin removal', function (done) {
  246. this.UserMembershipHandler.removeUser.yields({ isAdmin: true })
  247. return this.UserMembershipController.remove(this.req, {
  248. status: () => ({
  249. json: payload => {
  250. expect(payload.error.code).to.equal('managers_cannot_remove_admin')
  251. return done()
  252. },
  253. }),
  254. })
  255. })
  256. })
  257. describe('exportCsv', function () {
  258. beforeEach(function () {
  259. this.req.entity = this.subscription
  260. this.req.entityConfig = EntityConfigs.groupManagers
  261. this.res = new MockResponse()
  262. this.res.contentType = sinon.stub()
  263. this.res.header = sinon.stub()
  264. this.res.send = sinon.stub()
  265. return this.UserMembershipController.exportCsv(this.req, this.res)
  266. })
  267. it('get users', function () {
  268. return sinon.assert.calledWithMatch(
  269. this.UserMembershipHandler.getUsers,
  270. this.subscription,
  271. { modelName: 'Subscription' }
  272. )
  273. })
  274. it('should set the correct content type on the request', function () {
  275. return assertCalledWith(this.res.contentType, 'text/csv')
  276. })
  277. it('should name the exported csv file', function () {
  278. return assertCalledWith(
  279. this.res.header,
  280. 'Content-Disposition',
  281. 'attachment; filename=Group.csv'
  282. )
  283. })
  284. it('should export the correct csv', function () {
  285. return assertCalledWith(
  286. this.res.send,
  287. '"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"'
  288. )
  289. })
  290. })
  291. describe('new', function () {
  292. beforeEach(function () {
  293. this.req.params.name = 'publisher'
  294. return (this.req.params.id = 'abc')
  295. })
  296. it('renders view', function (done) {
  297. return this.UserMembershipController.new(this.req, {
  298. render: (viewPath, data) => {
  299. expect(data.entityName).to.eq('publisher')
  300. expect(data.entityId).to.eq('abc')
  301. return done()
  302. },
  303. })
  304. })
  305. })
  306. describe('create', function () {
  307. beforeEach(function () {
  308. this.req.params.name = 'institution'
  309. this.req.entityConfig = EntityConfigs.institution
  310. return (this.req.params.id = 123)
  311. })
  312. it('creates institution', function (done) {
  313. return this.UserMembershipController.create(this.req, {
  314. redirect: path => {
  315. expect(path).to.eq(EntityConfigs.institution.pathsFor(123).index)
  316. sinon.assert.calledWithMatch(
  317. this.UserMembershipHandler.createEntity,
  318. 123,
  319. { modelName: 'Institution' }
  320. )
  321. return done()
  322. },
  323. })
  324. })
  325. })
  326. })