UserMembershipControllerTests.js 11 KB

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