From 764a965778ac89e027fd05dd35697c6763e0dc18 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 11 Mar 2020 14:39:28 +0100 Subject: Implement pagination for overviews endpoint --- server/tests/api/check-params/index.ts | 1 + server/tests/api/check-params/videos-overviews.ts | 33 +++++++++ server/tests/api/videos/video-nsfw.ts | 38 ++++++++-- server/tests/api/videos/videos-overview.ts | 85 +++++++++++++++-------- 4 files changed, 122 insertions(+), 35 deletions(-) create mode 100644 server/tests/api/check-params/videos-overviews.ts (limited to 'server/tests') diff --git a/server/tests/api/check-params/index.ts b/server/tests/api/check-params/index.ts index 924c0df76..ef152f55c 100644 --- a/server/tests/api/check-params/index.ts +++ b/server/tests/api/check-params/index.ts @@ -23,3 +23,4 @@ import './video-playlists' import './videos' import './videos-filter' import './videos-history' +import './videos-overviews' diff --git a/server/tests/api/check-params/videos-overviews.ts b/server/tests/api/check-params/videos-overviews.ts new file mode 100644 index 000000000..69d7fc471 --- /dev/null +++ b/server/tests/api/check-params/videos-overviews.ts @@ -0,0 +1,33 @@ +/* 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' + +describe('Test videos overview', function () { + let server: ServerInfo + + // --------------------------------------------------------------- + + before(async function () { + this.timeout(30000) + + server = await flushAndRunServer(1) + }) + + 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) + }) + + it('Should succeed with a good pagination', async function () { + await getVideosOverview(server.url, 1) + }) + }) + + after(async function () { + await cleanupTests([ server ]) + }) +}) diff --git a/server/tests/api/videos/video-nsfw.ts b/server/tests/api/videos/video-nsfw.ts index 7eba8d7d9..b16b484b9 100644 --- a/server/tests/api/videos/video-nsfw.ts +++ b/server/tests/api/videos/video-nsfw.ts @@ -19,12 +19,20 @@ import { updateCustomConfig, updateMyUser } from '../../../../shared/extra-utils' -import { ServerConfig } from '../../../../shared/models' +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' const expect = chai.expect +function createOverviewRes (res: any) { + const overview = res.body as VideosOverview + + const videos = overview.categories[0].videos + return { body: { data: videos, total: videos.length } } +} + describe('Test video NSFW policy', function () { let server: ServerInfo let userAccessToken: string @@ -36,22 +44,38 @@ describe('Test video NSFW policy', function () { const user: User = res.body const videoChannelName = user.videoChannels[0].name const accountName = user.account.name + '@' + user.account.host + const hasQuery = Object.keys(query).length !== 0 + let promises: Promise[] if (token) { - return Promise.all([ + promises = [ getVideosListWithToken(server.url, token, query), searchVideoWithToken(server.url, 'n', token, query), getAccountVideos(server.url, token, accountName, 0, 5, undefined, query), getVideoChannelVideos(server.url, token, videoChannelName, 0, 5, undefined, query) - ]) + ] + + // Overviews do not support video filters + if (!hasQuery) { + promises.push(getVideosOverviewWithToken(server.url, 1, token).then(res => createOverviewRes(res))) + } + + return Promise.all(promises) } - return Promise.all([ + promises = [ getVideosList(server.url), searchVideo(server.url, 'n'), getAccountVideos(server.url, undefined, accountName, 0, 5), getVideoChannelVideos(server.url, undefined, videoChannelName, 0, 5) - ]) + ] + + // Overviews do not support video filters + if (!hasQuery) { + promises.push(getVideosOverview(server.url, 1).then(res => createOverviewRes(res))) + } + + return Promise.all(promises) }) } @@ -63,12 +87,12 @@ describe('Test video NSFW policy', function () { await setAccessTokensToServers([ server ]) { - const attributes = { name: 'nsfw', nsfw: true } + const attributes = { name: 'nsfw', nsfw: true, category: 1 } await uploadVideo(server.url, server.accessToken, attributes) } { - const attributes = { name: 'normal', nsfw: false } + const attributes = { name: 'normal', nsfw: false, category: 1 } await uploadVideo(server.url, server.accessToken, attributes) } diff --git a/server/tests/api/videos/videos-overview.ts b/server/tests/api/videos/videos-overview.ts index ca08ab5b1..d38bcb6eb 100644 --- a/server/tests/api/videos/videos-overview.ts +++ b/server/tests/api/videos/videos-overview.ts @@ -2,7 +2,7 @@ import * as chai from 'chai' import 'mocha' -import { cleanupTests, flushAndRunServer, ServerInfo, setAccessTokensToServers, uploadVideo } from '../../../../shared/extra-utils' +import { cleanupTests, flushAndRunServer, ServerInfo, setAccessTokensToServers, uploadVideo, wait } from '../../../../shared/extra-utils' import { getVideosOverview } from '../../../../shared/extra-utils/overviews/overviews' import { VideosOverview } from '../../../../shared/models/overviews' @@ -20,7 +20,7 @@ describe('Test a videos overview', function () { }) it('Should send empty overview', async function () { - const res = await getVideosOverview(server.url) + const res = await getVideosOverview(server.url, 1) const overview: VideosOverview = res.body expect(overview.tags).to.have.lengthOf(0) @@ -31,15 +31,15 @@ describe('Test a videos overview', function () { it('Should upload 5 videos in a specific category, tag and channel but not include them in overview', async function () { this.timeout(15000) - for (let i = 0; i < 5; i++) { - await uploadVideo(server.url, server.accessToken, { - name: 'video ' + i, - category: 3, - tags: [ 'coucou1', 'coucou2' ] - }) - } + await wait(3000) + + await uploadVideo(server.url, server.accessToken, { + name: 'video 0', + category: 3, + tags: [ 'coucou1', 'coucou2' ] + }) - const res = await getVideosOverview(server.url) + const res = await getVideosOverview(server.url, 1) const overview: VideosOverview = res.body expect(overview.tags).to.have.lengthOf(0) @@ -48,27 +48,55 @@ describe('Test a videos overview', function () { }) it('Should upload another video and include all videos in the overview', async function () { - await uploadVideo(server.url, server.accessToken, { - name: 'video 5', - category: 3, - tags: [ 'coucou1', 'coucou2' ] - }) + this.timeout(15000) - const res = await getVideosOverview(server.url) + for (let i = 1; i < 6; i++) { + await uploadVideo(server.url, server.accessToken, { + name: 'video ' + i, + category: 3, + tags: [ 'coucou1', 'coucou2' ] + }) + } - const overview: VideosOverview = res.body - expect(overview.tags).to.have.lengthOf(2) - expect(overview.categories).to.have.lengthOf(1) - expect(overview.channels).to.have.lengthOf(1) + await wait(3000) + + { + const res = await getVideosOverview(server.url, 1) + + const overview: VideosOverview = res.body + expect(overview.tags).to.have.lengthOf(1) + expect(overview.categories).to.have.lengthOf(1) + expect(overview.channels).to.have.lengthOf(1) + } + + { + const res = await getVideosOverview(server.url, 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) + } }) it('Should have the correct overview', async function () { - const res = await getVideosOverview(server.url) + const res1 = await getVideosOverview(server.url, 1) + const res2 = await getVideosOverview(server.url, 2) - const overview: VideosOverview = res.body + 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) { + expect(arr).to.have.lengthOf(1) - for (const attr of [ 'tags', 'categories', 'channels' ]) { - const obj = overview[attr][0] + const obj = arr[0] expect(obj.videos).to.have.lengthOf(6) expect(obj.videos[0].name).to.equal('video 5') @@ -79,12 +107,13 @@ describe('Test a videos overview', function () { expect(obj.videos[5].name).to.equal('video 0') } - expect(overview.tags.find(t => t.tag === 'coucou1')).to.not.be.undefined - expect(overview.tags.find(t => t.tag === 'coucou2')).to.not.be.undefined + const tags = [ overview1.tags[0].tag, overview2.tags[0].tag ] + expect(tags.find(t => t === 'coucou1')).to.not.be.undefined + expect(tags.find(t => t === 'coucou2')).to.not.be.undefined - expect(overview.categories[0].category.id).to.equal(3) + expect(overview1.categories[0].category.id).to.equal(3) - expect(overview.channels[0].channel.name).to.equal('root_channel') + expect(overview1.channels[0].channel.name).to.equal('root_channel') }) after(async function () { -- cgit v1.2.3