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