From 23a3a8827cb8b862f5cc7ee2819f39918303beca Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 6 Jul 2021 14:30:20 +0200 Subject: [PATCH] Introduce overviews command --- .../api/check-params/videos-overviews.ts | 9 +-- server/tests/api/videos/video-nsfw.ts | 35 +++++---- server/tests/api/videos/videos-overview.ts | 75 ++++++++----------- shared/extra-utils/index.ts | 1 + shared/extra-utils/overviews/index.ts | 1 + .../overviews/overviews-command.ts | 25 +++++++ shared/extra-utils/overviews/overviews.ts | 34 --------- shared/extra-utils/server/servers.ts | 3 + shared/extra-utils/shared/abstract-command.ts | 5 +- 9 files changed, 88 insertions(+), 100 deletions(-) create mode 100644 shared/extra-utils/overviews/index.ts create mode 100644 shared/extra-utils/overviews/overviews-command.ts delete mode 100644 shared/extra-utils/overviews/overviews.ts diff --git a/server/tests/api/check-params/videos-overviews.ts b/server/tests/api/check-params/videos-overviews.ts index 69d7fc471..44a936c9f 100644 --- a/server/tests/api/check-params/videos-overviews.ts +++ b/server/tests/api/check-params/videos-overviews.ts @@ -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 }) }) }) diff --git a/server/tests/api/videos/video-nsfw.ts b/server/tests/api/videos/video-nsfw.ts index b16b484b9..6c98c9f12 100644 --- a/server/tests/api/videos/video-nsfw.ts +++ b/server/tests/api/videos/video-nsfw.ts @@ -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) diff --git a/server/tests/api/videos/videos-overview.ts b/server/tests/api/videos/videos-overview.ts index c266a1dc5..c8e3df4bd 100644 --- a/server/tests/api/videos/videos-overview.ts +++ b/server/tests/api/videos/videos-overview.ts @@ -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,34 +51,35 @@ 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, - category: 3, - tags: [ 'coucou1', 'coucou2' ] - }) - } - - await wait(3000) - { - const res = await getVideosOverview(server.url, 1) + for (let i = 1; i < 6; i++) { + await uploadVideo(server.url, server.accessToken, { + name: 'video ' + i, + category: 3, + tags: [ 'coucou1', 'coucou2' ] + }) + } - testOverviewCount(res, 1) + await wait(3000) } { - 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) } }) diff --git a/shared/extra-utils/index.ts b/shared/extra-utils/index.ts index 7ca079c78..38c8c4c1c 100644 --- a/shared/extra-utils/index.ts +++ b/shared/extra-utils/index.ts @@ -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' diff --git a/shared/extra-utils/overviews/index.ts b/shared/extra-utils/overviews/index.ts new file mode 100644 index 000000000..e19551907 --- /dev/null +++ b/shared/extra-utils/overviews/index.ts @@ -0,0 +1 @@ +export * from './overviews-command' diff --git a/shared/extra-utils/overviews/overviews-command.ts b/shared/extra-utils/overviews/overviews-command.ts new file mode 100644 index 000000000..0ac3cbd33 --- /dev/null +++ b/shared/extra-utils/overviews/overviews-command.ts @@ -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({ + ...options, + + token: token || null, + path, + query, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } +} diff --git a/shared/extra-utils/overviews/overviews.ts b/shared/extra-utils/overviews/overviews.ts deleted file mode 100644 index 5e1a13e5e..000000000 --- a/shared/extra-utils/overviews/overviews.ts +++ /dev/null @@ -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 -} diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index f05f0dbbe..70be96cf6 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts @@ -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) }) diff --git a/shared/extra-utils/shared/abstract-command.ts b/shared/extra-utils/shared/abstract-command.ts index 3ee5cd865..3815fab0e 100644 --- a/shared/extra-utils/shared/abstract-command.ts +++ b/shared/extra-utils/shared/abstract-command.ts @@ -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 } }