Introduce accounts command

This commit is contained in:
Chocobozzz
2021-07-07 13:38:26 +02:00
parent 87e2635a50
commit 9fff08cf83
15 changed files with 172 additions and 167 deletions

View File

@@ -10,17 +10,11 @@ export * from './overviews'
export * from './search'
export * from './server'
export * from './socket'
export * from './users'
export * from './requests/check-api-params'
export * from './requests/requests'
export * from './users/accounts'
export * from './users/blocklist'
export * from './users/login'
export * from './users/user-notifications'
export * from './users/user-subscriptions'
export * from './users/users'
export * from './videos/live'
export * from './videos/services'
export * from './videos/video-blacklist'

View File

@@ -17,6 +17,7 @@ import { OverviewsCommand } from '../overviews'
import { makeGetRequest } from '../requests/requests'
import { SearchCommand } from '../search'
import { SocketIOCommand } from '../socket'
import { AccountsCommand } from '../users'
import { ConfigCommand } from './config-command'
import { ContactFormCommand } from './contact-form-command'
import { DebugCommand } from './debug-command'
@@ -95,6 +96,7 @@ interface ServerInfo {
statsCommand?: StatsCommand
configCommand?: ConfigCommand
socketIOCommand?: SocketIOCommand
accountsCommand?: AccountsCommand
}
function parallelTests () {
@@ -317,6 +319,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = []
server.statsCommand = new StatsCommand(server)
server.configCommand = new ConfigCommand(server)
server.socketIOCommand = new SocketIOCommand(server)
server.accountsCommand = new AccountsCommand(server)
res(server)
})

View File

@@ -0,0 +1,54 @@
import { ResultList } from '@shared/models'
import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes'
import { Account } from '../../models/actors'
import { AccountVideoRate, VideoRateType } from '../../models/videos'
import { AbstractCommand, OverrideCommandOptions } from '../shared'
export class AccountsCommand extends AbstractCommand {
list (options: OverrideCommandOptions & {
sort?: string // default -createdAt
} = {}) {
const { sort = '-createdAt' } = options
const path = '/api/v1/accounts'
return this.getRequestBody<ResultList<Account>>({
...options,
path,
query: { sort },
defaultExpectedStatus: HttpStatusCode.OK_200
})
}
get (options: OverrideCommandOptions & {
accountName: string
}) {
const path = '/api/v1/accounts/' + options.accountName
return this.getRequestBody<Account>({
...options,
path,
defaultExpectedStatus: HttpStatusCode.OK_200
})
}
listRatings (options: OverrideCommandOptions & {
accountName: string
rating?: VideoRateType
}) {
const { rating, accountName } = options
const path = '/api/v1/accounts/' + accountName + '/ratings'
const query = { rating }
return this.getRequestBody<ResultList<AccountVideoRate>>({
...options,
path,
query,
defaultExpectedStatus: HttpStatusCode.OK_200
})
}
}

View File

@@ -1,43 +1,25 @@
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
import * as request from 'supertest'
import { expect } from 'chai'
import { existsSync, readdir } from 'fs-extra'
import { pathExists, readdir } from 'fs-extra'
import { join } from 'path'
import { Account } from '../../models/actors'
import { root } from '../miscs/miscs'
import { makeGetRequest } from '../requests/requests'
import { VideoRateType } from '../../models/videos'
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
import { root } from '@server/helpers/core-utils'
import { ServerInfo } from '../server'
function getAccountsList (url: string, sort = '-createdAt', statusCodeExpected = HttpStatusCode.OK_200) {
const path = '/api/v1/accounts'
async function expectAccountFollows (options: {
server: ServerInfo
handle: string
followers: number
following: number
}) {
const { server, handle, followers, following } = options
return makeGetRequest({
url,
query: { sort },
path,
statusCodeExpected
})
}
const body = await server.accountsCommand.list()
const account = body.data.find(a => a.name + '@' + a.host === handle)
function getAccount (url: string, accountName: string, statusCodeExpected = HttpStatusCode.OK_200) {
const path = '/api/v1/accounts/' + accountName
return makeGetRequest({
url,
path,
statusCodeExpected
})
}
async function expectAccountFollows (url: string, nameWithDomain: string, followersCount: number, followingCount: number) {
const res = await getAccountsList(url)
const account = res.body.data.find((a: Account) => a.name + '@' + a.host === nameWithDomain)
const message = `${nameWithDomain} on ${url}`
expect(account.followersCount).to.equal(followersCount, message)
expect(account.followingCount).to.equal(followingCount, message)
const message = `${handle} on ${server.url}`
expect(account.followersCount).to.equal(followers, message)
expect(account.followingCount).to.equal(following, message)
}
async function checkActorFilesWereRemoved (filename: string, serverNumber: number) {
@@ -46,7 +28,7 @@ async function checkActorFilesWereRemoved (filename: string, serverNumber: numbe
for (const directory of [ 'avatars' ]) {
const directoryPath = join(root(), testDirectory, directory)
const directoryExists = existsSync(directoryPath)
const directoryExists = await pathExists(directoryPath)
expect(directoryExists).to.be.true
const files = await readdir(directoryPath)
@@ -56,32 +38,7 @@ async function checkActorFilesWereRemoved (filename: string, serverNumber: numbe
}
}
function getAccountRatings (
url: string,
accountName: string,
accessToken: string,
rating?: VideoRateType,
statusCodeExpected = HttpStatusCode.OK_200
) {
const path = '/api/v1/accounts/' + accountName + '/ratings'
const query = rating ? { rating } : {}
return request(url)
.get(path)
.query(query)
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + accessToken)
.expect(statusCodeExpected)
.expect('Content-Type', /json/)
}
// ---------------------------------------------------------------------------
export {
getAccount,
expectAccountFollows,
getAccountsList,
checkActorFilesWereRemoved,
getAccountRatings
checkActorFilesWereRemoved
}

View File

@@ -0,0 +1,8 @@
export * from './accounts'
export * from './accounts-command'
export * from './blocklist'
export * from './login'
export * from './user-notifications'
export * from './user-subscriptions'
export * from './users'