UserMembershipAuthorizationTests.js 4.7 KB

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