PersistenceManagerTests.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. const sinon = require('sinon')
  2. const modulePath = '../../../../app/js/PersistenceManager.js'
  3. const SandboxedModule = require('sandboxed-module')
  4. const Errors = require('../../../../app/js/Errors')
  5. describe('PersistenceManager', function () {
  6. beforeEach(function () {
  7. this.request = sinon.stub()
  8. this.request.defaults = () => this.request
  9. this.Metrics = {
  10. Timer: class Timer {},
  11. inc: sinon.stub(),
  12. }
  13. this.Metrics.Timer.prototype.done = sinon.stub()
  14. this.Settings = {}
  15. this.PersistenceManager = SandboxedModule.require(modulePath, {
  16. requires: {
  17. requestretry: this.request,
  18. '@overleaf/settings': this.Settings,
  19. './Metrics': this.Metrics,
  20. './Errors': Errors,
  21. },
  22. })
  23. this.project_id = 'project-id-123'
  24. this.projectHistoryId = 'history-id-123'
  25. this.doc_id = 'doc-id-123'
  26. this.lines = ['one', 'two', 'three']
  27. this.version = 42
  28. this.callback = sinon.stub()
  29. this.ranges = { comments: 'mock', entries: 'mock' }
  30. this.pathname = '/a/b/c.tex'
  31. this.lastUpdatedAt = Date.now()
  32. this.lastUpdatedBy = 'last-author-id'
  33. this.doc = {
  34. lines: this.lines,
  35. version: this.version,
  36. ranges: this.ranges,
  37. projectHistoryId: this.projectHistoryId,
  38. pathname: this.pathname,
  39. historyRangesSupport: false,
  40. }
  41. this.Settings.apis = {
  42. web: {
  43. url: (this.url = 'www.example.com'),
  44. user: (this.user = 'sharelatex'),
  45. pass: (this.pass = 'password'),
  46. },
  47. }
  48. })
  49. describe('getDoc', function () {
  50. beforeEach(function () {
  51. this.webResponse = {
  52. lines: this.lines,
  53. version: this.version,
  54. ranges: this.ranges,
  55. pathname: this.pathname,
  56. projectHistoryId: this.projectHistoryId,
  57. }
  58. })
  59. describe('with a successful response from the web api', function () {
  60. beforeEach(function () {
  61. this.request.callsArgWith(
  62. 1,
  63. null,
  64. { statusCode: 200 },
  65. JSON.stringify(this.webResponse)
  66. )
  67. this.PersistenceManager.getDoc(
  68. this.project_id,
  69. this.doc_id,
  70. this.callback
  71. )
  72. })
  73. it('should call the web api', function () {
  74. this.request
  75. .calledWith({
  76. url: `${this.url}/project/${this.project_id}/doc/${this.doc_id}`,
  77. method: 'GET',
  78. headers: {
  79. accept: 'application/json',
  80. },
  81. auth: {
  82. user: this.user,
  83. pass: this.pass,
  84. sendImmediately: true,
  85. },
  86. jar: false,
  87. timeout: 5000,
  88. })
  89. .should.equal(true)
  90. })
  91. it('should call the callback with the doc lines, version and ranges', function () {
  92. this.callback.should.have.been.calledWith(null, this.doc)
  93. })
  94. it('should time the execution', function () {
  95. this.Metrics.Timer.prototype.done.called.should.equal(true)
  96. })
  97. it('should increment the metric', function () {
  98. this.Metrics.inc
  99. .calledWith('getDoc', 1, { status: 200 })
  100. .should.equal(true)
  101. })
  102. })
  103. describe('with the peek option', function () {
  104. beforeEach(function () {
  105. this.request.yields(
  106. null,
  107. { statusCode: 200 },
  108. JSON.stringify(this.webResponse)
  109. )
  110. this.PersistenceManager.getDoc(
  111. this.project_id,
  112. this.doc_id,
  113. { peek: true },
  114. this.callback
  115. )
  116. })
  117. it('should call the web api with a peek param', function () {
  118. this.request
  119. .calledWith({
  120. url: `${this.url}/project/${this.project_id}/doc/${this.doc_id}`,
  121. qs: { peek: 'true' },
  122. method: 'GET',
  123. headers: {
  124. accept: 'application/json',
  125. },
  126. auth: {
  127. user: this.user,
  128. pass: this.pass,
  129. sendImmediately: true,
  130. },
  131. jar: false,
  132. timeout: 5000,
  133. })
  134. .should.equal(true)
  135. })
  136. })
  137. describe('when request returns an error', function () {
  138. beforeEach(function () {
  139. this.error = new Error('oops')
  140. this.error.code = 'EOOPS'
  141. this.request.callsArgWith(1, this.error, null, null)
  142. this.PersistenceManager.getDoc(
  143. this.project_id,
  144. this.doc_id,
  145. this.callback
  146. )
  147. })
  148. it('should return a generic connection error', function () {
  149. this.callback
  150. .calledWith(
  151. sinon.match
  152. .instanceOf(Error)
  153. .and(sinon.match.has('message', 'error connecting to web API'))
  154. )
  155. .should.equal(true)
  156. })
  157. it('should time the execution', function () {
  158. this.Metrics.Timer.prototype.done.called.should.equal(true)
  159. })
  160. it('should increment the metric', function () {
  161. this.Metrics.inc
  162. .calledWith('getDoc', 1, { status: 'EOOPS' })
  163. .should.equal(true)
  164. })
  165. })
  166. describe('when the request returns 404', function () {
  167. beforeEach(function () {
  168. this.request.callsArgWith(1, null, { statusCode: 404 }, '')
  169. this.PersistenceManager.getDoc(
  170. this.project_id,
  171. this.doc_id,
  172. this.callback
  173. )
  174. })
  175. it('should return a NotFoundError', function () {
  176. this.callback
  177. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  178. .should.equal(true)
  179. })
  180. it('should time the execution', function () {
  181. this.Metrics.Timer.prototype.done.called.should.equal(true)
  182. })
  183. it('should increment the metric', function () {
  184. this.Metrics.inc
  185. .calledWith('getDoc', 1, { status: 404 })
  186. .should.equal(true)
  187. })
  188. })
  189. describe('when the request returns 413', function () {
  190. beforeEach(function () {
  191. this.request.callsArgWith(1, null, { statusCode: 413 }, '')
  192. this.PersistenceManager.getDoc(
  193. this.project_id,
  194. this.doc_id,
  195. this.callback
  196. )
  197. })
  198. it('should return a FileTooLargeError', function () {
  199. this.callback
  200. .calledWith(sinon.match.instanceOf(Errors.FileTooLargeError))
  201. .should.equal(true)
  202. })
  203. it('should time the execution', function () {
  204. this.Metrics.Timer.prototype.done.called.should.equal(true)
  205. })
  206. it('should increment the metric', function () {
  207. this.Metrics.inc
  208. .calledWith('getDoc', 1, { status: 413 })
  209. .should.equal(true)
  210. })
  211. })
  212. describe('when the request returns an error status code', function () {
  213. beforeEach(function () {
  214. this.request.callsArgWith(1, null, { statusCode: 500 }, '')
  215. this.PersistenceManager.getDoc(
  216. this.project_id,
  217. this.doc_id,
  218. this.callback
  219. )
  220. })
  221. it('should return an error', function () {
  222. this.callback
  223. .calledWith(sinon.match.instanceOf(Error))
  224. .should.equal(true)
  225. })
  226. it('should time the execution', function () {
  227. this.Metrics.Timer.prototype.done.called.should.equal(true)
  228. })
  229. it('should increment the metric', function () {
  230. this.Metrics.inc
  231. .calledWith('getDoc', 1, { status: 500 })
  232. .should.equal(true)
  233. })
  234. })
  235. describe('when request returns an doc without lines', function () {
  236. beforeEach(function () {
  237. delete this.webResponse.lines
  238. this.request.callsArgWith(
  239. 1,
  240. null,
  241. { statusCode: 200 },
  242. JSON.stringify(this.webResponse)
  243. )
  244. this.PersistenceManager.getDoc(
  245. this.project_id,
  246. this.doc_id,
  247. this.callback
  248. )
  249. })
  250. it('should return and error', function () {
  251. this.callback
  252. .calledWith(sinon.match.instanceOf(Error))
  253. .should.equal(true)
  254. })
  255. })
  256. describe('when request returns an doc without a version', function () {
  257. beforeEach(function () {
  258. delete this.webResponse.version
  259. this.request.callsArgWith(
  260. 1,
  261. null,
  262. { statusCode: 200 },
  263. JSON.stringify(this.webResponse)
  264. )
  265. this.PersistenceManager.getDoc(
  266. this.project_id,
  267. this.doc_id,
  268. this.callback
  269. )
  270. })
  271. it('should return and error', function () {
  272. this.callback
  273. .calledWith(sinon.match.instanceOf(Error))
  274. .should.equal(true)
  275. })
  276. })
  277. describe('when request returns an doc without a pathname', function () {
  278. beforeEach(function () {
  279. delete this.webResponse.pathname
  280. this.request.callsArgWith(
  281. 1,
  282. null,
  283. { statusCode: 200 },
  284. JSON.stringify(this.webResponse)
  285. )
  286. this.PersistenceManager.getDoc(
  287. this.project_id,
  288. this.doc_id,
  289. this.callback
  290. )
  291. })
  292. it('should return and error', function () {
  293. this.callback
  294. .calledWith(sinon.match.instanceOf(Error))
  295. .should.equal(true)
  296. })
  297. })
  298. })
  299. describe('setDoc', function () {
  300. describe('with a successful response from the web api', function () {
  301. beforeEach(function () {
  302. this.request.callsArgWith(1, null, { statusCode: 200 })
  303. this.PersistenceManager.setDoc(
  304. this.project_id,
  305. this.doc_id,
  306. this.lines,
  307. this.version,
  308. this.ranges,
  309. this.lastUpdatedAt,
  310. this.lastUpdatedBy,
  311. this.callback
  312. )
  313. })
  314. it('should call the web api', function () {
  315. this.request
  316. .calledWith({
  317. url: `${this.url}/project/${this.project_id}/doc/${this.doc_id}`,
  318. json: {
  319. lines: this.lines,
  320. version: this.version,
  321. ranges: this.ranges,
  322. lastUpdatedAt: this.lastUpdatedAt,
  323. lastUpdatedBy: this.lastUpdatedBy,
  324. },
  325. method: 'POST',
  326. auth: {
  327. user: this.user,
  328. pass: this.pass,
  329. sendImmediately: true,
  330. },
  331. jar: false,
  332. timeout: 5000,
  333. })
  334. .should.equal(true)
  335. })
  336. it('should call the callback without error', function () {
  337. this.callback.calledWith(null).should.equal(true)
  338. })
  339. it('should time the execution', function () {
  340. this.Metrics.Timer.prototype.done.called.should.equal(true)
  341. })
  342. it('should increment the metric', function () {
  343. this.Metrics.inc
  344. .calledWith('setDoc', 1, { status: 200 })
  345. .should.equal(true)
  346. })
  347. })
  348. describe('when request returns an error', function () {
  349. beforeEach(function () {
  350. this.error = new Error('oops')
  351. this.error.code = 'EOOPS'
  352. this.request.callsArgWith(1, this.error, null, null)
  353. this.PersistenceManager.setDoc(
  354. this.project_id,
  355. this.doc_id,
  356. this.lines,
  357. this.version,
  358. this.ranges,
  359. this.lastUpdatedAt,
  360. this.lastUpdatedBy,
  361. this.callback
  362. )
  363. })
  364. it('should return a generic connection error', function () {
  365. this.callback
  366. .calledWith(
  367. sinon.match
  368. .instanceOf(Error)
  369. .and(sinon.match.has('message', 'error connecting to web API'))
  370. )
  371. .should.equal(true)
  372. })
  373. it('should time the execution', function () {
  374. this.Metrics.Timer.prototype.done.called.should.equal(true)
  375. })
  376. it('should increment the metric', function () {
  377. this.Metrics.inc
  378. .calledWith('setDoc', 1, { status: 'EOOPS' })
  379. .should.equal(true)
  380. })
  381. })
  382. describe('when the request returns 404', function () {
  383. beforeEach(function () {
  384. this.request.callsArgWith(1, null, { statusCode: 404 }, '')
  385. this.PersistenceManager.setDoc(
  386. this.project_id,
  387. this.doc_id,
  388. this.lines,
  389. this.version,
  390. this.ranges,
  391. this.lastUpdatedAt,
  392. this.lastUpdatedBy,
  393. this.callback
  394. )
  395. })
  396. it('should return a NotFoundError', function () {
  397. this.callback
  398. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  399. .should.equal(true)
  400. })
  401. it('should time the execution', function () {
  402. this.Metrics.Timer.prototype.done.called.should.equal(true)
  403. })
  404. it('should increment the metric', function () {
  405. this.Metrics.inc
  406. .calledWith('setDoc', 1, { status: 404 })
  407. .should.equal(true)
  408. })
  409. })
  410. describe('when the request returns 413', function () {
  411. beforeEach(function () {
  412. this.request.callsArgWith(1, null, { statusCode: 413 }, '')
  413. this.PersistenceManager.setDoc(
  414. this.project_id,
  415. this.doc_id,
  416. this.lines,
  417. this.version,
  418. this.ranges,
  419. this.lastUpdatedAt,
  420. this.lastUpdatedBy,
  421. this.callback
  422. )
  423. })
  424. it('should return a FileTooLargeError', function () {
  425. this.callback
  426. .calledWith(sinon.match.instanceOf(Errors.FileTooLargeError))
  427. .should.equal(true)
  428. })
  429. it('should time the execution', function () {
  430. this.Metrics.Timer.prototype.done.called.should.equal(true)
  431. })
  432. it('should increment the metric', function () {
  433. this.Metrics.inc
  434. .calledWith('setDoc', 1, { status: 413 })
  435. .should.equal(true)
  436. })
  437. })
  438. describe('when the request returns an error status code', function () {
  439. beforeEach(function () {
  440. this.request.callsArgWith(1, null, { statusCode: 500 }, '')
  441. this.PersistenceManager.setDoc(
  442. this.project_id,
  443. this.doc_id,
  444. this.lines,
  445. this.version,
  446. this.ranges,
  447. this.lastUpdatedAt,
  448. this.lastUpdatedBy,
  449. this.callback
  450. )
  451. })
  452. it('should return an error', function () {
  453. this.callback
  454. .calledWith(sinon.match.instanceOf(Error))
  455. .should.equal(true)
  456. })
  457. it('should time the execution', function () {
  458. this.Metrics.Timer.prototype.done.called.should.equal(true)
  459. })
  460. it('should increment the metric', function () {
  461. this.Metrics.inc
  462. .calledWith('setDoc', 1, { status: 500 })
  463. .should.equal(true)
  464. })
  465. })
  466. })
  467. })