PersistenceManagerTests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 = 'sharelatex'),
  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 an error status code', function () {
  191. beforeEach(function () {
  192. this.request.callsArgWith(1, null, { statusCode: 500 }, '')
  193. this.PersistenceManager.getDoc(
  194. this.project_id,
  195. this.doc_id,
  196. this.callback
  197. )
  198. })
  199. it('should return an error', function () {
  200. this.callback
  201. .calledWith(sinon.match.instanceOf(Error))
  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: 500 })
  210. .should.equal(true)
  211. })
  212. })
  213. describe('when request returns an doc without lines', function () {
  214. beforeEach(function () {
  215. delete this.webResponse.lines
  216. this.request.callsArgWith(
  217. 1,
  218. null,
  219. { statusCode: 200 },
  220. JSON.stringify(this.webResponse)
  221. )
  222. this.PersistenceManager.getDoc(
  223. this.project_id,
  224. this.doc_id,
  225. this.callback
  226. )
  227. })
  228. it('should return and error', function () {
  229. this.callback
  230. .calledWith(sinon.match.instanceOf(Error))
  231. .should.equal(true)
  232. })
  233. })
  234. describe('when request returns an doc without a version', function () {
  235. beforeEach(function () {
  236. delete this.webResponse.version
  237. this.request.callsArgWith(
  238. 1,
  239. null,
  240. { statusCode: 200 },
  241. JSON.stringify(this.webResponse)
  242. )
  243. this.PersistenceManager.getDoc(
  244. this.project_id,
  245. this.doc_id,
  246. this.callback
  247. )
  248. })
  249. it('should return and error', function () {
  250. this.callback
  251. .calledWith(sinon.match.instanceOf(Error))
  252. .should.equal(true)
  253. })
  254. })
  255. describe('when request returns an doc without a pathname', function () {
  256. beforeEach(function () {
  257. delete this.webResponse.pathname
  258. this.request.callsArgWith(
  259. 1,
  260. null,
  261. { statusCode: 200 },
  262. JSON.stringify(this.webResponse)
  263. )
  264. this.PersistenceManager.getDoc(
  265. this.project_id,
  266. this.doc_id,
  267. this.callback
  268. )
  269. })
  270. it('should return and error', function () {
  271. this.callback
  272. .calledWith(sinon.match.instanceOf(Error))
  273. .should.equal(true)
  274. })
  275. })
  276. })
  277. describe('setDoc', function () {
  278. describe('with a successful response from the web api', function () {
  279. beforeEach(function () {
  280. this.request.callsArgWith(1, null, { statusCode: 200 })
  281. this.PersistenceManager.setDoc(
  282. this.project_id,
  283. this.doc_id,
  284. this.lines,
  285. this.version,
  286. this.ranges,
  287. this.lastUpdatedAt,
  288. this.lastUpdatedBy,
  289. this.callback
  290. )
  291. })
  292. it('should call the web api', function () {
  293. this.request
  294. .calledWith({
  295. url: `${this.url}/project/${this.project_id}/doc/${this.doc_id}`,
  296. json: {
  297. lines: this.lines,
  298. version: this.version,
  299. ranges: this.ranges,
  300. lastUpdatedAt: this.lastUpdatedAt,
  301. lastUpdatedBy: this.lastUpdatedBy,
  302. },
  303. method: 'POST',
  304. auth: {
  305. user: this.user,
  306. pass: this.pass,
  307. sendImmediately: true,
  308. },
  309. jar: false,
  310. timeout: 5000,
  311. })
  312. .should.equal(true)
  313. })
  314. it('should call the callback without error', function () {
  315. this.callback.calledWith(null).should.equal(true)
  316. })
  317. it('should time the execution', function () {
  318. this.Metrics.Timer.prototype.done.called.should.equal(true)
  319. })
  320. it('should increment the metric', function () {
  321. this.Metrics.inc
  322. .calledWith('setDoc', 1, { status: 200 })
  323. .should.equal(true)
  324. })
  325. })
  326. describe('when request returns an error', function () {
  327. beforeEach(function () {
  328. this.error = new Error('oops')
  329. this.error.code = 'EOOPS'
  330. this.request.callsArgWith(1, this.error, null, null)
  331. this.PersistenceManager.setDoc(
  332. this.project_id,
  333. this.doc_id,
  334. this.lines,
  335. this.version,
  336. this.ranges,
  337. this.lastUpdatedAt,
  338. this.lastUpdatedBy,
  339. this.callback
  340. )
  341. })
  342. it('should return a generic connection error', function () {
  343. this.callback
  344. .calledWith(
  345. sinon.match
  346. .instanceOf(Error)
  347. .and(sinon.match.has('message', 'error connecting to web API'))
  348. )
  349. .should.equal(true)
  350. })
  351. it('should time the execution', function () {
  352. this.Metrics.Timer.prototype.done.called.should.equal(true)
  353. })
  354. it('should increment the metric', function () {
  355. this.Metrics.inc
  356. .calledWith('setDoc', 1, { status: 'EOOPS' })
  357. .should.equal(true)
  358. })
  359. })
  360. describe('when the request returns 404', function () {
  361. beforeEach(function () {
  362. this.request.callsArgWith(1, null, { statusCode: 404 }, '')
  363. this.PersistenceManager.setDoc(
  364. this.project_id,
  365. this.doc_id,
  366. this.lines,
  367. this.version,
  368. this.ranges,
  369. this.lastUpdatedAt,
  370. this.lastUpdatedBy,
  371. this.callback
  372. )
  373. })
  374. it('should return a NotFoundError', function () {
  375. this.callback
  376. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  377. .should.equal(true)
  378. })
  379. it('should time the execution', function () {
  380. this.Metrics.Timer.prototype.done.called.should.equal(true)
  381. })
  382. it('should increment the metric', function () {
  383. this.Metrics.inc
  384. .calledWith('setDoc', 1, { status: 404 })
  385. .should.equal(true)
  386. })
  387. })
  388. describe('when the request returns an error status code', function () {
  389. beforeEach(function () {
  390. this.request.callsArgWith(1, null, { statusCode: 500 }, '')
  391. this.PersistenceManager.setDoc(
  392. this.project_id,
  393. this.doc_id,
  394. this.lines,
  395. this.version,
  396. this.ranges,
  397. this.lastUpdatedAt,
  398. this.lastUpdatedBy,
  399. this.callback
  400. )
  401. })
  402. it('should return an error', function () {
  403. this.callback
  404. .calledWith(sinon.match.instanceOf(Error))
  405. .should.equal(true)
  406. })
  407. it('should time the execution', function () {
  408. this.Metrics.Timer.prototype.done.called.should.equal(true)
  409. })
  410. it('should increment the metric', function () {
  411. this.Metrics.inc
  412. .calledWith('setDoc', 1, { status: 500 })
  413. .should.equal(true)
  414. })
  415. })
  416. })
  417. })