PersistenceManagerTests.js 13 KB

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