LabelsManagerTests.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* eslint-disable
  2. no-return-assign,
  3. no-undef,
  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. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. import sinon from 'sinon'
  13. import { expect } from 'chai'
  14. import { ObjectId } from 'mongodb'
  15. import tk from 'timekeeper'
  16. import { strict as esmock } from 'esmock'
  17. const MODULE_PATH = '../../../../app/js/LabelsManager.js'
  18. describe('LabelsManager', function () {
  19. beforeEach(async function () {
  20. this.now = new Date()
  21. tk.freeze(this.now)
  22. this.db = {
  23. projectHistoryLabels: {
  24. deleteOne: sinon.stub(),
  25. find: sinon.stub(),
  26. insertOne: sinon.stub(),
  27. },
  28. }
  29. this.mongodb = {
  30. ObjectId,
  31. db: this.db,
  32. }
  33. this.HistoryStoreManager = {
  34. getChunkAtVersion: sinon.stub().yields(),
  35. }
  36. this.UpdatesProcessor = {
  37. processUpdatesForProject: sinon.stub().yields(),
  38. }
  39. this.WebApiManager = {
  40. getHistoryId: sinon.stub(),
  41. }
  42. this.LabelsManager = await esmock(MODULE_PATH, {
  43. '../../../../app/js/mongodb.js': this.mongodb,
  44. '../../../../app/js/HistoryStoreManager.js': this.HistoryStoreManager,
  45. '../../../../app/js/UpdatesProcessor.js': this.UpdatesProcessor,
  46. '../../../../app/js/WebApiManager.js': this.WebApiManager,
  47. })
  48. this.project_id = new ObjectId().toString()
  49. this.historyId = 123
  50. this.user_id = new ObjectId().toString()
  51. this.label_id = new ObjectId().toString()
  52. return (this.callback = sinon.stub())
  53. })
  54. afterEach(function () {
  55. return tk.reset()
  56. })
  57. describe('getLabels', function () {
  58. beforeEach(function () {
  59. this.label = {
  60. _id: ObjectId(),
  61. comment: 'some comment',
  62. version: 123,
  63. user_id: ObjectId(),
  64. created_at: new Date(),
  65. }
  66. this.db.projectHistoryLabels.find.returns({
  67. toArray: sinon.stub().yields(null, [this.label]),
  68. })
  69. })
  70. describe('with valid project id', function () {
  71. beforeEach(function () {
  72. return this.LabelsManager.getLabels(this.project_id, this.callback)
  73. })
  74. it('gets the labels state from mongo', function () {
  75. return expect(
  76. this.db.projectHistoryLabels.find
  77. ).to.have.been.calledWith({ project_id: ObjectId(this.project_id) })
  78. })
  79. return it('returns formatted labels', function () {
  80. return expect(this.callback).to.have.been.calledWith(null, [
  81. sinon.match({
  82. id: this.label._id,
  83. comment: this.label.comment,
  84. version: this.label.version,
  85. user_id: this.label.user_id,
  86. created_at: this.label.created_at,
  87. }),
  88. ])
  89. })
  90. })
  91. return describe('with invalid project id', function () {
  92. return it('returns an error', function (done) {
  93. return this.LabelsManager.getLabels('invalid id', error => {
  94. expect(error).to.exist
  95. return done()
  96. })
  97. })
  98. })
  99. })
  100. describe('createLabel', function () {
  101. beforeEach(function () {
  102. this.version = 123
  103. this.comment = 'a comment'
  104. this.WebApiManager.getHistoryId.yields(null, this.historyId)
  105. })
  106. describe('with createdAt', function () {
  107. beforeEach(function () {
  108. this.createdAt = new Date(1)
  109. this.db.projectHistoryLabels.insertOne.yields(null, {
  110. insertedId: ObjectId(this.label_id),
  111. })
  112. return this.LabelsManager.createLabel(
  113. this.project_id,
  114. this.user_id,
  115. this.version,
  116. this.comment,
  117. this.createdAt,
  118. true,
  119. this.callback
  120. )
  121. })
  122. it('flushes unprocessed updates', function () {
  123. return expect(
  124. this.UpdatesProcessor.processUpdatesForProject
  125. ).to.have.been.calledWith(this.project_id)
  126. })
  127. it('finds the V1 project id', function () {
  128. return expect(this.WebApiManager.getHistoryId).to.have.been.calledWith(
  129. this.project_id
  130. )
  131. })
  132. it('checks there is a chunk for the project + version', function () {
  133. return expect(
  134. this.HistoryStoreManager.getChunkAtVersion
  135. ).to.have.been.calledWith(this.project_id, this.historyId, this.version)
  136. })
  137. it('create the label in mongo', function () {
  138. return expect(
  139. this.db.projectHistoryLabels.insertOne
  140. ).to.have.been.calledWith(
  141. sinon.match({
  142. project_id: ObjectId(this.project_id),
  143. comment: this.comment,
  144. version: this.version,
  145. user_id: ObjectId(this.user_id),
  146. created_at: this.createdAt,
  147. }),
  148. sinon.match.any
  149. )
  150. })
  151. return it('returns the label', function () {
  152. return expect(this.callback).to.have.been.calledWith(null, {
  153. id: ObjectId(this.label_id),
  154. comment: this.comment,
  155. version: this.version,
  156. user_id: ObjectId(this.user_id),
  157. created_at: this.createdAt,
  158. })
  159. })
  160. })
  161. describe('without createdAt', function () {
  162. beforeEach(function () {
  163. this.db.projectHistoryLabels.insertOne.yields(null, {
  164. insertedId: ObjectId(this.label_id),
  165. })
  166. return this.LabelsManager.createLabel(
  167. this.project_id,
  168. this.user_id,
  169. this.version,
  170. this.comment,
  171. undefined,
  172. true,
  173. this.callback
  174. )
  175. })
  176. return it('create the label with the current date', function () {
  177. return expect(
  178. this.db.projectHistoryLabels.insertOne
  179. ).to.have.been.calledWith(
  180. sinon.match({
  181. project_id: ObjectId(this.project_id),
  182. comment: this.comment,
  183. version: this.version,
  184. user_id: ObjectId(this.user_id),
  185. created_at: this.now,
  186. })
  187. )
  188. })
  189. })
  190. return describe('with shouldValidateExists = false', function () {
  191. beforeEach(function () {
  192. this.createdAt = new Date(1)
  193. this.db.projectHistoryLabels.insertOne.yields(null, {
  194. insertedId: ObjectId(this.label_id),
  195. })
  196. return this.LabelsManager.createLabel(
  197. this.project_id,
  198. this.user_id,
  199. this.version,
  200. this.comment,
  201. this.createdAt,
  202. false,
  203. this.callback
  204. )
  205. })
  206. return it('checks there is a chunk for the project + version', function () {
  207. return expect(this.HistoryStoreManager.getChunkAtVersion).to.not.have
  208. .been.called
  209. })
  210. })
  211. })
  212. return describe('deleteLabel', function () {
  213. beforeEach(function () {
  214. this.db.projectHistoryLabels.deleteOne.yields()
  215. return this.LabelsManager.deleteLabel(
  216. this.project_id,
  217. this.user_id,
  218. this.label_id,
  219. this.callback
  220. )
  221. })
  222. return it('removes the label from the database', function () {
  223. return expect(
  224. this.db.projectHistoryLabels.deleteOne
  225. ).to.have.been.calledWith(
  226. {
  227. _id: ObjectId(this.label_id),
  228. project_id: ObjectId(this.project_id),
  229. user_id: ObjectId(this.user_id),
  230. },
  231. this.callback
  232. )
  233. })
  234. })
  235. })