]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Introduce bulk command
authorChocobozzz <me@florianbigard.com>
Mon, 5 Jul 2021 12:57:03 +0000 (14:57 +0200)
committerChocobozzz <me@florianbigard.com>
Tue, 20 Jul 2021 13:27:16 +0000 (15:27 +0200)
server/tests/api/server/bulk.ts
shared/extra-utils/bulk/bulk.ts
shared/extra-utils/shared/abstract-command.ts [new file with mode: 0644]
shared/extra-utils/shared/index.ts [new file with mode: 0644]

index 80fa7fce67779f4e14ba40e35f984edc0d576b5e..ca98a2eb2c448f74d260438a1eb57e6011adad6b 100644 (file)
@@ -6,7 +6,7 @@ import { Video, VideoComment } from '@shared/models'
 import {
   addVideoCommentReply,
   addVideoCommentThread,
-  bulkRemoveCommentsOf,
+  BulkCommand,
   cleanupTests,
   createUser,
   doubleFollow,
@@ -30,6 +30,8 @@ describe('Test bulk actions', function () {
   let user2AccessToken: string
   let user3AccessToken: string
 
+  let bulkCommand: BulkCommand
+
   before(async function () {
     this.timeout(30000)
 
@@ -60,6 +62,8 @@ describe('Test bulk actions', function () {
     }
 
     await doubleFollow(servers[0], servers[1])
+
+    bulkCommand = new BulkCommand(servers[0])
   })
 
   describe('Bulk remove comments', function () {
@@ -133,8 +137,7 @@ describe('Test bulk actions', function () {
     it('Should delete comments of an account on my videos', async function () {
       this.timeout(60000)
 
-      await bulkRemoveCommentsOf({
-        url: servers[0].url,
+      await bulkCommand.removeCommentsOf({
         token: user1AccessToken,
         attributes: {
           accountName: 'user2',
@@ -164,9 +167,7 @@ describe('Test bulk actions', function () {
     it('Should delete comments of an account on the instance', async function () {
       this.timeout(60000)
 
-      await bulkRemoveCommentsOf({
-        url: servers[0].url,
-        token: servers[0].accessToken,
+      await bulkCommand.removeCommentsOf({
         attributes: {
           accountName: 'user3@localhost:' + servers[1].port,
           scope: 'instance'
index b6f437b8bfcf30b21751d352765c77f44d492a69..c102383e33605caf59b1b0fa8b8948b8cd678983 100644 (file)
@@ -1,25 +1,24 @@
-import { BulkRemoveCommentsOfBody } from "@shared/models/bulk/bulk-remove-comments-of-body.model"
-import { makePostBodyRequest } from "../requests/requests"
+
+import { BulkRemoveCommentsOfBody } from '@shared/models/bulk/bulk-remove-comments-of-body.model'
 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
+import { AbstractCommand, CommonCommandOptions } from '../shared'
+
+class BulkCommand extends AbstractCommand {
 
-function bulkRemoveCommentsOf (options: {
-  url: string
-  token: string
-  attributes: BulkRemoveCommentsOfBody
-  expectedStatus?: number
-}) {
-  const { url, token, attributes, expectedStatus } = options
-  const path = '/api/v1/bulk/remove-comments-of'
+  removeCommentsOf (options: CommonCommandOptions & {
+    attributes: BulkRemoveCommentsOfBody
+  }) {
+    const { attributes } = options
 
-  return makePostBodyRequest({
-    url,
-    path,
-    token,
-    fields: attributes,
-    statusCodeExpected: expectedStatus || HttpStatusCode.NO_CONTENT_204
-  })
+    return this.postBodyRequest({
+      ...options,
+      path: '/api/v1/bulk/remove-comments-of',
+      fields: attributes,
+      defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
+    })
+  }
 }
 
 export {
-  bulkRemoveCommentsOf
+  BulkCommand
 }
diff --git a/shared/extra-utils/shared/abstract-command.ts b/shared/extra-utils/shared/abstract-command.ts
new file mode 100644 (file)
index 0000000..bb06b6b
--- /dev/null
@@ -0,0 +1,47 @@
+import { HttpStatusCode } from '@shared/core-utils'
+import { makePostBodyRequest } from '../requests/requests'
+import { ServerInfo } from '../server/servers'
+
+export interface CommonCommandOptions {
+  token?: string
+  expectedStatus?: number
+}
+
+abstract class AbstractCommand {
+
+  private expectedStatus = HttpStatusCode.OK_200
+
+  constructor (
+    protected server: ServerInfo
+  ) {
+
+  }
+
+  setServer (server: ServerInfo) {
+    this.server = server
+  }
+
+  setExpectedStatus (status: HttpStatusCode) {
+    this.expectedStatus = status
+  }
+
+  protected postBodyRequest (options: CommonCommandOptions & {
+    path: string
+    defaultExpectedStatus: number
+    fields?: { [ fieldName: string ]: any }
+  }) {
+    const { token, fields, expectedStatus, defaultExpectedStatus, path } = options
+
+    return makePostBodyRequest({
+      url: this.server.url,
+      path,
+      token: token ?? this.server.accessToken,
+      fields,
+      statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
+    })
+  }
+}
+
+export {
+  AbstractCommand
+}
diff --git a/shared/extra-utils/shared/index.ts b/shared/extra-utils/shared/index.ts
new file mode 100644 (file)
index 0000000..e807ab4
--- /dev/null
@@ -0,0 +1 @@
+export * from './abstract-command'