UserMembershipControllerTests.js 11 KB

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