PersistenceManagerTests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. './Errors': Errors
  45. }
  46. })
  47. this.project_id = 'project-id-123'
  48. this.projectHistoryId = 'history-id-123'
  49. this.doc_id = 'doc-id-123'
  50. this.lines = ['one', 'two', 'three']
  51. this.version = 42
  52. this.callback = sinon.stub()
  53. this.ranges = { comments: 'mock', entries: 'mock' }
  54. this.pathname = '/a/b/c.tex'
  55. this.lastUpdatedAt = Date.now()
  56. this.lastUpdatedBy = 'last-author-id'
  57. return (this.Settings.apis = {
  58. web: {
  59. url: (this.url = 'www.example.com'),
  60. user: (this.user = 'sharelatex'),
  61. pass: (this.pass = 'password')
  62. }
  63. })
  64. })
  65. describe('getDoc', function () {
  66. beforeEach(function () {
  67. return (this.webResponse = {
  68. lines: this.lines,
  69. version: this.version,
  70. ranges: this.ranges,
  71. pathname: this.pathname,
  72. projectHistoryId: this.projectHistoryId
  73. })
  74. })
  75. describe('with a successful response from the web api', function () {
  76. beforeEach(function () {
  77. this.request.callsArgWith(
  78. 1,
  79. null,
  80. { statusCode: 200 },
  81. JSON.stringify(this.webResponse)
  82. )
  83. return this.PersistenceManager.getDoc(
  84. this.project_id,
  85. this.doc_id,
  86. this.callback
  87. )
  88. })
  89. it('should call the web api', function () {
  90. return this.request
  91. .calledWith({
  92. url: `${this.url}/project/${this.project_id}/doc/${this.doc_id}`,
  93. method: 'GET',
  94. headers: {
  95. accept: 'application/json'
  96. },
  97. auth: {
  98. user: this.user,
  99. pass: this.pass,
  100. sendImmediately: true
  101. },
  102. jar: false,
  103. timeout: 5000
  104. })
  105. .should.equal(true)
  106. })
  107. it('should call the callback with the doc lines, version and ranges', function () {
  108. return this.callback
  109. .calledWith(
  110. null,
  111. this.lines,
  112. this.version,
  113. this.ranges,
  114. this.pathname,
  115. this.projectHistoryId
  116. )
  117. .should.equal(true)
  118. })
  119. it('should time the execution', function () {
  120. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  121. })
  122. return it('should increment the metric', function () {
  123. return this.Metrics.inc
  124. .calledWith('getDoc', 1, { status: 200 })
  125. .should.equal(true)
  126. })
  127. })
  128. describe('when request returns an error', function () {
  129. beforeEach(function () {
  130. this.error = new Error('oops')
  131. this.error.code = 'EOOPS'
  132. this.request.callsArgWith(1, this.error, null, null)
  133. return this.PersistenceManager.getDoc(
  134. this.project_id,
  135. this.doc_id,
  136. this.callback
  137. )
  138. })
  139. it('should return the error', function () {
  140. return this.callback.calledWith(this.error).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 the error', function () {
  327. return this.callback.calledWith(this.error).should.equal(true)
  328. })
  329. it('should time the execution', function () {
  330. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  331. })
  332. return it('should increment the metric', function () {
  333. return this.Metrics.inc
  334. .calledWith('setDoc', 1, { status: 'EOOPS' })
  335. .should.equal(true)
  336. })
  337. })
  338. describe('when the request returns 404', function () {
  339. beforeEach(function () {
  340. this.request.callsArgWith(1, null, { statusCode: 404 }, '')
  341. return this.PersistenceManager.setDoc(
  342. this.project_id,
  343. this.doc_id,
  344. this.lines,
  345. this.version,
  346. this.ranges,
  347. this.lastUpdatedAt,
  348. this.lastUpdatedBy,
  349. this.callback
  350. )
  351. })
  352. it('should return a NotFoundError', function () {
  353. return this.callback
  354. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  355. .should.equal(true)
  356. })
  357. it('should time the execution', function () {
  358. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  359. })
  360. return it('should increment the metric', function () {
  361. return this.Metrics.inc
  362. .calledWith('setDoc', 1, { status: 404 })
  363. .should.equal(true)
  364. })
  365. })
  366. return describe('when the request returns an error status code', function () {
  367. beforeEach(function () {
  368. this.request.callsArgWith(1, null, { statusCode: 500 }, '')
  369. return this.PersistenceManager.setDoc(
  370. this.project_id,
  371. this.doc_id,
  372. this.lines,
  373. this.version,
  374. this.ranges,
  375. this.lastUpdatedAt,
  376. this.lastUpdatedBy,
  377. this.callback
  378. )
  379. })
  380. it('should return an error', function () {
  381. return this.callback
  382. .calledWith(sinon.match.instanceOf(Error))
  383. .should.equal(true)
  384. })
  385. it('should time the execution', function () {
  386. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  387. })
  388. return it('should increment the metric', function () {
  389. return this.Metrics.inc
  390. .calledWith('setDoc', 1, { status: 500 })
  391. .should.equal(true)
  392. })
  393. })
  394. })
  395. })