LabelsTests.js 7.1 KB

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