UserMembershipAuthorizationTests.mjs 4.9 KB

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