UserMembershipControllerTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. return (this.UserMembershipController = SandboxedModule.require(
  72. modulePath,
  73. {
  74. requires: {
  75. './UserMembershipErrors': { UserIsManagerError },
  76. '../Authentication/SessionManager': this.SessionManager,
  77. './UserMembershipHandler': this.UserMembershipHandler,
  78. },
  79. }
  80. ))
  81. })
  82. describe('index', function () {
  83. beforeEach(function () {
  84. this.req.entity = this.subscription
  85. return (this.req.entityConfig = EntityConfigs.group)
  86. })
  87. it('get users', function (done) {
  88. return this.UserMembershipController.index(this.req, {
  89. render: () => {
  90. sinon.assert.calledWithMatch(
  91. this.UserMembershipHandler.getUsers,
  92. this.subscription,
  93. { modelName: 'Subscription' }
  94. )
  95. return done()
  96. },
  97. })
  98. })
  99. it('render group view', function (done) {
  100. return this.UserMembershipController.index(this.req, {
  101. render: (viewPath, viewParams) => {
  102. expect(viewPath).to.equal('user_membership/index')
  103. expect(viewParams.users).to.deep.equal(this.users)
  104. expect(viewParams.groupSize).to.equal(this.subscription.membersLimit)
  105. expect(viewParams.translations.title).to.equal('group_subscription')
  106. expect(viewParams.paths.addMember).to.equal(
  107. `/manage/groups/${this.subscription._id}/invites`
  108. )
  109. return done()
  110. },
  111. })
  112. })
  113. it('render group managers view', function (done) {
  114. this.req.entityConfig = EntityConfigs.groupManagers
  115. return this.UserMembershipController.index(this.req, {
  116. render: (viewPath, viewParams) => {
  117. expect(viewPath).to.equal('user_membership/index')
  118. expect(viewParams.groupSize).to.equal(undefined)
  119. expect(viewParams.translations.title).to.equal('group_subscription')
  120. expect(viewParams.translations.subtitle).to.equal(
  121. 'managers_management'
  122. )
  123. expect(viewParams.paths.exportMembers).to.be.undefined
  124. return done()
  125. },
  126. })
  127. })
  128. it('render institution view', function (done) {
  129. this.req.entity = this.institution
  130. this.req.entityConfig = EntityConfigs.institution
  131. return this.UserMembershipController.index(this.req, {
  132. render: (viewPath, viewParams) => {
  133. expect(viewPath).to.equal('user_membership/index')
  134. expect(viewParams.name).to.equal('Test Institution Name')
  135. expect(viewParams.groupSize).to.equal(undefined)
  136. expect(viewParams.translations.title).to.equal('institution_account')
  137. expect(viewParams.paths.exportMembers).to.be.undefined
  138. return done()
  139. },
  140. })
  141. })
  142. })
  143. describe('add', function () {
  144. beforeEach(function () {
  145. this.req.body.email = this.newUser.email
  146. this.req.entity = this.subscription
  147. return (this.req.entityConfig = EntityConfigs.groupManagers)
  148. })
  149. it('add user', function (done) {
  150. return this.UserMembershipController.add(this.req, {
  151. json: () => {
  152. sinon.assert.calledWithMatch(
  153. this.UserMembershipHandler.addUser,
  154. this.subscription,
  155. { modelName: 'Subscription' },
  156. this.newUser.email
  157. )
  158. return done()
  159. },
  160. })
  161. })
  162. it('return user object', function (done) {
  163. return this.UserMembershipController.add(this.req, {
  164. json: payload => {
  165. payload.user.should.equal(this.newUser)
  166. return done()
  167. },
  168. })
  169. })
  170. it('handle readOnly entity', function (done) {
  171. this.req.entityConfig = EntityConfigs.group
  172. return this.UserMembershipController.add(this.req, null, error => {
  173. expect(error).to.exist
  174. expect(error).to.be.an.instanceof(Errors.NotFoundError)
  175. return done()
  176. })
  177. })
  178. it('handle user already added', function (done) {
  179. this.UserMembershipHandler.addUser.yields({ alreadyAdded: true })
  180. return this.UserMembershipController.add(this.req, {
  181. status: () => ({
  182. json: payload => {
  183. expect(payload.error.code).to.equal('user_already_added')
  184. return done()
  185. },
  186. }),
  187. })
  188. })
  189. it('handle user not found', function (done) {
  190. this.UserMembershipHandler.addUser.yields({ userNotFound: true })
  191. return this.UserMembershipController.add(this.req, {
  192. status: () => ({
  193. json: payload => {
  194. expect(payload.error.code).to.equal('user_not_found')
  195. return done()
  196. },
  197. }),
  198. })
  199. })
  200. it('handle invalid email', function (done) {
  201. this.req.body.email = 'not_valid_email'
  202. return this.UserMembershipController.add(this.req, {
  203. status: () => ({
  204. json: payload => {
  205. expect(payload.error.code).to.equal('invalid_email')
  206. return done()
  207. },
  208. }),
  209. })
  210. })
  211. })
  212. describe('remove', function () {
  213. beforeEach(function () {
  214. this.req.params.userId = this.newUser._id
  215. this.req.entity = this.subscription
  216. return (this.req.entityConfig = EntityConfigs.groupManagers)
  217. })
  218. it('remove user', function (done) {
  219. return this.UserMembershipController.remove(this.req, {
  220. sendStatus: () => {
  221. sinon.assert.calledWithMatch(
  222. this.UserMembershipHandler.removeUser,
  223. this.subscription,
  224. { modelName: 'Subscription' },
  225. this.newUser._id
  226. )
  227. return done()
  228. },
  229. })
  230. })
  231. it('handle readOnly entity', function (done) {
  232. this.req.entityConfig = EntityConfigs.group
  233. return this.UserMembershipController.remove(this.req, null, error => {
  234. expect(error).to.exist
  235. expect(error).to.be.an.instanceof(Errors.NotFoundError)
  236. return done()
  237. })
  238. })
  239. it('prevent self removal', function (done) {
  240. this.req.params.userId = this.user._id
  241. return this.UserMembershipController.remove(this.req, {
  242. status: () => ({
  243. json: payload => {
  244. expect(payload.error.code).to.equal('managers_cannot_remove_self')
  245. return done()
  246. },
  247. }),
  248. })
  249. })
  250. it('prevent admin removal', function (done) {
  251. this.UserMembershipHandler.removeUser.yields(new UserIsManagerError())
  252. return this.UserMembershipController.remove(this.req, {
  253. status: () => ({
  254. json: payload => {
  255. expect(payload.error.code).to.equal('managers_cannot_remove_admin')
  256. return done()
  257. },
  258. }),
  259. })
  260. })
  261. })
  262. describe('exportCsv', function () {
  263. beforeEach(function () {
  264. this.req.entity = this.subscription
  265. this.req.entityConfig = EntityConfigs.groupManagers
  266. this.res = new MockResponse()
  267. return this.UserMembershipController.exportCsv(this.req, this.res)
  268. })
  269. it('get users', function () {
  270. return sinon.assert.calledWithMatch(
  271. this.UserMembershipHandler.getUsers,
  272. this.subscription,
  273. { modelName: 'Subscription' }
  274. )
  275. })
  276. it('should set the correct content type on the request', function () {
  277. return assertCalledWith(this.res.contentType, 'text/csv; charset=utf-8')
  278. })
  279. it('should name the exported csv file', function () {
  280. return assertCalledWith(
  281. this.res.header,
  282. 'Content-Disposition',
  283. 'attachment; filename="Group.csv"'
  284. )
  285. })
  286. it('should export the correct csv', function () {
  287. return assertCalledWith(
  288. this.res.send,
  289. '"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"'
  290. )
  291. })
  292. })
  293. describe('new', function () {
  294. beforeEach(function () {
  295. this.req.params.name = 'publisher'
  296. return (this.req.params.id = 'abc')
  297. })
  298. it('renders view', function (done) {
  299. return this.UserMembershipController.new(this.req, {
  300. render: (viewPath, data) => {
  301. expect(data.entityName).to.eq('publisher')
  302. expect(data.entityId).to.eq('abc')
  303. return done()
  304. },
  305. })
  306. })
  307. })
  308. describe('create', function () {
  309. beforeEach(function () {
  310. this.req.params.name = 'institution'
  311. this.req.entityConfig = EntityConfigs.institution
  312. return (this.req.params.id = 123)
  313. })
  314. it('creates institution', function (done) {
  315. return this.UserMembershipController.create(this.req, {
  316. redirect: path => {
  317. expect(path).to.eq(EntityConfigs.institution.pathsFor(123).index)
  318. sinon.assert.calledWithMatch(
  319. this.UserMembershipHandler.createEntity,
  320. 123,
  321. { modelName: 'Institution' }
  322. )
  323. return done()
  324. },
  325. })
  326. })
  327. })
  328. })