UserMembershipAuthorizationTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. const { expect } = require('chai')
  2. const async = require('async')
  3. const { ObjectId } = require('../../../app/src/infrastructure/mongojs')
  4. const User = require('./helpers/User')
  5. const Institution = require('./helpers/Institution')
  6. const Subscription = require('./helpers/Subscription')
  7. const Publisher = require('./helpers/Publisher')
  8. const MockV1Api = require('./helpers/MockV1Api')
  9. describe('UserMembershipAuthorization', function() {
  10. beforeEach(function(done) {
  11. this.user = new User()
  12. async.series([this.user.ensureUserExists.bind(this.user)], done)
  13. })
  14. describe('team', function() {
  15. beforeEach(function(done) {
  16. this.subscription = new Subscription({
  17. groupPlan: true,
  18. overleaf: { id: 123 }
  19. })
  20. async.series(
  21. [
  22. this.subscription.ensureExists.bind(this.subscription),
  23. cb => this.user.login(cb)
  24. ],
  25. done
  26. )
  27. })
  28. describe('metrics', function() {
  29. it('should allow managers only', function(done) {
  30. const url = `/metrics/teams/123`
  31. async.series(
  32. [
  33. expectAccess(this.user, url, 302, /\/restricted/),
  34. cb => this.subscription.setManagerIds([this.user._id], cb),
  35. expectAccess(this.user, url, 200)
  36. ],
  37. done
  38. )
  39. })
  40. })
  41. })
  42. describe('group', function() {
  43. beforeEach(function(done) {
  44. this.subscription = new Subscription({
  45. groupPlan: true
  46. })
  47. async.series(
  48. [
  49. this.subscription.ensureExists.bind(this.subscription),
  50. cb => this.user.login(cb)
  51. ],
  52. done
  53. )
  54. })
  55. describe('users management', function() {
  56. it('should allow managers only', function(done) {
  57. const url = `/manage/groups/${this.subscription._id}/members`
  58. async.series(
  59. [
  60. expectAccess(this.user, url, 302, /\/restricted/),
  61. cb => this.subscription.setManagerIds([this.user._id], cb),
  62. expectAccess(this.user, url, 200)
  63. ],
  64. done
  65. )
  66. })
  67. })
  68. describe('metrics', function() {
  69. it('should allow managers only', function(done) {
  70. const url = `/metrics/groups/${this.subscription._id}`
  71. async.series(
  72. [
  73. expectAccess(this.user, url, 302, /\/restricted/),
  74. cb => this.subscription.setManagerIds([this.user._id], cb),
  75. expectAccess(this.user, url, 200)
  76. ],
  77. done
  78. )
  79. })
  80. it('should handle groups not found', function(done) {
  81. const url = `/metrics/groups/${ObjectId()}`
  82. async.series([expectAccess(this.user, url, 404)], done)
  83. })
  84. })
  85. describe('managers management', function() {
  86. it('should allow managers only', function(done) {
  87. const url = `/manage/groups/${this.subscription._id}/managers`
  88. async.series(
  89. [
  90. expectAccess(this.user, url, 302, /\/restricted/),
  91. cb => this.subscription.setManagerIds([this.user._id], cb),
  92. expectAccess(this.user, url, 200)
  93. ],
  94. done
  95. )
  96. })
  97. })
  98. })
  99. describe('institution', function() {
  100. beforeEach(function(done) {
  101. this.institution = new Institution()
  102. async.series([this.institution.ensureExists.bind(this.institution)], done)
  103. })
  104. describe('metrics', function() {
  105. it('should allow users with staff access', function(done) {
  106. const url = `/metrics/institutions/${this.institution.v1Id}`
  107. async.series(
  108. [
  109. cb => this.user.ensureStaffAccess('institutionMetrics', cb),
  110. this.user.login.bind(this.user),
  111. expectAccess(this.user, url, 200)
  112. ],
  113. done
  114. )
  115. })
  116. it('should allow admins', function(done) {
  117. const url = `/metrics/institutions/${this.institution.v1Id}`
  118. async.series(
  119. [
  120. this.user.ensure_admin.bind(this.user),
  121. this.user.login.bind(this.user),
  122. expectAccess(this.user, url, 200)
  123. ],
  124. done
  125. )
  126. })
  127. it('should allow managers', function(done) {
  128. const url = `/metrics/institutions/${this.institution.v1Id}`
  129. async.series(
  130. [
  131. this.user.login.bind(this.user),
  132. cb => this.institution.setManagerIds([this.user._id], cb),
  133. expectAccess(this.user, url, 200)
  134. ],
  135. done
  136. )
  137. })
  138. it('should not allow users without access', function(done) {
  139. const url = `/metrics/institutions/${this.institution.v1Id}`
  140. async.series(
  141. [
  142. this.user.login.bind(this.user),
  143. expectAccess(this.user, url, 302, /\/restricted/)
  144. ],
  145. done
  146. )
  147. })
  148. })
  149. describe('users management', function() {
  150. it('should allow managers only', function(done) {
  151. const url = `/manage/institutions/${this.institution.v1Id}/managers`
  152. async.series(
  153. [
  154. this.user.login.bind(this.user),
  155. expectAccess(this.user, url, 302, /\/restricted/),
  156. cb => this.institution.setManagerIds([this.user._id], cb),
  157. expectAccess(this.user, url, 200)
  158. ],
  159. done
  160. )
  161. })
  162. })
  163. describe('hub', function() {
  164. it('should allow managers only', function(done) {
  165. const url = `/institutions/${this.institution.v1Id}/hub`
  166. async.series(
  167. [
  168. this.user.login.bind(this.user),
  169. expectAccess(this.user, url, 302, /\/restricted/),
  170. cb => this.institution.setManagerIds([this.user._id], cb),
  171. expectAccess(this.user, url, 200)
  172. ],
  173. done
  174. )
  175. })
  176. })
  177. describe('creation', function() {
  178. it('should allow staff only', function(done) {
  179. const url = `/entities/institution/create/foo`
  180. async.series(
  181. [
  182. this.user.login.bind(this.user),
  183. expectAccess(this.user, url, 302, /\/restricted/),
  184. cb => this.user.ensureStaffAccess('institutionManagement', cb),
  185. this.user.login.bind(this.user),
  186. expectAccess(this.user, url, 200)
  187. ],
  188. done
  189. )
  190. })
  191. })
  192. })
  193. describe('publisher', function() {
  194. beforeEach(function(done) {
  195. this.publisher = new Publisher({})
  196. async.series(
  197. [
  198. this.publisher.ensureExists.bind(this.publisher),
  199. cb => this.user.login(cb)
  200. ],
  201. done
  202. )
  203. })
  204. describe('conversion metrics', function() {
  205. it('should allow managers only', function(done) {
  206. const url = `/metrics/conversions/${this.publisher.slug}`
  207. async.series(
  208. [
  209. expectAccess(this.user, url, 302, /\/restricted/),
  210. cb => this.publisher.setManagerIds([this.user._id], cb),
  211. expectAccess(this.user, url, 200)
  212. ],
  213. done
  214. )
  215. })
  216. })
  217. describe('managers management', function() {
  218. it('should allow managers only', function(done) {
  219. const url = `/manage/publishers/${this.publisher.slug}/managers`
  220. async.series(
  221. [
  222. expectAccess(this.user, url, 302, /\/restricted/),
  223. cb => this.publisher.setManagerIds([this.user._id], cb),
  224. expectAccess(this.user, url, 200)
  225. ],
  226. done
  227. )
  228. })
  229. })
  230. describe('creation', function() {
  231. it('should redirect staff only', function(done) {
  232. const url = `/publishers/foo/hub`
  233. async.series(
  234. [
  235. this.user.login.bind(this.user),
  236. expectAccess(this.user, url, 404),
  237. cb => this.user.ensureStaffAccess('publisherManagement', cb),
  238. this.user.login.bind(this.user),
  239. expectAccess(this.user, url, 302, /\/create/)
  240. ],
  241. done
  242. )
  243. })
  244. it('should allow staff only', function(done) {
  245. const url = `/entities/publisher/create/foo`
  246. async.series(
  247. [
  248. expectAccess(this.user, url, 302, /\/restricted/),
  249. cb => this.user.ensureStaffAccess('publisherManagement', cb),
  250. this.user.login.bind(this.user),
  251. expectAccess(this.user, url, 200)
  252. ],
  253. done
  254. )
  255. })
  256. })
  257. })
  258. describe('template', function() {
  259. beforeEach(function(done) {
  260. this.publisher = new Publisher({})
  261. async.series(
  262. [
  263. this.publisher.ensureExists.bind(this.publisher),
  264. cb => this.user.login(cb)
  265. ],
  266. done
  267. )
  268. })
  269. it('allow publisher managers only', function(done) {
  270. MockV1Api.setTemplates({
  271. 123: {
  272. id: 123,
  273. title: '123 title',
  274. brand: { slug: this.publisher.slug }
  275. }
  276. })
  277. const url = '/metrics/templates/123'
  278. async.series(
  279. [
  280. expectAccess(this.user, url, 302, /\/restricted/),
  281. cb => this.publisher.setManagerIds([this.user._id], cb),
  282. expectAccess(this.user, url, 200)
  283. ],
  284. done
  285. )
  286. })
  287. it('handle templates without publisher', function(done) {
  288. MockV1Api.setTemplates({
  289. 456: {
  290. id: 456,
  291. title: '456 title',
  292. brand: { slug: null }
  293. }
  294. })
  295. const url = '/metrics/templates/456'
  296. async.series(
  297. [
  298. expectAccess(this.user, url, 302, /\/restricted/),
  299. this.user.ensure_admin.bind(this.user),
  300. this.user.login.bind(this.user),
  301. expectAccess(this.user, url, 200)
  302. ],
  303. done
  304. )
  305. })
  306. it('handle templates not found', function(done) {
  307. const url = '/metrics/templates/789'
  308. async.series(
  309. [
  310. this.user.ensure_admin.bind(this.user),
  311. this.user.login.bind(this.user),
  312. expectAccess(this.user, url, 404)
  313. ],
  314. done
  315. )
  316. })
  317. })
  318. describe('graph', function() {
  319. it('allow admins only', function(done) {
  320. const url = '/graphs/foo?resource_type=admin'
  321. async.series(
  322. [
  323. this.user.login.bind(this.user),
  324. expectAccess(this.user, url, 302, /\/restricted/),
  325. this.user.ensure_admin.bind(this.user),
  326. this.user.login.bind(this.user),
  327. expectAccess(this.user, url, 200)
  328. ],
  329. done
  330. )
  331. })
  332. })
  333. describe('admin metrics', function() {
  334. it('should not allow anonymous users', function(done) {
  335. expectAccess(this.user, '/metrics/admin', 302, /\/restricted/)(done)
  336. })
  337. it('should not allow all users', function(done) {
  338. async.series(
  339. [
  340. this.user.login.bind(this.user),
  341. expectAccess(this.user, '/metrics/admin', 302, /\/restricted/)
  342. ],
  343. done
  344. )
  345. })
  346. it('should allow admin users', function(done) {
  347. async.series(
  348. [
  349. this.user.ensure_admin.bind(this.user),
  350. this.user.login.bind(this.user),
  351. expectAccess(this.user, '/metrics/admin', 200)
  352. ],
  353. done
  354. )
  355. })
  356. it('should allow users with staff access', function(done) {
  357. async.series(
  358. [
  359. cb => this.user.ensureStaffAccess('adminMetrics', cb),
  360. this.user.login.bind(this.user),
  361. expectAccess(this.user, '/metrics/admin', 200)
  362. ],
  363. done
  364. )
  365. })
  366. })
  367. })
  368. function expectAccess(user, url, status, pattern) {
  369. return callback => {
  370. user.request.get({ url }, (error, response, body) => {
  371. if (error) {
  372. return callback(error)
  373. }
  374. expect(response.statusCode).to.equal(status)
  375. if (pattern) {
  376. expect(body).to.match(pattern)
  377. }
  378. callback()
  379. })
  380. }
  381. }