PersistenceManagerTests.js 14 KB

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