updateProjectTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. const sinon = require('sinon')
  6. const chai = require('chai')
  7. const should = chai.should()
  8. const modulePath = '../../../../app/js/ProjectManager.js'
  9. const SandboxedModule = require('sandboxed-module')
  10. const _ = require('lodash')
  11. describe('ProjectManager', function () {
  12. beforeEach(function () {
  13. this.RedisManager = {}
  14. this.ProjectHistoryRedisManager = {
  15. queueRenameEntity: sinon.stub().yields(),
  16. queueAddEntity: sinon.stub().yields()
  17. }
  18. this.DocumentManager = {
  19. renameDocWithLock: sinon.stub().yields()
  20. }
  21. this.HistoryManager = {
  22. flushProjectChangesAsync: sinon.stub(),
  23. shouldFlushHistoryOps: sinon.stub().returns(false)
  24. }
  25. this.Metrics = {
  26. Timer: class Timer {}
  27. }
  28. this.Metrics.Timer.prototype.done = sinon.stub()
  29. this.logger = {
  30. log: sinon.stub(),
  31. error: sinon.stub()
  32. }
  33. this.ProjectManager = SandboxedModule.require(modulePath, {
  34. requires: {
  35. './RedisManager': this.RedisManager,
  36. './ProjectHistoryRedisManager': this.ProjectHistoryRedisManager,
  37. './DocumentManager': this.DocumentManager,
  38. 'logger-sharelatex': this.logger,
  39. './HistoryManager': this.HistoryManager,
  40. './Metrics': this.Metrics
  41. }
  42. })
  43. this.project_id = 'project-id-123'
  44. this.projectHistoryId = 'history-id-123'
  45. this.user_id = 'user-id-123'
  46. this.version = 1234567
  47. this.callback = sinon.stub()
  48. })
  49. describe('updateProjectWithLocks', function () {
  50. describe('rename operations', function () {
  51. beforeEach(function () {
  52. this.firstDocUpdate = {
  53. id: 1,
  54. pathname: 'foo',
  55. newPathname: 'foo'
  56. }
  57. this.secondDocUpdate = {
  58. id: 2,
  59. pathname: 'bar',
  60. newPathname: 'bar2'
  61. }
  62. this.docUpdates = [this.firstDocUpdate, this.secondDocUpdate]
  63. this.firstFileUpdate = {
  64. id: 2,
  65. pathname: 'bar',
  66. newPathname: 'bar2'
  67. }
  68. this.fileUpdates = [this.firstFileUpdate]
  69. })
  70. describe('successfully', function () {
  71. beforeEach(function () {
  72. this.ProjectManager.updateProjectWithLocks(
  73. this.project_id,
  74. this.projectHistoryId,
  75. this.user_id,
  76. this.docUpdates,
  77. this.fileUpdates,
  78. this.version,
  79. this.callback
  80. )
  81. })
  82. it('should rename the docs in the updates', function () {
  83. const firstDocUpdateWithVersion = _.extend({}, this.firstDocUpdate, {
  84. version: `${this.version}.0`
  85. })
  86. const secondDocUpdateWithVersion = _.extend(
  87. {},
  88. this.secondDocUpdate,
  89. { version: `${this.version}.1` }
  90. )
  91. this.DocumentManager.renameDocWithLock
  92. .calledWith(
  93. this.project_id,
  94. this.firstDocUpdate.id,
  95. this.user_id,
  96. firstDocUpdateWithVersion,
  97. this.projectHistoryId
  98. )
  99. .should.equal(true)
  100. this.DocumentManager.renameDocWithLock
  101. .calledWith(
  102. this.project_id,
  103. this.secondDocUpdate.id,
  104. this.user_id,
  105. secondDocUpdateWithVersion,
  106. this.projectHistoryId
  107. )
  108. .should.equal(true)
  109. })
  110. it('should rename the files in the updates', function () {
  111. const firstFileUpdateWithVersion = _.extend(
  112. {},
  113. this.firstFileUpdate,
  114. { version: `${this.version}.2` }
  115. )
  116. this.ProjectHistoryRedisManager.queueRenameEntity
  117. .calledWith(
  118. this.project_id,
  119. this.projectHistoryId,
  120. 'file',
  121. this.firstFileUpdate.id,
  122. this.user_id,
  123. firstFileUpdateWithVersion
  124. )
  125. .should.equal(true)
  126. })
  127. it('should not flush the history', function () {
  128. this.HistoryManager.flushProjectChangesAsync
  129. .calledWith(this.project_id)
  130. .should.equal(false)
  131. })
  132. it('should call the callback', function () {
  133. this.callback.called.should.equal(true)
  134. })
  135. })
  136. describe('when renaming a doc fails', function () {
  137. beforeEach(function () {
  138. this.error = new Error('error')
  139. this.DocumentManager.renameDocWithLock.yields(this.error)
  140. this.ProjectManager.updateProjectWithLocks(
  141. this.project_id,
  142. this.projectHistoryId,
  143. this.user_id,
  144. this.docUpdates,
  145. this.fileUpdates,
  146. this.version,
  147. this.callback
  148. )
  149. })
  150. it('should call the callback with the error', function () {
  151. this.callback.calledWith(this.error).should.equal(true)
  152. })
  153. })
  154. describe('when renaming a file fails', function () {
  155. beforeEach(function () {
  156. this.error = new Error('error')
  157. this.ProjectHistoryRedisManager.queueRenameEntity.yields(this.error)
  158. this.ProjectManager.updateProjectWithLocks(
  159. this.project_id,
  160. this.projectHistoryId,
  161. this.user_id,
  162. this.docUpdates,
  163. this.fileUpdates,
  164. this.version,
  165. this.callback
  166. )
  167. })
  168. it('should call the callback with the error', function () {
  169. this.callback.calledWith(this.error).should.equal(true)
  170. })
  171. })
  172. describe('with enough ops to flush', function () {
  173. beforeEach(function () {
  174. this.HistoryManager.shouldFlushHistoryOps.returns(true)
  175. this.ProjectManager.updateProjectWithLocks(
  176. this.project_id,
  177. this.projectHistoryId,
  178. this.user_id,
  179. this.docUpdates,
  180. this.fileUpdates,
  181. this.version,
  182. this.callback
  183. )
  184. })
  185. it('should flush the history', function () {
  186. this.HistoryManager.flushProjectChangesAsync
  187. .calledWith(this.project_id)
  188. .should.equal(true)
  189. })
  190. })
  191. })
  192. describe('add operations', function () {
  193. beforeEach(function () {
  194. this.firstDocUpdate = {
  195. id: 1,
  196. docLines: 'a\nb'
  197. }
  198. this.secondDocUpdate = {
  199. id: 2,
  200. docLines: 'a\nb'
  201. }
  202. this.docUpdates = [this.firstDocUpdate, this.secondDocUpdate]
  203. this.firstFileUpdate = {
  204. id: 3,
  205. url: 'filestore.example.com/2'
  206. }
  207. this.secondFileUpdate = {
  208. id: 4,
  209. url: 'filestore.example.com/3'
  210. }
  211. this.fileUpdates = [this.firstFileUpdate, this.secondFileUpdate]
  212. })
  213. describe('successfully', function () {
  214. beforeEach(function () {
  215. this.ProjectManager.updateProjectWithLocks(
  216. this.project_id,
  217. this.projectHistoryId,
  218. this.user_id,
  219. this.docUpdates,
  220. this.fileUpdates,
  221. this.version,
  222. this.callback
  223. )
  224. })
  225. it('should add the docs in the updates', function () {
  226. const firstDocUpdateWithVersion = _.extend({}, this.firstDocUpdate, {
  227. version: `${this.version}.0`
  228. })
  229. const secondDocUpdateWithVersion = _.extend(
  230. {},
  231. this.secondDocUpdate,
  232. { version: `${this.version}.1` }
  233. )
  234. this.ProjectHistoryRedisManager.queueAddEntity
  235. .getCall(0)
  236. .calledWith(
  237. this.project_id,
  238. this.projectHistoryId,
  239. 'doc',
  240. this.firstDocUpdate.id,
  241. this.user_id,
  242. firstDocUpdateWithVersion
  243. )
  244. .should.equal(true)
  245. this.ProjectHistoryRedisManager.queueAddEntity
  246. .getCall(1)
  247. .calledWith(
  248. this.project_id,
  249. this.projectHistoryId,
  250. 'doc',
  251. this.secondDocUpdate.id,
  252. this.user_id,
  253. secondDocUpdateWithVersion
  254. )
  255. .should.equal(true)
  256. })
  257. it('should add the files in the updates', function () {
  258. const firstFileUpdateWithVersion = _.extend(
  259. {},
  260. this.firstFileUpdate,
  261. { version: `${this.version}.2` }
  262. )
  263. const secondFileUpdateWithVersion = _.extend(
  264. {},
  265. this.secondFileUpdate,
  266. { version: `${this.version}.3` }
  267. )
  268. this.ProjectHistoryRedisManager.queueAddEntity
  269. .getCall(2)
  270. .calledWith(
  271. this.project_id,
  272. this.projectHistoryId,
  273. 'file',
  274. this.firstFileUpdate.id,
  275. this.user_id,
  276. firstFileUpdateWithVersion
  277. )
  278. .should.equal(true)
  279. this.ProjectHistoryRedisManager.queueAddEntity
  280. .getCall(3)
  281. .calledWith(
  282. this.project_id,
  283. this.projectHistoryId,
  284. 'file',
  285. this.secondFileUpdate.id,
  286. this.user_id,
  287. secondFileUpdateWithVersion
  288. )
  289. .should.equal(true)
  290. })
  291. it('should not flush the history', function () {
  292. this.HistoryManager.flushProjectChangesAsync
  293. .calledWith(this.project_id)
  294. .should.equal(false)
  295. })
  296. it('should call the callback', function () {
  297. this.callback.called.should.equal(true)
  298. })
  299. })
  300. describe('when adding a doc fails', function () {
  301. beforeEach(function () {
  302. this.error = new Error('error')
  303. this.ProjectHistoryRedisManager.queueAddEntity.yields(this.error)
  304. this.ProjectManager.updateProjectWithLocks(
  305. this.project_id,
  306. this.projectHistoryId,
  307. this.user_id,
  308. this.docUpdates,
  309. this.fileUpdates,
  310. this.version,
  311. this.callback
  312. )
  313. })
  314. it('should call the callback with the error', function () {
  315. this.callback.calledWith(this.error).should.equal(true)
  316. })
  317. })
  318. describe('when adding a file fails', function () {
  319. beforeEach(function () {
  320. this.error = new Error('error')
  321. this.ProjectHistoryRedisManager.queueAddEntity.yields(this.error)
  322. this.ProjectManager.updateProjectWithLocks(
  323. this.project_id,
  324. this.projectHistoryId,
  325. this.user_id,
  326. this.docUpdates,
  327. this.fileUpdates,
  328. this.version,
  329. this.callback
  330. )
  331. })
  332. it('should call the callback with the error', function () {
  333. this.callback.calledWith(this.error).should.equal(true)
  334. })
  335. })
  336. describe('with enough ops to flush', function () {
  337. beforeEach(function () {
  338. this.HistoryManager.shouldFlushHistoryOps.returns(true)
  339. this.ProjectManager.updateProjectWithLocks(
  340. this.project_id,
  341. this.projectHistoryId,
  342. this.user_id,
  343. this.docUpdates,
  344. this.fileUpdates,
  345. this.version,
  346. this.callback
  347. )
  348. })
  349. it('should flush the history', function () {
  350. this.HistoryManager.flushProjectChangesAsync
  351. .calledWith(this.project_id)
  352. .should.equal(true)
  353. })
  354. })
  355. })
  356. })
  357. })