ProjectControllerTests.js 49 KB

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