ProjectControllerTests.js 44 KB

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