Introduce overviews command

This commit is contained in:
Chocobozzz 2021-07-06 14:30:20 +02:00
parent 480d6ea679
commit 23a3a8827c
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
9 changed files with 88 additions and 100 deletions

View File

@ -1,8 +1,7 @@
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
import 'mocha'
import { cleanupTests, flushAndRunServer, ServerInfo } from '../../../../shared/extra-utils'
import { getVideosOverview } from '@shared/extra-utils/overviews/overviews'
import { cleanupTests, flushAndRunServer, ServerInfo } from '@shared/extra-utils'
describe('Test videos overview', function () {
let server: ServerInfo
@ -18,12 +17,12 @@ describe('Test videos overview', function () {
describe('When getting videos overview', function () {
it('Should fail with a bad pagination', async function () {
await getVideosOverview(server.url, 0, 400)
await getVideosOverview(server.url, 100, 400)
await server.overviewsCommand.getVideos({ page: 0, expectedStatus: 400 })
await server.overviewsCommand.getVideos({ page: 100, expectedStatus: 400 })
})
it('Should succeed with a good pagination', async function () {
await getVideosOverview(server.url, 1)
await server.overviewsCommand.getVideos({ page: 1 })
})
})

View File

@ -1,34 +1,33 @@
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
import * as chai from 'chai'
import 'mocha'
import { cleanupTests, getVideosList, ServerInfo, setAccessTokensToServers, uploadVideo } from '../../../../shared/extra-utils/index'
import { userLogin } from '../../../../shared/extra-utils/users/login'
import { createUser } from '../../../../shared/extra-utils/users/users'
import { getMyVideos } from '../../../../shared/extra-utils/videos/videos'
import * as chai from 'chai'
import {
cleanupTests,
createUser,
flushAndRunServer,
getAccountVideos,
getConfig,
getCustomConfig,
getMyUserInformation,
getMyVideos,
getVideoChannelVideos,
getVideosList,
getVideosListWithToken,
searchVideo,
searchVideoWithToken,
ServerInfo,
setAccessTokensToServers,
updateCustomConfig,
updateMyUser
} from '../../../../shared/extra-utils'
import { ServerConfig, VideosOverview } from '../../../../shared/models'
import { CustomConfig } from '../../../../shared/models/server/custom-config.model'
import { User } from '../../../../shared/models/users'
import { getVideosOverview, getVideosOverviewWithToken } from '@shared/extra-utils/overviews/overviews'
updateMyUser,
uploadVideo,
userLogin
} from '@shared/extra-utils'
import { CustomConfig, ServerConfig, User, VideosOverview } from '@shared/models'
const expect = chai.expect
function createOverviewRes (res: any) {
const overview = res.body as VideosOverview
function createOverviewRes (overview: VideosOverview) {
const videos = overview.categories[0].videos
return { body: { data: videos, total: videos.length } }
}
@ -57,7 +56,9 @@ describe('Test video NSFW policy', function () {
// Overviews do not support video filters
if (!hasQuery) {
promises.push(getVideosOverviewWithToken(server.url, 1, token).then(res => createOverviewRes(res)))
const p = server.overviewsCommand.getVideos({ page: 1, token })
.then(res => createOverviewRes(res))
promises.push(p)
}
return Promise.all(promises)
@ -72,7 +73,9 @@ describe('Test video NSFW policy', function () {
// Overviews do not support video filters
if (!hasQuery) {
promises.push(getVideosOverview(server.url, 1).then(res => createOverviewRes(res)))
const p = server.overviewsCommand.getVideos({ page: 1 })
.then(res => createOverviewRes(res))
promises.push(p)
}
return Promise.all(promises)

View File

@ -2,8 +2,9 @@
import 'mocha'
import * as chai from 'chai'
import { Response } from 'superagent'
import {
addAccountToAccountBlocklist,
cleanupTests,
flushAndRunServer,
generateUserAccessToken,
@ -11,20 +12,15 @@ import {
setAccessTokensToServers,
uploadVideo,
wait
} from '../../../../shared/extra-utils'
import { getVideosOverview, getVideosOverviewWithToken } from '../../../../shared/extra-utils/overviews/overviews'
import { VideosOverview } from '../../../../shared/models/overviews'
import { addAccountToAccountBlocklist } from '@shared/extra-utils/users/blocklist'
import { Response } from 'superagent'
} from '@shared/extra-utils'
import { VideosOverview } from '@shared/models'
const expect = chai.expect
describe('Test a videos overview', function () {
let server: ServerInfo = null
function testOverviewCount (res: Response, expected: number) {
const overview: VideosOverview = res.body
function testOverviewCount (overview: VideosOverview, expected: number) {
expect(overview.tags).to.have.lengthOf(expected)
expect(overview.categories).to.have.lengthOf(expected)
expect(overview.channels).to.have.lengthOf(expected)
@ -39,9 +35,9 @@ describe('Test a videos overview', function () {
})
it('Should send empty overview', async function () {
const res = await getVideosOverview(server.url, 1)
const body = await server.overviewsCommand.getVideos({ page: 1 })
testOverviewCount(res, 0)
testOverviewCount(body, 0)
})
it('Should upload 5 videos in a specific category, tag and channel but not include them in overview', async function () {
@ -55,14 +51,15 @@ describe('Test a videos overview', function () {
tags: [ 'coucou1', 'coucou2' ]
})
const res = await getVideosOverview(server.url, 1)
const body = await server.overviewsCommand.getVideos({ page: 1 })
testOverviewCount(res, 0)
testOverviewCount(body, 0)
})
it('Should upload another video and include all videos in the overview', async function () {
this.timeout(30000)
{
for (let i = 1; i < 6; i++) {
await uploadVideo(server.url, server.accessToken, {
name: 'video ' + i,
@ -72,17 +69,17 @@ describe('Test a videos overview', function () {
}
await wait(3000)
{
const res = await getVideosOverview(server.url, 1)
testOverviewCount(res, 1)
}
{
const res = await getVideosOverview(server.url, 2)
const body = await server.overviewsCommand.getVideos({ page: 1 })
testOverviewCount(body, 1)
}
{
const overview = await server.overviewsCommand.getVideos({ page: 2 })
const overview: VideosOverview = res.body
expect(overview.tags).to.have.lengthOf(1)
expect(overview.categories).to.have.lengthOf(0)
expect(overview.channels).to.have.lengthOf(0)
@ -90,20 +87,10 @@ describe('Test a videos overview', function () {
})
it('Should have the correct overview', async function () {
const res1 = await getVideosOverview(server.url, 1)
const res2 = await getVideosOverview(server.url, 2)
const overview1 = await server.overviewsCommand.getVideos({ page: 1 })
const overview2 = await server.overviewsCommand.getVideos({ page: 2 })
const overview1: VideosOverview = res1.body
const overview2: VideosOverview = res2.body
const tmp = [
overview1.tags,
overview1.categories,
overview1.channels,
overview2.tags
]
for (const arr of tmp) {
for (const arr of [ overview1.tags, overview1.categories, overview1.channels, overview2.tags ]) {
expect(arr).to.have.lengthOf(1)
const obj = arr[0]
@ -132,15 +119,15 @@ describe('Test a videos overview', function () {
await addAccountToAccountBlocklist(server.url, token, 'root@' + server.host)
{
const res = await getVideosOverview(server.url, 1)
const body = await server.overviewsCommand.getVideos({ page: 1 })
testOverviewCount(res, 1)
testOverviewCount(body, 1)
}
{
const res = await getVideosOverviewWithToken(server.url, 1, token)
const body = await server.overviewsCommand.getVideos({ page: 1, token })
testOverviewCount(res, 0)
testOverviewCount(body, 0)
}
})

View File

@ -6,6 +6,7 @@ export * from './logs'
export * from './miscs'
export * from './mock-servers'
export * from './moderation'
export * from './overviews'
export * from './requests/check-api-params'
export * from './requests/requests'

View File

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

View File

@ -0,0 +1,25 @@
import { HttpStatusCode } from '@shared/core-utils'
import { VideosOverview } from '@shared/models'
import { AbstractCommand, OverrideCommandOptions } from '../shared'
export class OverviewsCommand extends AbstractCommand {
getVideos (options: OverrideCommandOptions & {
page: number
token?: string
}) {
const { token, page } = options
const path = '/api/v1/overviews/videos'
const query = { page }
return this.getRequestBody<VideosOverview>({
...options,
token: token || null,
path,
query,
defaultExpectedStatus: HttpStatusCode.OK_200
})
}
}

View File

@ -1,34 +0,0 @@
import { makeGetRequest } from '../requests/requests'
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
function getVideosOverview (url: string, page: number, statusCodeExpected = HttpStatusCode.OK_200) {
const path = '/api/v1/overviews/videos'
const query = { page }
return makeGetRequest({
url,
path,
query,
statusCodeExpected
})
}
function getVideosOverviewWithToken (url: string, page: number, token: string, statusCodeExpected = HttpStatusCode.OK_200) {
const path = '/api/v1/overviews/videos'
const query = { page }
return makeGetRequest({
url,
path,
query,
token,
statusCodeExpected
})
}
export {
getVideosOverview,
getVideosOverviewWithToken
}

View File

@ -13,6 +13,7 @@ import { FeedCommand } from '../feeds'
import { LogsCommand } from '../logs'
import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs'
import { AbusesCommand } from '../moderation'
import { OverviewsCommand } from '../overviews'
import { makeGetRequest } from '../requests/requests'
interface ServerInfo {
@ -73,6 +74,7 @@ interface ServerInfo {
feedCommand?: FeedCommand
logsCommand?: LogsCommand
abusesCommand?: AbusesCommand
overviewsCommand?: OverviewsCommand
}
function parallelTests () {
@ -284,6 +286,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = []
server.feedCommand = new FeedCommand(server)
server.logsCommand = new LogsCommand(server)
server.abusesCommand = new AbusesCommand(server)
server.overviewsCommand = new OverviewsCommand(server)
res(server)
})

View File

@ -78,7 +78,10 @@ abstract class AbstractCommand {
return {
url: this.server.url,
path,
token: token ?? this.server.accessToken,
// Token can be null if we don't want to add it
token: token !== undefined ? token : this.server.accessToken,
statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
}
}