UserMembershipControllerTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 chai = require('chai')
  17. const should = chai.should()
  18. const { assert } = chai
  19. const { expect } = require('chai')
  20. const modulePath =
  21. '../../../../app/src/Features/UserMembership/UserMembershipController.js'
  22. const SandboxedModule = require('sandboxed-module')
  23. const MockRequest = require('../helpers/MockRequest')
  24. const MockResponse = require('../helpers/MockResponse')
  25. const EntityConfigs = require('../../../../app/src/Features/UserMembership/UserMembershipEntityConfigs')
  26. const Errors = require('../../../../app/src/Features/Errors/Errors')
  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. { _id: 'mock-member-id-1', email: 'mock-email-1@foo.com' },
  48. { _id: 'mock-member-id-2', email: 'mock-email-2@foo.com' }
  49. ]
  50. this.AuthenticationController = {
  51. getSessionUser: sinon.stub().returns(this.user),
  52. getLoggedInUserId: sinon.stub().returns(this.user._id)
  53. }
  54. this.UserMembershipHandler = {
  55. getEntity: sinon.stub().yields(null, this.subscription),
  56. createEntity: sinon.stub().yields(null, this.institution),
  57. getUsers: sinon.stub().yields(null, this.users),
  58. addUser: sinon.stub().yields(null, this.newUser),
  59. removeUser: sinon.stub().yields(null)
  60. }
  61. return (this.UserMembershipController = SandboxedModule.require(
  62. modulePath,
  63. {
  64. requires: {
  65. '../Authentication/AuthenticationController': this
  66. .AuthenticationController,
  67. './UserMembershipHandler': this.UserMembershipHandler,
  68. '../Errors/Errors': Errors,
  69. 'logger-sharelatex': {
  70. log() {},
  71. err() {}
  72. }
  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. return 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.extist
  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. return 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. send: () => {
  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.extist
  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. return 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. return it('should export the correct csv', function() {
  285. return assertCalledWith(
  286. this.res.send,
  287. 'mock-email-1@foo.com\nmock-email-2@foo.com\n'
  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. return 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. return describe('create', function() {
  307. beforeEach(function() {
  308. this.req.params.name = '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. return it('checks canCreate', function(done) {
  325. this.req.params.name = 'group'
  326. return this.UserMembershipController.create(this.req, null, error => {
  327. expect(error).to.extist
  328. expect(error).to.be.an.instanceof(Errors.NotFoundError)
  329. sinon.assert.notCalled(this.UserMembershipHandler.createEntity)
  330. return done()
  331. })
  332. })
  333. })
  334. })