updateProjectTests.js 11 KB

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