mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
da6711dc99
Authorization flags for metrics GitOrigin-RevId: 651587c11317bfc8bb7b1e8143e8c2c820683cb5
212 lines
8.2 KiB
CoffeeScript
212 lines
8.2 KiB
CoffeeScript
chai = require('chai')
|
|
should = chai.should()
|
|
expect = require('chai').expect
|
|
sinon = require('sinon')
|
|
assertCalledWith = sinon.assert.calledWith
|
|
assertNotCalled = sinon.assert.notCalled
|
|
ObjectId = require("../../../../app/js/infrastructure/mongojs").ObjectId
|
|
modulePath = "../../../../app/js/Features/UserMembership/UserMembershipHandler"
|
|
SandboxedModule = require("sandboxed-module")
|
|
Errors = require("../../../../app/js/Features/Errors/Errors")
|
|
EntityConfigs = require("../../../../app/js/Features/UserMembership/UserMembershipEntityConfigs")
|
|
|
|
describe 'UserMembershipHandler', ->
|
|
beforeEach ->
|
|
@user = _id: ObjectId()
|
|
@newUser = _id: ObjectId(), email: 'new-user-email@foo.bar'
|
|
@fakeEntityId = ObjectId()
|
|
@subscription =
|
|
_id: 'mock-subscription-id'
|
|
groupPlan: true
|
|
membersLimit: 10
|
|
member_ids: [ObjectId(), ObjectId()]
|
|
manager_ids: [ObjectId()]
|
|
invited_emails: ['mock-email-1@foo.com']
|
|
teamInvites: [{ email: 'mock-email-1@bar.com' }]
|
|
update: sinon.stub().yields(null)
|
|
@institution =
|
|
_id: 'mock-institution-id'
|
|
v1Id: 123
|
|
managerIds: [ObjectId(), ObjectId(), ObjectId()]
|
|
update: sinon.stub().yields(null)
|
|
@publisher =
|
|
_id: 'mock-publisher-id'
|
|
slug: 'slug'
|
|
managerIds: [ObjectId(), ObjectId()]
|
|
update: sinon.stub().yields(null)
|
|
|
|
@UserMembershipViewModel =
|
|
buildAsync: sinon.stub().yields(null, { _id: 'mock-member-id'})
|
|
build: sinon.stub().returns(@newUser)
|
|
@UserGetter =
|
|
getUserByAnyEmail: sinon.stub().yields(null, @newUser)
|
|
@Institution =
|
|
findOne: sinon.stub().yields(null, @institution)
|
|
@Subscription =
|
|
findOne: sinon.stub().yields(null, @subscription)
|
|
@Publisher =
|
|
findOne: sinon.stub().yields(null, @publisher)
|
|
create: sinon.stub().yields(null, @publisher)
|
|
@UserMembershipHandler = SandboxedModule.require modulePath, requires:
|
|
'./UserMembershipViewModel': @UserMembershipViewModel
|
|
'../User/UserGetter': @UserGetter
|
|
'../Errors/Errors': Errors
|
|
'../../models/Institution': Institution: @Institution
|
|
'../../models/Subscription': Subscription: @Subscription
|
|
'../../models/Publisher': Publisher: @Publisher
|
|
'logger-sharelatex':
|
|
log: ->
|
|
err: ->
|
|
|
|
describe 'getEntity', ->
|
|
describe 'group subscriptions', ->
|
|
it 'get subscription', (done) ->
|
|
@UserMembershipHandler.getEntity @fakeEntityId, EntityConfigs.group, @user, null, (error, subscription) =>
|
|
should.not.exist(error)
|
|
expectedQuery =
|
|
groupPlan: true
|
|
_id: @fakeEntityId
|
|
manager_ids: ObjectId(@user._id)
|
|
assertCalledWith(@Subscription.findOne, expectedQuery)
|
|
expect(subscription).to.equal @subscription
|
|
expect(subscription.membersLimit).to.equal 10
|
|
done()
|
|
|
|
it 'get for admin', (done) ->
|
|
@UserMembershipHandler.getEntity @fakeEntityId, EntityConfigs.group, { isAdmin: true }, null, (error, subscription) =>
|
|
should.not.exist(error)
|
|
expectedQuery =
|
|
groupPlan: true
|
|
_id: @fakeEntityId
|
|
assertCalledWith(@Subscription.findOne, expectedQuery)
|
|
done()
|
|
|
|
it 'get with staffAccess field', (done) ->
|
|
@UserMembershipHandler.getEntity @fakeEntityId, EntityConfigs.group, { staffAccess: {institutionMetrics: true}}, 'institutionMetrics', (error, subscription) =>
|
|
should.not.exist(error)
|
|
expectedQuery =
|
|
groupPlan: true
|
|
_id: @fakeEntityId
|
|
assertCalledWith(@Subscription.findOne, expectedQuery)
|
|
done()
|
|
|
|
it 'handle error', (done) ->
|
|
@Subscription.findOne.yields(new Error('some error'))
|
|
@UserMembershipHandler.getEntity @fakeEntityId, EntityConfigs.group, @user._id, null, (error, subscription) =>
|
|
should.exist(error)
|
|
done()
|
|
|
|
describe 'getEntityWithoutAuthorizationCheck', ->
|
|
it 'get publisher', (done) ->
|
|
@UserMembershipHandler.getEntityWithoutAuthorizationCheck @fakeEntityId, EntityConfigs.publisher, (error, subscription) =>
|
|
should.not.exist(error)
|
|
expectedQuery = slug: @fakeEntityId
|
|
assertCalledWith(@Publisher.findOne, expectedQuery)
|
|
expect(subscription).to.equal @publisher
|
|
done()
|
|
|
|
describe 'institutions', ->
|
|
it 'get institution', (done) ->
|
|
@UserMembershipHandler.getEntity @institution.v1Id, EntityConfigs.institution, @user, null, (error, institution) =>
|
|
should.not.exist(error)
|
|
expectedQuery = v1Id: @institution.v1Id, managerIds: ObjectId(@user._id)
|
|
assertCalledWith(@Institution.findOne, expectedQuery)
|
|
expect(institution).to.equal @institution
|
|
done()
|
|
|
|
it 'handle errors', (done) ->
|
|
@Institution.findOne.yields(new Error('nope'))
|
|
@UserMembershipHandler.getEntity @fakeEntityId, EntityConfigs.institution, @user._id, null, (error, institution) =>
|
|
should.exist(error)
|
|
expect(error).to.not.be.an.instanceof(Errors.NotFoundError)
|
|
done()
|
|
|
|
describe 'publishers', ->
|
|
it 'get publisher', (done) ->
|
|
@UserMembershipHandler.getEntity @publisher.slug, EntityConfigs.publisher, @user, null, (error, institution) =>
|
|
should.not.exist(error)
|
|
expectedQuery = slug: @publisher.slug, managerIds: ObjectId(@user._id)
|
|
assertCalledWith(@Publisher.findOne, expectedQuery)
|
|
expect(institution).to.equal @publisher
|
|
done()
|
|
|
|
describe 'getUsers', ->
|
|
describe 'group', ->
|
|
it 'build view model for all users', (done) ->
|
|
@UserMembershipHandler.getUsers @subscription, EntityConfigs.group, (error, users) =>
|
|
expectedCallcount =
|
|
@subscription.member_ids.length +
|
|
@subscription.invited_emails.length +
|
|
@subscription.teamInvites.length
|
|
expect(@UserMembershipViewModel.buildAsync.callCount).to.equal expectedCallcount
|
|
done()
|
|
|
|
describe 'group mamagers', ->
|
|
it 'build view model for all managers', (done) ->
|
|
@UserMembershipHandler.getUsers @subscription, EntityConfigs.groupManagers, (error, users) =>
|
|
expectedCallcount = @subscription.manager_ids.length
|
|
expect(@UserMembershipViewModel.buildAsync.callCount).to.equal expectedCallcount
|
|
done()
|
|
|
|
describe 'institution', ->
|
|
it 'build view model for all managers', (done) ->
|
|
@UserMembershipHandler.getUsers @institution, EntityConfigs.institution, (error, users) =>
|
|
expectedCallcount = @institution.managerIds.length
|
|
expect(@UserMembershipViewModel.buildAsync.callCount).to.equal expectedCallcount
|
|
done()
|
|
|
|
describe 'createEntity', ->
|
|
it 'creates publisher', (done) ->
|
|
@UserMembershipHandler.createEntity @fakeEntityId, EntityConfigs.publisher, (error, publisher) =>
|
|
should.not.exist(error)
|
|
assertCalledWith(@Publisher.create, slug: @fakeEntityId)
|
|
done()
|
|
|
|
describe 'addUser', ->
|
|
beforeEach ->
|
|
@email = @newUser.email
|
|
|
|
describe 'institution', ->
|
|
it 'get user', (done) ->
|
|
@UserMembershipHandler.addUser @institution, EntityConfigs.institution, @email, (error, user) =>
|
|
assertCalledWith(@UserGetter.getUserByAnyEmail, @email)
|
|
done()
|
|
|
|
it 'handle user not found', (done) ->
|
|
@UserGetter.getUserByAnyEmail.yields(null, null)
|
|
@UserMembershipHandler.addUser @institution, EntityConfigs.institution, @email, (error) =>
|
|
expect(error).to.exist
|
|
expect(error.userNotFound).to.equal true
|
|
done()
|
|
|
|
it 'handle user already added', (done) ->
|
|
@institution.managerIds.push(@newUser._id)
|
|
@UserMembershipHandler.addUser @institution, EntityConfigs.institution, @email, (error, users) =>
|
|
expect(error).to.exist
|
|
expect(error.alreadyAdded).to.equal true
|
|
done()
|
|
|
|
it 'add user to institution', (done) ->
|
|
@UserMembershipHandler.addUser @institution, EntityConfigs.institution, @email, (error, user) =>
|
|
assertCalledWith(@institution.update, { $addToSet: managerIds: @newUser._id })
|
|
done()
|
|
|
|
it 'return user view', (done) ->
|
|
@UserMembershipHandler.addUser @institution, EntityConfigs.institution, @email, (error, user) =>
|
|
user.should.equal @newUser
|
|
done()
|
|
|
|
describe 'removeUser', ->
|
|
describe 'institution', ->
|
|
it 'remove user from institution', (done) ->
|
|
@UserMembershipHandler.removeUser @institution, EntityConfigs.institution, @newUser._id, (error, user) =>
|
|
lastCall = @institution.update.lastCall
|
|
assertCalledWith(@institution.update, { $pull: managerIds: @newUser._id })
|
|
done()
|
|
|
|
it 'handle admin', (done) ->
|
|
@subscription.admin_id = @newUser._id
|
|
@UserMembershipHandler.removeUser @subscription, EntityConfigs.groupManagers, @newUser._id, (error, user) =>
|
|
expect(error).to.exist
|
|
expect(error.isAdmin).to.equal true
|
|
done()
|