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