Quellcode durchsuchen

promisify LabelsTests and SummarisedUpdatesTests

GitOrigin-RevId: 32552c8c63ebbd8e73e8c179fcce67b3b955251d
Domagoj Kriskovic vor 1 Jahr
Ursprung
Commit
64d1f160a1

+ 117 - 213
services/project-history/test/acceptance/js/LabelsTests.js

@@ -11,272 +11,176 @@ const MockWeb = () => nock('http://127.0.0.1:3000')
 const fixture = path => new URL(`../fixtures/${path}`, import.meta.url)
 
 describe('Labels', function () {
-  beforeEach(function (done) {
-    ProjectHistoryApp.ensureRunning(error => {
-      if (error != null) {
-        throw error
-      }
+  beforeEach(async function () {
+    await ProjectHistoryApp.promises.ensureRunning()
 
-      this.historyId = new ObjectId().toString()
-      MockHistoryStore().post('/api/projects').reply(200, {
-        projectId: this.historyId,
-      })
+    this.historyId = new ObjectId().toString()
+    MockHistoryStore().post('/api/projects').reply(200, {
+      projectId: this.historyId,
+    })
 
-      ProjectHistoryClient.initializeProject(
-        this.historyId,
-        (error, olProject) => {
-          if (error != null) {
-            throw error
-          }
-          this.project_id = new ObjectId().toString()
-          MockWeb()
-            .get(`/project/${this.project_id}/details`)
-            .reply(200, {
-              name: 'Test Project',
-              overleaf: { history: { id: olProject.id } },
-            })
+    const olProject = await ProjectHistoryClient.promises.initializeProject(
+      this.historyId
+    )
+    this.project_id = new ObjectId().toString()
+    MockWeb()
+      .get(`/project/${this.project_id}/details`)
+      .reply(200, {
+        name: 'Test Project',
+        overleaf: { history: { id: olProject.id } },
+      })
 
-          MockHistoryStore()
-            .get(`/api/projects/${this.historyId}/latest/history`)
-            .replyWithFile(200, fixture('chunks/7-8.json'))
+    MockHistoryStore()
+      .get(`/api/projects/${this.historyId}/latest/history`)
+      .replyWithFile(200, fixture('chunks/7-8.json'))
 
-          MockHistoryStore()
-            .get(`/api/projects/${this.historyId}/versions/7/history`)
-            .replyWithFile(200, fixture('chunks/7-8.json'))
-            .persist()
-          MockHistoryStore()
-            .get(`/api/projects/${this.historyId}/versions/8/history`)
-            .replyWithFile(200, fixture('chunks/7-8.json'))
-            .persist()
+    MockHistoryStore()
+      .get(`/api/projects/${this.historyId}/versions/7/history`)
+      .replyWithFile(200, fixture('chunks/7-8.json'))
+      .persist()
+    MockHistoryStore()
+      .get(`/api/projects/${this.historyId}/versions/8/history`)
+      .replyWithFile(200, fixture('chunks/7-8.json'))
+      .persist()
 
-          this.comment = 'a saved version comment'
-          this.comment2 = 'another saved version comment'
-          this.user_id = new ObjectId().toString()
-          this.created_at = new Date(1)
-          done()
-        }
-      )
-    })
+    this.comment = 'a saved version comment'
+    this.comment2 = 'another saved version comment'
+    this.user_id = new ObjectId().toString()
+    this.created_at = new Date(1)
   })
 
   afterEach(function () {
     nock.cleanAll()
   })
 
-  it('can create and get labels', function (done) {
-    ProjectHistoryClient.createLabel(
+  it('can create and get labels', async function () {
+    const label = await ProjectHistoryClient.createLabel(
       this.project_id,
       this.user_id,
       7,
       this.comment,
-      this.created_at,
-      (error, label) => {
-        if (error != null) {
-          throw error
-        }
-        ProjectHistoryClient.getLabels(this.project_id, (error, labels) => {
-          if (error != null) {
-            throw error
-          }
-          expect(labels).to.deep.equal([label])
-          done()
-        })
-      }
+      this.created_at
     )
+    const labels = await ProjectHistoryClient.getLabels(this.project_id)
+    expect(labels).to.deep.equal([label])
   })
 
-  it('can create and get labels with no user id', function (done) {
+  it('can create and get labels with no user id', async function () {
     const userId = undefined
-    ProjectHistoryClient.createLabel(
+    const label = await ProjectHistoryClient.createLabel(
       this.project_id,
       userId,
       7,
       this.comment,
-      this.created_at,
-      (error, label) => {
-        if (error != null) {
-          throw error
-        }
-        ProjectHistoryClient.getLabels(this.project_id, (error, labels) => {
-          if (error != null) {
-            throw error
-          }
-          expect(labels).to.deep.equal([label])
-          done()
-        })
-      }
+      this.created_at
     )
+    const labels = await ProjectHistoryClient.getLabels(this.project_id)
+    expect(labels).to.deep.equal([label])
   })
 
-  it('can delete labels', function (done) {
-    ProjectHistoryClient.createLabel(
+  it('can delete labels', async function () {
+    const label = await ProjectHistoryClient.createLabel(
       this.project_id,
       this.user_id,
       7,
       this.comment,
-      this.created_at,
-      (error, label) => {
-        if (error != null) {
-          throw error
-        }
-        ProjectHistoryClient.deleteLabel(this.project_id, label.id, error => {
-          if (error != null) {
-            throw error
-          }
-          ProjectHistoryClient.getLabels(this.project_id, (error, labels) => {
-            if (error != null) {
-              throw error
-            }
-            expect(labels).to.deep.equal([])
-            done()
-          })
-        })
-      }
+      this.created_at
     )
+    await ProjectHistoryClient.deleteLabel(this.project_id, label.id)
+    const labels = await ProjectHistoryClient.getLabels(this.project_id)
+    expect(labels).to.deep.equal([])
   })
 
-  it('can delete labels for the current user', function (done) {
-    ProjectHistoryClient.createLabel(
+  it('can delete labels for the current user', async function () {
+    const label = await ProjectHistoryClient.createLabel(
       this.project_id,
       this.user_id,
       7,
       this.comment,
-      this.created_at,
-      (error, label) => {
-        if (error != null) {
-          throw error
-        }
-        ProjectHistoryClient.deleteLabelForUser(
-          this.project_id,
-          this.user_id,
-          label.id,
-          error => {
-            if (error != null) {
-              throw error
-            }
-            ProjectHistoryClient.getLabels(this.project_id, (error, labels) => {
-              if (error != null) {
-                throw error
-              }
-              expect(labels).to.deep.equal([])
-              done()
-            })
-          }
-        )
-      }
+      this.created_at
+    )
+    await ProjectHistoryClient.deleteLabelForUser(
+      this.project_id,
+      this.user_id,
+      label.id
     )
+    const labels = await ProjectHistoryClient.getLabels(this.project_id)
+    expect(labels).to.deep.equal([])
   })
 
-  it('can transfer ownership of labels', function (done) {
+  it('can transfer ownership of labels', async function () {
     const fromUser = new ObjectId().toString()
     const toUser = new ObjectId().toString()
-    ProjectHistoryClient.createLabel(
+    const label = await ProjectHistoryClient.createLabel(
       this.project_id,
       fromUser,
       7,
       this.comment,
-      this.created_at,
-      (error, label) => {
-        if (error != null) {
-          throw error
-        }
-        ProjectHistoryClient.createLabel(
-          this.project_id,
-          fromUser,
-          7,
-          this.comment2,
-          this.created_at,
-          (error, label2) => {
-            if (error != null) {
-              throw error
-            }
-            ProjectHistoryClient.transferLabelOwnership(
-              fromUser,
-              toUser,
-              error => {
-                if (error != null) {
-                  throw error
-                }
-                ProjectHistoryClient.getLabels(
-                  this.project_id,
-                  (error, labels) => {
-                    if (error != null) {
-                      throw error
-                    }
-                    expect(labels).to.deep.equal([
-                      {
-                        id: label.id,
-                        comment: label.comment,
-                        version: label.version,
-                        created_at: label.created_at,
-                        user_id: toUser,
-                      },
-                      {
-                        id: label2.id,
-                        comment: label2.comment,
-                        version: label2.version,
-                        created_at: label2.created_at,
-                        user_id: toUser,
-                      },
-                    ])
-                    done()
-                  }
-                )
-              }
-            )
-          }
-        )
-      }
+      this.created_at
     )
+    const label2 = await ProjectHistoryClient.createLabel(
+      this.project_id,
+      fromUser,
+      7,
+      this.comment2,
+      this.created_at
+    )
+    await ProjectHistoryClient.transferLabelOwnership(fromUser, toUser)
+    const labels = await ProjectHistoryClient.getLabels(this.project_id)
+    expect(labels).to.deep.equal([
+      {
+        id: label.id,
+        comment: label.comment,
+        version: label.version,
+        created_at: label.created_at,
+        user_id: toUser,
+      },
+      {
+        id: label2.id,
+        comment: label2.comment,
+        version: label2.version,
+        created_at: label2.created_at,
+        user_id: toUser,
+      },
+    ])
   })
 
-  it('should return labels with summarized updates', function (done) {
-    ProjectHistoryClient.createLabel(
+  it('should return labels with summarized updates', async function () {
+    const label = await ProjectHistoryClient.createLabel(
       this.project_id,
       this.user_id,
       8,
       this.comment,
-      this.created_at,
-      (error, label) => {
-        if (error != null) {
-          throw error
-        }
-        ProjectHistoryClient.getSummarizedUpdates(
-          this.project_id,
-          { min_count: 1 },
-          (error, updates) => {
-            if (error != null) {
-              throw error
-            }
-            expect(updates).to.deep.equal({
-              nextBeforeTimestamp: 6,
-              updates: [
-                {
-                  fromV: 6,
-                  toV: 8,
-                  meta: {
-                    users: ['5a5637efdac84e81b71014c4', 31],
-                    start_ts: 1512383567277,
-                    end_ts: 1512383572877,
-                  },
-                  pathnames: ['bar.tex', 'main.tex'],
-                  project_ops: [],
-                  labels: [
-                    {
-                      id: label.id.toString(),
-                      comment: this.comment,
-                      version: 8,
-                      user_id: this.user_id,
-                      created_at: this.created_at.toISOString(),
-                    },
-                  ],
-                },
-              ],
-            })
-            done()
-          }
-        )
-      }
+      this.created_at
     )
+    const updates = await ProjectHistoryClient.getSummarizedUpdates(
+      this.project_id,
+      { min_count: 1 }
+    )
+    expect(updates).to.deep.equal({
+      nextBeforeTimestamp: 6,
+      updates: [
+        {
+          fromV: 6,
+          toV: 8,
+          meta: {
+            users: ['5a5637efdac84e81b71014c4', 31],
+            start_ts: 1512383567277,
+            end_ts: 1512383572877,
+          },
+          pathnames: ['bar.tex', 'main.tex'],
+          project_ops: [],
+          labels: [
+            {
+              id: label.id.toString(),
+              comment: this.comment,
+              version: 8,
+              user_id: this.user_id,
+              created_at: this.created_at.toISOString(),
+            },
+          ],
+        },
+      ],
+    })
   })
 })

+ 5 - 5
services/project-history/test/acceptance/js/SummarisedUpdatesTests.js

@@ -48,7 +48,7 @@ describe('Summarized updates', function () {
   })
 
   it('should return the latest summarized updates from a single chunk', async function () {
-    const updates = await ProjectHistoryClient.promises.getSummarizedUpdates(
+    const updates = await ProjectHistoryClient.getSummarizedUpdates(
       this.projectId,
       { min_count: 1 }
     )
@@ -73,7 +73,7 @@ describe('Summarized updates', function () {
   })
 
   it('should return the latest summarized updates, with min_count spanning multiple chunks', async function () {
-    const updates = await ProjectHistoryClient.promises.getSummarizedUpdates(
+    const updates = await ProjectHistoryClient.getSummarizedUpdates(
       this.projectId,
       { min_count: 5 }
     )
@@ -165,7 +165,7 @@ describe('Summarized updates', function () {
       .get(`/api/projects/${this.historyId}/versions/4/history`)
       .replyWithFile(200, fixture('chunks/4-6.json'))
 
-    const updates = await ProjectHistoryClient.promises.getSummarizedUpdates(
+    const updates = await ProjectHistoryClient.getSummarizedUpdates(
       this.projectId,
       { before: 4 }
     )
@@ -178,7 +178,7 @@ describe('Summarized updates', function () {
       .get(`/api/projects/${this.historyId}/versions/5/history`)
       .replyWithFile(200, fixture('chunks/4-6.json'))
 
-    const updates = await ProjectHistoryClient.promises.getSummarizedUpdates(
+    const updates = await ProjectHistoryClient.getSummarizedUpdates(
       this.projectId,
       { before: 5 }
     )
@@ -191,7 +191,7 @@ describe('Summarized updates', function () {
       .get(`/api/projects/${this.historyId}/versions/6/history`)
       .replyWithFile(200, fixture('chunks/4-6.json'))
 
-    const updates = await ProjectHistoryClient.promises.getSummarizedUpdates(
+    const updates = await ProjectHistoryClient.getSummarizedUpdates(
       this.projectId,
       { before: 6 }
     )

+ 36 - 76
services/project-history/test/acceptance/js/helpers/ProjectHistoryClient.js

@@ -57,21 +57,15 @@ export function flushProject(projectId, options, callback) {
   )
 }
 
-export function getSummarizedUpdates(projectId, query, callback) {
-  request.get(
-    {
-      url: `http://127.0.0.1:3054/project/${projectId}/updates`,
-      qs: query,
-      json: true,
-    },
-    (error, res, body) => {
-      if (error) {
-        return callback(error)
-      }
-      expect(res.statusCode).to.equal(200)
-      callback(error, body)
-    }
-  )
+export async function getSummarizedUpdates(projectId, query) {
+  const url = new URL(`http://127.0.0.1:3054/project/${projectId}/updates`)
+  Object.keys(query).forEach(key => {
+    url.searchParams.set(key, query[key])
+  })
+
+  const { response, json } = await fetchJsonWithResponse(url.toString())
+  expect(response.status).to.equal(200)
+  return json
 }
 
 export async function getDiff(projectId, pathname, from, to) {
@@ -223,73 +217,46 @@ export function resyncHistory(projectId, callback) {
   )
 }
 
-export function createLabel(
+export async function createLabel(
   projectId,
   userId,
   version,
   comment,
-  createdAt,
-  callback
+  createdAt
 ) {
-  request.post(
+  const { response, json } = await fetchJsonWithResponse(
+    `http://127.0.0.1:3054/project/${projectId}/labels`,
     {
-      url: `http://127.0.0.1:3054/project/${projectId}/labels`,
+      method: 'POST',
       json: { comment, version, created_at: createdAt, user_id: userId },
-    },
-    (error, res, body) => {
-      if (error) {
-        return callback(error)
-      }
-      expect(res.statusCode).to.equal(200)
-      callback(null, body)
     }
   )
+  expect(response.status).to.equal(200)
+  return json
 }
 
-export function getLabels(projectId, callback) {
-  request.get(
-    {
-      url: `http://127.0.0.1:3054/project/${projectId}/labels`,
-      json: true,
-    },
-    (error, res, body) => {
-      if (error) {
-        return callback(error)
-      }
-      expect(res.statusCode).to.equal(200)
-      callback(null, body)
-    }
+export async function getLabels(projectId) {
+  const { response, json } = await fetchJsonWithResponse(
+    `http://127.0.0.1:3054/project/${projectId}/labels`
   )
+  expect(response.status).to.equal(200)
+  return json
 }
 
-export function deleteLabelForUser(projectId, userId, labelId, callback) {
-  request.delete(
-    {
-      url: `http://127.0.0.1:3054/project/${projectId}/user/${userId}/labels/${labelId}`,
-    },
-    (error, res, body) => {
-      if (error) {
-        return callback(error)
-      }
-      expect(res.statusCode).to.equal(204)
-      callback(null, body)
-    }
+export async function deleteLabelForUser(projectId, userId, labelId) {
+  const response = await fetchNothing(
+    `http://127.0.0.1:3054/project/${projectId}/user/${userId}/labels/${labelId}`,
+    { method: 'DELETE' }
   )
+  expect(response.status).to.equal(204)
 }
 
-export function deleteLabel(projectId, labelId, callback) {
-  request.delete(
-    {
-      url: `http://127.0.0.1:3054/project/${projectId}/labels/${labelId}`,
-    },
-    (error, res, body) => {
-      if (error) {
-        return callback(error)
-      }
-      expect(res.statusCode).to.equal(204)
-      callback(null, body)
-    }
+export async function deleteLabel(projectId, labelId) {
+  const response = await fetchNothing(
+    `http://127.0.0.1:3054/project/${projectId}/labels/${labelId}`,
+    { method: 'DELETE' }
   )
+  expect(response.status).to.equal(204)
 }
 
 export async function setFailure(failureEntry) {
@@ -301,19 +268,12 @@ export function getFailure(projectId, callback) {
   db.projectHistoryFailures.findOne({ project_id: projectId }, callback)
 }
 
-export function transferLabelOwnership(fromUser, toUser, callback) {
-  request.post(
-    {
-      url: `http://127.0.0.1:3054/user/${fromUser}/labels/transfer/${toUser}`,
-    },
-    (error, res, body) => {
-      if (error) {
-        return callback(error)
-      }
-      expect(res.statusCode).to.equal(204)
-      callback(null, body)
-    }
+export async function transferLabelOwnership(fromUser, toUser) {
+  const response = await fetchNothing(
+    `http://127.0.0.1:3054/user/${fromUser}/labels/transfer/${toUser}`,
+    { method: 'POST' }
   )
+  expect(response.status).to.equal(204)
 }
 
 export async function getDump(projectId) {