2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
2020-12-15 05:23:54 -05:00
|
|
|
node/handle-callback-err,
|
2019-05-29 05:21:06 -04:00
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const path = require('path')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const modulePath = path.join(
|
|
|
|
__dirname,
|
|
|
|
'../../../../app/src/Features/TokenAccess/TokenAccessHandler'
|
|
|
|
)
|
|
|
|
const { expect } = require('chai')
|
2020-09-23 04:49:26 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('TokenAccessHandler', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-25 04:39:53 -05:00
|
|
|
this.token = 'abcdefabcdef'
|
2019-05-29 05:21:06 -04:00
|
|
|
this.projectId = ObjectId()
|
|
|
|
this.project = {
|
|
|
|
_id: this.projectId,
|
2020-02-25 04:39:53 -05:00
|
|
|
publicAccesLevel: 'tokenBased',
|
2021-04-27 03:52:58 -04:00
|
|
|
owner_ref: ObjectId(),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.userId = ObjectId()
|
|
|
|
this.req = {}
|
|
|
|
return (this.TokenAccessHandler = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2020-10-05 04:23:21 -04:00
|
|
|
mongodb: { ObjectId },
|
2019-05-29 05:21:06 -04:00
|
|
|
'../../models/Project': { Project: (this.Project = {}) },
|
2021-07-07 05:38:56 -04:00
|
|
|
'@overleaf/settings': (this.settings = {}),
|
2019-05-29 05:21:06 -04:00
|
|
|
'../V1/V1Api': (this.V1Api = {
|
2021-04-27 03:52:58 -04:00
|
|
|
request: sinon.stub(),
|
2020-02-25 04:39:53 -05:00
|
|
|
}),
|
2021-03-10 04:20:55 -05:00
|
|
|
crypto: (this.Crypto = require('crypto')),
|
|
|
|
'../Analytics/AnalyticsManager': (this.Analytics = {
|
2021-09-09 10:45:41 -04:00
|
|
|
recordEventForUser: sinon.stub(),
|
2021-04-27 03:52:58 -04:00
|
|
|
}),
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('getTokenType', function () {
|
|
|
|
it('should determine tokens correctly', function () {
|
2020-02-25 04:39:53 -05:00
|
|
|
const specs = {
|
|
|
|
abcdefabcdef: 'readOnly',
|
|
|
|
aaaaaabbbbbb: 'readOnly',
|
|
|
|
'54325aaaaaa': 'readAndWrite',
|
|
|
|
'54325aaaaaabbbbbb': 'readAndWrite',
|
|
|
|
'': null,
|
2021-04-27 03:52:58 -04:00
|
|
|
abc123def: null,
|
2020-02-25 04:39:53 -05:00
|
|
|
}
|
|
|
|
for (var token of Object.keys(specs)) {
|
|
|
|
expect(this.TokenAccessHandler.getTokenType(token)).to.equal(
|
|
|
|
specs[token]
|
|
|
|
)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
2020-02-25 04:39:53 -05:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('getProjectByReadOnlyToken', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-25 04:39:53 -05:00
|
|
|
this.token = 'abcdefabcdef'
|
|
|
|
this.Project.findOne = sinon.stub().callsArgWith(2, null, this.project)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should get the project', function (done) {
|
2020-02-25 04:39:53 -05:00
|
|
|
this.TokenAccessHandler.getProjectByReadOnlyToken(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.token,
|
|
|
|
(err, project) => {
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(project).to.exist
|
2020-02-25 04:39:53 -05:00
|
|
|
expect(this.Project.findOne.callCount).to.equal(1)
|
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('getProjectByReadAndWriteToken', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-25 04:39:53 -05:00
|
|
|
sinon.spy(this.Crypto, 'timingSafeEqual')
|
|
|
|
this.token = '1234abcdefabcdef'
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project.tokens = {
|
|
|
|
readAndWrite: this.token,
|
2021-04-27 03:52:58 -04:00
|
|
|
readAndWritePrefix: '1234',
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-02-25 04:39:53 -05:00
|
|
|
this.Project.findOne = sinon.stub().callsArgWith(2, null, this.project)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
afterEach(function () {
|
2020-02-25 04:39:53 -05:00
|
|
|
this.Crypto.timingSafeEqual.restore()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should get the project and do timing-safe comparison', function (done) {
|
2020-02-25 04:39:53 -05:00
|
|
|
this.TokenAccessHandler.getProjectByReadAndWriteToken(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.token,
|
|
|
|
(err, project) => {
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(project).to.exist
|
2020-02-25 04:39:53 -05:00
|
|
|
expect(this.Crypto.timingSafeEqual.callCount).to.equal(1)
|
|
|
|
expect(
|
|
|
|
this.Crypto.timingSafeEqual.calledWith(Buffer.from(this.token))
|
|
|
|
).to.equal(true)
|
|
|
|
expect(this.Project.findOne.callCount).to.equal(1)
|
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('addReadOnlyUserToProject', function () {
|
|
|
|
beforeEach(function () {
|
2020-11-03 04:19:05 -05:00
|
|
|
return (this.Project.updateOne = sinon.stub().callsArgWith(2, null))
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should call Project.updateOne', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.TokenAccessHandler.addReadOnlyUserToProject(
|
|
|
|
this.userId,
|
|
|
|
this.projectId,
|
|
|
|
err => {
|
2020-11-03 04:19:05 -05:00
|
|
|
expect(this.Project.updateOne.callCount).to.equal(1)
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(
|
2020-11-03 04:19:05 -05:00
|
|
|
this.Project.updateOne.calledWith({
|
2021-04-27 03:52:58 -04:00
|
|
|
_id: this.projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
).to.equal(true)
|
|
|
|
expect(
|
2020-11-03 04:19:05 -05:00
|
|
|
this.Project.updateOne.lastCall.args[1].$addToSet
|
2019-05-29 05:21:06 -04:00
|
|
|
).to.have.keys('tokenAccessReadOnly_refs')
|
2021-03-10 04:20:55 -05:00
|
|
|
sinon.assert.calledWith(
|
2021-09-09 10:45:41 -04:00
|
|
|
this.Analytics.recordEventForUser,
|
2021-03-10 04:20:55 -05:00
|
|
|
this.userId,
|
|
|
|
'project-joined',
|
|
|
|
{ mode: 'read-only' }
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not produce an error', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.TokenAccessHandler.addReadOnlyUserToProject(
|
|
|
|
this.userId,
|
|
|
|
this.projectId,
|
|
|
|
err => {
|
|
|
|
expect(err).to.not.exist
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when Project.updateOne produces an error', function () {
|
|
|
|
beforeEach(function () {
|
2020-11-03 04:19:05 -05:00
|
|
|
return (this.Project.updateOne = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, new Error('woops')))
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should produce an error', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.TokenAccessHandler.addReadOnlyUserToProject(
|
|
|
|
this.userId,
|
|
|
|
this.projectId,
|
|
|
|
err => {
|
|
|
|
expect(err).to.exist
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('addReadAndWriteUserToProject', function () {
|
|
|
|
beforeEach(function () {
|
2020-11-03 04:19:05 -05:00
|
|
|
return (this.Project.updateOne = sinon.stub().callsArgWith(2, null))
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should call Project.updateOne', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.TokenAccessHandler.addReadAndWriteUserToProject(
|
|
|
|
this.userId,
|
|
|
|
this.projectId,
|
|
|
|
err => {
|
2020-11-03 04:19:05 -05:00
|
|
|
expect(this.Project.updateOne.callCount).to.equal(1)
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(
|
2020-11-03 04:19:05 -05:00
|
|
|
this.Project.updateOne.calledWith({
|
2021-04-27 03:52:58 -04:00
|
|
|
_id: this.projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
).to.equal(true)
|
|
|
|
expect(
|
2020-11-03 04:19:05 -05:00
|
|
|
this.Project.updateOne.lastCall.args[1].$addToSet
|
2019-05-29 05:21:06 -04:00
|
|
|
).to.have.keys('tokenAccessReadAndWrite_refs')
|
2021-03-10 04:20:55 -05:00
|
|
|
sinon.assert.calledWith(
|
2021-09-09 10:45:41 -04:00
|
|
|
this.Analytics.recordEventForUser,
|
2021-03-10 04:20:55 -05:00
|
|
|
this.userId,
|
|
|
|
'project-joined',
|
|
|
|
{ mode: 'read-write' }
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not produce an error', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.TokenAccessHandler.addReadAndWriteUserToProject(
|
|
|
|
this.userId,
|
|
|
|
this.projectId,
|
|
|
|
err => {
|
|
|
|
expect(err).to.not.exist
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when Project.updateOne produces an error', function () {
|
|
|
|
beforeEach(function () {
|
2020-11-03 04:19:05 -05:00
|
|
|
return (this.Project.updateOne = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, new Error('woops')))
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should produce an error', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.TokenAccessHandler.addReadAndWriteUserToProject(
|
|
|
|
this.userId,
|
|
|
|
this.projectId,
|
|
|
|
err => {
|
|
|
|
expect(err).to.exist
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('grantSessionTokenAccess', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return (this.req = { session: {}, headers: {} })
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should add the token to the session', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.TokenAccessHandler.grantSessionTokenAccess(
|
|
|
|
this.req,
|
|
|
|
this.projectId,
|
|
|
|
this.token
|
|
|
|
)
|
|
|
|
expect(
|
|
|
|
this.req.session.anonTokenAccess[this.projectId.toString()]
|
|
|
|
).to.equal(this.token)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('validateTokenForAnonymousAccess', function () {
|
|
|
|
describe('when a read-only project is found', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-25 04:39:53 -05:00
|
|
|
this.TokenAccessHandler.getTokenType = sinon.stub().returns('readOnly')
|
|
|
|
this.TokenAccessHandler.getProjectByToken = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
2020-02-25 04:39:53 -05:00
|
|
|
.callsArgWith(2, null, this.project)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should try to find projects with both kinds of token', function (done) {
|
2020-02-25 04:39:53 -05:00
|
|
|
return this.TokenAccessHandler.validateTokenForAnonymousAccess(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.projectId,
|
|
|
|
this.token,
|
|
|
|
(err, allowed) => {
|
|
|
|
expect(
|
2020-02-25 04:39:53 -05:00
|
|
|
this.TokenAccessHandler.getProjectByToken.callCount
|
2019-05-29 05:21:06 -04:00
|
|
|
).to.equal(1)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should allow read-only access', function (done) {
|
2020-02-25 04:39:53 -05:00
|
|
|
return this.TokenAccessHandler.validateTokenForAnonymousAccess(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.projectId,
|
|
|
|
this.token,
|
|
|
|
(err, rw, ro) => {
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(rw).to.equal(false)
|
|
|
|
expect(ro).to.equal(true)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when a read-and-write project is found', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-25 04:39:53 -05:00
|
|
|
this.TokenAccessHandler.getTokenType = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
2020-02-25 04:39:53 -05:00
|
|
|
.returns('readAndWrite')
|
|
|
|
this.TokenAccessHandler.getProjectByToken = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
2020-02-25 04:39:53 -05:00
|
|
|
.callsArgWith(2, null, this.project)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when Anonymous token access is not enabled', function (done) {
|
|
|
|
beforeEach(function () {
|
2020-02-25 04:39:53 -05:00
|
|
|
this.TokenAccessHandler.ANONYMOUS_READ_AND_WRITE_ENABLED = false
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should try to find projects with both kinds of token', function (done) {
|
2020-02-25 04:39:53 -05:00
|
|
|
return this.TokenAccessHandler.validateTokenForAnonymousAccess(
|
|
|
|
this.projectId,
|
|
|
|
this.token,
|
|
|
|
(err, rw, ro) => {
|
|
|
|
expect(
|
|
|
|
this.TokenAccessHandler.getProjectByToken.callCount
|
|
|
|
).to.equal(1)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not allow read-and-write access', function (done) {
|
2020-02-25 04:39:53 -05:00
|
|
|
return this.TokenAccessHandler.validateTokenForAnonymousAccess(
|
|
|
|
this.projectId,
|
|
|
|
this.token,
|
|
|
|
(err, rw, ro) => {
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(rw).to.equal(false)
|
|
|
|
expect(ro).to.equal(false)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when anonymous token access is enabled', function (done) {
|
|
|
|
beforeEach(function () {
|
2020-02-25 04:39:53 -05:00
|
|
|
this.TokenAccessHandler.ANONYMOUS_READ_AND_WRITE_ENABLED = true
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should try to find projects with both kinds of token', function (done) {
|
2020-02-25 04:39:53 -05:00
|
|
|
return this.TokenAccessHandler.validateTokenForAnonymousAccess(
|
|
|
|
this.projectId,
|
|
|
|
this.token,
|
|
|
|
(err, rw, ro) => {
|
|
|
|
expect(
|
|
|
|
this.TokenAccessHandler.getProjectByToken.callCount
|
|
|
|
).to.equal(1)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should allow read-and-write access', function (done) {
|
2020-02-25 04:39:53 -05:00
|
|
|
return this.TokenAccessHandler.validateTokenForAnonymousAccess(
|
|
|
|
this.projectId,
|
|
|
|
this.token,
|
|
|
|
(err, rw, ro) => {
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(rw).to.equal(true)
|
|
|
|
expect(ro).to.equal(false)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when no project is found', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-25 04:39:53 -05:00
|
|
|
this.TokenAccessHandler.getProjectByToken = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
2020-02-25 04:39:53 -05:00
|
|
|
.callsArgWith(2, null, null, null)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should try to find projects with both kinds of token', function (done) {
|
2020-02-25 04:39:53 -05:00
|
|
|
return this.TokenAccessHandler.validateTokenForAnonymousAccess(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.projectId,
|
|
|
|
this.token,
|
|
|
|
(err, allowed) => {
|
|
|
|
expect(
|
2020-02-25 04:39:53 -05:00
|
|
|
this.TokenAccessHandler.getProjectByToken.callCount
|
2019-05-29 05:21:06 -04:00
|
|
|
).to.equal(1)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not allow any access', function (done) {
|
2020-02-25 04:39:53 -05:00
|
|
|
return this.TokenAccessHandler.validateTokenForAnonymousAccess(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.projectId,
|
|
|
|
this.token,
|
|
|
|
(err, rw, ro) => {
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(rw).to.equal(false)
|
|
|
|
expect(ro).to.equal(false)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when findProject produces an error', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-25 04:39:53 -05:00
|
|
|
this.TokenAccessHandler.getProjectByToken = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
2020-02-25 04:39:53 -05:00
|
|
|
.callsArgWith(2, new Error('woops'))
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should try to find projects with both kinds of token', function (done) {
|
2020-02-25 04:39:53 -05:00
|
|
|
return this.TokenAccessHandler.validateTokenForAnonymousAccess(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.projectId,
|
|
|
|
this.token,
|
|
|
|
(err, allowed) => {
|
|
|
|
expect(
|
2020-02-25 04:39:53 -05:00
|
|
|
this.TokenAccessHandler.getProjectByToken.callCount
|
2019-05-29 05:21:06 -04:00
|
|
|
).to.equal(1)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should produce an error and not allow access', function (done) {
|
2020-02-25 04:39:53 -05:00
|
|
|
return this.TokenAccessHandler.validateTokenForAnonymousAccess(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.projectId,
|
|
|
|
this.token,
|
|
|
|
(err, rw, ro) => {
|
|
|
|
expect(err).to.exist
|
|
|
|
expect(err).to.be.instanceof(Error)
|
|
|
|
expect(rw).to.equal(undefined)
|
|
|
|
expect(ro).to.equal(undefined)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when project is not set to token-based access', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return (this.project.publicAccesLevel = 'private')
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('for read-and-write project', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-25 04:39:53 -05:00
|
|
|
this.TokenAccessHandler.getTokenType = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
2020-02-25 04:39:53 -05:00
|
|
|
.returns('readAndWrite')
|
|
|
|
this.TokenAccessHandler.getProjectByToken = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
2020-02-25 04:39:53 -05:00
|
|
|
.callsArgWith(2, null, this.project)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not allow any access', function (done) {
|
2020-02-25 04:39:53 -05:00
|
|
|
return this.TokenAccessHandler.validateTokenForAnonymousAccess(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.projectId,
|
|
|
|
this.token,
|
|
|
|
(err, rw, ro) => {
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(rw).to.equal(false)
|
|
|
|
expect(ro).to.equal(false)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('for read-only project', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-25 04:39:53 -05:00
|
|
|
this.TokenAccessHandler.getTokenType = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
2020-02-25 04:39:53 -05:00
|
|
|
.returns('readOnly')
|
|
|
|
this.TokenAccessHandler.getProjectByToken = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
2020-02-25 04:39:53 -05:00
|
|
|
.callsArgWith(2, null, this.project)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not allow any access', function (done) {
|
2020-02-25 04:39:53 -05:00
|
|
|
return this.TokenAccessHandler.validateTokenForAnonymousAccess(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.projectId,
|
|
|
|
this.token,
|
2020-02-25 04:39:53 -05:00
|
|
|
(err, rw, ro) => {
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(rw).to.equal(false)
|
|
|
|
expect(ro).to.equal(false)
|
2019-05-29 05:21:06 -04:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2020-02-25 04:39:53 -05:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('with nothing', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-25 04:39:53 -05:00
|
|
|
this.TokenAccessHandler.getProjectByToken = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, null, null)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not allow any access', function (done) {
|
2020-02-25 04:39:53 -05:00
|
|
|
return this.TokenAccessHandler.validateTokenForAnonymousAccess(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.projectId,
|
2020-02-25 04:39:53 -05:00
|
|
|
null,
|
2019-05-29 05:21:06 -04:00
|
|
|
(err, rw, ro) => {
|
|
|
|
expect(err).to.not.exist
|
|
|
|
expect(rw).to.equal(false)
|
|
|
|
expect(ro).to.equal(false)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('protectTokens', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return (this.project = {
|
|
|
|
tokens: {
|
|
|
|
readAndWrite: 'rw',
|
|
|
|
readOnly: 'ro',
|
2021-04-27 03:52:58 -04:00
|
|
|
readAndWritePrefix: 'pre',
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should hide write token from read-only user', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.TokenAccessHandler.protectTokens(this.project, 'readOnly')
|
|
|
|
expect(this.project.tokens.readAndWrite).to.equal('')
|
|
|
|
expect(this.project.tokens.readAndWritePrefix).to.equal('')
|
|
|
|
return expect(this.project.tokens.readOnly).to.equal('ro')
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should hide read token from read-write user', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.TokenAccessHandler.protectTokens(this.project, 'readAndWrite')
|
|
|
|
expect(this.project.tokens.readAndWrite).to.equal('rw')
|
|
|
|
return expect(this.project.tokens.readOnly).to.equal('')
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should leave tokens in place for owner', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.TokenAccessHandler.protectTokens(this.project, 'owner')
|
|
|
|
expect(this.project.tokens.readAndWrite).to.equal('rw')
|
|
|
|
return expect(this.project.tokens.readOnly).to.equal('ro')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('getDocPublishedInfo', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return (this.callback = sinon.stub())
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when v1 api not set', function () {
|
|
|
|
beforeEach(function () {
|
2019-11-25 05:39:14 -05:00
|
|
|
this.settings.apis = { v1: undefined }
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.TokenAccessHandler.getV1DocPublishedInfo(
|
|
|
|
this.token,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not check access and return default info', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(this.V1Api.request.called).to.equal(false)
|
|
|
|
return expect(
|
|
|
|
this.callback.calledWith(null, {
|
2021-04-27 03:52:58 -04:00
|
|
|
allow: true,
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
).to.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when v1 api is set', function () {
|
|
|
|
beforeEach(function () {
|
2019-11-25 05:39:14 -05:00
|
|
|
return (this.settings.apis = { v1: { url: 'v1Url' } })
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('on V1Api.request success', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.V1Api.request = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, null, 'mock-data')
|
|
|
|
return this.TokenAccessHandler.getV1DocPublishedInfo(
|
|
|
|
this.token,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return response body', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(
|
|
|
|
this.V1Api.request.calledWith({
|
2021-04-27 03:52:58 -04:00
|
|
|
url: `/api/v1/sharelatex/docs/${this.token}/is_published`,
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
).to.equal(true)
|
|
|
|
return expect(this.callback.calledWith(null, 'mock-data')).to.equal(
|
|
|
|
true
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('on V1Api.request error', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.V1Api.request = sinon.stub().callsArgWith(1, 'error')
|
|
|
|
return this.TokenAccessHandler.getV1DocPublishedInfo(
|
|
|
|
this.token,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should callback with error', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return expect(this.callback.calledWith('error')).to.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('getV1DocInfo', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return (this.callback = sinon.stub())
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when v1 api not set', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.TokenAccessHandler.getV1DocInfo(
|
|
|
|
this.token,
|
|
|
|
this.v2UserId,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not check access and return default info', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(this.V1Api.request.called).to.equal(false)
|
|
|
|
return expect(
|
|
|
|
this.callback.calledWith(null, {
|
|
|
|
exists: true,
|
2021-04-27 03:52:58 -04:00
|
|
|
exported: false,
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
).to.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when v1 api is set', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return (this.settings.apis = { v1: 'v1' })
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('on V1Api.request success', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.V1Api.request = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, null, 'mock-data')
|
|
|
|
return this.TokenAccessHandler.getV1DocInfo(
|
|
|
|
this.token,
|
|
|
|
this.v2UserId,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return response body', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(
|
|
|
|
this.V1Api.request.calledWith({
|
2021-06-24 04:27:19 -04:00
|
|
|
url: `/api/v1/sharelatex/docs/${this.token}/info`,
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
).to.equal(true)
|
|
|
|
return expect(this.callback.calledWith(null, 'mock-data')).to.equal(
|
|
|
|
true
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('on V1Api.request error', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.V1Api.request = sinon.stub().callsArgWith(1, 'error')
|
|
|
|
return this.TokenAccessHandler.getV1DocInfo(
|
|
|
|
this.token,
|
|
|
|
this.v2UserId,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should callback with error', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return expect(this.callback.calledWith('error')).to.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|