PersistenceManagerTests.js 14 KB

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