LabelsTests.js 7.6 KB

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