ProjectControllerTests.js 57 KB

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