Introduce bulk command

This commit is contained in:
Chocobozzz 2021-07-05 14:57:03 +02:00
parent cf21b2cbef
commit a6a79eae0d
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
4 changed files with 72 additions and 24 deletions

View File

@ -6,7 +6,7 @@ import { Video, VideoComment } from '@shared/models'
import { import {
addVideoCommentReply, addVideoCommentReply,
addVideoCommentThread, addVideoCommentThread,
bulkRemoveCommentsOf, BulkCommand,
cleanupTests, cleanupTests,
createUser, createUser,
doubleFollow, doubleFollow,
@ -30,6 +30,8 @@ describe('Test bulk actions', function () {
let user2AccessToken: string let user2AccessToken: string
let user3AccessToken: string let user3AccessToken: string
let bulkCommand: BulkCommand
before(async function () { before(async function () {
this.timeout(30000) this.timeout(30000)
@ -60,6 +62,8 @@ describe('Test bulk actions', function () {
} }
await doubleFollow(servers[0], servers[1]) await doubleFollow(servers[0], servers[1])
bulkCommand = new BulkCommand(servers[0])
}) })
describe('Bulk remove comments', function () { 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 () { it('Should delete comments of an account on my videos', async function () {
this.timeout(60000) this.timeout(60000)
await bulkRemoveCommentsOf({ await bulkCommand.removeCommentsOf({
url: servers[0].url,
token: user1AccessToken, token: user1AccessToken,
attributes: { attributes: {
accountName: 'user2', accountName: 'user2',
@ -164,9 +167,7 @@ describe('Test bulk actions', function () {
it('Should delete comments of an account on the instance', async function () { it('Should delete comments of an account on the instance', async function () {
this.timeout(60000) this.timeout(60000)
await bulkRemoveCommentsOf({ await bulkCommand.removeCommentsOf({
url: servers[0].url,
token: servers[0].accessToken,
attributes: { attributes: {
accountName: 'user3@localhost:' + servers[1].port, accountName: 'user3@localhost:' + servers[1].port,
scope: 'instance' scope: 'instance'

View 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 { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
import { AbstractCommand, CommonCommandOptions } from '../shared'
function bulkRemoveCommentsOf (options: { class BulkCommand extends AbstractCommand {
url: string
token: string
attributes: BulkRemoveCommentsOfBody
expectedStatus?: number
}) {
const { url, token, attributes, expectedStatus } = options
const path = '/api/v1/bulk/remove-comments-of'
return makePostBodyRequest({ removeCommentsOf (options: CommonCommandOptions & {
url, attributes: BulkRemoveCommentsOfBody
path, }) {
token, const { attributes } = options
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 { export {
bulkRemoveCommentsOf BulkCommand
} }

View File

@ -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
}

View File

@ -0,0 +1 @@
export * from './abstract-command'