LabelsTests.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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://127.0.0.1:3100')
  22. const MockFileStore = () => nock('http://127.0.0.1:3009')
  23. const MockWeb = () => nock('http://127.0.0.1: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. label.id,
  109. error => {
  110. if (error != null) {
  111. throw error
  112. }
  113. return ProjectHistoryClient.getLabels(
  114. this.project_id,
  115. (error, labels) => {
  116. if (error != null) {
  117. throw error
  118. }
  119. expect(labels).to.deep.equal([])
  120. return done()
  121. }
  122. )
  123. }
  124. )
  125. }
  126. )
  127. })
  128. it('can delete labels for the current user', function (done) {
  129. return ProjectHistoryClient.createLabel(
  130. this.project_id,
  131. this.user_id,
  132. 7,
  133. this.comment,
  134. this.created_at,
  135. (error, label) => {
  136. if (error != null) {
  137. throw error
  138. }
  139. return ProjectHistoryClient.deleteLabelForUser(
  140. this.project_id,
  141. this.user_id,
  142. label.id,
  143. error => {
  144. if (error != null) {
  145. throw error
  146. }
  147. return ProjectHistoryClient.getLabels(
  148. this.project_id,
  149. (error, labels) => {
  150. if (error != null) {
  151. throw error
  152. }
  153. expect(labels).to.deep.equal([])
  154. return done()
  155. }
  156. )
  157. }
  158. )
  159. }
  160. )
  161. })
  162. it('can transfer ownership of labels', function (done) {
  163. const fromUser = new ObjectId().toString()
  164. const toUser = new ObjectId().toString()
  165. return ProjectHistoryClient.createLabel(
  166. this.project_id,
  167. fromUser,
  168. 7,
  169. this.comment,
  170. this.created_at,
  171. (error, label) => {
  172. if (error != null) {
  173. throw error
  174. }
  175. return ProjectHistoryClient.createLabel(
  176. this.project_id,
  177. fromUser,
  178. 7,
  179. this.comment2,
  180. this.created_at,
  181. (error, label2) => {
  182. if (error != null) {
  183. throw error
  184. }
  185. return ProjectHistoryClient.transferLabelOwnership(
  186. fromUser,
  187. toUser,
  188. error => {
  189. if (error != null) {
  190. throw error
  191. }
  192. return ProjectHistoryClient.getLabels(
  193. this.project_id,
  194. (error, labels) => {
  195. if (error != null) {
  196. throw error
  197. }
  198. expect(labels).to.deep.equal([
  199. {
  200. id: label.id,
  201. comment: label.comment,
  202. version: label.version,
  203. created_at: label.created_at,
  204. user_id: toUser,
  205. },
  206. {
  207. id: label2.id,
  208. comment: label2.comment,
  209. version: label2.version,
  210. created_at: label2.created_at,
  211. user_id: toUser,
  212. },
  213. ])
  214. return done()
  215. }
  216. )
  217. }
  218. )
  219. }
  220. )
  221. }
  222. )
  223. })
  224. return it('should return labels with summarized updates', function (done) {
  225. return ProjectHistoryClient.createLabel(
  226. this.project_id,
  227. this.user_id,
  228. 8,
  229. this.comment,
  230. this.created_at,
  231. (error, label) => {
  232. if (error != null) {
  233. throw error
  234. }
  235. return ProjectHistoryClient.getSummarizedUpdates(
  236. this.project_id,
  237. { min_count: 1 },
  238. (error, updates) => {
  239. if (error != null) {
  240. throw error
  241. }
  242. expect(updates).to.deep.equal({
  243. nextBeforeTimestamp: 6,
  244. updates: [
  245. {
  246. fromV: 6,
  247. toV: 8,
  248. meta: {
  249. users: ['5a5637efdac84e81b71014c4', 31],
  250. start_ts: 1512383567277,
  251. end_ts: 1512383572877,
  252. },
  253. pathnames: ['bar.tex', 'main.tex'],
  254. project_ops: [],
  255. labels: [
  256. {
  257. id: label.id.toString(),
  258. comment: this.comment,
  259. version: 8,
  260. user_id: this.user_id,
  261. created_at: this.created_at.toISOString(),
  262. },
  263. ],
  264. },
  265. ],
  266. })
  267. return done()
  268. }
  269. )
  270. }
  271. )
  272. })
  273. })