PersistenceManagerTests.js 13 KB

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