LabelsTests.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* eslint-disable
  2. no-undef,
  3. no-unused-vars,
  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. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. import sinon from 'sinon'
  14. import { expect } from 'chai'
  15. import Settings from '@overleaf/settings'
  16. import mongodb from 'mongodb-legacy'
  17. import nock from 'nock'
  18. import * as ProjectHistoryClient from './helpers/ProjectHistoryClient.js'
  19. import * as ProjectHistoryApp from './helpers/ProjectHistoryApp.js'
  20. const { ObjectId } = mongodb
  21. const MockHistoryStore = () => nock('http://localhost:3100')
  22. const MockFileStore = () => nock('http://localhost:3009')
  23. const MockWeb = () => nock('http://localhost:3000')
  24. const fixture = path => new URL(`../fixtures/${path}`, import.meta.url)
  25. describe('Labels', function () {
  26. beforeEach(function (done) {
  27. return ProjectHistoryApp.ensureRunning(error => {
  28. if (error != null) {
  29. throw error
  30. }
  31. this.historyId = new ObjectId().toString()
  32. MockHistoryStore().post('/api/projects').reply(200, {
  33. projectId: this.historyId,
  34. })
  35. return ProjectHistoryClient.initializeProject(
  36. this.historyId,
  37. (error, olProject) => {
  38. if (error != null) {
  39. throw error
  40. }
  41. this.project_id = new ObjectId().toString()
  42. MockWeb()
  43. .get(`/project/${this.project_id}/details`)
  44. .reply(200, {
  45. name: 'Test Project',
  46. overleaf: { history: { id: olProject.id } },
  47. })
  48. MockHistoryStore()
  49. .get(`/api/projects/${this.historyId}/latest/history`)
  50. .replyWithFile(200, fixture('chunks/7-8.json'))
  51. MockHistoryStore()
  52. .get(`/api/projects/${this.historyId}/versions/7/history`)
  53. .replyWithFile(200, fixture('chunks/7-8.json'))
  54. .persist()
  55. MockHistoryStore()
  56. .get(`/api/projects/${this.historyId}/versions/8/history`)
  57. .replyWithFile(200, fixture('chunks/7-8.json'))
  58. .persist()
  59. this.comment = 'a saved version comment'
  60. this.comment2 = 'another saved version comment'
  61. this.user_id = new ObjectId().toString()
  62. this.created_at = new Date(1)
  63. return done()
  64. }
  65. )
  66. })
  67. })
  68. afterEach(function () {
  69. return nock.cleanAll()
  70. })
  71. it('can create and get labels', function (done) {
  72. return ProjectHistoryClient.createLabel(
  73. this.project_id,
  74. this.user_id,
  75. 7,
  76. this.comment,
  77. this.created_at,
  78. (error, label) => {
  79. if (error != null) {
  80. throw error
  81. }
  82. return ProjectHistoryClient.getLabels(
  83. this.project_id,
  84. (error, labels) => {
  85. if (error != null) {
  86. throw error
  87. }
  88. expect(labels).to.deep.equal([label])
  89. return done()
  90. }
  91. )
  92. }
  93. )
  94. })
  95. it('can delete labels', function (done) {
  96. return ProjectHistoryClient.createLabel(
  97. this.project_id,
  98. this.user_id,
  99. 7,
  100. this.comment,
  101. this.created_at,
  102. (error, label) => {
  103. if (error != null) {
  104. throw error
  105. }
  106. return ProjectHistoryClient.deleteLabel(
  107. this.project_id,
  108. this.user_id,
  109. label.id,
  110. error => {
  111. if (error != null) {
  112. throw error
  113. }
  114. return ProjectHistoryClient.getLabels(
  115. this.project_id,
  116. (error, labels) => {
  117. if (error != null) {
  118. throw error
  119. }
  120. expect(labels).to.deep.equal([])
  121. return done()
  122. }
  123. )
  124. }
  125. )
  126. }
  127. )
  128. })
  129. it('can transfer ownership of labels', function (done) {
  130. const fromUser = new ObjectId().toString()
  131. const toUser = new ObjectId().toString()
  132. return ProjectHistoryClient.createLabel(
  133. this.project_id,
  134. fromUser,
  135. 7,
  136. this.comment,
  137. this.created_at,
  138. (error, label) => {
  139. if (error != null) {
  140. throw error
  141. }
  142. return ProjectHistoryClient.createLabel(
  143. this.project_id,
  144. fromUser,
  145. 7,
  146. this.comment2,
  147. this.created_at,
  148. (error, label2) => {
  149. if (error != null) {
  150. throw error
  151. }
  152. return ProjectHistoryClient.transferLabelOwnership(
  153. fromUser,
  154. toUser,
  155. error => {
  156. if (error != null) {
  157. throw error
  158. }
  159. return ProjectHistoryClient.getLabels(
  160. this.project_id,
  161. (error, labels) => {
  162. if (error != null) {
  163. throw error
  164. }
  165. expect(labels).to.deep.equal([
  166. {
  167. id: label.id,
  168. comment: label.comment,
  169. version: label.version,
  170. created_at: label.created_at,
  171. user_id: toUser,
  172. },
  173. {
  174. id: label2.id,
  175. comment: label2.comment,
  176. version: label2.version,
  177. created_at: label2.created_at,
  178. user_id: toUser,
  179. },
  180. ])
  181. return done()
  182. }
  183. )
  184. }
  185. )
  186. }
  187. )
  188. }
  189. )
  190. })
  191. return it('should return labels with summarized updates', function (done) {
  192. return ProjectHistoryClient.createLabel(
  193. this.project_id,
  194. this.user_id,
  195. 8,
  196. this.comment,
  197. this.created_at,
  198. (error, label) => {
  199. if (error != null) {
  200. throw error
  201. }
  202. return ProjectHistoryClient.getSummarizedUpdates(
  203. this.project_id,
  204. { min_count: 1 },
  205. (error, updates) => {
  206. if (error != null) {
  207. throw error
  208. }
  209. expect(updates).to.deep.equal({
  210. nextBeforeTimestamp: 6,
  211. updates: [
  212. {
  213. fromV: 6,
  214. toV: 8,
  215. meta: {
  216. users: ['5a5637efdac84e81b71014c4', 31],
  217. start_ts: 1512383567277,
  218. end_ts: 1512383572877,
  219. },
  220. pathnames: ['bar.tex', 'main.tex'],
  221. project_ops: [],
  222. labels: [
  223. {
  224. id: label.id.toString(),
  225. comment: this.comment,
  226. version: 8,
  227. user_id: this.user_id,
  228. created_at: this.created_at.toISOString(),
  229. },
  230. ],
  231. },
  232. ],
  233. })
  234. return done()
  235. }
  236. )
  237. }
  238. )
  239. })
  240. })