UserMembershipHandlerTests.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* eslint-disable
  2. handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const chai = require('chai')
  15. const should = chai.should()
  16. const { expect } = require('chai')
  17. const sinon = require('sinon')
  18. const assertCalledWith = sinon.assert.calledWith
  19. const assertNotCalled = sinon.assert.notCalled
  20. const { ObjectId } = require('../../../../app/src/infrastructure/mongojs')
  21. const modulePath =
  22. '../../../../app/src/Features/UserMembership/UserMembershipHandler'
  23. const SandboxedModule = require('sandboxed-module')
  24. const Errors = require('../../../../app/src/Features/Errors/Errors')
  25. const EntityConfigs = require('../../../../app/src/Features/UserMembership/UserMembershipEntityConfigs')
  26. describe('UserMembershipHandler', function() {
  27. beforeEach(function() {
  28. this.user = { _id: ObjectId() }
  29. this.newUser = { _id: ObjectId(), email: 'new-user-email@foo.bar' }
  30. this.fakeEntityId = ObjectId()
  31. this.subscription = {
  32. _id: 'mock-subscription-id',
  33. groupPlan: true,
  34. membersLimit: 10,
  35. member_ids: [ObjectId(), ObjectId()],
  36. manager_ids: [ObjectId()],
  37. invited_emails: ['mock-email-1@foo.com'],
  38. teamInvites: [{ email: 'mock-email-1@bar.com' }],
  39. update: sinon.stub().yields(null)
  40. }
  41. this.institution = {
  42. _id: 'mock-institution-id',
  43. v1Id: 123,
  44. managerIds: [ObjectId(), ObjectId(), ObjectId()],
  45. update: sinon.stub().yields(null)
  46. }
  47. this.publisher = {
  48. _id: 'mock-publisher-id',
  49. slug: 'slug',
  50. managerIds: [ObjectId(), ObjectId()],
  51. update: sinon.stub().yields(null)
  52. }
  53. this.UserMembershipViewModel = {
  54. buildAsync: sinon.stub().yields(null, { _id: 'mock-member-id' }),
  55. build: sinon.stub().returns(this.newUser)
  56. }
  57. this.UserGetter = {
  58. getUserByAnyEmail: sinon.stub().yields(null, this.newUser)
  59. }
  60. this.Institution = { findOne: sinon.stub().yields(null, this.institution) }
  61. this.Subscription = {
  62. findOne: sinon.stub().yields(null, this.subscription)
  63. }
  64. this.Publisher = {
  65. findOne: sinon.stub().yields(null, this.publisher),
  66. create: sinon.stub().yields(null, this.publisher)
  67. }
  68. return (this.UserMembershipHandler = SandboxedModule.require(modulePath, {
  69. globals: {
  70. console: console
  71. },
  72. requires: {
  73. './UserMembershipViewModel': this.UserMembershipViewModel,
  74. '../User/UserGetter': this.UserGetter,
  75. '../Errors/Errors': Errors,
  76. '../../models/Institution': {
  77. Institution: this.Institution
  78. },
  79. '../../models/Subscription': {
  80. Subscription: this.Subscription
  81. },
  82. '../../models/Publisher': {
  83. Publisher: this.Publisher
  84. },
  85. 'logger-sharelatex': {
  86. log() {},
  87. err() {}
  88. }
  89. }
  90. }))
  91. })
  92. describe('getEntityWithoutAuthorizationCheck', function() {
  93. it('get publisher', function(done) {
  94. return this.UserMembershipHandler.getEntityWithoutAuthorizationCheck(
  95. this.fakeEntityId,
  96. EntityConfigs.publisher,
  97. (error, subscription) => {
  98. should.not.exist(error)
  99. const expectedQuery = { slug: this.fakeEntityId }
  100. assertCalledWith(this.Publisher.findOne, expectedQuery)
  101. expect(subscription).to.equal(this.publisher)
  102. return done()
  103. }
  104. )
  105. })
  106. })
  107. describe('getUsers', function() {
  108. describe('group', function() {
  109. it('build view model for all users', function(done) {
  110. return this.UserMembershipHandler.getUsers(
  111. this.subscription,
  112. EntityConfigs.group,
  113. (error, users) => {
  114. const expectedCallcount =
  115. this.subscription.member_ids.length +
  116. this.subscription.invited_emails.length +
  117. this.subscription.teamInvites.length
  118. expect(this.UserMembershipViewModel.buildAsync.callCount).to.equal(
  119. expectedCallcount
  120. )
  121. return done()
  122. }
  123. )
  124. })
  125. })
  126. describe('group mamagers', function() {
  127. it('build view model for all managers', function(done) {
  128. return this.UserMembershipHandler.getUsers(
  129. this.subscription,
  130. EntityConfigs.groupManagers,
  131. (error, users) => {
  132. const expectedCallcount = this.subscription.manager_ids.length
  133. expect(this.UserMembershipViewModel.buildAsync.callCount).to.equal(
  134. expectedCallcount
  135. )
  136. return done()
  137. }
  138. )
  139. })
  140. })
  141. describe('institution', function() {
  142. it('build view model for all managers', function(done) {
  143. return this.UserMembershipHandler.getUsers(
  144. this.institution,
  145. EntityConfigs.institution,
  146. (error, users) => {
  147. const expectedCallcount = this.institution.managerIds.length
  148. expect(this.UserMembershipViewModel.buildAsync.callCount).to.equal(
  149. expectedCallcount
  150. )
  151. return done()
  152. }
  153. )
  154. })
  155. })
  156. })
  157. describe('createEntity', function() {
  158. it('creates publisher', function(done) {
  159. return this.UserMembershipHandler.createEntity(
  160. this.fakeEntityId,
  161. EntityConfigs.publisher,
  162. (error, publisher) => {
  163. should.not.exist(error)
  164. assertCalledWith(this.Publisher.create, { slug: this.fakeEntityId })
  165. return done()
  166. }
  167. )
  168. })
  169. })
  170. describe('addUser', function() {
  171. beforeEach(function() {
  172. return (this.email = this.newUser.email)
  173. })
  174. describe('institution', function() {
  175. it('get user', function(done) {
  176. return this.UserMembershipHandler.addUser(
  177. this.institution,
  178. EntityConfigs.institution,
  179. this.email,
  180. (error, user) => {
  181. assertCalledWith(this.UserGetter.getUserByAnyEmail, this.email)
  182. return done()
  183. }
  184. )
  185. })
  186. it('handle user not found', function(done) {
  187. this.UserGetter.getUserByAnyEmail.yields(null, null)
  188. return this.UserMembershipHandler.addUser(
  189. this.institution,
  190. EntityConfigs.institution,
  191. this.email,
  192. error => {
  193. expect(error).to.exist
  194. expect(error.userNotFound).to.equal(true)
  195. return done()
  196. }
  197. )
  198. })
  199. it('handle user already added', function(done) {
  200. this.institution.managerIds.push(this.newUser._id)
  201. return this.UserMembershipHandler.addUser(
  202. this.institution,
  203. EntityConfigs.institution,
  204. this.email,
  205. (error, users) => {
  206. expect(error).to.exist
  207. expect(error.alreadyAdded).to.equal(true)
  208. return done()
  209. }
  210. )
  211. })
  212. it('add user to institution', function(done) {
  213. return this.UserMembershipHandler.addUser(
  214. this.institution,
  215. EntityConfigs.institution,
  216. this.email,
  217. (error, user) => {
  218. assertCalledWith(this.institution.update, {
  219. $addToSet: { managerIds: this.newUser._id }
  220. })
  221. return done()
  222. }
  223. )
  224. })
  225. it('return user view', function(done) {
  226. return this.UserMembershipHandler.addUser(
  227. this.institution,
  228. EntityConfigs.institution,
  229. this.email,
  230. (error, user) => {
  231. user.should.equal(this.newUser)
  232. return done()
  233. }
  234. )
  235. })
  236. })
  237. })
  238. describe('removeUser', function() {
  239. describe('institution', function() {
  240. it('remove user from institution', function(done) {
  241. return this.UserMembershipHandler.removeUser(
  242. this.institution,
  243. EntityConfigs.institution,
  244. this.newUser._id,
  245. (error, user) => {
  246. const { lastCall } = this.institution.update
  247. assertCalledWith(this.institution.update, {
  248. $pull: { managerIds: this.newUser._id }
  249. })
  250. return done()
  251. }
  252. )
  253. })
  254. it('handle admin', function(done) {
  255. this.subscription.admin_id = this.newUser._id
  256. return this.UserMembershipHandler.removeUser(
  257. this.subscription,
  258. EntityConfigs.groupManagers,
  259. this.newUser._id,
  260. (error, user) => {
  261. expect(error).to.exist
  262. expect(error.isAdmin).to.equal(true)
  263. return done()
  264. }
  265. )
  266. })
  267. })
  268. })
  269. })