ProjectControllerTests.js 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const sinon = require('sinon')
  4. const { expect } = require('chai')
  5. const HttpErrors = require('@overleaf/o-error/http')
  6. const { ObjectId } = require('mongodb')
  7. const Errors = require('../../../../app/src/Features/Errors/Errors')
  8. const MODULE_PATH = path.join(
  9. __dirname,
  10. '../../../../app/src/Features/Project/ProjectController'
  11. )
  12. describe('ProjectController', function() {
  13. beforeEach(function() {
  14. this.project_id = ObjectId('abcdefabcdefabcdefabcdef')
  15. this.user = {
  16. _id: ObjectId('123456123456123456123456'),
  17. email: 'test@overleaf.com',
  18. first_name: 'bjkdsjfk',
  19. features: {},
  20. emails: [{ email: 'test@overleaf.com' }]
  21. }
  22. this.settings = {
  23. apis: {
  24. chat: {
  25. url: 'chat.com'
  26. }
  27. },
  28. siteUrl: 'mysite.com',
  29. algolia: {}
  30. }
  31. this.brandVariationDetails = {
  32. id: '12',
  33. active: true,
  34. brand_name: 'The journal',
  35. home_url: 'http://www.thejournal.com/',
  36. publish_menu_link_html: 'Submit your paper to the <em>The Journal</em>'
  37. }
  38. this.token = 'some-token'
  39. this.ProjectDeleter = {
  40. deleteProject: sinon.stub().callsArg(2),
  41. restoreProject: sinon.stub().callsArg(1),
  42. findArchivedProjects: sinon.stub()
  43. }
  44. this.ProjectDuplicator = {
  45. duplicate: sinon.stub().callsArgWith(3, null, { _id: this.project_id })
  46. }
  47. this.ProjectCreationHandler = {
  48. createExampleProject: sinon
  49. .stub()
  50. .callsArgWith(2, null, { _id: this.project_id }),
  51. createBasicProject: sinon
  52. .stub()
  53. .callsArgWith(2, null, { _id: this.project_id })
  54. }
  55. this.SubscriptionLocator = { getUsersSubscription: sinon.stub() }
  56. this.LimitationsManager = { hasPaidSubscription: sinon.stub() }
  57. this.TagsHandler = { getAllTags: sinon.stub() }
  58. this.NotificationsHandler = { getUserNotifications: sinon.stub() }
  59. this.UserModel = { findById: sinon.stub() }
  60. this.AuthorizationManager = {
  61. getPrivilegeLevelForProject: sinon.stub(),
  62. isRestrictedUser: sinon.stub().returns(false)
  63. }
  64. this.EditorController = { renameProject: sinon.stub() }
  65. this.InactiveProjectManager = { reactivateProjectIfRequired: sinon.stub() }
  66. this.ProjectUpdateHandler = { markAsOpened: sinon.stub() }
  67. this.ReferencesSearchHandler = { indexProjectReferences: sinon.stub() }
  68. this.ProjectGetter = {
  69. findAllUsersProjects: sinon.stub(),
  70. getProject: sinon.stub()
  71. }
  72. this.ProjectHelper = {
  73. isArchived: sinon.stub(),
  74. isTrashed: sinon.stub(),
  75. isArchivedOrTrashed: sinon.stub()
  76. }
  77. this.AuthenticationController = {
  78. getLoggedInUser: sinon.stub().callsArgWith(1, null, this.user),
  79. getLoggedInUserId: sinon.stub().returns(this.user._id),
  80. getSessionUser: sinon.stub().returns(this.user),
  81. isUserLoggedIn: sinon.stub().returns(true)
  82. }
  83. this.UserController = {
  84. logout: sinon.stub()
  85. }
  86. this.TokenAccessHandler = {
  87. getRequestToken: sinon.stub().returns(this.token),
  88. protectTokens: sinon.stub()
  89. }
  90. this.CollaboratorsGetter = {
  91. userIsTokenMember: sinon.stub().callsArgWith(2, null, false)
  92. }
  93. this.ProjectEntityHandler = {}
  94. this.NotificationBuilder = {
  95. ipMatcherAffiliation: sinon.stub().returns({ create: sinon.stub() })
  96. }
  97. this.UserGetter = {
  98. getUser: sinon
  99. .stub()
  100. .callsArgWith(2, null, { lastLoginIp: '192.170.18.2' })
  101. }
  102. this.Features = {
  103. hasFeature: sinon.stub()
  104. }
  105. this.BrandVariationsHandler = {
  106. getBrandVariationById: sinon
  107. .stub()
  108. .callsArgWith(1, null, this.brandVariationDetails)
  109. }
  110. this.TpdsProjectFlusher = {
  111. flushProjectToTpdsIfNeeded: sinon.stub().yields()
  112. }
  113. this.getUserAffiliations = sinon.stub().callsArgWith(1, null, [
  114. {
  115. email: 'test@overleaf.com',
  116. institution: {
  117. id: 1,
  118. confirmed: true,
  119. name: 'Overleaf',
  120. ssoBeta: false,
  121. ssoEnabled: true
  122. }
  123. }
  124. ])
  125. this.Metrics = {
  126. Timer: class {
  127. done() {}
  128. },
  129. inc: sinon.stub()
  130. }
  131. this.ProjectController = SandboxedModule.require(MODULE_PATH, {
  132. globals: {
  133. console: console
  134. },
  135. requires: {
  136. 'settings-sharelatex': this.settings,
  137. 'logger-sharelatex': {
  138. log() {},
  139. err() {}
  140. },
  141. 'metrics-sharelatex': this.Metrics,
  142. '@overleaf/o-error/http': HttpErrors,
  143. './ProjectDeleter': this.ProjectDeleter,
  144. './ProjectDuplicator': this.ProjectDuplicator,
  145. './ProjectCreationHandler': this.ProjectCreationHandler,
  146. '../Editor/EditorController': this.EditorController,
  147. '../User/UserController': this.UserController,
  148. './ProjectHelper': this.ProjectHelper,
  149. '../Subscription/SubscriptionLocator': this.SubscriptionLocator,
  150. '../Subscription/LimitationsManager': this.LimitationsManager,
  151. '../Tags/TagsHandler': this.TagsHandler,
  152. '../Notifications/NotificationsHandler': this.NotificationsHandler,
  153. '../../models/User': { User: this.UserModel },
  154. '../Authorization/AuthorizationManager': this.AuthorizationManager,
  155. '../InactiveData/InactiveProjectManager': this.InactiveProjectManager,
  156. './ProjectUpdateHandler': this.ProjectUpdateHandler,
  157. '../ReferencesSearch/ReferencesSearchHandler': this
  158. .ReferencesSearchHandler,
  159. './ProjectGetter': this.ProjectGetter,
  160. './ProjectDetailsHandler': this.ProjectDetailsHandler,
  161. '../Authentication/AuthenticationController': this
  162. .AuthenticationController,
  163. '../TokenAccess/TokenAccessHandler': this.TokenAccessHandler,
  164. '../Collaborators/CollaboratorsGetter': this.CollaboratorsGetter,
  165. './ProjectEntityHandler': this.ProjectEntityHandler,
  166. '../Errors/Errors': Errors,
  167. '../../infrastructure/Features': this.Features,
  168. '../Notifications/NotificationsBuilder': this.NotificationBuilder,
  169. '../User/UserGetter': this.UserGetter,
  170. '../BrandVariations/BrandVariationsHandler': this
  171. .BrandVariationsHandler,
  172. '../Institutions/InstitutionsAPI': {
  173. getUserAffiliations: this.getUserAffiliations
  174. },
  175. '../ThirdPartyDataStore/TpdsProjectFlusher': this.TpdsProjectFlusher,
  176. '../../models/Project': {}
  177. }
  178. })
  179. this.projectName = '£12321jkj9ujkljds'
  180. this.req = {
  181. params: {
  182. Project_id: this.project_id
  183. },
  184. headers: {},
  185. connection: {
  186. remoteAddress: '192.170.18.1'
  187. },
  188. session: {
  189. user: this.user
  190. },
  191. body: {
  192. projectName: this.projectName
  193. },
  194. i18n: {
  195. translate() {}
  196. },
  197. ip: '192.170.18.1'
  198. }
  199. this.res = {
  200. locals: {
  201. jsPath: 'js path here'
  202. },
  203. setTimeout: sinon.stub()
  204. }
  205. })
  206. describe('updateProjectSettings', function() {
  207. it('should update the name', function(done) {
  208. this.EditorController.renameProject = sinon.stub().callsArg(2)
  209. this.req.body = { name: (this.name = 'New name') }
  210. this.res.sendStatus = code => {
  211. this.EditorController.renameProject
  212. .calledWith(this.project_id, this.name)
  213. .should.equal(true)
  214. code.should.equal(204)
  215. done()
  216. }
  217. this.ProjectController.updateProjectSettings(this.req, this.res)
  218. })
  219. it('should update the compiler', function(done) {
  220. this.EditorController.setCompiler = sinon.stub().callsArg(2)
  221. this.req.body = { compiler: (this.compiler = 'pdflatex') }
  222. this.res.sendStatus = code => {
  223. this.EditorController.setCompiler
  224. .calledWith(this.project_id, this.compiler)
  225. .should.equal(true)
  226. code.should.equal(204)
  227. done()
  228. }
  229. this.ProjectController.updateProjectSettings(this.req, this.res)
  230. })
  231. it('should update the imageName', function(done) {
  232. this.EditorController.setImageName = sinon.stub().callsArg(2)
  233. this.req.body = { imageName: (this.imageName = 'texlive-1234.5') }
  234. this.res.sendStatus = code => {
  235. this.EditorController.setImageName
  236. .calledWith(this.project_id, this.imageName)
  237. .should.equal(true)
  238. code.should.equal(204)
  239. done()
  240. }
  241. this.ProjectController.updateProjectSettings(this.req, this.res)
  242. })
  243. it('should update the spell check language', function(done) {
  244. this.EditorController.setSpellCheckLanguage = sinon.stub().callsArg(2)
  245. this.req.body = { spellCheckLanguage: (this.languageCode = 'fr') }
  246. this.res.sendStatus = code => {
  247. this.EditorController.setSpellCheckLanguage
  248. .calledWith(this.project_id, this.languageCode)
  249. .should.equal(true)
  250. code.should.equal(204)
  251. done()
  252. }
  253. this.ProjectController.updateProjectSettings(this.req, this.res)
  254. })
  255. it('should update the root doc', function(done) {
  256. this.EditorController.setRootDoc = sinon.stub().callsArg(2)
  257. this.req.body = { rootDocId: (this.rootDocId = 'root-doc-id') }
  258. this.res.sendStatus = code => {
  259. this.EditorController.setRootDoc
  260. .calledWith(this.project_id, this.rootDocId)
  261. .should.equal(true)
  262. code.should.equal(204)
  263. done()
  264. }
  265. this.ProjectController.updateProjectSettings(this.req, this.res)
  266. })
  267. })
  268. describe('updateProjectAdminSettings', function() {
  269. it('should update the public access level', function(done) {
  270. this.EditorController.setPublicAccessLevel = sinon.stub().callsArg(2)
  271. this.req.body = {
  272. publicAccessLevel: (this.publicAccessLevel = 'readonly')
  273. }
  274. this.res.sendStatus = code => {
  275. this.EditorController.setPublicAccessLevel
  276. .calledWith(this.project_id, this.publicAccessLevel)
  277. .should.equal(true)
  278. code.should.equal(204)
  279. done()
  280. }
  281. this.ProjectController.updateProjectAdminSettings(this.req, this.res)
  282. })
  283. })
  284. describe('deleteProject', function() {
  285. it('should call the project deleter', function(done) {
  286. this.res.sendStatus = code => {
  287. this.ProjectDeleter.deleteProject
  288. .calledWith(this.project_id, {
  289. deleterUser: this.user,
  290. ipAddress: this.req.ip
  291. })
  292. .should.equal(true)
  293. code.should.equal(200)
  294. done()
  295. }
  296. this.ProjectController.deleteProject(this.req, this.res)
  297. })
  298. })
  299. describe('restoreProject', function() {
  300. it('should tell the project deleter', function(done) {
  301. this.res.sendStatus = code => {
  302. this.ProjectDeleter.restoreProject
  303. .calledWith(this.project_id)
  304. .should.equal(true)
  305. code.should.equal(200)
  306. done()
  307. }
  308. this.ProjectController.restoreProject(this.req, this.res)
  309. })
  310. })
  311. describe('cloneProject', function() {
  312. it('should call the project duplicator', function(done) {
  313. this.res.send = json => {
  314. this.ProjectDuplicator.duplicate
  315. .calledWith(this.user, this.project_id, this.projectName)
  316. .should.equal(true)
  317. json.project_id.should.equal(this.project_id)
  318. done()
  319. }
  320. this.ProjectController.cloneProject(this.req, this.res)
  321. })
  322. })
  323. describe('newProject', function() {
  324. it('should call the projectCreationHandler with createExampleProject', function(done) {
  325. this.req.body.template = 'example'
  326. this.res.send = json => {
  327. this.ProjectCreationHandler.createExampleProject
  328. .calledWith(this.user._id, this.projectName)
  329. .should.equal(true)
  330. this.ProjectCreationHandler.createBasicProject.called.should.equal(
  331. false
  332. )
  333. done()
  334. }
  335. this.ProjectController.newProject(this.req, this.res)
  336. })
  337. it('should call the projectCreationHandler with createBasicProject', function(done) {
  338. this.req.body.template = 'basic'
  339. this.res.send = json => {
  340. this.ProjectCreationHandler.createExampleProject.called.should.equal(
  341. false
  342. )
  343. this.ProjectCreationHandler.createBasicProject
  344. .calledWith(this.user._id, this.projectName)
  345. .should.equal(true)
  346. done()
  347. }
  348. this.ProjectController.newProject(this.req, this.res)
  349. })
  350. })
  351. describe('projectListPage', function() {
  352. beforeEach(function() {
  353. this.tags = [
  354. { name: 1, project_ids: ['1', '2', '3'] },
  355. { name: 2, project_ids: ['a', '1'] },
  356. { name: 3, project_ids: ['a', 'b', 'c', 'd'] }
  357. ]
  358. this.notifications = [
  359. { _id: '1', user_id: '2', templateKey: '3', messageOpts: '4', key: '5' }
  360. ]
  361. this.projects = [
  362. { _id: 1, lastUpdated: 1, owner_ref: 'user-1' },
  363. { _id: 2, lastUpdated: 2, owner_ref: 'user-2', lastUpdatedBy: 'user-1' }
  364. ]
  365. this.collabertions = [{ _id: 5, lastUpdated: 5, owner_ref: 'user-1' }]
  366. this.readOnly = [{ _id: 3, lastUpdated: 3, owner_ref: 'user-1' }]
  367. this.tokenReadAndWrite = [{ _id: 6, lastUpdated: 5, owner_ref: 'user-4' }]
  368. this.tokenReadOnly = [{ _id: 7, lastUpdated: 4, owner_ref: 'user-5' }]
  369. this.allProjects = {
  370. owned: this.projects,
  371. readAndWrite: this.collabertions,
  372. readOnly: this.readOnly,
  373. tokenReadAndWrite: this.tokenReadAndWrite,
  374. tokenReadOnly: this.tokenReadOnly
  375. }
  376. this.users = {
  377. 'user-1': {
  378. first_name: 'James'
  379. },
  380. 'user-2': {
  381. first_name: 'Henry'
  382. }
  383. }
  384. this.users[this.user._id] = this.user // Owner
  385. this.UserModel.findById = (id, fields, callback) => {
  386. callback(null, this.users[id])
  387. }
  388. this.UserGetter.getUser = (id, fields, callback) => {
  389. callback(null, this.users[id])
  390. }
  391. this.LimitationsManager.hasPaidSubscription.callsArgWith(1, null, false)
  392. this.TagsHandler.getAllTags.callsArgWith(1, null, this.tags)
  393. this.NotificationsHandler.getUserNotifications = sinon
  394. .stub()
  395. .callsArgWith(1, null, this.notifications, {})
  396. this.ProjectGetter.findAllUsersProjects.callsArgWith(
  397. 2,
  398. null,
  399. this.allProjects
  400. )
  401. })
  402. it('should render the project/list page', function(done) {
  403. this.res.render = (pageName, opts) => {
  404. pageName.should.equal('project/list')
  405. done()
  406. }
  407. this.ProjectController.projectListPage(this.req, this.res)
  408. })
  409. it('should send the tags', function(done) {
  410. this.res.render = (pageName, opts) => {
  411. opts.tags.length.should.equal(this.tags.length)
  412. done()
  413. }
  414. this.ProjectController.projectListPage(this.req, this.res)
  415. })
  416. it('should create trigger ip matcher notifications', function(done) {
  417. this.settings.overleaf = true
  418. this.res.render = (pageName, opts) => {
  419. this.NotificationBuilder.ipMatcherAffiliation.called.should.equal(true)
  420. done()
  421. }
  422. this.ProjectController.projectListPage(this.req, this.res)
  423. })
  424. it('should send the projects', function(done) {
  425. this.res.render = (pageName, opts) => {
  426. opts.projects.length.should.equal(
  427. this.projects.length +
  428. this.collabertions.length +
  429. this.readOnly.length +
  430. this.tokenReadAndWrite.length +
  431. this.tokenReadOnly.length
  432. )
  433. done()
  434. }
  435. this.ProjectController.projectListPage(this.req, this.res)
  436. })
  437. it('should send the user', function(done) {
  438. this.res.render = (pageName, opts) => {
  439. opts.user.should.deep.equal(this.user)
  440. done()
  441. }
  442. this.ProjectController.projectListPage(this.req, this.res)
  443. })
  444. it('should inject the users', function(done) {
  445. this.res.render = (pageName, opts) => {
  446. opts.projects[0].owner.should.equal(
  447. this.users[this.projects[0].owner_ref]
  448. )
  449. opts.projects[1].owner.should.equal(
  450. this.users[this.projects[1].owner_ref]
  451. )
  452. opts.projects[1].lastUpdatedBy.should.equal(
  453. this.users[this.projects[1].lastUpdatedBy]
  454. )
  455. done()
  456. }
  457. this.ProjectController.projectListPage(this.req, this.res)
  458. })
  459. it('should send hasSubscription == false when no subscription', function(done) {
  460. this.res.render = (pageName, opts) => {
  461. opts.hasSubscription.should.equal(false)
  462. done()
  463. }
  464. this.ProjectController.projectListPage(this.req, this.res)
  465. })
  466. it('should send hasSubscription == true when there is a subscription', function(done) {
  467. this.LimitationsManager.hasPaidSubscription = sinon
  468. .stub()
  469. .callsArgWith(1, null, true)
  470. this.res.render = (pageName, opts) => {
  471. opts.hasSubscription.should.equal(true)
  472. done()
  473. }
  474. this.ProjectController.projectListPage(this.req, this.res)
  475. })
  476. describe('when there is a v1 connection error', function() {
  477. beforeEach(function() {
  478. this.Features.hasFeature = sinon
  479. .stub()
  480. .withArgs('overleaf-integration')
  481. .returns(true)
  482. this.connectionWarning =
  483. 'Error accessing Overleaf V1. Some of your projects or features may be missing.'
  484. })
  485. it('should show a warning when there is an error getting subscriptions from v1', function(done) {
  486. this.LimitationsManager.hasPaidSubscription.yields(
  487. new Errors.V1ConnectionError('error')
  488. )
  489. this.res.render = (pageName, opts) => {
  490. expect(opts.warnings).to.contain(this.connectionWarning)
  491. done()
  492. }
  493. this.ProjectController.projectListPage(this.req, this.res)
  494. })
  495. it('should show a warning when there is an error getting affiliations from v1', function(done) {
  496. this.getUserAffiliations.yields(new Errors.V1ConnectionError('error'))
  497. this.res.render = (pageName, opts) => {
  498. expect(opts.warnings).to.contain(this.connectionWarning)
  499. done()
  500. }
  501. this.ProjectController.projectListPage(this.req, this.res)
  502. })
  503. })
  504. describe('front widget', function(done) {
  505. beforeEach(function() {
  506. this.settings.overleaf = {
  507. front_chat_widget_room_id: 'chat-room-id'
  508. }
  509. })
  510. it('should show for paid users', function(done) {
  511. this.user.features.github = true
  512. this.user.features.dropbox = true
  513. this.res.render = (pageName, opts) => {
  514. opts.frontChatWidgetRoomId.should.equal(
  515. this.settings.overleaf.front_chat_widget_room_id
  516. )
  517. done()
  518. }
  519. this.ProjectController.projectListPage(this.req, this.res)
  520. })
  521. it('should show for sample users', function(done) {
  522. this.user._id = ObjectId('588f3ddae8ebc1bac07c9f00') // last two digits
  523. this.res.render = (pageName, opts) => {
  524. opts.frontChatWidgetRoomId.should.equal(
  525. this.settings.overleaf.front_chat_widget_room_id
  526. )
  527. done()
  528. }
  529. this.ProjectController.projectListPage(this.req, this.res)
  530. })
  531. it('should not show for non sample users', function(done) {
  532. this.user._id = ObjectId('588f3ddae8ebc1bac07c9fff') // last two digits
  533. this.res.render = (pageName, opts) => {
  534. expect(opts.frontChatWidgetRoomId).to.equal(undefined)
  535. done()
  536. }
  537. this.ProjectController.projectListPage(this.req, this.res)
  538. })
  539. })
  540. describe('With Institution SSO feature', function() {
  541. beforeEach(function(done) {
  542. this.institutionEmail = 'test@overleaf.com'
  543. this.institutionName = 'Overleaf'
  544. this.Features.hasFeature.withArgs('saml').returns(true)
  545. this.Features.hasFeature.withArgs('affiliations').returns(true)
  546. this.Features.hasFeature.withArgs('overleaf-integration').returns(true)
  547. done()
  548. })
  549. it('should show institution SSO available notification for confirmed domains', function() {
  550. this.res.render = (pageName, opts) => {
  551. expect(opts.notificationsInstitution).to.deep.include({
  552. email: this.institutionEmail,
  553. institutionId: 1,
  554. institutionName: this.institutionName,
  555. templateKey: 'notification_institution_sso_available'
  556. })
  557. }
  558. this.ProjectController.projectListPage(this.req, this.res)
  559. })
  560. it('should show a linked notification', function() {
  561. this.req.session.saml = {
  562. institutionEmail: this.institutionEmail,
  563. linked: {
  564. hasEntitlement: false,
  565. universityName: this.institutionName
  566. }
  567. }
  568. this.res.render = (pageName, opts) => {
  569. expect(opts.notificationsInstitution).to.deep.include({
  570. email: this.institutionEmail,
  571. institutionName: this.institutionName,
  572. templateKey: 'notification_institution_sso_linked'
  573. })
  574. }
  575. this.ProjectController.projectListPage(this.req, this.res)
  576. })
  577. it('should show a linked another email notification', function() {
  578. // when they request to link an email but the institution returns
  579. // a different email
  580. this.res.render = (pageName, opts) => {
  581. expect(opts.notificationsInstitution).to.deep.include({
  582. institutionEmail: this.institutionEmail,
  583. requestedEmail: 'requested@overleaf.com',
  584. templateKey: 'notification_institution_sso_non_canonical'
  585. })
  586. }
  587. this.req.session.saml = {
  588. emailNonCanonical: this.institutionEmail,
  589. institutionEmail: this.institutionEmail,
  590. requestedEmail: 'requested@overleaf.com',
  591. linked: {
  592. hasEntitlement: false,
  593. universityName: this.institutionName
  594. }
  595. }
  596. this.ProjectController.projectListPage(this.req, this.res)
  597. })
  598. it('should show a notification when intent was to register via SSO but account existed', function() {
  599. this.res.render = (pageName, opts) => {
  600. expect(opts.notificationsInstitution).to.deep.include({
  601. email: this.institutionEmail,
  602. templateKey: 'notification_institution_sso_already_registered'
  603. })
  604. }
  605. this.req.session.saml = {
  606. institutionEmail: this.institutionEmail,
  607. linked: {
  608. hasEntitlement: false,
  609. universityName: 'Overleaf'
  610. },
  611. registerIntercept: {
  612. id: 1,
  613. name: 'Example University'
  614. }
  615. }
  616. this.ProjectController.projectListPage(this.req, this.res)
  617. })
  618. it('should not show a register notification if the flow was abandoned', function() {
  619. // could initially start to register with an SSO email and then
  620. // abandon flow and login with an existing non-institution SSO email
  621. this.res.render = (pageName, opts) => {
  622. expect(opts.notificationsInstitution).to.deep.not.include({
  623. email: 'test@overleaf.com',
  624. templateKey: 'notification_institution_sso_already_registered'
  625. })
  626. }
  627. this.req.session.saml = {
  628. registerIntercept: {
  629. id: 1,
  630. name: 'Example University'
  631. }
  632. }
  633. this.ProjectController.projectListPage(this.req, this.res)
  634. })
  635. it('should show institution account linked to another account', function() {
  636. this.res.render = (pageName, opts) => {
  637. expect(opts.notificationsInstitution).to.deep.include({
  638. templateKey: 'notification_institution_sso_linked_by_another'
  639. })
  640. // Also check other notifications are not shown
  641. expect(opts.notificationsInstitution).to.not.deep.include({
  642. email: this.institutionEmail,
  643. templateKey: 'notification_institution_sso_already_registered'
  644. })
  645. expect(opts.notificationsInstitution).to.not.deep.include({
  646. institutionEmail: this.institutionEmail,
  647. requestedEmail: 'requested@overleaf.com',
  648. templateKey: 'notification_institution_sso_non_canonical'
  649. })
  650. expect(opts.notificationsInstitution).to.not.deep.include({
  651. email: this.institutionEmail,
  652. institutionName: this.institutionName,
  653. templateKey: 'notification_institution_sso_linked'
  654. })
  655. }
  656. this.req.session.saml = {
  657. emailNonCanonical: this.institutionEmail,
  658. institutionEmail: this.institutionEmail,
  659. requestedEmail: 'requested@overleaf.com',
  660. linkedToAnother: true
  661. }
  662. this.ProjectController.projectListPage(this.req, this.res)
  663. })
  664. describe('for an unconfirmed domain for an SSO institution', function() {
  665. beforeEach(function(done) {
  666. this.getUserAffiliations.yields(null, [
  667. {
  668. email: 'test@overleaf-uncofirmed.com',
  669. institution: {
  670. id: 1,
  671. confirmed: false,
  672. name: 'Overleaf',
  673. ssoBeta: false,
  674. ssoEnabled: true
  675. }
  676. }
  677. ])
  678. done()
  679. })
  680. it('should not show institution SSO available notification', function() {
  681. this.res.render = (pageName, opts) => {
  682. expect(opts.notificationsInstitution.length).to.equal(0)
  683. }
  684. this.ProjectController.projectListPage(this.req, this.res)
  685. })
  686. })
  687. describe('when linking/logging in initiated on institution side', function() {
  688. it('should not show a linked another email notification', function() {
  689. // this is only used when initated on Overleaf,
  690. // because we keep track of the requested email they tried to link
  691. this.res.render = (pageName, opts) => {
  692. expect(opts.notificationsInstitution).to.not.deep.include({
  693. institutionEmail: this.institutionEmail,
  694. requestedEmail: undefined,
  695. templateKey: 'notification_institution_sso_non_canonical'
  696. })
  697. }
  698. this.req.session.saml = {
  699. emailNonCanonical: this.institutionEmail,
  700. institutionEmail: this.institutionEmail,
  701. linked: {
  702. hasEntitlement: false,
  703. universityName: this.institutionName
  704. }
  705. }
  706. this.ProjectController.projectListPage(this.req, this.res)
  707. })
  708. })
  709. describe('Institution with SSO beta testable', function() {
  710. beforeEach(function(done) {
  711. this.getUserAffiliations.yields(null, [
  712. {
  713. email: 'beta@beta.com',
  714. institution: {
  715. id: 2,
  716. confirmed: true,
  717. name: 'Beta University',
  718. ssoBeta: true,
  719. ssoEnabled: false
  720. }
  721. }
  722. ])
  723. done()
  724. })
  725. it('should show institution SSO available notification when on a beta testing session', function() {
  726. this.req.session.samlBeta = true
  727. this.res.render = (pageName, opts) => {
  728. expect(opts.notificationsInstitution).to.deep.include({
  729. email: 'beta@beta.com',
  730. institutionId: 2,
  731. institutionName: 'Beta University',
  732. templateKey: 'notification_institution_sso_available'
  733. })
  734. }
  735. this.ProjectController.projectListPage(this.req, this.res)
  736. })
  737. it('should not show institution SSO available notification when not on a beta testing session', function() {
  738. this.req.session.samlBeta = false
  739. this.res.render = (pageName, opts) => {
  740. expect(opts.notificationsInstitution).to.deep.not.include({
  741. email: 'test@overleaf.com',
  742. institutionId: 1,
  743. institutionName: 'Overleaf',
  744. templateKey: 'notification_institution_sso_available'
  745. })
  746. }
  747. this.ProjectController.projectListPage(this.req, this.res)
  748. })
  749. })
  750. })
  751. describe('Without Institution SSO feature', function() {
  752. beforeEach(function(done) {
  753. this.Features.hasFeature.withArgs('saml').returns(false)
  754. done()
  755. })
  756. it('should not show institution sso available notification', function() {
  757. this.res.render = (pageName, opts) => {
  758. expect(opts.notificationsInstitution).to.deep.not.include({
  759. email: 'test@overleaf.com',
  760. institutionId: 1,
  761. institutionName: 'Overleaf',
  762. templateKey: 'notification_institution_sso_available'
  763. })
  764. }
  765. this.ProjectController.projectListPage(this.req, this.res)
  766. })
  767. })
  768. })
  769. describe('projectListPage with duplicate projects', function() {
  770. beforeEach(function() {
  771. this.tags = [
  772. { name: 1, project_ids: ['1', '2', '3'] },
  773. { name: 2, project_ids: ['a', '1'] },
  774. { name: 3, project_ids: ['a', 'b', 'c', 'd'] }
  775. ]
  776. this.notifications = [
  777. { _id: '1', user_id: '2', templateKey: '3', messageOpts: '4', key: '5' }
  778. ]
  779. this.projects = [
  780. { _id: 1, lastUpdated: 1, owner_ref: 'user-1' },
  781. { _id: 2, lastUpdated: 2, owner_ref: 'user-2' }
  782. ]
  783. this.collabertions = [{ _id: 5, lastUpdated: 5, owner_ref: 'user-1' }]
  784. this.readOnly = [{ _id: 3, lastUpdated: 3, owner_ref: 'user-1' }]
  785. this.tokenReadAndWrite = [{ _id: 6, lastUpdated: 5, owner_ref: 'user-4' }]
  786. this.tokenReadOnly = [
  787. { _id: 6, lastUpdated: 5, owner_ref: 'user-4' }, // Also in tokenReadAndWrite
  788. { _id: 7, lastUpdated: 4, owner_ref: 'user-5' }
  789. ]
  790. this.allProjects = {
  791. owned: this.projects,
  792. readAndWrite: this.collabertions,
  793. readOnly: this.readOnly,
  794. tokenReadAndWrite: this.tokenReadAndWrite,
  795. tokenReadOnly: this.tokenReadOnly
  796. }
  797. this.users = {
  798. 'user-1': {
  799. first_name: 'James'
  800. },
  801. 'user-2': {
  802. first_name: 'Henry'
  803. }
  804. }
  805. this.users[this.user._id] = this.user // Owner
  806. this.UserModel.findById = (id, fields, callback) => {
  807. callback(null, this.users[id])
  808. }
  809. this.LimitationsManager.hasPaidSubscription.callsArgWith(1, null, false)
  810. this.TagsHandler.getAllTags.callsArgWith(1, null, this.tags)
  811. this.NotificationsHandler.getUserNotifications = sinon
  812. .stub()
  813. .callsArgWith(1, null, this.notifications, {})
  814. this.ProjectGetter.findAllUsersProjects.callsArgWith(
  815. 2,
  816. null,
  817. this.allProjects
  818. )
  819. })
  820. it('should render the project/list page', function(done) {
  821. this.res.render = (pageName, opts) => {
  822. pageName.should.equal('project/list')
  823. done()
  824. }
  825. this.ProjectController.projectListPage(this.req, this.res)
  826. })
  827. it('should omit one of the projects', function(done) {
  828. this.res.render = (pageName, opts) => {
  829. opts.projects.length.should.equal(
  830. this.projects.length +
  831. this.collabertions.length +
  832. this.readOnly.length +
  833. this.tokenReadAndWrite.length +
  834. this.tokenReadOnly.length -
  835. 1
  836. )
  837. done()
  838. }
  839. this.ProjectController.projectListPage(this.req, this.res)
  840. })
  841. })
  842. describe('renameProject', function() {
  843. beforeEach(function() {
  844. this.newProjectName = 'my supper great new project'
  845. this.req.body.newProjectName = this.newProjectName
  846. })
  847. it('should call the editor controller', function(done) {
  848. this.EditorController.renameProject.callsArgWith(2)
  849. this.res.sendStatus = code => {
  850. code.should.equal(200)
  851. this.EditorController.renameProject
  852. .calledWith(this.project_id, this.newProjectName)
  853. .should.equal(true)
  854. done()
  855. }
  856. this.ProjectController.renameProject(this.req, this.res)
  857. })
  858. it('should send an error to next() if there is a problem', function(done) {
  859. let error
  860. this.EditorController.renameProject.callsArgWith(
  861. 2,
  862. (error = new Error('problem'))
  863. )
  864. const next = e => {
  865. e.should.equal(error)
  866. done()
  867. }
  868. this.ProjectController.renameProject(this.req, this.res, next)
  869. })
  870. })
  871. describe('loadEditor', function() {
  872. beforeEach(function() {
  873. this.settings.editorIsOpen = true
  874. this.project = {
  875. name: 'my proj',
  876. _id: '213123kjlkj',
  877. owner_ref: '59fc84d5fbea77482d436e1b'
  878. }
  879. this.brandedProject = {
  880. name: 'my branded proj',
  881. _id: '3252332',
  882. owner_ref: '59fc84d5fbea77482d436e1b',
  883. brandVariationId: '12'
  884. }
  885. this.user = {
  886. _id: '588f3ddae8ebc1bac07c9fa4',
  887. ace: {
  888. fontSize: 'massive',
  889. theme: 'sexy'
  890. },
  891. email: 'bob@bob.com'
  892. }
  893. this.ProjectGetter.getProject.callsArgWith(2, null, this.project)
  894. this.UserModel.findById.callsArgWith(1, null, this.user)
  895. this.SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {})
  896. this.AuthorizationManager.getPrivilegeLevelForProject.callsArgWith(
  897. 3,
  898. null,
  899. 'owner'
  900. )
  901. this.ProjectDeleter.unmarkAsDeletedByExternalSource = sinon.stub()
  902. this.InactiveProjectManager.reactivateProjectIfRequired.callsArgWith(1)
  903. this.ProjectUpdateHandler.markAsOpened.callsArgWith(1)
  904. })
  905. it('should render the project/editor page', function(done) {
  906. this.res.render = (pageName, opts) => {
  907. pageName.should.equal('project/editor')
  908. done()
  909. }
  910. this.ProjectController.loadEditor(this.req, this.res)
  911. })
  912. it('should add user', function(done) {
  913. this.res.render = (pageName, opts) => {
  914. opts.user.email.should.equal(this.user.email)
  915. done()
  916. }
  917. this.ProjectController.loadEditor(this.req, this.res)
  918. })
  919. it('should add on userSettings', function(done) {
  920. this.res.render = (pageName, opts) => {
  921. opts.userSettings.fontSize.should.equal(this.user.ace.fontSize)
  922. opts.userSettings.editorTheme.should.equal(this.user.ace.theme)
  923. done()
  924. }
  925. this.ProjectController.loadEditor(this.req, this.res)
  926. })
  927. it('should add isRestrictedTokenMember', function(done) {
  928. this.res.render = (pageName, opts) => {
  929. opts.isRestrictedTokenMember.should.exist
  930. opts.isRestrictedTokenMember.should.equal(false)
  931. return done()
  932. }
  933. return this.ProjectController.loadEditor(this.req, this.res)
  934. })
  935. it('should set isRestrictedTokenMember when appropriate', function(done) {
  936. this.AuthorizationManager.isRestrictedUser.returns(true)
  937. this.res.render = (pageName, opts) => {
  938. opts.isRestrictedTokenMember.should.exist
  939. opts.isRestrictedTokenMember.should.equal(true)
  940. return done()
  941. }
  942. return this.ProjectController.loadEditor(this.req, this.res)
  943. })
  944. it('should render the closed page if the editor is closed', function(done) {
  945. this.settings.editorIsOpen = false
  946. this.res.render = (pageName, opts) => {
  947. pageName.should.equal('general/closed')
  948. done()
  949. }
  950. this.ProjectController.loadEditor(this.req, this.res)
  951. })
  952. it('should not render the page if the project can not be accessed', function(done) {
  953. this.AuthorizationManager.getPrivilegeLevelForProject = sinon
  954. .stub()
  955. .callsArgWith(3, null, null)
  956. this.res.sendStatus = (resCode, opts) => {
  957. resCode.should.equal(401)
  958. done()
  959. }
  960. this.ProjectController.loadEditor(this.req, this.res)
  961. })
  962. it('should reactivateProjectIfRequired', function(done) {
  963. this.res.render = (pageName, opts) => {
  964. this.InactiveProjectManager.reactivateProjectIfRequired
  965. .calledWith(this.project_id)
  966. .should.equal(true)
  967. done()
  968. }
  969. this.ProjectController.loadEditor(this.req, this.res)
  970. })
  971. it('should mark project as opened', function(done) {
  972. this.res.render = (pageName, opts) => {
  973. this.ProjectUpdateHandler.markAsOpened
  974. .calledWith(this.project_id)
  975. .should.equal(true)
  976. done()
  977. }
  978. this.ProjectController.loadEditor(this.req, this.res)
  979. })
  980. it('should call the brand variations handler for branded projects', function(done) {
  981. this.ProjectGetter.getProject.callsArgWith(2, null, this.brandedProject)
  982. this.res.render = (pageName, opts) => {
  983. this.BrandVariationsHandler.getBrandVariationById
  984. .calledWith()
  985. .should.equal(true)
  986. done()
  987. }
  988. this.ProjectController.loadEditor(this.req, this.res)
  989. })
  990. it('should not call the brand variations handler for unbranded projects', function(done) {
  991. this.res.render = (pageName, opts) => {
  992. this.BrandVariationsHandler.getBrandVariationById.called.should.equal(
  993. false
  994. )
  995. done()
  996. }
  997. this.ProjectController.loadEditor(this.req, this.res)
  998. })
  999. it('should expose the brand variation details as locals for branded projects', function(done) {
  1000. this.ProjectGetter.getProject.callsArgWith(2, null, this.brandedProject)
  1001. this.res.render = (pageName, opts) => {
  1002. opts.brandVariation.should.deep.equal(this.brandVariationDetails)
  1003. done()
  1004. }
  1005. this.ProjectController.loadEditor(this.req, this.res)
  1006. })
  1007. it('flushes the project to TPDS if a flush is pending', function(done) {
  1008. this.res.render = () => {
  1009. this.TpdsProjectFlusher.flushProjectToTpdsIfNeeded.should.have.been.calledWith(
  1010. this.project_id
  1011. )
  1012. done()
  1013. }
  1014. this.ProjectController.loadEditor(this.req, this.res)
  1015. })
  1016. describe('wsUrl', function() {
  1017. function checkLoadEditorWsMetric(metric) {
  1018. it(`should inc metric ${metric}`, function(done) {
  1019. this.res.render = () => {
  1020. this.Metrics.inc.calledWith(metric).should.equal(true)
  1021. done()
  1022. }
  1023. this.ProjectController.loadEditor(this.req, this.res)
  1024. })
  1025. }
  1026. function checkWsFallback(isBeta, isV2) {
  1027. describe('with ws=fallback', function() {
  1028. beforeEach(function() {
  1029. this.req.query = {}
  1030. this.req.query.ws = 'fallback'
  1031. })
  1032. it('should unset the wsUrl', function(done) {
  1033. this.res.render = (pageName, opts) => {
  1034. ;(opts.wsUrl || '/socket.io').should.equal('/socket.io')
  1035. done()
  1036. }
  1037. this.ProjectController.loadEditor(this.req, this.res)
  1038. })
  1039. checkLoadEditorWsMetric(
  1040. `load-editor-ws${isBeta ? '-beta' : ''}${
  1041. isV2 ? '-v2' : ''
  1042. }-fallback`
  1043. )
  1044. })
  1045. }
  1046. beforeEach(function() {
  1047. this.settings.wsUrl = '/other.socket.io'
  1048. })
  1049. it('should set the custom wsUrl', function(done) {
  1050. this.res.render = (pageName, opts) => {
  1051. opts.wsUrl.should.equal('/other.socket.io')
  1052. done()
  1053. }
  1054. this.ProjectController.loadEditor(this.req, this.res)
  1055. })
  1056. checkLoadEditorWsMetric('load-editor-ws')
  1057. checkWsFallback(false)
  1058. describe('beta program', function() {
  1059. beforeEach(function() {
  1060. this.settings.wsUrlBeta = '/beta.socket.io'
  1061. })
  1062. describe('for a normal user', function() {
  1063. it('should set the normal custom wsUrl', function(done) {
  1064. this.res.render = (pageName, opts) => {
  1065. opts.wsUrl.should.equal('/other.socket.io')
  1066. done()
  1067. }
  1068. this.ProjectController.loadEditor(this.req, this.res)
  1069. })
  1070. checkLoadEditorWsMetric('load-editor-ws')
  1071. checkWsFallback(false)
  1072. })
  1073. describe('for a beta user', function() {
  1074. beforeEach(function() {
  1075. this.user.betaProgram = true
  1076. })
  1077. it('should set the beta wsUrl', function(done) {
  1078. this.res.render = (pageName, opts) => {
  1079. opts.wsUrl.should.equal('/beta.socket.io')
  1080. done()
  1081. }
  1082. this.ProjectController.loadEditor(this.req, this.res)
  1083. })
  1084. checkLoadEditorWsMetric('load-editor-ws-beta')
  1085. checkWsFallback(true)
  1086. })
  1087. })
  1088. describe('v2-rollout', function() {
  1089. beforeEach(function() {
  1090. this.settings.wsUrlBeta = '/beta.socket.io'
  1091. this.settings.wsUrlV2 = '/socket.io.v2'
  1092. })
  1093. function checkNonMatch() {
  1094. it('should set the normal custom wsUrl', function(done) {
  1095. this.res.render = (pageName, opts) => {
  1096. opts.wsUrl.should.equal('/other.socket.io')
  1097. done()
  1098. }
  1099. this.ProjectController.loadEditor(this.req, this.res)
  1100. })
  1101. checkLoadEditorWsMetric('load-editor-ws')
  1102. checkWsFallback(false)
  1103. }
  1104. function checkMatch() {
  1105. it('should set the v2 wsUrl', function(done) {
  1106. this.res.render = (pageName, opts) => {
  1107. opts.wsUrl.should.equal('/socket.io.v2')
  1108. done()
  1109. }
  1110. this.ProjectController.loadEditor(this.req, this.res)
  1111. })
  1112. checkLoadEditorWsMetric('load-editor-ws-v2')
  1113. checkWsFallback(false, true)
  1114. }
  1115. function checkForBetaUser() {
  1116. describe('for a beta user', function() {
  1117. beforeEach(function() {
  1118. this.user.betaProgram = true
  1119. })
  1120. it('should set the beta wsUrl', function(done) {
  1121. this.res.render = (pageName, opts) => {
  1122. opts.wsUrl.should.equal('/beta.socket.io')
  1123. done()
  1124. }
  1125. this.ProjectController.loadEditor(this.req, this.res)
  1126. })
  1127. checkLoadEditorWsMetric('load-editor-ws-beta')
  1128. checkWsFallback(true)
  1129. })
  1130. }
  1131. describe('when the roll out percentage is 0', function() {
  1132. beforeEach(function() {
  1133. this.settings.wsUrlV2Percentage = 0
  1134. })
  1135. describe('when the projectId does not match (0)', function() {
  1136. beforeEach(function() {
  1137. this.req.params.Project_id = ObjectId.createFromTime(0)
  1138. })
  1139. checkNonMatch()
  1140. })
  1141. describe('when the projectId does not match (42)', function() {
  1142. beforeEach(function() {
  1143. this.req.params.Project_id = ObjectId.createFromTime(42)
  1144. })
  1145. checkNonMatch()
  1146. })
  1147. checkForBetaUser()
  1148. })
  1149. describe('when the roll out percentage is 1', function() {
  1150. beforeEach(function() {
  1151. this.settings.wsUrlV2Percentage = 1
  1152. })
  1153. describe('when the projectId matches (0)', function() {
  1154. beforeEach(function() {
  1155. this.req.params.Project_id = ObjectId.createFromTime(0)
  1156. })
  1157. checkMatch()
  1158. checkForBetaUser()
  1159. })
  1160. describe('when the projectId does not match (1)', function() {
  1161. beforeEach(function() {
  1162. this.req.params.Project_id = ObjectId.createFromTime(1)
  1163. })
  1164. checkNonMatch()
  1165. checkForBetaUser()
  1166. })
  1167. describe('when the projectId does not match (42)', function() {
  1168. beforeEach(function() {
  1169. this.req.params.Project_id = ObjectId.createFromTime(42)
  1170. })
  1171. checkNonMatch()
  1172. })
  1173. })
  1174. describe('when the roll out percentage is 10', function() {
  1175. beforeEach(function() {
  1176. this.settings.wsUrlV2Percentage = 10
  1177. })
  1178. describe('when the projectId matches (0)', function() {
  1179. beforeEach(function() {
  1180. this.req.params.Project_id = ObjectId.createFromTime(0)
  1181. })
  1182. checkMatch()
  1183. })
  1184. describe('when the projectId matches (9)', function() {
  1185. beforeEach(function() {
  1186. this.req.params.Project_id = ObjectId.createFromTime(9)
  1187. })
  1188. checkMatch()
  1189. checkForBetaUser()
  1190. })
  1191. describe('when the projectId does not match (10)', function() {
  1192. beforeEach(function() {
  1193. this.req.params.Project_id = ObjectId.createFromTime(10)
  1194. })
  1195. checkNonMatch()
  1196. })
  1197. describe('when the projectId does not match (42)', function() {
  1198. beforeEach(function() {
  1199. this.req.params.Project_id = ObjectId.createFromTime(42)
  1200. })
  1201. checkNonMatch()
  1202. checkForBetaUser()
  1203. })
  1204. })
  1205. describe('when the roll out percentage is 100', function() {
  1206. beforeEach(function() {
  1207. this.settings.wsUrlV2Percentage = 100
  1208. })
  1209. describe('when the projectId matches (0)', function() {
  1210. beforeEach(function() {
  1211. this.req.params.Project_id = ObjectId.createFromTime(0)
  1212. })
  1213. checkMatch()
  1214. checkForBetaUser()
  1215. })
  1216. describe('when the projectId matches (10)', function() {
  1217. beforeEach(function() {
  1218. this.req.params.Project_id = ObjectId.createFromTime(10)
  1219. })
  1220. checkMatch()
  1221. })
  1222. describe('when the projectId matches (42)', function() {
  1223. beforeEach(function() {
  1224. this.req.params.Project_id = ObjectId.createFromTime(42)
  1225. })
  1226. checkMatch()
  1227. })
  1228. describe('when the projectId matches (99)', function() {
  1229. beforeEach(function() {
  1230. this.req.params.Project_id = ObjectId.createFromTime(99)
  1231. })
  1232. checkMatch()
  1233. })
  1234. })
  1235. })
  1236. })
  1237. })
  1238. describe('userProjectsJson', function() {
  1239. beforeEach(function(done) {
  1240. const projects = [
  1241. {
  1242. archived: true,
  1243. trashed: true,
  1244. id: 'a',
  1245. name: 'A',
  1246. accessLevel: 'a',
  1247. somethingElse: 1
  1248. },
  1249. {
  1250. archived: false,
  1251. id: 'b',
  1252. name: 'B',
  1253. accessLevel: 'b',
  1254. somethingElse: 1
  1255. },
  1256. {
  1257. archived: false,
  1258. trashed: true,
  1259. id: 'c',
  1260. name: 'C',
  1261. accessLevel: 'c',
  1262. somethingElse: 1
  1263. },
  1264. {
  1265. archived: false,
  1266. trashed: false,
  1267. id: 'd',
  1268. name: 'D',
  1269. accessLevel: 'd',
  1270. somethingElse: 1
  1271. }
  1272. ]
  1273. this.ProjectHelper.isArchivedOrTrashed
  1274. .withArgs(projects[0], this.user._id)
  1275. .returns(true)
  1276. this.ProjectHelper.isArchivedOrTrashed
  1277. .withArgs(projects[1], this.user._id)
  1278. .returns(false)
  1279. this.ProjectHelper.isArchivedOrTrashed
  1280. .withArgs(projects[2], this.user._id)
  1281. .returns(true)
  1282. this.ProjectHelper.isArchivedOrTrashed
  1283. .withArgs(projects[3], this.user._id)
  1284. .returns(false)
  1285. this.ProjectGetter.findAllUsersProjects = sinon
  1286. .stub()
  1287. .callsArgWith(2, null, [])
  1288. this.ProjectController._buildProjectList = sinon.stub().returns(projects)
  1289. this.AuthenticationController.getLoggedInUserId = sinon
  1290. .stub()
  1291. .returns(this.user._id)
  1292. done()
  1293. })
  1294. it('should produce a list of projects', function(done) {
  1295. this.res.json = data => {
  1296. expect(data).to.deep.equal({
  1297. projects: [
  1298. { _id: 'b', name: 'B', accessLevel: 'b' },
  1299. { _id: 'd', name: 'D', accessLevel: 'd' }
  1300. ]
  1301. })
  1302. done()
  1303. }
  1304. this.ProjectController.userProjectsJson(this.req, this.res, this.next)
  1305. })
  1306. })
  1307. describe('projectEntitiesJson', function() {
  1308. beforeEach(function() {
  1309. this.AuthenticationController.getLoggedInUserId = sinon
  1310. .stub()
  1311. .returns('abc')
  1312. this.req.params = { Project_id: 'abcd' }
  1313. this.project = { _id: 'abcd' }
  1314. this.docs = [
  1315. { path: '/things/b.txt', doc: true },
  1316. { path: '/main.tex', doc: true }
  1317. ]
  1318. this.files = [{ path: '/things/a.txt' }]
  1319. this.ProjectGetter.getProject = sinon
  1320. .stub()
  1321. .callsArgWith(1, null, this.project)
  1322. this.ProjectEntityHandler.getAllEntitiesFromProject = sinon
  1323. .stub()
  1324. .callsArgWith(1, null, this.docs, this.files)
  1325. })
  1326. it('should produce a list of entities', function(done) {
  1327. this.res.json = data => {
  1328. expect(data).to.deep.equal({
  1329. project_id: 'abcd',
  1330. entities: [
  1331. { path: '/main.tex', type: 'doc' },
  1332. { path: '/things/a.txt', type: 'file' },
  1333. { path: '/things/b.txt', type: 'doc' }
  1334. ]
  1335. })
  1336. expect(this.ProjectGetter.getProject.callCount).to.equal(1)
  1337. expect(
  1338. this.ProjectEntityHandler.getAllEntitiesFromProject.callCount
  1339. ).to.equal(1)
  1340. done()
  1341. }
  1342. this.ProjectController.projectEntitiesJson(this.req, this.res, this.next)
  1343. })
  1344. })
  1345. describe('_buildProjectViewModel', function() {
  1346. beforeEach(function() {
  1347. this.ProjectHelper.isArchived.returns(false)
  1348. this.ProjectHelper.isTrashed.returns(false)
  1349. this.project = {
  1350. _id: 'abcd',
  1351. name: 'netsenits',
  1352. lastUpdated: 1,
  1353. lastUpdatedBy: 2,
  1354. publicAccesLevel: 'private',
  1355. archived: false,
  1356. owner_ref: 'defg',
  1357. tokens: {
  1358. readAndWrite: '1abcd',
  1359. readAndWritePrefix: '1',
  1360. readOnly: 'neiotsranteoia'
  1361. }
  1362. }
  1363. })
  1364. describe('project not being archived or trashed', function() {
  1365. it('should produce a model of the project', function() {
  1366. const result = this.ProjectController._buildProjectViewModel(
  1367. this.project,
  1368. 'readAndWrite',
  1369. 'owner',
  1370. this.user._id
  1371. )
  1372. expect(result).to.exist
  1373. expect(result).to.be.an('object')
  1374. expect(result).to.deep.equal({
  1375. id: 'abcd',
  1376. name: 'netsenits',
  1377. lastUpdated: 1,
  1378. lastUpdatedBy: 2,
  1379. publicAccessLevel: 'private',
  1380. accessLevel: 'readAndWrite',
  1381. source: 'owner',
  1382. archived: false,
  1383. trashed: false,
  1384. owner_ref: 'defg',
  1385. isV1Project: false
  1386. })
  1387. })
  1388. })
  1389. describe('project being simultaneously archived and trashed', function() {
  1390. beforeEach(function() {
  1391. this.ProjectHelper.isArchived.returns(true)
  1392. this.ProjectHelper.isTrashed.returns(true)
  1393. })
  1394. it('should produce a model of the project', function() {
  1395. const result = this.ProjectController._buildProjectViewModel(
  1396. this.project,
  1397. 'readAndWrite',
  1398. 'owner',
  1399. this.user._id
  1400. )
  1401. expect(result).to.exist
  1402. expect(result).to.be.an('object')
  1403. expect(result).to.deep.equal({
  1404. id: 'abcd',
  1405. name: 'netsenits',
  1406. lastUpdated: 1,
  1407. lastUpdatedBy: 2,
  1408. publicAccessLevel: 'private',
  1409. accessLevel: 'readAndWrite',
  1410. source: 'owner',
  1411. archived: true,
  1412. trashed: false,
  1413. owner_ref: 'defg',
  1414. isV1Project: false
  1415. })
  1416. })
  1417. })
  1418. describe('when token-read-only access', function() {
  1419. it('should redact the owner and last-updated data', function() {
  1420. const result = this.ProjectController._buildProjectViewModel(
  1421. this.project,
  1422. 'readOnly',
  1423. 'token',
  1424. this.user._id
  1425. )
  1426. expect(result).to.exist
  1427. expect(result).to.be.an('object')
  1428. expect(result).to.deep.equal({
  1429. id: 'abcd',
  1430. name: 'netsenits',
  1431. lastUpdated: 1,
  1432. lastUpdatedBy: null,
  1433. publicAccessLevel: 'private',
  1434. accessLevel: 'readOnly',
  1435. source: 'token',
  1436. archived: false,
  1437. trashed: false,
  1438. owner_ref: null,
  1439. isV1Project: false
  1440. })
  1441. })
  1442. })
  1443. })
  1444. describe('_isInPercentageRollout', function() {
  1445. before(function() {
  1446. this.ids = [
  1447. '5a05cd7621f9fe22be131740',
  1448. '5a05cd7821f9fe22be131741',
  1449. '5a05cd7921f9fe22be131742',
  1450. '5a05cd7a21f9fe22be131743',
  1451. '5a05cd7b21f9fe22be131744',
  1452. '5a05cd7c21f9fe22be131745',
  1453. '5a05cd7d21f9fe22be131746',
  1454. '5a05cd7e21f9fe22be131747',
  1455. '5a05cd7f21f9fe22be131748',
  1456. '5a05cd8021f9fe22be131749',
  1457. '5a05cd8021f9fe22be13174a',
  1458. '5a05cd8121f9fe22be13174b',
  1459. '5a05cd8221f9fe22be13174c',
  1460. '5a05cd8221f9fe22be13174d',
  1461. '5a05cd8321f9fe22be13174e',
  1462. '5a05cd8321f9fe22be13174f',
  1463. '5a05cd8421f9fe22be131750',
  1464. '5a05cd8421f9fe22be131751',
  1465. '5a05cd8421f9fe22be131752',
  1466. '5a05cd8521f9fe22be131753'
  1467. ]
  1468. })
  1469. it('should produce the expected results', function() {
  1470. expect(
  1471. this.ids.map(i =>
  1472. this.ProjectController._isInPercentageRollout('abcd', i, 50)
  1473. )
  1474. ).to.deep.equal([
  1475. false,
  1476. false,
  1477. false,
  1478. false,
  1479. false,
  1480. false,
  1481. true,
  1482. false,
  1483. true,
  1484. true,
  1485. true,
  1486. true,
  1487. true,
  1488. true,
  1489. false,
  1490. false,
  1491. false,
  1492. true,
  1493. false,
  1494. true
  1495. ])
  1496. expect(
  1497. this.ids.map(i =>
  1498. this.ProjectController._isInPercentageRollout('efgh', i, 50)
  1499. )
  1500. ).to.deep.equal([
  1501. false,
  1502. false,
  1503. false,
  1504. false,
  1505. true,
  1506. false,
  1507. false,
  1508. true,
  1509. false,
  1510. false,
  1511. true,
  1512. true,
  1513. true,
  1514. false,
  1515. true,
  1516. false,
  1517. true,
  1518. true,
  1519. false,
  1520. false
  1521. ])
  1522. })
  1523. })
  1524. })