]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/videos-filter.ts
Shorter server command names
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / videos-filter.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import {
5 cleanupTests,
6 flushAndRunServer,
7 makeGetRequest,
8 ServerInfo,
9 setAccessTokensToServers,
10 setDefaultVideoChannel
11 } from '../../../../shared/extra-utils'
12 import { UserRole } from '../../../../shared/models/users'
13 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
14
15 async function testEndpoints (server: ServerInfo, token: string, filter: string, statusCodeExpected: HttpStatusCode) {
16 const paths = [
17 '/api/v1/video-channels/root_channel/videos',
18 '/api/v1/accounts/root/videos',
19 '/api/v1/videos',
20 '/api/v1/search/videos'
21 ]
22
23 for (const path of paths) {
24 await makeGetRequest({
25 url: server.url,
26 path,
27 token,
28 query: {
29 filter
30 },
31 statusCodeExpected
32 })
33 }
34 }
35
36 describe('Test video filters validators', function () {
37 let server: ServerInfo
38 let userAccessToken: string
39 let moderatorAccessToken: string
40
41 // ---------------------------------------------------------------
42
43 before(async function () {
44 this.timeout(30000)
45
46 server = await flushAndRunServer(1)
47
48 await setAccessTokensToServers([ server ])
49 await setDefaultVideoChannel([ server ])
50
51 const user = { username: 'user1', password: 'my super password' }
52 await server.users.create({ username: user.username, password: user.password })
53 userAccessToken = await server.login.getAccessToken(user)
54
55 const moderator = { username: 'moderator', password: 'my super password' }
56 await server.users.create({ username: moderator.username, password: moderator.password, role: UserRole.MODERATOR })
57
58 moderatorAccessToken = await server.login.getAccessToken(moderator)
59 })
60
61 describe('When setting a video filter', function () {
62
63 it('Should fail with a bad filter', async function () {
64 await testEndpoints(server, server.accessToken, 'bad-filter', HttpStatusCode.BAD_REQUEST_400)
65 })
66
67 it('Should succeed with a good filter', async function () {
68 await testEndpoints(server, server.accessToken, 'local', HttpStatusCode.OK_200)
69 })
70
71 it('Should fail to list all-local/all with a simple user', async function () {
72 await testEndpoints(server, userAccessToken, 'all-local', HttpStatusCode.UNAUTHORIZED_401)
73 await testEndpoints(server, userAccessToken, 'all', HttpStatusCode.UNAUTHORIZED_401)
74 })
75
76 it('Should succeed to list all-local/all with a moderator', async function () {
77 await testEndpoints(server, moderatorAccessToken, 'all-local', HttpStatusCode.OK_200)
78 await testEndpoints(server, moderatorAccessToken, 'all', HttpStatusCode.OK_200)
79 })
80
81 it('Should succeed to list all-local/all with an admin', async function () {
82 await testEndpoints(server, server.accessToken, 'all-local', HttpStatusCode.OK_200)
83 await testEndpoints(server, server.accessToken, 'all', HttpStatusCode.OK_200)
84 })
85
86 // Because we cannot authenticate the user on the RSS endpoint
87 it('Should fail on the feeds endpoint with the all-local/all filter', async function () {
88 for (const filter of [ 'all', 'all-local' ]) {
89 await makeGetRequest({
90 url: server.url,
91 path: '/feeds/videos.json',
92 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401,
93 query: {
94 filter
95 }
96 })
97 }
98 })
99
100 it('Should succeed on the feeds endpoint with the local filter', async function () {
101 await makeGetRequest({
102 url: server.url,
103 path: '/feeds/videos.json',
104 statusCodeExpected: HttpStatusCode.OK_200,
105 query: {
106 filter: 'local'
107 }
108 })
109 })
110 })
111
112 after(async function () {
113 await cleanupTests([ server ])
114 })
115 })