LabelsManagerTests.js 7.9 KB

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