aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/videos/videos-common-filters.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests/api/videos/videos-common-filters.ts')
-rw-r--r--server/tests/api/videos/videos-common-filters.ts403
1 files changed, 403 insertions, 0 deletions
diff --git a/server/tests/api/videos/videos-common-filters.ts b/server/tests/api/videos/videos-common-filters.ts
new file mode 100644
index 000000000..eb2d2ab50
--- /dev/null
+++ b/server/tests/api/videos/videos-common-filters.ts
@@ -0,0 +1,403 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import 'mocha'
4import { expect } from 'chai'
5import { pick } from '@shared/core-utils'
6import {
7 cleanupTests,
8 createMultipleServers,
9 doubleFollow,
10 makeGetRequest,
11 PeerTubeServer,
12 setAccessTokensToServers,
13 setDefaultVideoChannel,
14 waitJobs
15} from '@shared/extra-utils'
16import { HttpStatusCode, UserRole, Video, VideoInclude, VideoPrivacy } from '@shared/models'
17
18describe('Test videos filter', function () {
19 let servers: PeerTubeServer[]
20 let paths: string[]
21 let remotePaths: string[]
22
23 // ---------------------------------------------------------------
24
25 before(async function () {
26 this.timeout(160000)
27
28 servers = await createMultipleServers(2)
29
30 await setAccessTokensToServers(servers)
31 await setDefaultVideoChannel(servers)
32
33 for (const server of servers) {
34 const moderator = { username: 'moderator', password: 'my super password' }
35 await server.users.create({ username: moderator.username, password: moderator.password, role: UserRole.MODERATOR })
36 server['moderatorAccessToken'] = await server.login.getAccessToken(moderator)
37
38 await server.videos.upload({ attributes: { name: 'public ' + server.serverNumber } })
39
40 {
41 const attributes = { name: 'unlisted ' + server.serverNumber, privacy: VideoPrivacy.UNLISTED }
42 await server.videos.upload({ attributes })
43 }
44
45 {
46 const attributes = { name: 'private ' + server.serverNumber, privacy: VideoPrivacy.PRIVATE }
47 await server.videos.upload({ attributes })
48 }
49 }
50
51 await doubleFollow(servers[0], servers[1])
52
53 paths = [
54 `/api/v1/video-channels/root_channel/videos`,
55 `/api/v1/accounts/root/videos`,
56 '/api/v1/videos',
57 '/api/v1/search/videos'
58 ]
59
60 remotePaths = [
61 `/api/v1/video-channels/root_channel@${servers[1].host}/videos`,
62 `/api/v1/accounts/root@${servers[1].host}/videos`,
63 '/api/v1/videos',
64 '/api/v1/search/videos'
65 ]
66 })
67
68 describe('Check deprecated videos filter', function () {
69
70 async function getVideosNames (server: PeerTubeServer, token: string, filter: string, expectedStatus = HttpStatusCode.OK_200) {
71 const videosResults: Video[][] = []
72
73 for (const path of paths) {
74 const res = await makeGetRequest({
75 url: server.url,
76 path,
77 token,
78 query: {
79 sort: 'createdAt',
80 filter
81 },
82 expectedStatus
83 })
84
85 videosResults.push(res.body.data.map(v => v.name))
86 }
87
88 return videosResults
89 }
90
91 it('Should display local videos', async function () {
92 for (const server of servers) {
93 const namesResults = await getVideosNames(server, server.accessToken, 'local')
94 for (const names of namesResults) {
95 expect(names).to.have.lengthOf(1)
96 expect(names[0]).to.equal('public ' + server.serverNumber)
97 }
98 }
99 })
100
101 it('Should display all local videos by the admin or the moderator', async function () {
102 for (const server of servers) {
103 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
104
105 const namesResults = await getVideosNames(server, token, 'all-local')
106 for (const names of namesResults) {
107 expect(names).to.have.lengthOf(3)
108
109 expect(names[0]).to.equal('public ' + server.serverNumber)
110 expect(names[1]).to.equal('unlisted ' + server.serverNumber)
111 expect(names[2]).to.equal('private ' + server.serverNumber)
112 }
113 }
114 }
115 })
116
117 it('Should display all videos by the admin or the moderator', async function () {
118 for (const server of servers) {
119 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
120
121 const [ channelVideos, accountVideos, videos, searchVideos ] = await getVideosNames(server, token, 'all')
122 expect(channelVideos).to.have.lengthOf(3)
123 expect(accountVideos).to.have.lengthOf(3)
124
125 expect(videos).to.have.lengthOf(5)
126 expect(searchVideos).to.have.lengthOf(5)
127 }
128 }
129 })
130 })
131
132 describe('Check videos filters', function () {
133
134 async function listVideos (options: {
135 server: PeerTubeServer
136 path: string
137 isLocal?: boolean
138 include?: VideoInclude
139 category?: number
140 tagsAllOf?: string[]
141 token?: string
142 expectedStatus?: HttpStatusCode
143 }) {
144 const res = await makeGetRequest({
145 url: options.server.url,
146 path: options.path,
147 token: options.token ?? options.server.accessToken,
148 query: {
149 ...pick(options, [ 'isLocal', 'include', 'category', 'tagsAllOf' ]),
150
151 sort: 'createdAt'
152 },
153 expectedStatus: options.expectedStatus ?? HttpStatusCode.OK_200
154 })
155
156 return res.body.data as Video[]
157 }
158
159 async function getVideosNames (options: {
160 server: PeerTubeServer
161 isLocal?: boolean
162 include?: VideoInclude
163 token?: string
164 expectedStatus?: HttpStatusCode
165 }) {
166 const videosResults: string[][] = []
167
168 for (const path of paths) {
169 const videos = await listVideos({ ...options, path })
170
171 videosResults.push(videos.map(v => v.name))
172 }
173
174 return videosResults
175 }
176
177 it('Should display local videos', async function () {
178 for (const server of servers) {
179 const namesResults = await getVideosNames({ server, isLocal: true })
180
181 for (const names of namesResults) {
182 expect(names).to.have.lengthOf(1)
183 expect(names[0]).to.equal('public ' + server.serverNumber)
184 }
185 }
186 })
187
188 it('Should display local videos with hidden privacy by the admin or the moderator', async function () {
189 for (const server of servers) {
190 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
191
192 const namesResults = await getVideosNames({
193 server,
194 token,
195 isLocal: true,
196 include: VideoInclude.HIDDEN_PRIVACY
197 })
198
199 for (const names of namesResults) {
200 expect(names).to.have.lengthOf(3)
201
202 expect(names[0]).to.equal('public ' + server.serverNumber)
203 expect(names[1]).to.equal('unlisted ' + server.serverNumber)
204 expect(names[2]).to.equal('private ' + server.serverNumber)
205 }
206 }
207 }
208 })
209
210 it('Should display all videos by the admin or the moderator', async function () {
211 for (const server of servers) {
212 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
213
214 const [ channelVideos, accountVideos, videos, searchVideos ] = await getVideosNames({
215 server,
216 token,
217 include: VideoInclude.HIDDEN_PRIVACY
218 })
219
220 expect(channelVideos).to.have.lengthOf(3)
221 expect(accountVideos).to.have.lengthOf(3)
222
223 expect(videos).to.have.lengthOf(5)
224 expect(searchVideos).to.have.lengthOf(5)
225 }
226 }
227 })
228
229 it('Should display only remote videos', async function () {
230 this.timeout(40000)
231
232 await servers[1].videos.upload({ attributes: { name: 'remote video' } })
233
234 await waitJobs(servers)
235
236 const finder = (videos: Video[]) => videos.find(v => v.name === 'remote video')
237
238 for (const path of remotePaths) {
239 {
240 const videos = await listVideos({ server: servers[0], path })
241 const video = finder(videos)
242 expect(video).to.exist
243 }
244
245 {
246 const videos = await listVideos({ server: servers[0], path, isLocal: false })
247 const video = finder(videos)
248 expect(video).to.exist
249 }
250
251 {
252 const videos = await listVideos({ server: servers[0], path, isLocal: true })
253 const video = finder(videos)
254 expect(video).to.not.exist
255 }
256 }
257 })
258
259 it('Should include not published videos', async function () {
260 await servers[0].config.enableLive({ allowReplay: false, transcoding: false })
261 await servers[0].live.create({ fields: { name: 'live video', channelId: servers[0].store.channel.id, privacy: VideoPrivacy.PUBLIC } })
262
263 const finder = (videos: Video[]) => videos.find(v => v.name === 'live video')
264
265 for (const path of paths) {
266 {
267 const videos = await listVideos({ server: servers[0], path })
268 const video = finder(videos)
269 expect(video).to.not.exist
270 expect(videos[0].state).to.not.exist
271 expect(videos[0].waitTranscoding).to.not.exist
272 }
273
274 {
275 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.NOT_PUBLISHED_STATE })
276 const video = finder(videos)
277 expect(video).to.exist
278 expect(video.state).to.exist
279 }
280 }
281 })
282
283 it('Should include blacklisted videos', async function () {
284 const { id } = await servers[0].videos.upload({ attributes: { name: 'blacklisted' } })
285
286 await servers[0].blacklist.add({ videoId: id })
287
288 const finder = (videos: Video[]) => videos.find(v => v.name === 'blacklisted')
289
290 for (const path of paths) {
291 {
292 const videos = await listVideos({ server: servers[0], path })
293 const video = finder(videos)
294 expect(video).to.not.exist
295 expect(videos[0].blacklisted).to.not.exist
296 }
297
298 {
299 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.BLACKLISTED })
300 const video = finder(videos)
301 expect(video).to.exist
302 expect(video.blacklisted).to.be.true
303 }
304 }
305 })
306
307 it('Should include videos from muted account', async function () {
308 const finder = (videos: Video[]) => videos.find(v => v.name === 'remote video')
309
310 await servers[0].blocklist.addToServerBlocklist({ account: 'root@' + servers[1].host })
311
312 for (const path of remotePaths) {
313 {
314 const videos = await listVideos({ server: servers[0], path })
315 const video = finder(videos)
316 expect(video).to.not.exist
317
318 // Some paths won't have videos
319 if (videos[0]) {
320 expect(videos[0].blockedOwner).to.not.exist
321 expect(videos[0].blockedServer).to.not.exist
322 }
323 }
324
325 {
326 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.BLOCKED_OWNER })
327
328 const video = finder(videos)
329 expect(video).to.exist
330 expect(video.blockedServer).to.be.false
331 expect(video.blockedOwner).to.be.true
332 }
333 }
334
335 await servers[0].blocklist.removeFromServerBlocklist({ account: 'root@' + servers[1].host })
336 })
337
338 it('Should include videos from muted server', async function () {
339 const finder = (videos: Video[]) => videos.find(v => v.name === 'remote video')
340
341 await servers[0].blocklist.addToServerBlocklist({ server: servers[1].host })
342
343 for (const path of remotePaths) {
344 {
345 const videos = await listVideos({ server: servers[0], path })
346 const video = finder(videos)
347 expect(video).to.not.exist
348
349 // Some paths won't have videos
350 if (videos[0]) {
351 expect(videos[0].blockedOwner).to.not.exist
352 expect(videos[0].blockedServer).to.not.exist
353 }
354 }
355
356 {
357 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.BLOCKED_OWNER })
358 const video = finder(videos)
359 expect(video).to.exist
360 expect(video.blockedServer).to.be.true
361 expect(video.blockedOwner).to.be.false
362 }
363 }
364
365 await servers[0].blocklist.removeFromServerBlocklist({ server: servers[1].host })
366 })
367
368 it('Should filter by tags and category', async function () {
369 await servers[0].videos.upload({ attributes: { name: 'tag filter', tags: [ 'tag1', 'tag2' ] } })
370 await servers[0].videos.upload({ attributes: { name: 'tag filter with category', tags: [ 'tag3' ], category: 4 } })
371
372 for (const path of paths) {
373 {
374
375 const videos = await listVideos({ server: servers[0], path, tagsAllOf: [ 'tag1', 'tag2' ] })
376 expect(videos).to.have.lengthOf(1)
377 expect(videos[0].name).to.equal('tag filter')
378
379 }
380
381 {
382 const videos = await listVideos({ server: servers[0], path, tagsAllOf: [ 'tag1', 'tag3' ] })
383 expect(videos).to.have.lengthOf(0)
384 }
385
386 {
387 const { data, total } = await servers[0].videos.list({ tagsAllOf: [ 'tag3' ], categoryOneOf: [ 4 ] })
388 expect(total).to.equal(1)
389 expect(data[0].name).to.equal('tag filter with category')
390 }
391
392 {
393 const { total } = await servers[0].videos.list({ tagsAllOf: [ 'tag4' ], categoryOneOf: [ 4 ] })
394 expect(total).to.equal(0)
395 }
396 }
397 })
398 })
399
400 after(async function () {
401 await cleanupTests(servers)
402 })
403})