LabelsTests.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { expect } from 'chai'
  2. import mongodb from 'mongodb-legacy'
  3. import nock from 'nock'
  4. import * as ProjectHistoryClient from './helpers/ProjectHistoryClient.js'
  5. import * as ProjectHistoryApp from './helpers/ProjectHistoryApp.js'
  6. const { ObjectId } = mongodb
  7. const MockHistoryStore = () => nock('http://127.0.0.1:3100')
  8. const MockWeb = () => nock('http://127.0.0.1:3000')
  9. const fixture = path => new URL(`../fixtures/${path}`, import.meta.url)
  10. describe('Labels', function () {
  11. beforeEach(async function () {
  12. await ProjectHistoryApp.ensureRunning()
  13. this.historyId = new ObjectId().toString()
  14. MockHistoryStore().post('/api/projects').reply(200, {
  15. projectId: this.historyId,
  16. })
  17. const olProject = await ProjectHistoryClient.initializeProject(
  18. this.historyId
  19. )
  20. this.project_id = new ObjectId().toString()
  21. MockWeb()
  22. .get(`/project/${this.project_id}/details`)
  23. .reply(200, {
  24. name: 'Test Project',
  25. overleaf: { history: { id: olProject.id } },
  26. })
  27. MockHistoryStore()
  28. .get(`/api/projects/${this.historyId}/latest/history`)
  29. .replyWithFile(200, fixture('chunks/7-8.json'))
  30. MockHistoryStore()
  31. .get(`/api/projects/${this.historyId}/versions/7/history`)
  32. .replyWithFile(200, fixture('chunks/7-8.json'))
  33. .persist()
  34. MockHistoryStore()
  35. .get(`/api/projects/${this.historyId}/versions/8/history`)
  36. .replyWithFile(200, fixture('chunks/7-8.json'))
  37. .persist()
  38. this.comment = 'a saved version comment'
  39. this.comment2 = 'another saved version comment'
  40. this.user_id = new ObjectId().toString()
  41. this.created_at = new Date(1)
  42. })
  43. afterEach(function () {
  44. nock.cleanAll()
  45. })
  46. it('can create and get labels', async function () {
  47. const label = await ProjectHistoryClient.createLabel(
  48. this.project_id,
  49. this.user_id,
  50. 7,
  51. this.comment,
  52. this.created_at
  53. )
  54. const labels = await ProjectHistoryClient.getLabels(this.project_id)
  55. expect(labels).to.deep.equal([label])
  56. })
  57. it('can create and get labels with no user id', async function () {
  58. const userId = undefined
  59. const label = await ProjectHistoryClient.createLabel(
  60. this.project_id,
  61. userId,
  62. 7,
  63. this.comment,
  64. this.created_at
  65. )
  66. const labels = await ProjectHistoryClient.getLabels(this.project_id)
  67. expect(labels).to.deep.equal([label])
  68. })
  69. it('can delete labels', async function () {
  70. const label = await ProjectHistoryClient.createLabel(
  71. this.project_id,
  72. this.user_id,
  73. 7,
  74. this.comment,
  75. this.created_at
  76. )
  77. await ProjectHistoryClient.deleteLabel(this.project_id, label.id)
  78. const labels = await ProjectHistoryClient.getLabels(this.project_id)
  79. expect(labels).to.deep.equal([])
  80. })
  81. it('can delete labels for the current user', async function () {
  82. const label = await ProjectHistoryClient.createLabel(
  83. this.project_id,
  84. this.user_id,
  85. 7,
  86. this.comment,
  87. this.created_at
  88. )
  89. await ProjectHistoryClient.deleteLabelForUser(
  90. this.project_id,
  91. this.user_id,
  92. label.id
  93. )
  94. const labels = await ProjectHistoryClient.getLabels(this.project_id)
  95. expect(labels).to.deep.equal([])
  96. })
  97. it('can transfer ownership of labels', async function () {
  98. const fromUser = new ObjectId().toString()
  99. const toUser = new ObjectId().toString()
  100. const label = await ProjectHistoryClient.createLabel(
  101. this.project_id,
  102. fromUser,
  103. 7,
  104. this.comment,
  105. this.created_at
  106. )
  107. const label2 = await ProjectHistoryClient.createLabel(
  108. this.project_id,
  109. fromUser,
  110. 7,
  111. this.comment2,
  112. this.created_at
  113. )
  114. await ProjectHistoryClient.transferLabelOwnership(fromUser, toUser)
  115. const labels = await ProjectHistoryClient.getLabels(this.project_id)
  116. expect(labels).to.deep.equal([
  117. {
  118. id: label.id,
  119. comment: label.comment,
  120. version: label.version,
  121. created_at: label.created_at,
  122. user_id: toUser,
  123. },
  124. {
  125. id: label2.id,
  126. comment: label2.comment,
  127. version: label2.version,
  128. created_at: label2.created_at,
  129. user_id: toUser,
  130. },
  131. ])
  132. })
  133. it('should return labels with summarized updates', async function () {
  134. const label = await ProjectHistoryClient.createLabel(
  135. this.project_id,
  136. this.user_id,
  137. 8,
  138. this.comment,
  139. this.created_at
  140. )
  141. const updates = await ProjectHistoryClient.getSummarizedUpdates(
  142. this.project_id,
  143. { min_count: 1 }
  144. )
  145. expect(updates).to.deep.equal({
  146. nextBeforeTimestamp: 6,
  147. updates: [
  148. {
  149. fromV: 6,
  150. toV: 8,
  151. meta: {
  152. users: ['5a5637efdac84e81b71014c4', 31],
  153. start_ts: 1512383567277,
  154. end_ts: 1512383572877,
  155. },
  156. pathnames: ['bar.tex', 'main.tex'],
  157. project_ops: [],
  158. labels: [
  159. {
  160. id: label.id.toString(),
  161. comment: this.comment,
  162. version: 8,
  163. user_id: this.user_id,
  164. created_at: this.created_at.toISOString(),
  165. },
  166. ],
  167. },
  168. ],
  169. })
  170. })
  171. })