UserMembershipHandlerTests.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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('mongodb')
  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. mongodb: { ObjectId },
  74. './UserMembershipViewModel': this.UserMembershipViewModel,
  75. '../User/UserGetter': this.UserGetter,
  76. '../Errors/Errors': Errors,
  77. '../../models/Institution': {
  78. Institution: this.Institution
  79. },
  80. '../../models/Subscription': {
  81. Subscription: this.Subscription
  82. },
  83. '../../models/Publisher': {
  84. Publisher: this.Publisher
  85. },
  86. 'logger-sharelatex': {
  87. log() {},
  88. err() {}
  89. }
  90. }
  91. }))
  92. })
  93. describe('getEntityWithoutAuthorizationCheck', function() {
  94. it('get publisher', function(done) {
  95. return this.UserMembershipHandler.getEntityWithoutAuthorizationCheck(
  96. this.fakeEntityId,
  97. EntityConfigs.publisher,
  98. (error, subscription) => {
  99. should.not.exist(error)
  100. const expectedQuery = { slug: this.fakeEntityId }
  101. assertCalledWith(this.Publisher.findOne, expectedQuery)
  102. expect(subscription).to.equal(this.publisher)
  103. return done()
  104. }
  105. )
  106. })
  107. })
  108. describe('getUsers', function() {
  109. describe('group', function() {
  110. it('build view model for all users', function(done) {
  111. return this.UserMembershipHandler.getUsers(
  112. this.subscription,
  113. EntityConfigs.group,
  114. (error, users) => {
  115. const expectedCallcount =
  116. this.subscription.member_ids.length +
  117. this.subscription.invited_emails.length +
  118. this.subscription.teamInvites.length
  119. expect(this.UserMembershipViewModel.buildAsync.callCount).to.equal(
  120. expectedCallcount
  121. )
  122. return done()
  123. }
  124. )
  125. })
  126. })
  127. describe('group mamagers', function() {
  128. it('build view model for all managers', function(done) {
  129. return this.UserMembershipHandler.getUsers(
  130. this.subscription,
  131. EntityConfigs.groupManagers,
  132. (error, users) => {
  133. const expectedCallcount = this.subscription.manager_ids.length
  134. expect(this.UserMembershipViewModel.buildAsync.callCount).to.equal(
  135. expectedCallcount
  136. )
  137. return done()
  138. }
  139. )
  140. })
  141. })
  142. describe('institution', function() {
  143. it('build view model for all managers', function(done) {
  144. return this.UserMembershipHandler.getUsers(
  145. this.institution,
  146. EntityConfigs.institution,
  147. (error, users) => {
  148. const expectedCallcount = this.institution.managerIds.length
  149. expect(this.UserMembershipViewModel.buildAsync.callCount).to.equal(
  150. expectedCallcount
  151. )
  152. return done()
  153. }
  154. )
  155. })
  156. })
  157. })
  158. describe('createEntity', function() {
  159. it('creates publisher', function(done) {
  160. return this.UserMembershipHandler.createEntity(
  161. this.fakeEntityId,
  162. EntityConfigs.publisher,
  163. (error, publisher) => {
  164. should.not.exist(error)
  165. assertCalledWith(this.Publisher.create, { slug: this.fakeEntityId })
  166. return done()
  167. }
  168. )
  169. })
  170. })
  171. describe('addUser', function() {
  172. beforeEach(function() {
  173. return (this.email = this.newUser.email)
  174. })
  175. describe('institution', function() {
  176. it('get user', function(done) {
  177. return this.UserMembershipHandler.addUser(
  178. this.institution,
  179. EntityConfigs.institution,
  180. this.email,
  181. (error, user) => {
  182. assertCalledWith(this.UserGetter.getUserByAnyEmail, this.email)
  183. return done()
  184. }
  185. )
  186. })
  187. it('handle user not found', function(done) {
  188. this.UserGetter.getUserByAnyEmail.yields(null, null)
  189. return this.UserMembershipHandler.addUser(
  190. this.institution,
  191. EntityConfigs.institution,
  192. this.email,
  193. error => {
  194. expect(error).to.exist
  195. expect(error.userNotFound).to.equal(true)
  196. return done()
  197. }
  198. )
  199. })
  200. it('handle user already added', function(done) {
  201. this.institution.managerIds.push(this.newUser._id)
  202. return this.UserMembershipHandler.addUser(
  203. this.institution,
  204. EntityConfigs.institution,
  205. this.email,
  206. (error, users) => {
  207. expect(error).to.exist
  208. expect(error.alreadyAdded).to.equal(true)
  209. return done()
  210. }
  211. )
  212. })
  213. it('add user to institution', function(done) {
  214. return this.UserMembershipHandler.addUser(
  215. this.institution,
  216. EntityConfigs.institution,
  217. this.email,
  218. (error, user) => {
  219. assertCalledWith(this.institution.update, {
  220. $addToSet: { managerIds: this.newUser._id }
  221. })
  222. return done()
  223. }
  224. )
  225. })
  226. it('return user view', function(done) {
  227. return this.UserMembershipHandler.addUser(
  228. this.institution,
  229. EntityConfigs.institution,
  230. this.email,
  231. (error, user) => {
  232. user.should.equal(this.newUser)
  233. return done()
  234. }
  235. )
  236. })
  237. })
  238. })
  239. describe('removeUser', function() {
  240. describe('institution', function() {
  241. it('remove user from institution', function(done) {
  242. return this.UserMembershipHandler.removeUser(
  243. this.institution,
  244. EntityConfigs.institution,
  245. this.newUser._id,
  246. (error, user) => {
  247. const { lastCall } = this.institution.update
  248. assertCalledWith(this.institution.update, {
  249. $pull: { managerIds: this.newUser._id }
  250. })
  251. return done()
  252. }
  253. )
  254. })
  255. it('handle admin', function(done) {
  256. this.subscription.admin_id = this.newUser._id
  257. return this.UserMembershipHandler.removeUser(
  258. this.subscription,
  259. EntityConfigs.groupManagers,
  260. this.newUser._id,
  261. (error, user) => {
  262. expect(error).to.exist
  263. expect(error.isAdmin).to.equal(true)
  264. return done()
  265. }
  266. )
  267. })
  268. })
  269. })
  270. })