UserMembershipAuthorizationTests.mjs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { expect } from 'chai'
  2. import async from 'async'
  3. import User from './helpers/User.mjs'
  4. import Institution from './helpers/Institution.mjs'
  5. import Subscription from './helpers/Subscription.mjs'
  6. import Publisher from './helpers/Publisher.mjs'
  7. import sinon from 'sinon'
  8. import RecurlyClient from '../../../app/src/Features/Subscription/RecurlyClient.mjs'
  9. import Features from '../../../app/src/infrastructure/Features.mjs'
  10. describe('UserMembershipAuthorization', function () {
  11. beforeEach(function (done) {
  12. if (!Features.hasFeature('saas')) {
  13. this.skip()
  14. }
  15. this.user = new User()
  16. sinon.stub(RecurlyClient.promises, 'getSubscription').resolves({})
  17. async.series([this.user.ensureUserExists.bind(this.user)], done)
  18. })
  19. afterEach(function () {
  20. if (!Features.hasFeature('saas')) {
  21. return
  22. }
  23. RecurlyClient.promises.getSubscription.restore()
  24. })
  25. describe('group', function () {
  26. beforeEach(function (done) {
  27. this.subscription = new Subscription({
  28. groupPlan: true,
  29. })
  30. async.series(
  31. [
  32. this.subscription.ensureExists.bind(this.subscription),
  33. cb => this.user.login(cb),
  34. ],
  35. done
  36. )
  37. })
  38. describe('users management', function () {
  39. it('should allow managers only', function (done) {
  40. const url = `/manage/groups/${this.subscription._id}/members`
  41. async.series(
  42. [
  43. expectAccess(this.user, url, 403),
  44. cb => this.subscription.setManagerIds([this.user._id], cb),
  45. expectAccess(this.user, url, 200),
  46. ],
  47. done
  48. )
  49. })
  50. })
  51. describe('managers management', function () {
  52. it('should allow managers only', function (done) {
  53. const url = `/manage/groups/${this.subscription._id}/managers`
  54. async.series(
  55. [
  56. expectAccess(this.user, url, 403),
  57. cb => this.subscription.setManagerIds([this.user._id], cb),
  58. expectAccess(this.user, url, 200),
  59. ],
  60. done
  61. )
  62. })
  63. })
  64. })
  65. describe('institution', function () {
  66. beforeEach(async function () {
  67. this.institution = new Institution()
  68. await this.institution.ensureExists(this.institution)
  69. })
  70. describe('users management', function () {
  71. it('should allow managers only', function (done) {
  72. const url = `/manage/institutions/${this.institution.v1Id}/managers`
  73. async.series(
  74. [
  75. this.user.login.bind(this.user),
  76. expectAccess(this.user, url, 403),
  77. cb => this.institution.setManagerIds([this.user._id], cb),
  78. expectAccess(this.user, url, 200),
  79. ],
  80. done
  81. )
  82. })
  83. })
  84. describe('creation', function () {
  85. it('should allow staff only', function (done) {
  86. const url = `/entities/institution/create/foo`
  87. async.series(
  88. [
  89. this.user.login.bind(this.user),
  90. expectAccess(this.user, url, 403),
  91. cb => this.user.ensureStaffAccess('institutionManagement', cb),
  92. this.user.login.bind(this.user),
  93. expectAccess(this.user, url, 200),
  94. ],
  95. done
  96. )
  97. })
  98. })
  99. })
  100. describe('publisher', function () {
  101. beforeEach(function (done) {
  102. this.publisher = new Publisher({})
  103. async.series(
  104. [
  105. this.publisher.ensureExists.bind(this.publisher),
  106. cb => this.user.login(cb),
  107. ],
  108. done
  109. )
  110. })
  111. describe('managers management', function () {
  112. it('should allow managers only', function (done) {
  113. const url = `/manage/publishers/${this.publisher.slug}/managers`
  114. async.series(
  115. [
  116. expectAccess(this.user, url, 403),
  117. cb => this.publisher.setManagerIds([this.user._id], cb),
  118. expectAccess(this.user, url, 200),
  119. ],
  120. done
  121. )
  122. })
  123. })
  124. describe('creation', function () {
  125. it('should redirect staff only', function (done) {
  126. const url = `/manage/publishers/foo/managers`
  127. async.series(
  128. [
  129. this.user.login.bind(this.user),
  130. expectAccess(this.user, url, 404),
  131. cb => this.user.ensureStaffAccess('publisherManagement', cb),
  132. this.user.login.bind(this.user),
  133. expectAccess(this.user, url, 302, /\/create/),
  134. ],
  135. done
  136. )
  137. })
  138. it('should allow staff only', function (done) {
  139. const url = `/entities/publisher/create/foo`
  140. async.series(
  141. [
  142. expectAccess(this.user, url, 403),
  143. cb => this.user.ensureStaffAccess('publisherManagement', cb),
  144. this.user.login.bind(this.user),
  145. expectAccess(this.user, url, 200),
  146. ],
  147. done
  148. )
  149. })
  150. })
  151. })
  152. })
  153. function expectAccess(user, url, status, pattern) {
  154. return callback => {
  155. user.request.get({ url }, (error, response, body) => {
  156. if (error) {
  157. return callback(error)
  158. }
  159. expect(response.statusCode).to.equal(status)
  160. if (pattern) {
  161. expect(body).to.match(pattern)
  162. }
  163. callback()
  164. })
  165. }
  166. }