]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/videos-common-filters.ts
6b3ec917ecdee6e761b0f86633f46b662f9a6bee
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / videos-common-filters.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import {
5 cleanupTests,
6 createSingleServer,
7 makeGetRequest,
8 PeerTubeServer,
9 setAccessTokensToServers,
10 setDefaultVideoChannel
11 } from '@shared/server-commands'
12 import { HttpStatusCode, UserRole, VideoInclude, VideoPrivacy } from '@shared/models'
13
14 describe('Test video filters validators', function () {
15 let server: PeerTubeServer
16 let userAccessToken: string
17 let moderatorAccessToken: string
18
19 // ---------------------------------------------------------------
20
21 before(async function () {
22 this.timeout(30000)
23
24 server = await createSingleServer(1)
25
26 await setAccessTokensToServers([ server ])
27 await setDefaultVideoChannel([ server ])
28
29 const user = { username: 'user1', password: 'my super password' }
30 await server.users.create({ username: user.username, password: user.password })
31 userAccessToken = await server.login.getAccessToken(user)
32
33 const moderator = { username: 'moderator', password: 'my super password' }
34 await server.users.create({ username: moderator.username, password: moderator.password, role: UserRole.MODERATOR })
35
36 moderatorAccessToken = await server.login.getAccessToken(moderator)
37 })
38
39 describe('When setting a deprecated video filter', function () {
40
41 async function testEndpoints (token: string, filter: string, expectedStatus: HttpStatusCode) {
42 const paths = [
43 '/api/v1/video-channels/root_channel/videos',
44 '/api/v1/accounts/root/videos',
45 '/api/v1/videos',
46 '/api/v1/search/videos'
47 ]
48
49 for (const path of paths) {
50 await makeGetRequest({
51 url: server.url,
52 path,
53 token,
54 query: {
55 filter
56 },
57 expectedStatus
58 })
59 }
60 }
61
62 it('Should fail with a bad filter', async function () {
63 await testEndpoints(server.accessToken, 'bad-filter', HttpStatusCode.BAD_REQUEST_400)
64 })
65
66 it('Should succeed with a good filter', async function () {
67 await testEndpoints(server.accessToken, 'local', HttpStatusCode.OK_200)
68 })
69
70 it('Should fail to list all-local/all with a simple user', async function () {
71 await testEndpoints(userAccessToken, 'all-local', HttpStatusCode.UNAUTHORIZED_401)
72 await testEndpoints(userAccessToken, 'all', HttpStatusCode.UNAUTHORIZED_401)
73 })
74
75 it('Should succeed to list all-local/all with a moderator', async function () {
76 await testEndpoints(moderatorAccessToken, 'all-local', HttpStatusCode.OK_200)
77 await testEndpoints(moderatorAccessToken, 'all', HttpStatusCode.OK_200)
78 })
79
80 it('Should succeed to list all-local/all with an admin', async function () {
81 await testEndpoints(server.accessToken, 'all-local', HttpStatusCode.OK_200)
82 await testEndpoints(server.accessToken, 'all', HttpStatusCode.OK_200)
83 })
84
85 // Because we cannot authenticate the user on the RSS endpoint
86 it('Should fail on the feeds endpoint with the all-local/all filter', async function () {
87 for (const filter of [ 'all', 'all-local' ]) {
88 await makeGetRequest({
89 url: server.url,
90 path: '/feeds/videos.json',
91 expectedStatus: HttpStatusCode.UNAUTHORIZED_401,
92 query: {
93 filter
94 }
95 })
96 }
97 })
98
99 it('Should succeed on the feeds endpoint with the local filter', async function () {
100 await makeGetRequest({
101 url: server.url,
102 path: '/feeds/videos.json',
103 expectedStatus: HttpStatusCode.OK_200,
104 query: {
105 filter: 'local'
106 }
107 })
108 })
109 })
110
111 describe('When setting video filters', function () {
112
113 const validIncludes = [
114 VideoInclude.NONE,
115 VideoInclude.BLOCKED_OWNER,
116 VideoInclude.NOT_PUBLISHED_STATE | VideoInclude.BLACKLISTED
117 ]
118
119 async function testEndpoints (options: {
120 token?: string
121 isLocal?: boolean
122 include?: VideoInclude
123 privacyOneOf?: VideoPrivacy[]
124 expectedStatus: HttpStatusCode
125 }) {
126 const paths = [
127 '/api/v1/video-channels/root_channel/videos',
128 '/api/v1/accounts/root/videos',
129 '/api/v1/videos',
130 '/api/v1/search/videos'
131 ]
132
133 for (const path of paths) {
134 await makeGetRequest({
135 url: server.url,
136 path,
137 token: options.token || server.accessToken,
138 query: {
139 isLocal: options.isLocal,
140 privacyOneOf: options.privacyOneOf,
141 include: options.include
142 },
143 expectedStatus: options.expectedStatus
144 })
145 }
146 }
147
148 it('Should fail with a bad privacyOneOf', async function () {
149 await testEndpoints({ privacyOneOf: [ 'toto' ] as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
150 })
151
152 it('Should succeed with a good privacyOneOf', async function () {
153 await testEndpoints({ privacyOneOf: [ VideoPrivacy.INTERNAL ], expectedStatus: HttpStatusCode.OK_200 })
154 })
155
156 it('Should fail to use privacyOneOf with a simple user', async function () {
157 await testEndpoints({
158 privacyOneOf: [ VideoPrivacy.INTERNAL ],
159 token: userAccessToken,
160 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
161 })
162 })
163
164 it('Should fail with a bad include', async function () {
165 await testEndpoints({ include: 'toto' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
166 })
167
168 it('Should succeed with a good include', async function () {
169 for (const include of validIncludes) {
170 await testEndpoints({ include, expectedStatus: HttpStatusCode.OK_200 })
171 }
172 })
173
174 it('Should fail to include more videos with a simple user', async function () {
175 for (const include of validIncludes) {
176 await testEndpoints({ token: userAccessToken, include, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
177 }
178 })
179
180 it('Should succeed to list all local/all with a moderator', async function () {
181 for (const include of validIncludes) {
182 await testEndpoints({ token: moderatorAccessToken, include, expectedStatus: HttpStatusCode.OK_200 })
183 }
184 })
185
186 it('Should succeed to list all local/all with an admin', async function () {
187 for (const include of validIncludes) {
188 await testEndpoints({ token: server.accessToken, include, expectedStatus: HttpStatusCode.OK_200 })
189 }
190 })
191
192 // Because we cannot authenticate the user on the RSS endpoint
193 it('Should fail on the feeds endpoint with the all filter', async function () {
194 for (const include of [ VideoInclude.NOT_PUBLISHED_STATE ]) {
195 await makeGetRequest({
196 url: server.url,
197 path: '/feeds/videos.json',
198 expectedStatus: HttpStatusCode.UNAUTHORIZED_401,
199 query: {
200 include
201 }
202 })
203 }
204 })
205
206 it('Should succeed on the feeds endpoint with the local filter', async function () {
207 await makeGetRequest({
208 url: server.url,
209 path: '/feeds/videos.json',
210 expectedStatus: HttpStatusCode.OK_200,
211 query: {
212 isLocal: true
213 }
214 })
215 })
216 })
217
218 after(async function () {
219 await cleanupTests([ server ])
220 })
221 })