| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- import { expect } from 'chai'
- import async from 'async'
- import User from './helpers/User.mjs'
- import Institution from './helpers/Institution.mjs'
- import Subscription from './helpers/Subscription.mjs'
- import Publisher from './helpers/Publisher.mjs'
- import sinon from 'sinon'
- import RecurlyClient from '../../../app/src/Features/Subscription/RecurlyClient.mjs'
- import Settings from '@overleaf/settings'
- import Features from '../../../app/src/infrastructure/Features.mjs'
- describe('UserMembershipAuthorization', function () {
- beforeEach(function (done) {
- if (!Features.hasFeature('saas')) {
- this.skip()
- }
- this.user = new User()
- sinon.stub(RecurlyClient.promises, 'getSubscription').resolves({})
- this.adminRolesEnabledStub = sinon.stub(Settings, 'adminRolesEnabled')
- this.adminRolesEnabledStub.value(true)
- this.adminPrivilegeAvailableStub = sinon.stub(
- Settings,
- 'adminPrivilegeAvailable'
- )
- this.adminPrivilegeAvailableStub.value(true)
- async.series([this.user.ensureUserExists.bind(this.user)], done)
- })
- afterEach(function () {
- if (!Features.hasFeature('saas')) {
- return
- }
- RecurlyClient.promises.getSubscription.restore()
- this.adminRolesEnabledStub.restore()
- this.adminPrivilegeAvailableStub.restore()
- })
- 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('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(async function () {
- this.institution = new Institution()
- await this.institution.ensureExists(this.institution)
- })
- describe('users management', function () {
- it('should allow managers', 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
- )
- })
- it('should allow admin users', 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.user.ensureAdmin(cb),
- cb => this.user.ensureAdminRole('sales', cb),
- this.user.login.bind(this.user),
- expectAccess(this.user, url, 200),
- ],
- done
- )
- })
- it('should not allow "sales" admin to add managers', function (done) {
- const url = `/manage/institutions/${this.institution.v1Id}/managers`
- async.series(
- [
- this.user.login.bind(this.user),
- expectPost(this.user, url, { email: this.user.email }, 403),
- cb => this.user.ensureAdmin(cb),
- cb => this.user.ensureAdminRole('sales', cb),
- this.user.login.bind(this.user),
- expectPost(this.user, url, { email: this.user.email }, 403),
- ],
- done
- )
- })
- it('should allow "engineering" admin to add managers', function (done) {
- const user = new User()
- const url = `/manage/institutions/${this.institution.v1Id}/managers`
- async.series(
- [
- user.ensureUserExists.bind(user),
- this.user.login.bind(this.user),
- expectPost(this.user, url, { email: user.email }, 403),
- cb => this.user.ensureAdmin(cb),
- cb => this.user.ensureAdminRole('engineering', cb),
- this.user.login.bind(this.user),
- expectPost(this.user, url, { email: user.email }, 200),
- cb => this.institution.setManagerIds([], cb),
- ],
- done
- )
- })
- })
- describe('creation', function () {
- it('should allow admin 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.ensureAdmin(cb),
- cb => this.user.ensureAdminRole('engineering', 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('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 admin only', function (done) {
- const url = `/manage/publishers/foo/managers`
- async.series(
- [
- this.user.login.bind(this.user),
- expectAccess(this.user, url, 404),
- cb => this.user.ensureAdmin(cb),
- cb => this.user.ensureAdminRole('engineering', cb),
- this.user.login.bind(this.user),
- expectAccess(this.user, url, 302, /\/create/),
- ],
- done
- )
- })
- it('should allow admin only', function (done) {
- const url = `/entities/publisher/create/foo`
- async.series(
- [
- expectAccess(this.user, url, 403),
- cb => this.user.ensureAdmin(cb),
- cb => this.user.ensureAdminRole('engineering', cb),
- this.user.login.bind(this.user),
- expectAccess(this.user, url, 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()
- })
- }
- }
- function expectPost(user, url, body, status, pattern) {
- return callback => {
- user.request.post({ url, json: body }, (error, response, body) => {
- if (error) {
- return callback(error)
- }
- expect(response.statusCode).to.equal(status)
- if (pattern) {
- expect(body).to.match(pattern)
- }
- callback()
- })
- }
- }
|