TokenAccessControllerTests.mjs 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. import esmock from 'esmock'
  2. import sinon from 'sinon'
  3. import { expect } from 'chai'
  4. import mongodb from 'mongodb-legacy'
  5. import MockRequest from '../helpers/MockRequest.js'
  6. import MockResponse from '../helpers/MockResponse.js'
  7. import PrivilegeLevels from '../../../../app/src/Features/Authorization/PrivilegeLevels.js'
  8. import { getSafeRedirectPath } from '../../../../app/src/Features/Helpers/UrlHelper.js'
  9. const ObjectId = mongodb.ObjectId
  10. const MODULE_PATH =
  11. '../../../../app/src/Features/TokenAccess/TokenAccessController'
  12. describe('TokenAccessController', function () {
  13. beforeEach(async function () {
  14. this.token = 'abc123'
  15. this.user = { _id: new ObjectId() }
  16. this.project = {
  17. _id: new ObjectId(),
  18. name: 'test',
  19. tokenAccessReadAndWrite_refs: [],
  20. tokenAccessReadOnly_refs: [],
  21. }
  22. this.req = new MockRequest()
  23. this.res = new MockResponse()
  24. this.next = sinon.stub().returns()
  25. this.Settings = {
  26. siteUrl: 'https://www.dev-overleaf.com',
  27. adminPrivilegeAvailable: false,
  28. adminUrl: 'https://admin.dev-overleaf.com',
  29. adminDomains: ['overleaf.com'],
  30. }
  31. this.TokenAccessHandler = {
  32. TOKEN_TYPES: {
  33. READ_ONLY: 'readOnly',
  34. READ_AND_WRITE: 'readAndWrite',
  35. },
  36. isReadAndWriteToken: sinon.stub().returns(true),
  37. isReadOnlyToken: sinon.stub().returns(true),
  38. tokenAccessEnabledForProject: sinon.stub().returns(true),
  39. checkTokenHashPrefix: sinon.stub(),
  40. makeTokenUrl: sinon.stub().returns('/'),
  41. grantSessionTokenAccess: sinon.stub(),
  42. promises: {
  43. addReadOnlyUserToProject: sinon.stub().resolves(),
  44. getProjectByToken: sinon.stub().resolves(this.project),
  45. getV1DocPublishedInfo: sinon.stub().resolves({ allow: true }),
  46. getV1DocInfo: sinon.stub(),
  47. removeReadAndWriteUserFromProject: sinon.stub().resolves(),
  48. moveReadAndWriteUserToReadOnly: sinon.stub().resolves(),
  49. },
  50. }
  51. this.SessionManager = {
  52. getLoggedInUserId: sinon.stub().returns(this.user._id),
  53. getSessionUser: sinon.stub().returns(this.user._id),
  54. }
  55. this.AuthenticationController = {
  56. setRedirectInSession: sinon.stub(),
  57. }
  58. this.AuthorizationManager = {
  59. promises: {
  60. getPrivilegeLevelForProject: sinon
  61. .stub()
  62. .resolves(PrivilegeLevels.NONE),
  63. },
  64. }
  65. this.AuthorizationMiddleware = {}
  66. this.ProjectAuditLogHandler = {
  67. promises: {
  68. addEntry: sinon.stub().resolves(),
  69. },
  70. }
  71. this.SplitTestHandler = {
  72. promises: {
  73. getAssignment: sinon.stub().resolves({ variant: 'default' }),
  74. getAssignmentForUser: sinon.stub().resolves({ variant: 'default' }),
  75. },
  76. }
  77. this.CollaboratorsInviteHandler = {
  78. promises: {
  79. revokeInviteForUser: sinon.stub().resolves(),
  80. },
  81. }
  82. this.CollaboratorsHandler = {
  83. promises: {
  84. addUserIdToProject: sinon.stub().resolves(),
  85. setCollaboratorPrivilegeLevel: sinon.stub().resolves(),
  86. },
  87. }
  88. this.CollaboratorsGetter = {
  89. promises: {
  90. userIsReadWriteTokenMember: sinon.stub().resolves(),
  91. isUserInvitedReadWriteMemberOfProject: sinon.stub().resolves(),
  92. isUserInvitedMemberOfProject: sinon.stub().resolves(),
  93. },
  94. }
  95. this.EditorRealTimeController = { emitToRoom: sinon.stub() }
  96. this.ProjectGetter = {
  97. promises: {
  98. getProject: sinon.stub().resolves(this.project),
  99. },
  100. }
  101. this.AnalyticsManager = {
  102. recordEventForSession: sinon.stub(),
  103. recordEventForUserInBackground: sinon.stub(),
  104. }
  105. this.UserGetter = {
  106. promises: {
  107. getUser: sinon.stub().callsFake(async (userId, filter) => {
  108. if (userId === this.userId) {
  109. return this.user
  110. } else {
  111. return null
  112. }
  113. }),
  114. getUserEmail: sinon.stub().resolves(),
  115. getUserConfirmedEmails: sinon.stub().resolves(),
  116. },
  117. }
  118. this.LimitationsManager = {
  119. promises: {
  120. canAcceptEditCollaboratorInvite: sinon.stub().resolves(),
  121. },
  122. }
  123. this.TokenAccessController = await esmock.strict(MODULE_PATH, {
  124. '@overleaf/settings': this.Settings,
  125. '../../../../app/src/Features/TokenAccess/TokenAccessHandler':
  126. this.TokenAccessHandler,
  127. '../../../../app/src/Features/Authentication/AuthenticationController':
  128. this.AuthenticationController,
  129. '../../../../app/src/Features/Authentication/SessionManager':
  130. this.SessionManager,
  131. '../../../../app/src/Features/Authorization/AuthorizationManager':
  132. this.AuthorizationManager,
  133. '../../../../app/src/Features/Authorization/AuthorizationMiddleware':
  134. this.AuthorizationMiddleware,
  135. '../../../../app/src/Features/Project/ProjectAuditLogHandler':
  136. this.ProjectAuditLogHandler,
  137. '../../../../app/src/Features/SplitTests/SplitTestHandler':
  138. this.SplitTestHandler,
  139. '../../../../app/src/Features/Errors/Errors': (this.Errors = {
  140. NotFoundError: sinon.stub(),
  141. }),
  142. '../../../../app/src/Features/Collaborators/CollaboratorsHandler':
  143. this.CollaboratorsHandler,
  144. '../../../../app/src/Features/Collaborators/CollaboratorsInviteHandler':
  145. this.CollaboratorsInviteHandler,
  146. '../../../../app/src/Features/Collaborators/CollaboratorsGetter':
  147. this.CollaboratorsGetter,
  148. '../../../../app/src/Features/Editor/EditorRealTimeController':
  149. this.EditorRealTimeController,
  150. '../../../../app/src/Features/Project/ProjectGetter': this.ProjectGetter,
  151. '../../../../app/src/Features/Helpers/AsyncFormHelper':
  152. (this.AsyncFormHelper = {
  153. redirect: sinon.stub(),
  154. }),
  155. '../../../../app/src/Features/Helpers/AdminAuthorizationHelper':
  156. (this.AdminAuthorizationHelper = {
  157. canRedirectToAdminDomain: sinon.stub(),
  158. }),
  159. '../../../../app/src/Features/Helpers/UrlHelper': (this.UrlHelper = {
  160. getSafeAdminDomainRedirect: sinon
  161. .stub()
  162. .callsFake(
  163. path => `${this.Settings.adminUrl}${getSafeRedirectPath(path)}`
  164. ),
  165. }),
  166. '../../../../app/src/Features/Analytics/AnalyticsManager':
  167. this.AnalyticsManager,
  168. '../../../../app/src/Features/User/UserGetter': this.UserGetter,
  169. '../../../../app/src/Features/Subscription/LimitationsManager':
  170. this.LimitationsManager,
  171. })
  172. })
  173. describe('grantTokenAccessReadAndWrite', function () {
  174. beforeEach(function () {
  175. this.LimitationsManager.promises.canAcceptEditCollaboratorInvite.resolves(
  176. true
  177. )
  178. })
  179. describe('normal case (edit slot available)', function () {
  180. beforeEach(function (done) {
  181. this.LimitationsManager.promises.canAcceptEditCollaboratorInvite.resolves(
  182. true
  183. )
  184. this.req.params = { token: this.token }
  185. this.req.body = {
  186. confirmedByUser: true,
  187. tokenHashPrefix: '#prefix',
  188. }
  189. this.res.callback = done
  190. this.TokenAccessController.grantTokenAccessReadAndWrite(
  191. this.req,
  192. this.res,
  193. done
  194. )
  195. })
  196. it('adds the user as a read and write invited member', function () {
  197. expect(
  198. this.CollaboratorsHandler.promises.addUserIdToProject
  199. ).to.have.been.calledWith(
  200. this.project._id,
  201. undefined,
  202. this.user._id,
  203. PrivilegeLevels.READ_AND_WRITE
  204. )
  205. })
  206. it('writes a project audit log', function () {
  207. expect(
  208. this.ProjectAuditLogHandler.promises.addEntry
  209. ).to.have.been.calledWith(
  210. this.project._id,
  211. 'accept-via-link-sharing',
  212. this.user._id,
  213. this.req.ip,
  214. { privileges: 'readAndWrite' }
  215. )
  216. })
  217. it('records a project-joined event for the user', function () {
  218. expect(
  219. this.AnalyticsManager.recordEventForUserInBackground
  220. ).to.have.been.calledWith(this.user._id, 'project-joined', {
  221. mode: 'read-write',
  222. projectId: this.project._id.toString(),
  223. })
  224. })
  225. it('emits a project membership changed event', function () {
  226. expect(
  227. this.EditorRealTimeController.emitToRoom
  228. ).to.have.been.calledWith(
  229. this.project._id,
  230. 'project:membership:changed',
  231. { members: true, invites: true }
  232. )
  233. })
  234. it('checks token hash', function () {
  235. expect(
  236. this.TokenAccessHandler.checkTokenHashPrefix
  237. ).to.have.been.calledWith(
  238. this.token,
  239. '#prefix',
  240. 'readAndWrite',
  241. this.user._id,
  242. { projectId: this.project._id, action: 'continue' }
  243. )
  244. })
  245. })
  246. describe('when there are no edit collaborator slots available', function () {
  247. beforeEach(function (done) {
  248. this.LimitationsManager.promises.canAcceptEditCollaboratorInvite.resolves(
  249. false
  250. )
  251. this.req.params = { token: this.token }
  252. this.req.body = {
  253. confirmedByUser: true,
  254. tokenHashPrefix: '#prefix',
  255. }
  256. this.res.callback = done
  257. this.TokenAccessController.grantTokenAccessReadAndWrite(
  258. this.req,
  259. this.res,
  260. done
  261. )
  262. })
  263. it('adds the user as a read only invited member instead (pendingEditor)', function () {
  264. expect(
  265. this.CollaboratorsHandler.promises.addUserIdToProject
  266. ).to.have.been.calledWith(
  267. this.project._id,
  268. undefined,
  269. this.user._id,
  270. PrivilegeLevels.READ_ONLY,
  271. { pendingEditor: true }
  272. )
  273. })
  274. it('writes a project audit log', function () {
  275. expect(
  276. this.ProjectAuditLogHandler.promises.addEntry
  277. ).to.have.been.calledWith(
  278. this.project._id,
  279. 'accept-via-link-sharing',
  280. this.user._id,
  281. this.req.ip,
  282. { privileges: 'readOnly', pendingEditor: true }
  283. )
  284. })
  285. it('records a project-joined event for the user', function () {
  286. expect(
  287. this.AnalyticsManager.recordEventForUserInBackground
  288. ).to.have.been.calledWith(this.user._id, 'project-joined', {
  289. mode: 'read-only',
  290. projectId: this.project._id.toString(),
  291. pendingEditor: true,
  292. })
  293. })
  294. it('emits a project membership changed event', function () {
  295. expect(
  296. this.EditorRealTimeController.emitToRoom
  297. ).to.have.been.calledWith(
  298. this.project._id,
  299. 'project:membership:changed',
  300. { members: true, invites: true }
  301. )
  302. })
  303. it('checks token hash', function () {
  304. expect(
  305. this.TokenAccessHandler.checkTokenHashPrefix
  306. ).to.have.been.calledWith(
  307. this.token,
  308. '#prefix',
  309. 'readAndWrite',
  310. this.user._id,
  311. { projectId: this.project._id, action: 'continue' }
  312. )
  313. })
  314. })
  315. describe('when the access was already granted', function () {
  316. beforeEach(function (done) {
  317. this.project.tokenAccessReadAndWrite_refs.push(this.user._id)
  318. this.req.params = { token: this.token }
  319. this.req.body = { confirmedByUser: true }
  320. this.res.callback = done
  321. this.TokenAccessController.grantTokenAccessReadAndWrite(
  322. this.req,
  323. this.res,
  324. done
  325. )
  326. })
  327. it('writes a project audit log', function () {
  328. expect(
  329. this.ProjectAuditLogHandler.promises.addEntry
  330. ).to.have.been.calledWith(
  331. this.project._id,
  332. 'accept-via-link-sharing',
  333. this.user._id,
  334. this.req.ip,
  335. { privileges: 'readAndWrite' }
  336. )
  337. })
  338. it('checks token hash', function () {
  339. expect(
  340. this.TokenAccessHandler.checkTokenHashPrefix
  341. ).to.have.been.calledWith(
  342. this.token,
  343. undefined,
  344. 'readAndWrite',
  345. this.user._id,
  346. { projectId: this.project._id, action: 'continue' }
  347. )
  348. })
  349. })
  350. describe('hash prefix missing in request', function () {
  351. beforeEach(function (done) {
  352. this.req.params = { token: this.token }
  353. this.req.body = { confirmedByUser: true }
  354. this.res.callback = done
  355. this.TokenAccessController.grantTokenAccessReadAndWrite(
  356. this.req,
  357. this.res,
  358. done
  359. )
  360. })
  361. it('adds the user as a read and write invited member', function () {
  362. expect(
  363. this.CollaboratorsHandler.promises.addUserIdToProject
  364. ).to.have.been.calledWith(
  365. this.project._id,
  366. undefined,
  367. this.user._id,
  368. PrivilegeLevels.READ_AND_WRITE
  369. )
  370. })
  371. it('checks the hash prefix', function () {
  372. expect(
  373. this.TokenAccessHandler.checkTokenHashPrefix
  374. ).to.have.been.calledWith(
  375. this.token,
  376. undefined,
  377. 'readAndWrite',
  378. this.user._id,
  379. { projectId: this.project._id, action: 'continue' }
  380. )
  381. })
  382. })
  383. describe('user is owner of project', function () {
  384. beforeEach(function (done) {
  385. this.AuthorizationManager.promises.getPrivilegeLevelForProject.returns(
  386. PrivilegeLevels.OWNER
  387. )
  388. this.req.params = { token: this.token }
  389. this.req.body = {}
  390. this.res.callback = done
  391. this.TokenAccessController.grantTokenAccessReadAndWrite(
  392. this.req,
  393. this.res,
  394. done
  395. )
  396. })
  397. it('checks token hash and includes log data', function () {
  398. expect(
  399. this.TokenAccessHandler.checkTokenHashPrefix
  400. ).to.have.been.calledWith(
  401. this.token,
  402. undefined,
  403. 'readAndWrite',
  404. this.user._id,
  405. {
  406. projectId: this.project._id,
  407. action: 'user already has higher or same privilege',
  408. }
  409. )
  410. })
  411. })
  412. describe('when user is not logged in', function () {
  413. beforeEach(function () {
  414. this.SessionManager.getLoggedInUserId.returns(null)
  415. this.req.params = { token: this.token }
  416. this.req.body = { tokenHashPrefix: '#prefix' }
  417. })
  418. describe('ANONYMOUS_READ_AND_WRITE_ENABLED is undefined', function () {
  419. beforeEach(function (done) {
  420. this.res.callback = done
  421. this.TokenAccessController.grantTokenAccessReadAndWrite(
  422. this.req,
  423. this.res,
  424. done
  425. )
  426. })
  427. it('redirects to restricted', function () {
  428. expect(this.res.json).to.have.been.calledWith({
  429. redirect: '/restricted',
  430. anonWriteAccessDenied: true,
  431. })
  432. })
  433. it('checks the hash prefix and includes log data', function () {
  434. expect(
  435. this.TokenAccessHandler.checkTokenHashPrefix
  436. ).to.have.been.calledWith(
  437. this.token,
  438. '#prefix',
  439. 'readAndWrite',
  440. null,
  441. {
  442. action: 'denied anonymous read-and-write token access',
  443. }
  444. )
  445. })
  446. it('saves redirect URL with URL fragment', function () {
  447. expect(
  448. this.AuthenticationController.setRedirectInSession.lastCall.args[1]
  449. ).to.equal('/#prefix')
  450. })
  451. })
  452. describe('ANONYMOUS_READ_AND_WRITE_ENABLED is true', function () {
  453. beforeEach(function (done) {
  454. this.TokenAccessHandler.ANONYMOUS_READ_AND_WRITE_ENABLED = true
  455. this.res.callback = done
  456. this.TokenAccessController.grantTokenAccessReadAndWrite(
  457. this.req,
  458. this.res,
  459. done
  460. )
  461. })
  462. it('redirects to project', function () {
  463. expect(this.res.json).to.have.been.calledWith({
  464. redirect: `/project/${this.project._id}`,
  465. grantAnonymousAccess: 'readAndWrite',
  466. })
  467. })
  468. it('checks the hash prefix and includes log data', function () {
  469. expect(
  470. this.TokenAccessHandler.checkTokenHashPrefix
  471. ).to.have.been.calledWith(
  472. this.token,
  473. '#prefix',
  474. 'readAndWrite',
  475. null,
  476. {
  477. projectId: this.project._id,
  478. action: 'granting read-write anonymous access',
  479. }
  480. )
  481. })
  482. })
  483. })
  484. describe('when Overleaf SaaS', function () {
  485. beforeEach(function () {
  486. this.Settings.overleaf = {}
  487. })
  488. describe('when token is for v1 project', function () {
  489. beforeEach(function (done) {
  490. this.TokenAccessHandler.promises.getProjectByToken.resolves(undefined)
  491. this.TokenAccessHandler.promises.getV1DocInfo.resolves({
  492. exists: true,
  493. has_owner: true,
  494. })
  495. this.req.params = { token: this.token }
  496. this.req.body = { tokenHashPrefix: '#prefix' }
  497. this.res.callback = done
  498. this.TokenAccessController.grantTokenAccessReadAndWrite(
  499. this.req,
  500. this.res,
  501. done
  502. )
  503. })
  504. it('returns v1 import data', function () {
  505. expect(this.res.json).to.have.been.calledWith({
  506. v1Import: {
  507. status: 'canDownloadZip',
  508. projectId: this.token,
  509. hasOwner: true,
  510. name: 'Untitled',
  511. brandInfo: undefined,
  512. },
  513. })
  514. })
  515. it('checks the hash prefix and includes log data', function () {
  516. expect(
  517. this.TokenAccessHandler.checkTokenHashPrefix
  518. ).to.have.been.calledWith(
  519. this.token,
  520. '#prefix',
  521. 'readAndWrite',
  522. this.user._id,
  523. {
  524. action: 'import v1',
  525. }
  526. )
  527. })
  528. })
  529. describe('when token is not for a v1 or v2 project', function () {
  530. beforeEach(function (done) {
  531. this.TokenAccessHandler.promises.getProjectByToken.resolves(undefined)
  532. this.TokenAccessHandler.promises.getV1DocInfo.resolves({
  533. exists: false,
  534. })
  535. this.req.params = { token: this.token }
  536. this.req.body = { tokenHashPrefix: '#prefix' }
  537. this.res.callback = done
  538. this.TokenAccessController.grantTokenAccessReadAndWrite(
  539. this.req,
  540. this.res,
  541. done
  542. )
  543. })
  544. it('returns 404', function () {
  545. expect(this.res.sendStatus).to.have.been.calledWith(404)
  546. })
  547. it('checks the hash prefix and includes log data', function () {
  548. expect(
  549. this.TokenAccessHandler.checkTokenHashPrefix
  550. ).to.have.been.calledWith(
  551. this.token,
  552. '#prefix',
  553. 'readAndWrite',
  554. this.user._id,
  555. {
  556. action: '404',
  557. }
  558. )
  559. })
  560. })
  561. })
  562. describe('not Overleaf SaaS', function () {
  563. beforeEach(function () {
  564. this.TokenAccessHandler.promises.getProjectByToken.resolves(undefined)
  565. this.req.params = { token: this.token }
  566. this.req.body = { tokenHashPrefix: '#prefix' }
  567. })
  568. it('passes Errors.NotFoundError to next when project not found and still checks token hash', function (done) {
  569. this.TokenAccessController.grantTokenAccessReadAndWrite(
  570. this.req,
  571. this.res,
  572. args => {
  573. expect(args).to.be.instanceof(this.Errors.NotFoundError)
  574. expect(
  575. this.TokenAccessHandler.checkTokenHashPrefix
  576. ).to.have.been.calledWith(
  577. this.token,
  578. '#prefix',
  579. 'readAndWrite',
  580. this.user._id,
  581. {
  582. action: '404',
  583. }
  584. )
  585. done()
  586. }
  587. )
  588. })
  589. })
  590. describe('when user is admin', function () {
  591. const admin = { _id: new ObjectId(), isAdmin: true }
  592. beforeEach(function () {
  593. this.SessionManager.getLoggedInUserId.returns(admin._id)
  594. this.SessionManager.getSessionUser.returns(admin)
  595. this.AdminAuthorizationHelper.canRedirectToAdminDomain.returns(true)
  596. this.req.params = { token: this.token }
  597. this.req.body = { confirmedByUser: true, tokenHashPrefix: '#prefix' }
  598. })
  599. it('redirects if project owner is non-admin', function () {
  600. this.UserGetter.promises.getUserConfirmedEmails = sinon
  601. .stub()
  602. .resolves([{ email: 'test@not-overleaf.com' }])
  603. this.res.callback = () => {
  604. expect(this.res.json).to.have.been.calledWith({
  605. redirect: `${this.Settings.adminUrl}/#prefix`,
  606. })
  607. }
  608. this.TokenAccessController.grantTokenAccessReadAndWrite(
  609. this.req,
  610. this.res
  611. )
  612. })
  613. it('grants access if project owner is an internal staff', function () {
  614. const internalStaff = { _id: new ObjectId(), isAdmin: true }
  615. const projectFromInternalStaff = {
  616. _id: new ObjectId(),
  617. name: 'test',
  618. tokenAccessReadAndWrite_refs: [],
  619. tokenAccessReadOnly_refs: [],
  620. owner_ref: internalStaff._id,
  621. }
  622. this.UserGetter.promises.getUser = sinon.stub().resolves(internalStaff)
  623. this.UserGetter.promises.getUserConfirmedEmails = sinon
  624. .stub()
  625. .resolves([{ email: 'test@overleaf.com' }])
  626. this.TokenAccessHandler.promises.getProjectByToken = sinon
  627. .stub()
  628. .resolves(projectFromInternalStaff)
  629. this.res.callback = () => {
  630. expect(
  631. this.CollaboratorsHandler.promises.addUserIdToProject
  632. ).to.have.been.calledWith(
  633. projectFromInternalStaff._id,
  634. undefined,
  635. admin._id,
  636. PrivilegeLevels.READ_AND_WRITE
  637. )
  638. }
  639. this.TokenAccessController.grantTokenAccessReadAndWrite(
  640. this.req,
  641. this.res
  642. )
  643. })
  644. })
  645. it('passes Errors.NotFoundError to next when token access is not enabled but still checks token hash', function (done) {
  646. this.TokenAccessHandler.tokenAccessEnabledForProject.returns(false)
  647. this.req.params = { token: this.token }
  648. this.req.body = { tokenHashPrefix: '#prefix' }
  649. this.TokenAccessController.grantTokenAccessReadAndWrite(
  650. this.req,
  651. this.res,
  652. args => {
  653. expect(args).to.be.instanceof(this.Errors.NotFoundError)
  654. expect(
  655. this.TokenAccessHandler.checkTokenHashPrefix
  656. ).to.have.been.calledWith(
  657. this.token,
  658. '#prefix',
  659. 'readAndWrite',
  660. this.user._id,
  661. {
  662. projectId: this.project._id,
  663. action: 'token access not enabled',
  664. }
  665. )
  666. done()
  667. }
  668. )
  669. })
  670. it('returns 400 when not using a read write token', function () {
  671. this.TokenAccessHandler.isReadAndWriteToken.returns(false)
  672. this.req.params = { token: this.token }
  673. this.req.body = { tokenHashPrefix: '#prefix' }
  674. this.TokenAccessController.grantTokenAccessReadAndWrite(
  675. this.req,
  676. this.res
  677. )
  678. expect(this.res.sendStatus).to.have.been.calledWith(400)
  679. })
  680. })
  681. describe('grantTokenAccessReadOnly', function () {
  682. describe('normal case', function () {
  683. beforeEach(function (done) {
  684. this.req.params = { token: this.token }
  685. this.req.body = { confirmedByUser: true, tokenHashPrefix: '#prefix' }
  686. this.res.callback = done
  687. this.TokenAccessController.grantTokenAccessReadOnly(
  688. this.req,
  689. this.res,
  690. done
  691. )
  692. })
  693. it('grants read-only access', function () {
  694. expect(
  695. this.TokenAccessHandler.promises.addReadOnlyUserToProject
  696. ).to.have.been.calledWith(this.user._id, this.project._id)
  697. })
  698. it('writes a project audit log', function () {
  699. expect(
  700. this.ProjectAuditLogHandler.promises.addEntry
  701. ).to.have.been.calledWith(
  702. this.project._id,
  703. 'join-via-token',
  704. this.user._id,
  705. this.req.ip,
  706. { privileges: 'readOnly' }
  707. )
  708. })
  709. it('checks if hash prefix matches', function () {
  710. expect(
  711. this.TokenAccessHandler.checkTokenHashPrefix
  712. ).to.have.been.calledWith(
  713. this.token,
  714. '#prefix',
  715. 'readOnly',
  716. this.user._id,
  717. { projectId: this.project._id, action: 'continue' }
  718. )
  719. })
  720. })
  721. describe('when the access was already granted', function () {
  722. beforeEach(function (done) {
  723. this.project.tokenAccessReadOnly_refs.push(this.user._id)
  724. this.req.params = { token: this.token }
  725. this.req.body = { confirmedByUser: true }
  726. this.res.callback = done
  727. this.TokenAccessController.grantTokenAccessReadOnly(
  728. this.req,
  729. this.res,
  730. done
  731. )
  732. })
  733. it("doesn't write a project audit log", function () {
  734. expect(this.ProjectAuditLogHandler.promises.addEntry).to.not.have.been
  735. .called
  736. })
  737. it('still checks if hash prefix matches', function () {
  738. expect(
  739. this.TokenAccessHandler.checkTokenHashPrefix
  740. ).to.have.been.calledWith(
  741. this.token,
  742. undefined,
  743. 'readOnly',
  744. this.user._id,
  745. { projectId: this.project._id, action: 'continue' }
  746. )
  747. })
  748. })
  749. it('returns 400 when not using a read only token', function () {
  750. this.TokenAccessHandler.isReadOnlyToken.returns(false)
  751. this.req.params = { token: this.token }
  752. this.req.body = { tokenHashPrefix: '#prefix' }
  753. this.TokenAccessController.grantTokenAccessReadOnly(this.req, this.res)
  754. expect(this.res.sendStatus).to.have.been.calledWith(400)
  755. })
  756. describe('anonymous users', function () {
  757. beforeEach(function (done) {
  758. this.req.params = { token: this.token }
  759. this.SessionManager.getLoggedInUserId.returns(null)
  760. this.res.callback = done
  761. this.TokenAccessController.grantTokenAccessReadOnly(
  762. this.req,
  763. this.res,
  764. done
  765. )
  766. })
  767. it('allows anonymous users and checks the token hash', function () {
  768. expect(this.res.json).to.have.been.calledWith({
  769. redirect: `/project/${this.project._id}`,
  770. grantAnonymousAccess: 'readOnly',
  771. })
  772. expect(
  773. this.TokenAccessHandler.checkTokenHashPrefix
  774. ).to.have.been.calledWith(this.token, undefined, 'readOnly', null, {
  775. projectId: this.project._id,
  776. action: 'granting read-only anonymous access',
  777. })
  778. })
  779. })
  780. describe('user is owner of project', function () {
  781. beforeEach(function (done) {
  782. this.AuthorizationManager.promises.getPrivilegeLevelForProject.returns(
  783. PrivilegeLevels.OWNER
  784. )
  785. this.req.params = { token: this.token }
  786. this.req.body = {}
  787. this.res.callback = done
  788. this.TokenAccessController.grantTokenAccessReadOnly(
  789. this.req,
  790. this.res,
  791. done
  792. )
  793. })
  794. it('checks token hash and includes log data', function () {
  795. expect(
  796. this.TokenAccessHandler.checkTokenHashPrefix
  797. ).to.have.been.calledWith(
  798. this.token,
  799. undefined,
  800. 'readOnly',
  801. this.user._id,
  802. {
  803. projectId: this.project._id,
  804. action: 'user already has higher or same privilege',
  805. }
  806. )
  807. })
  808. })
  809. it('passes Errors.NotFoundError to next when token access is not enabled but still checks token hash', function (done) {
  810. this.TokenAccessHandler.tokenAccessEnabledForProject.returns(false)
  811. this.req.params = { token: this.token }
  812. this.req.body = { tokenHashPrefix: '#prefix' }
  813. this.TokenAccessController.grantTokenAccessReadOnly(
  814. this.req,
  815. this.res,
  816. args => {
  817. expect(args).to.be.instanceof(this.Errors.NotFoundError)
  818. expect(
  819. this.TokenAccessHandler.checkTokenHashPrefix
  820. ).to.have.been.calledWith(
  821. this.token,
  822. '#prefix',
  823. 'readOnly',
  824. this.user._id,
  825. {
  826. projectId: this.project._id,
  827. action: 'token access not enabled',
  828. }
  829. )
  830. done()
  831. }
  832. )
  833. })
  834. })
  835. describe('ensureUserCanUseSharingUpdatesConsentPage', function () {
  836. beforeEach(function () {
  837. this.req.params = { Project_id: this.project._id }
  838. })
  839. describe('when not in link sharing changes test', function () {
  840. beforeEach(function (done) {
  841. this.AsyncFormHelper.redirect = sinon.stub().callsFake(() => done())
  842. this.TokenAccessController.ensureUserCanUseSharingUpdatesConsentPage(
  843. this.req,
  844. this.res,
  845. done
  846. )
  847. })
  848. it('redirects to the project/editor', function () {
  849. expect(this.AsyncFormHelper.redirect).to.have.been.calledWith(
  850. this.req,
  851. this.res,
  852. `/project/${this.project._id}`
  853. )
  854. })
  855. })
  856. describe('when link sharing changes test active', function () {
  857. beforeEach(function () {
  858. this.SplitTestHandler.promises.getAssignmentForUser.resolves({
  859. variant: 'active',
  860. })
  861. })
  862. describe('when user is not an invited editor and is a read write token member', function () {
  863. beforeEach(function (done) {
  864. this.CollaboratorsGetter.promises.isUserInvitedReadWriteMemberOfProject.resolves(
  865. false
  866. )
  867. this.CollaboratorsGetter.promises.userIsReadWriteTokenMember.resolves(
  868. true
  869. )
  870. this.next.callsFake(() => done())
  871. this.TokenAccessController.ensureUserCanUseSharingUpdatesConsentPage(
  872. this.req,
  873. this.res,
  874. this.next
  875. )
  876. })
  877. it('calls next', function () {
  878. expect(
  879. this.CollaboratorsGetter.promises
  880. .isUserInvitedReadWriteMemberOfProject
  881. ).to.have.been.calledWith(this.user._id, this.project._id)
  882. expect(
  883. this.CollaboratorsGetter.promises.userIsReadWriteTokenMember
  884. ).to.have.been.calledWith(this.user._id, this.project._id)
  885. expect(this.next).to.have.been.calledOnce
  886. expect(this.next.firstCall.args[0]).to.not.exist
  887. })
  888. })
  889. describe('when user is already an invited editor', function () {
  890. beforeEach(function (done) {
  891. this.CollaboratorsGetter.promises.isUserInvitedReadWriteMemberOfProject.resolves(
  892. true
  893. )
  894. this.AsyncFormHelper.redirect = sinon.stub().callsFake(() => done())
  895. this.TokenAccessController.ensureUserCanUseSharingUpdatesConsentPage(
  896. this.req,
  897. this.res,
  898. done
  899. )
  900. })
  901. it('redirects to the project/editor', function () {
  902. expect(this.AsyncFormHelper.redirect).to.have.been.calledWith(
  903. this.req,
  904. this.res,
  905. `/project/${this.project._id}`
  906. )
  907. })
  908. })
  909. describe('when user not a read write token member', function () {
  910. beforeEach(function (done) {
  911. this.CollaboratorsGetter.promises.userIsReadWriteTokenMember.resolves(
  912. false
  913. )
  914. this.AsyncFormHelper.redirect = sinon.stub().callsFake(() => done())
  915. this.TokenAccessController.ensureUserCanUseSharingUpdatesConsentPage(
  916. this.req,
  917. this.res,
  918. done
  919. )
  920. })
  921. it('redirects to the project/editor', function () {
  922. expect(this.AsyncFormHelper.redirect).to.have.been.calledWith(
  923. this.req,
  924. this.res,
  925. `/project/${this.project._id}`
  926. )
  927. })
  928. })
  929. })
  930. })
  931. describe('moveReadWriteToCollaborators', function () {
  932. beforeEach(function () {
  933. this.req.params = { Project_id: this.project._id }
  934. })
  935. describe('when there are collaborator slots available', function () {
  936. beforeEach(function () {
  937. this.LimitationsManager.promises.canAcceptEditCollaboratorInvite.resolves(
  938. true
  939. )
  940. })
  941. describe('previously joined token access user moving to named collaborator', function () {
  942. beforeEach(function (done) {
  943. this.CollaboratorsGetter.promises.isUserInvitedMemberOfProject.resolves(
  944. false
  945. )
  946. this.res.callback = done
  947. this.TokenAccessController.moveReadWriteToCollaborators(
  948. this.req,
  949. this.res,
  950. done
  951. )
  952. })
  953. it('sets the privilege level to read and write for the invited viewer', function () {
  954. expect(
  955. this.TokenAccessHandler.promises.removeReadAndWriteUserFromProject
  956. ).to.have.been.calledWith(this.user._id, this.project._id)
  957. expect(
  958. this.CollaboratorsHandler.promises.addUserIdToProject
  959. ).to.have.been.calledWith(
  960. this.project._id,
  961. undefined,
  962. this.user._id,
  963. PrivilegeLevels.READ_AND_WRITE
  964. )
  965. expect(this.res.sendStatus).to.have.been.calledWith(204)
  966. })
  967. })
  968. })
  969. describe('when there are no edit collaborator slots available', function () {
  970. beforeEach(function () {
  971. this.LimitationsManager.promises.canAcceptEditCollaboratorInvite.resolves(
  972. false
  973. )
  974. })
  975. describe('previously joined token access user moving to named collaborator', function () {
  976. beforeEach(function (done) {
  977. this.CollaboratorsGetter.promises.isUserInvitedMemberOfProject.resolves(
  978. false
  979. )
  980. this.res.callback = done
  981. this.TokenAccessController.moveReadWriteToCollaborators(
  982. this.req,
  983. this.res,
  984. done
  985. )
  986. })
  987. it('sets the privilege level to read only for the invited viewer (pendingEditor)', function () {
  988. expect(
  989. this.TokenAccessHandler.promises.removeReadAndWriteUserFromProject
  990. ).to.have.been.calledWith(this.user._id, this.project._id)
  991. expect(
  992. this.CollaboratorsHandler.promises.addUserIdToProject
  993. ).to.have.been.calledWith(
  994. this.project._id,
  995. undefined,
  996. this.user._id,
  997. PrivilegeLevels.READ_ONLY,
  998. { pendingEditor: true }
  999. )
  1000. expect(this.res.sendStatus).to.have.been.calledWith(204)
  1001. })
  1002. })
  1003. })
  1004. })
  1005. describe('moveReadWriteToReadOnly', function () {
  1006. beforeEach(function () {
  1007. this.req.params = { Project_id: this.project._id }
  1008. })
  1009. describe('previously joined token access user moving to anonymous viewer', function () {
  1010. beforeEach(function (done) {
  1011. this.res.callback = done
  1012. this.TokenAccessController.moveReadWriteToReadOnly(
  1013. this.req,
  1014. this.res,
  1015. done
  1016. )
  1017. })
  1018. it('removes them from read write token access refs and adds them to read only token access refs', function () {
  1019. expect(
  1020. this.TokenAccessHandler.promises.moveReadAndWriteUserToReadOnly
  1021. ).to.have.been.calledWith(this.user._id, this.project._id)
  1022. expect(this.res.sendStatus).to.have.been.calledWith(204)
  1023. })
  1024. it('writes a project audit log', function () {
  1025. expect(
  1026. this.ProjectAuditLogHandler.promises.addEntry
  1027. ).to.have.been.calledWith(
  1028. this.project._id,
  1029. 'readonly-via-sharing-updates',
  1030. this.user._id,
  1031. this.req.ip
  1032. )
  1033. })
  1034. })
  1035. })
  1036. })