UserMembershipHandlerTests.js 8.7 KB

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