From 1cd3facc3de899ac864e979cd6d6a704b712cce3 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 10 Oct 2018 11:46:50 +0200 Subject: Add ability to list all local videos Including private/unlisted for moderators/admins --- server/tests/api/check-params/index.ts | 1 + server/tests/api/check-params/videos-filter.ts | 127 ++++++++++++++++++++++++ server/tests/api/videos/index.ts | 1 + server/tests/api/videos/videos-filter.ts | 130 +++++++++++++++++++++++++ 4 files changed, 259 insertions(+) create mode 100644 server/tests/api/check-params/videos-filter.ts create mode 100644 server/tests/api/videos/videos-filter.ts (limited to 'server/tests') diff --git a/server/tests/api/check-params/index.ts b/server/tests/api/check-params/index.ts index 71a217649..bfc550ae5 100644 --- a/server/tests/api/check-params/index.ts +++ b/server/tests/api/check-params/index.ts @@ -15,4 +15,5 @@ import './video-channels' import './video-comments' import './video-imports' import './videos' +import './videos-filter' import './videos-history' diff --git a/server/tests/api/check-params/videos-filter.ts b/server/tests/api/check-params/videos-filter.ts new file mode 100644 index 000000000..784cd8ba1 --- /dev/null +++ b/server/tests/api/check-params/videos-filter.ts @@ -0,0 +1,127 @@ +/* tslint:disable:no-unused-expression */ + +import * as chai from 'chai' +import 'mocha' +import { + createUser, + flushTests, + killallServers, + makeGetRequest, + runServer, + ServerInfo, + setAccessTokensToServers, + userLogin +} from '../../utils' +import { UserRole } from '../../../../shared/models/users' + +const expect = chai.expect + +async function testEndpoints (server: ServerInfo, token: string, filter: string, statusCodeExpected: number) { + const paths = [ + '/api/v1/video-channels/root_channel/videos', + '/api/v1/accounts/root/videos', + '/api/v1/videos', + '/api/v1/search/videos' + ] + + for (const path of paths) { + await makeGetRequest({ + url: server.url, + path, + token, + query: { + filter + }, + statusCodeExpected + }) + } +} + +describe('Test videos filters', function () { + let server: ServerInfo + let userAccessToken: string + let moderatorAccessToken: string + + // --------------------------------------------------------------- + + before(async function () { + this.timeout(30000) + + await flushTests() + + server = await runServer(1) + + await setAccessTokensToServers([ server ]) + + const user = { username: 'user1', password: 'my super password' } + await createUser(server.url, server.accessToken, user.username, user.password) + userAccessToken = await userLogin(server, user) + + const moderator = { username: 'moderator', password: 'my super password' } + await createUser( + server.url, + server.accessToken, + moderator.username, + moderator.password, + undefined, + undefined, + UserRole.MODERATOR + ) + moderatorAccessToken = await userLogin(server, moderator) + }) + + describe('When setting a video filter', function () { + + it('Should fail with a bad filter', async function () { + await testEndpoints(server, server.accessToken, 'bad-filter', 400) + }) + + it('Should succeed with a good filter', async function () { + await testEndpoints(server, server.accessToken,'local', 200) + }) + + it('Should fail to list all-local with a simple user', async function () { + await testEndpoints(server, userAccessToken, 'all-local', 401) + }) + + it('Should succeed to list all-local with a moderator', async function () { + await testEndpoints(server, moderatorAccessToken, 'all-local', 200) + }) + + it('Should succeed to list all-local with an admin', async function () { + await testEndpoints(server, server.accessToken, 'all-local', 200) + }) + + // Because we cannot authenticate the user on the RSS endpoint + it('Should fail on the feeds endpoint with the all-local filter', async function () { + await makeGetRequest({ + url: server.url, + path: '/feeds/videos.json', + statusCodeExpected: 401, + query: { + filter: 'all-local' + } + }) + }) + + it('Should succed on the feeds endpoint with the local filter', async function () { + await makeGetRequest({ + url: server.url, + path: '/feeds/videos.json', + statusCodeExpected: 200, + query: { + filter: 'local' + } + }) + }) + }) + + after(async function () { + killallServers([ server ]) + + // Keep the logs if the test failed + if (this['ok']) { + await flushTests() + } + }) +}) diff --git a/server/tests/api/videos/index.ts b/server/tests/api/videos/index.ts index 09bb62a8d..9bdb78491 100644 --- a/server/tests/api/videos/index.ts +++ b/server/tests/api/videos/index.ts @@ -14,5 +14,6 @@ import './video-nsfw' import './video-privacy' import './video-schedule-update' import './video-transcoder' +import './videos-filter' import './videos-history' import './videos-overview' diff --git a/server/tests/api/videos/videos-filter.ts b/server/tests/api/videos/videos-filter.ts new file mode 100644 index 000000000..a7588129f --- /dev/null +++ b/server/tests/api/videos/videos-filter.ts @@ -0,0 +1,130 @@ +/* tslint:disable:no-unused-expression */ + +import * as chai from 'chai' +import 'mocha' +import { + createUser, + doubleFollow, + flushAndRunMultipleServers, + flushTests, + killallServers, + makeGetRequest, + ServerInfo, + setAccessTokensToServers, + uploadVideo, + userLogin +} from '../../utils' +import { Video, VideoPrivacy } from '../../../../shared/models/videos' +import { UserRole } from '../../../../shared/models/users' + +const expect = chai.expect + +async function getVideosNames (server: ServerInfo, token: string, filter: string, statusCodeExpected = 200) { + const paths = [ + '/api/v1/video-channels/root_channel/videos', + '/api/v1/accounts/root/videos', + '/api/v1/videos', + '/api/v1/search/videos' + ] + + const videosResults: Video[][] = [] + + for (const path of paths) { + const res = await makeGetRequest({ + url: server.url, + path, + token, + query: { + sort: 'createdAt', + filter + }, + statusCodeExpected + }) + + videosResults.push(res.body.data.map(v => v.name)) + } + + return videosResults +} + +describe('Test videos filter validator', function () { + let servers: ServerInfo[] + + // --------------------------------------------------------------- + + before(async function () { + this.timeout(120000) + + await flushTests() + + servers = await flushAndRunMultipleServers(2) + + await setAccessTokensToServers(servers) + + for (const server of servers) { + const moderator = { username: 'moderator', password: 'my super password' } + await createUser( + server.url, + server.accessToken, + moderator.username, + moderator.password, + undefined, + undefined, + UserRole.MODERATOR + ) + server['moderatorAccessToken'] = await userLogin(server, moderator) + + await uploadVideo(server.url, server.accessToken, { name: 'public ' + server.serverNumber }) + + { + const attributes = { name: 'unlisted ' + server.serverNumber, privacy: VideoPrivacy.UNLISTED } + await uploadVideo(server.url, server.accessToken, attributes) + } + + { + const attributes = { name: 'private ' + server.serverNumber, privacy: VideoPrivacy.PRIVATE } + await uploadVideo(server.url, server.accessToken, attributes) + } + } + + await doubleFollow(servers[0], servers[1]) + }) + + describe('Check videos filter', function () { + + it('Should display local videos', async function () { + for (const server of servers) { + const namesResults = await getVideosNames(server, server.accessToken, 'local') + for (const names of namesResults) { + expect(names).to.have.lengthOf(1) + expect(names[ 0 ]).to.equal('public ' + server.serverNumber) + } + } + }) + + it('Should display all local videos by the admin or the moderator', async function () { + for (const server of servers) { + for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) { + + const namesResults = await getVideosNames(server, token, 'all-local') + for (const names of namesResults) { + expect(names).to.have.lengthOf(3) + + expect(names[ 0 ]).to.equal('public ' + server.serverNumber) + expect(names[ 1 ]).to.equal('unlisted ' + server.serverNumber) + expect(names[ 2 ]).to.equal('private ' + server.serverNumber) + } + } + } + }) + }) + + after(async function () { + killallServers(servers) + + // Keep the logs if the test failed + if (this['ok']) { + await flushTests() + } + }) +}) -- cgit v1.2.3