updateProjectTests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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.ProjectManager = SandboxedModule.require(modulePath, {
  24. requires: {
  25. './RedisManager': this.RedisManager,
  26. './ProjectHistoryRedisManager': this.ProjectHistoryRedisManager,
  27. './DocumentManager': this.DocumentManager,
  28. './HistoryManager': this.HistoryManager,
  29. './Metrics': this.Metrics
  30. }
  31. })
  32. this.project_id = 'project-id-123'
  33. this.projectHistoryId = 'history-id-123'
  34. this.user_id = 'user-id-123'
  35. this.version = 1234567
  36. this.callback = sinon.stub()
  37. })
  38. describe('updateProjectWithLocks', function () {
  39. describe('rename operations', function () {
  40. beforeEach(function () {
  41. this.firstDocUpdate = {
  42. type: 'rename-doc',
  43. id: 1,
  44. pathname: 'foo',
  45. newPathname: 'foo'
  46. }
  47. this.secondDocUpdate = {
  48. type: 'rename-doc',
  49. id: 2,
  50. pathname: 'bar',
  51. newPathname: 'bar2'
  52. }
  53. this.firstFileUpdate = {
  54. type: 'rename-file',
  55. id: 2,
  56. pathname: 'bar',
  57. newPathname: 'bar2'
  58. }
  59. this.updates = [
  60. this.firstDocUpdate,
  61. this.secondDocUpdate,
  62. this.firstFileUpdate
  63. ]
  64. })
  65. describe('successfully', function () {
  66. beforeEach(function () {
  67. this.ProjectManager.updateProjectWithLocks(
  68. this.project_id,
  69. this.projectHistoryId,
  70. this.user_id,
  71. this.updates,
  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.updates,
  139. this.version,
  140. this.callback
  141. )
  142. })
  143. it('should call the callback with the error', function () {
  144. this.callback.calledWith(this.error).should.equal(true)
  145. })
  146. })
  147. describe('when renaming a file fails', function () {
  148. beforeEach(function () {
  149. this.error = new Error('error')
  150. this.ProjectHistoryRedisManager.queueRenameEntity.yields(this.error)
  151. this.ProjectManager.updateProjectWithLocks(
  152. this.project_id,
  153. this.projectHistoryId,
  154. this.user_id,
  155. this.updates,
  156. this.version,
  157. this.callback
  158. )
  159. })
  160. it('should call the callback with the error', function () {
  161. this.callback.calledWith(this.error).should.equal(true)
  162. })
  163. })
  164. describe('with enough ops to flush', function () {
  165. beforeEach(function () {
  166. this.HistoryManager.shouldFlushHistoryOps.returns(true)
  167. this.ProjectManager.updateProjectWithLocks(
  168. this.project_id,
  169. this.projectHistoryId,
  170. this.user_id,
  171. this.updates,
  172. this.version,
  173. this.callback
  174. )
  175. })
  176. it('should flush the history', function () {
  177. this.HistoryManager.flushProjectChangesAsync
  178. .calledWith(this.project_id)
  179. .should.equal(true)
  180. })
  181. })
  182. })
  183. describe('add operations', function () {
  184. beforeEach(function () {
  185. this.firstDocUpdate = {
  186. type: 'add-doc',
  187. id: 1,
  188. docLines: 'a\nb'
  189. }
  190. this.secondDocUpdate = {
  191. type: 'add-doc',
  192. id: 2,
  193. docLines: 'a\nb'
  194. }
  195. this.firstFileUpdate = {
  196. type: 'add-file',
  197. id: 3,
  198. url: 'filestore.example.com/2'
  199. }
  200. this.secondFileUpdate = {
  201. type: 'add-file',
  202. id: 4,
  203. url: 'filestore.example.com/3'
  204. }
  205. this.updates = [
  206. this.firstDocUpdate,
  207. this.secondDocUpdate,
  208. this.firstFileUpdate,
  209. this.secondFileUpdate
  210. ]
  211. })
  212. describe('successfully', function () {
  213. beforeEach(function () {
  214. this.ProjectManager.updateProjectWithLocks(
  215. this.project_id,
  216. this.projectHistoryId,
  217. this.user_id,
  218. this.updates,
  219. this.version,
  220. this.callback
  221. )
  222. })
  223. it('should add the docs in the updates', function () {
  224. const firstDocUpdateWithVersion = _.extend({}, this.firstDocUpdate, {
  225. version: `${this.version}.0`
  226. })
  227. const secondDocUpdateWithVersion = _.extend(
  228. {},
  229. this.secondDocUpdate,
  230. { version: `${this.version}.1` }
  231. )
  232. this.ProjectHistoryRedisManager.queueAddEntity
  233. .getCall(0)
  234. .calledWith(
  235. this.project_id,
  236. this.projectHistoryId,
  237. 'doc',
  238. this.firstDocUpdate.id,
  239. this.user_id,
  240. firstDocUpdateWithVersion
  241. )
  242. .should.equal(true)
  243. this.ProjectHistoryRedisManager.queueAddEntity
  244. .getCall(1)
  245. .calledWith(
  246. this.project_id,
  247. this.projectHistoryId,
  248. 'doc',
  249. this.secondDocUpdate.id,
  250. this.user_id,
  251. secondDocUpdateWithVersion
  252. )
  253. .should.equal(true)
  254. })
  255. it('should add the files in the updates', function () {
  256. const firstFileUpdateWithVersion = _.extend(
  257. {},
  258. this.firstFileUpdate,
  259. { version: `${this.version}.2` }
  260. )
  261. const secondFileUpdateWithVersion = _.extend(
  262. {},
  263. this.secondFileUpdate,
  264. { version: `${this.version}.3` }
  265. )
  266. this.ProjectHistoryRedisManager.queueAddEntity
  267. .getCall(2)
  268. .calledWith(
  269. this.project_id,
  270. this.projectHistoryId,
  271. 'file',
  272. this.firstFileUpdate.id,
  273. this.user_id,
  274. firstFileUpdateWithVersion
  275. )
  276. .should.equal(true)
  277. this.ProjectHistoryRedisManager.queueAddEntity
  278. .getCall(3)
  279. .calledWith(
  280. this.project_id,
  281. this.projectHistoryId,
  282. 'file',
  283. this.secondFileUpdate.id,
  284. this.user_id,
  285. secondFileUpdateWithVersion
  286. )
  287. .should.equal(true)
  288. })
  289. it('should not flush the history', function () {
  290. this.HistoryManager.flushProjectChangesAsync
  291. .calledWith(this.project_id)
  292. .should.equal(false)
  293. })
  294. it('should call the callback', function () {
  295. this.callback.called.should.equal(true)
  296. })
  297. })
  298. describe('when adding a doc fails', function () {
  299. beforeEach(function () {
  300. this.error = new Error('error')
  301. this.ProjectHistoryRedisManager.queueAddEntity.yields(this.error)
  302. this.ProjectManager.updateProjectWithLocks(
  303. this.project_id,
  304. this.projectHistoryId,
  305. this.user_id,
  306. this.updates,
  307. this.version,
  308. this.callback
  309. )
  310. })
  311. it('should call the callback with the error', function () {
  312. this.callback.calledWith(this.error).should.equal(true)
  313. })
  314. })
  315. describe('when adding a file fails', function () {
  316. beforeEach(function () {
  317. this.error = new Error('error')
  318. this.ProjectHistoryRedisManager.queueAddEntity.yields(this.error)
  319. this.ProjectManager.updateProjectWithLocks(
  320. this.project_id,
  321. this.projectHistoryId,
  322. this.user_id,
  323. this.updates,
  324. this.version,
  325. this.callback
  326. )
  327. })
  328. it('should call the callback with the error', function () {
  329. this.callback.calledWith(this.error).should.equal(true)
  330. })
  331. })
  332. describe('with enough ops to flush', function () {
  333. beforeEach(function () {
  334. this.HistoryManager.shouldFlushHistoryOps.returns(true)
  335. this.ProjectManager.updateProjectWithLocks(
  336. this.project_id,
  337. this.projectHistoryId,
  338. this.user_id,
  339. this.updates,
  340. this.version,
  341. this.callback
  342. )
  343. })
  344. it('should flush the history', function () {
  345. this.HistoryManager.flushProjectChangesAsync
  346. .calledWith(this.project_id)
  347. .should.equal(true)
  348. })
  349. })
  350. })
  351. describe('when given an unknown operation type', function () {
  352. beforeEach(function () {
  353. this.updates = [{ type: 'brew-coffee' }]
  354. this.ProjectManager.updateProjectWithLocks(
  355. this.project_id,
  356. this.projectHistoryId,
  357. this.user_id,
  358. this.updates,
  359. this.version,
  360. this.callback
  361. )
  362. })
  363. it('should call back with an error', function () {
  364. this.callback.calledWith(sinon.match.instanceOf(Error)).should.be.true
  365. })
  366. })
  367. })
  368. })