| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- const { expect } = require('chai')
- const async = require('async')
- const { ObjectId } = require('../../../app/src/infrastructure/mongojs')
- const User = require('./helpers/User')
- const Institution = require('./helpers/Institution')
- const Subscription = require('./helpers/Subscription')
- const Publisher = require('./helpers/Publisher')
- const MockV1Api = require('./helpers/MockV1Api')
- describe('UserMembershipAuthorization', function() {
- beforeEach(function(done) {
- this.user = new User()
- async.series([this.user.ensureUserExists.bind(this.user)], done)
- })
- describe('team', function() {
- beforeEach(function(done) {
- this.subscription = new Subscription({
- groupPlan: true,
- overleaf: { id: 123 }
- })
- async.series(
- [
- this.subscription.ensureExists.bind(this.subscription),
- cb => this.user.login(cb)
- ],
- done
- )
- })
- describe('metrics', function() {
- it('should allow managers only', function(done) {
- const url = `/metrics/teams/123`
- async.series(
- [
- expectAccess(this.user, url, 403),
- cb => this.subscription.setManagerIds([this.user._id], cb),
- expectAccess(this.user, url, 200)
- ],
- done
- )
- })
- })
- })
- describe('group', function() {
- beforeEach(function(done) {
- this.subscription = new Subscription({
- groupPlan: true
- })
- async.series(
- [
- this.subscription.ensureExists.bind(this.subscription),
- cb => this.user.login(cb)
- ],
- done
- )
- })
- describe('users management', function() {
- it('should allow managers only', function(done) {
- const url = `/manage/groups/${this.subscription._id}/members`
- async.series(
- [
- expectAccess(this.user, url, 403),
- cb => this.subscription.setManagerIds([this.user._id], cb),
- expectAccess(this.user, url, 200)
- ],
- done
- )
- })
- })
- describe('metrics', function() {
- it('should allow managers only', function(done) {
- const url = `/metrics/groups/${this.subscription._id}`
- async.series(
- [
- expectAccess(this.user, url, 403),
- cb => this.subscription.setManagerIds([this.user._id], cb),
- expectAccess(this.user, url, 200)
- ],
- done
- )
- })
- it('should handle groups not found', function(done) {
- const url = `/metrics/groups/${ObjectId()}`
- async.series([expectAccess(this.user, url, 404)], done)
- })
- })
- describe('managers management', function() {
- it('should allow managers only', function(done) {
- const url = `/manage/groups/${this.subscription._id}/managers`
- async.series(
- [
- expectAccess(this.user, url, 403),
- cb => this.subscription.setManagerIds([this.user._id], cb),
- expectAccess(this.user, url, 200)
- ],
- done
- )
- })
- })
- })
- describe('institution', function() {
- beforeEach(function(done) {
- this.institution = new Institution()
- async.series([this.institution.ensureExists.bind(this.institution)], done)
- })
- describe('metrics', function() {
- it('should allow users with staff access', function(done) {
- const url = `/metrics/institutions/${this.institution.v1Id}`
- async.series(
- [
- cb => this.user.ensureStaffAccess('institutionMetrics', cb),
- this.user.login.bind(this.user),
- expectAccess(this.user, url, 200)
- ],
- done
- )
- })
- it('should allow admins', function(done) {
- const url = `/metrics/institutions/${this.institution.v1Id}`
- async.series(
- [
- this.user.ensureAdmin.bind(this.user),
- this.user.login.bind(this.user),
- expectAccess(this.user, url, 200)
- ],
- done
- )
- })
- it('should allow managers', function(done) {
- const url = `/metrics/institutions/${this.institution.v1Id}`
- async.series(
- [
- this.user.login.bind(this.user),
- cb => this.institution.setManagerIds([this.user._id], cb),
- expectAccess(this.user, url, 200)
- ],
- done
- )
- })
- it('should not allow users without access', function(done) {
- const url = `/metrics/institutions/${this.institution.v1Id}`
- async.series(
- [this.user.login.bind(this.user), expectAccess(this.user, url, 403)],
- done
- )
- })
- })
- describe('users management', function() {
- it('should allow managers only', function(done) {
- const url = `/manage/institutions/${this.institution.v1Id}/managers`
- async.series(
- [
- this.user.login.bind(this.user),
- expectAccess(this.user, url, 403),
- cb => this.institution.setManagerIds([this.user._id], cb),
- expectAccess(this.user, url, 200)
- ],
- done
- )
- })
- })
- describe('hub', function() {
- it('should allow managers only', function(done) {
- const url = `/institutions/${this.institution.v1Id}/hub`
- async.series(
- [
- this.user.login.bind(this.user),
- expectAccess(this.user, url, 403),
- cb => this.institution.setManagerIds([this.user._id], cb),
- expectAccess(this.user, url, 200)
- ],
- done
- )
- })
- })
- describe('creation', function() {
- it('should allow staff only', function(done) {
- const url = `/entities/institution/create/foo`
- async.series(
- [
- this.user.login.bind(this.user),
- expectAccess(this.user, url, 403),
- cb => this.user.ensureStaffAccess('institutionManagement', cb),
- this.user.login.bind(this.user),
- expectAccess(this.user, url, 200)
- ],
- done
- )
- })
- })
- })
- describe('publisher', function() {
- beforeEach(function(done) {
- this.publisher = new Publisher({})
- async.series(
- [
- this.publisher.ensureExists.bind(this.publisher),
- cb => this.user.login(cb)
- ],
- done
- )
- })
- describe('conversion metrics', function() {
- it('should allow managers only', function(done) {
- const url = `/metrics/conversions/${this.publisher.slug}`
- async.series(
- [
- expectAccess(this.user, url, 403),
- cb => this.publisher.setManagerIds([this.user._id], cb),
- expectAccess(this.user, url, 200)
- ],
- done
- )
- })
- })
- describe('managers management', function() {
- it('should allow managers only', function(done) {
- const url = `/manage/publishers/${this.publisher.slug}/managers`
- async.series(
- [
- expectAccess(this.user, url, 403),
- cb => this.publisher.setManagerIds([this.user._id], cb),
- expectAccess(this.user, url, 200)
- ],
- done
- )
- })
- })
- describe('creation', function() {
- it('should redirect staff only', function(done) {
- const url = `/publishers/foo/hub`
- async.series(
- [
- this.user.login.bind(this.user),
- expectAccess(this.user, url, 404),
- cb => this.user.ensureStaffAccess('publisherManagement', cb),
- this.user.login.bind(this.user),
- expectAccess(this.user, url, 302, /\/create/)
- ],
- done
- )
- })
- it('should allow staff only', function(done) {
- const url = `/entities/publisher/create/foo`
- async.series(
- [
- expectAccess(this.user, url, 403),
- cb => this.user.ensureStaffAccess('publisherManagement', cb),
- this.user.login.bind(this.user),
- expectAccess(this.user, url, 200)
- ],
- done
- )
- })
- })
- })
- describe('template', function() {
- beforeEach(function(done) {
- this.publisher = new Publisher({})
- async.series(
- [
- this.publisher.ensureExists.bind(this.publisher),
- cb => this.user.login(cb)
- ],
- done
- )
- })
- it('allow publisher managers only', function(done) {
- MockV1Api.setTemplates({
- 123: {
- id: 123,
- title: '123 title',
- brand: { slug: this.publisher.slug }
- }
- })
- const url = '/metrics/templates/123'
- async.series(
- [
- expectAccess(this.user, url, 403),
- cb => this.publisher.setManagerIds([this.user._id], cb),
- expectAccess(this.user, url, 200)
- ],
- done
- )
- })
- it('handle templates without publisher', function(done) {
- MockV1Api.setTemplates({
- 456: {
- id: 456,
- title: '456 title',
- brand: { slug: null }
- }
- })
- const url = '/metrics/templates/456'
- async.series(
- [
- expectAccess(this.user, url, 403),
- this.user.ensureAdmin.bind(this.user),
- this.user.login.bind(this.user),
- expectAccess(this.user, url, 200)
- ],
- done
- )
- })
- it('handle templates not found', function(done) {
- const url = '/metrics/templates/789'
- async.series(
- [
- this.user.ensureAdmin.bind(this.user),
- this.user.login.bind(this.user),
- expectAccess(this.user, url, 404)
- ],
- done
- )
- })
- })
- describe('graph', function() {
- describe('admin', function() {
- it('allow admins only', function(done) {
- const url = '/graphs/foo?resource_type=admin'
- async.series(
- [
- this.user.login.bind(this.user),
- expectAccess(this.user, url, 403),
- this.user.ensureAdmin.bind(this.user),
- this.user.login.bind(this.user),
- expectAccess(this.user, url, 200)
- ],
- done
- )
- })
- it('handle missing resource type', function(done) {
- const url = '/graphs/foo'
- expectAccess(this.user, url, 404)(done)
- })
- it('handle incorrect resource type', function(done) {
- const url = '/graphs/foo?resource_type=evil'
- expectAccess(this.user, url, 404)(done)
- })
- })
- describe('template', function() {
- beforeEach(function(done) {
- this.publisher = new Publisher({})
- async.series(
- [
- this.publisher.ensureExists.bind(this.publisher),
- cb => this.user.login(cb)
- ],
- done
- )
- })
- it('get template graphs', function(done) {
- MockV1Api.setTemplates({
- 123: {
- id: 123,
- title: '123 title',
- brand: { slug: this.publisher.slug }
- }
- })
- const url = '/graphs/foo?resource_type=template&resource_id=123'
- async.series(
- [
- this.user.ensureAdmin.bind(this.user),
- this.user.login.bind(this.user),
- expectAccess(this.user, url, 200)
- ],
- done
- )
- })
- })
- })
- describe('admin metrics', function() {
- it('should not allow anonymous users', function(done) {
- expectAccess(this.user, '/metrics/admin', 302, /\/login/)(done)
- })
- it('should not allow all users', function(done) {
- async.series(
- [
- this.user.login.bind(this.user),
- expectAccess(this.user, '/metrics/admin', 403)
- ],
- done
- )
- })
- it('should allow admin users', function(done) {
- async.series(
- [
- this.user.ensureAdmin.bind(this.user),
- this.user.login.bind(this.user),
- expectAccess(this.user, '/metrics/admin', 200)
- ],
- done
- )
- })
- it('should allow users with staff access', function(done) {
- async.series(
- [
- cb => this.user.ensureStaffAccess('adminMetrics', cb),
- this.user.login.bind(this.user),
- expectAccess(this.user, '/metrics/admin', 200)
- ],
- done
- )
- })
- })
- })
- function expectAccess(user, url, status, pattern) {
- return callback => {
- user.request.get({ url }, (error, response, body) => {
- if (error) {
- return callback(error)
- }
- expect(response.statusCode).to.equal(status)
- if (pattern) {
- expect(body).to.match(pattern)
- }
- callback()
- })
- }
- }
|