1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { expect } from 'chai'
5 import { pick } from '@shared/core-utils'
12 setAccessTokensToServers,
13 setDefaultVideoChannel,
15 } from '@shared/server-commands'
16 import { HttpStatusCode, UserRole, Video, VideoDetails, VideoInclude, VideoPrivacy } from '@shared/models'
18 describe('Test videos filter', function () {
19 let servers: PeerTubeServer[]
21 let remotePaths: string[]
23 // ---------------------------------------------------------------
25 before(async function () {
28 servers = await createMultipleServers(2)
30 await setAccessTokensToServers(servers)
31 await setDefaultVideoChannel(servers)
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)
38 await server.videos.upload({ attributes: { name: 'public ' + server.serverNumber } })
41 const attributes = { name: 'unlisted ' + server.serverNumber, privacy: VideoPrivacy.UNLISTED }
42 await server.videos.upload({ attributes })
46 const attributes = { name: 'private ' + server.serverNumber, privacy: VideoPrivacy.PRIVATE }
47 await server.videos.upload({ attributes })
51 await doubleFollow(servers[0], servers[1])
54 `/api/v1/video-channels/root_channel/videos`,
55 `/api/v1/accounts/root/videos`,
57 '/api/v1/search/videos'
61 `/api/v1/video-channels/root_channel@${servers[1].host}/videos`,
62 `/api/v1/accounts/root@${servers[1].host}/videos`,
64 '/api/v1/search/videos'
68 describe('Check deprecated videos filter', function () {
70 async function getVideosNames (server: PeerTubeServer, token: string, filter: string, expectedStatus = HttpStatusCode.OK_200) {
71 const videosResults: Video[][] = []
73 for (const path of paths) {
74 const res = await makeGetRequest({
85 videosResults.push(res.body.data.map(v => v.name))
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)
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'] ]) {
105 const namesResults = await getVideosNames(server, token, 'all-local')
106 for (const names of namesResults) {
107 expect(names).to.have.lengthOf(3)
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)
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'] ]) {
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)
125 expect(videos).to.have.lengthOf(5)
126 expect(searchVideos).to.have.lengthOf(5)
132 describe('Check videos filters', function () {
134 async function listVideos (options: {
135 server: PeerTubeServer
138 hasWebtorrentFiles?: boolean
139 hasHLSFiles?: boolean
140 include?: VideoInclude
141 privacyOneOf?: VideoPrivacy[]
145 expectedStatus?: HttpStatusCode
147 const res = await makeGetRequest({
148 url: options.server.url,
150 token: options.token ?? options.server.accessToken,
152 ...pick(options, [ 'isLocal', 'include', 'category', 'tagsAllOf', 'hasWebtorrentFiles', 'hasHLSFiles', 'privacyOneOf' ]),
156 expectedStatus: options.expectedStatus ?? HttpStatusCode.OK_200
159 return res.body.data as Video[]
162 async function getVideosNames (options: {
163 server: PeerTubeServer
165 include?: VideoInclude
166 privacyOneOf?: VideoPrivacy[]
168 expectedStatus?: HttpStatusCode
170 const videosResults: string[][] = []
172 for (const path of paths) {
173 const videos = await listVideos({ ...options, path })
175 videosResults.push(videos.map(v => v.name))
181 it('Should display local videos', async function () {
182 for (const server of servers) {
183 const namesResults = await getVideosNames({ server, isLocal: true })
185 for (const names of namesResults) {
186 expect(names).to.have.lengthOf(1)
187 expect(names[0]).to.equal('public ' + server.serverNumber)
192 it('Should display local videos with hidden privacy by the admin or the moderator', async function () {
193 for (const server of servers) {
194 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
196 const namesResults = await getVideosNames({
200 privacyOneOf: [ VideoPrivacy.UNLISTED, VideoPrivacy.PUBLIC, VideoPrivacy.PRIVATE ]
203 for (const names of namesResults) {
204 expect(names).to.have.lengthOf(3)
206 expect(names[0]).to.equal('public ' + server.serverNumber)
207 expect(names[1]).to.equal('unlisted ' + server.serverNumber)
208 expect(names[2]).to.equal('private ' + server.serverNumber)
214 it('Should display all videos by the admin or the moderator', async function () {
215 for (const server of servers) {
216 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
218 const [ channelVideos, accountVideos, videos, searchVideos ] = await getVideosNames({
221 privacyOneOf: [ VideoPrivacy.UNLISTED, VideoPrivacy.PUBLIC, VideoPrivacy.PRIVATE ]
224 expect(channelVideos).to.have.lengthOf(3)
225 expect(accountVideos).to.have.lengthOf(3)
227 expect(videos).to.have.lengthOf(5)
228 expect(searchVideos).to.have.lengthOf(5)
233 it('Should display only remote videos', async function () {
236 await servers[1].videos.upload({ attributes: { name: 'remote video' } })
238 await waitJobs(servers)
240 const finder = (videos: Video[]) => videos.find(v => v.name === 'remote video')
242 for (const path of remotePaths) {
244 const videos = await listVideos({ server: servers[0], path })
245 const video = finder(videos)
246 expect(video).to.exist
250 const videos = await listVideos({ server: servers[0], path, isLocal: false })
251 const video = finder(videos)
252 expect(video).to.exist
256 const videos = await listVideos({ server: servers[0], path, isLocal: true })
257 const video = finder(videos)
258 expect(video).to.not.exist
263 it('Should include not published videos', async function () {
264 await servers[0].config.enableLive({ allowReplay: false, transcoding: false })
265 await servers[0].live.create({ fields: { name: 'live video', channelId: servers[0].store.channel.id, privacy: VideoPrivacy.PUBLIC } })
267 const finder = (videos: Video[]) => videos.find(v => v.name === 'live video')
269 for (const path of paths) {
271 const videos = await listVideos({ server: servers[0], path })
272 const video = finder(videos)
273 expect(video).to.not.exist
274 expect(videos[0].state).to.not.exist
275 expect(videos[0].waitTranscoding).to.not.exist
279 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.NOT_PUBLISHED_STATE })
280 const video = finder(videos)
281 expect(video).to.exist
282 expect(video.state).to.exist
287 it('Should include blacklisted videos', async function () {
288 const { id } = await servers[0].videos.upload({ attributes: { name: 'blacklisted' } })
290 await servers[0].blacklist.add({ videoId: id })
292 const finder = (videos: Video[]) => videos.find(v => v.name === 'blacklisted')
294 for (const path of paths) {
296 const videos = await listVideos({ server: servers[0], path })
297 const video = finder(videos)
298 expect(video).to.not.exist
299 expect(videos[0].blacklisted).to.not.exist
303 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.BLACKLISTED })
304 const video = finder(videos)
305 expect(video).to.exist
306 expect(video.blacklisted).to.be.true
311 it('Should include videos from muted account', async function () {
312 const finder = (videos: Video[]) => videos.find(v => v.name === 'remote video')
314 await servers[0].blocklist.addToServerBlocklist({ account: 'root@' + servers[1].host })
316 for (const path of remotePaths) {
318 const videos = await listVideos({ server: servers[0], path })
319 const video = finder(videos)
320 expect(video).to.not.exist
322 // Some paths won't have videos
324 expect(videos[0].blockedOwner).to.not.exist
325 expect(videos[0].blockedServer).to.not.exist
330 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.BLOCKED_OWNER })
332 const video = finder(videos)
333 expect(video).to.exist
334 expect(video.blockedServer).to.be.false
335 expect(video.blockedOwner).to.be.true
339 await servers[0].blocklist.removeFromServerBlocklist({ account: 'root@' + servers[1].host })
342 it('Should include videos from muted server', async function () {
343 const finder = (videos: Video[]) => videos.find(v => v.name === 'remote video')
345 await servers[0].blocklist.addToServerBlocklist({ server: servers[1].host })
347 for (const path of remotePaths) {
349 const videos = await listVideos({ server: servers[0], path })
350 const video = finder(videos)
351 expect(video).to.not.exist
353 // Some paths won't have videos
355 expect(videos[0].blockedOwner).to.not.exist
356 expect(videos[0].blockedServer).to.not.exist
361 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.BLOCKED_OWNER })
362 const video = finder(videos)
363 expect(video).to.exist
364 expect(video.blockedServer).to.be.true
365 expect(video.blockedOwner).to.be.false
369 await servers[0].blocklist.removeFromServerBlocklist({ server: servers[1].host })
372 it('Should include video files', async function () {
373 for (const path of paths) {
375 const videos = await listVideos({ server: servers[0], path })
377 for (const video of videos) {
378 const videoWithFiles = video as VideoDetails
380 expect(videoWithFiles.files).to.not.exist
381 expect(videoWithFiles.streamingPlaylists).to.not.exist
386 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.FILES })
388 for (const video of videos) {
389 const videoWithFiles = video as VideoDetails
391 expect(videoWithFiles.files).to.exist
392 expect(videoWithFiles.files).to.have.length.at.least(1)
398 it('Should filter by tags and category', async function () {
399 await servers[0].videos.upload({ attributes: { name: 'tag filter', tags: [ 'tag1', 'tag2' ] } })
400 await servers[0].videos.upload({ attributes: { name: 'tag filter with category', tags: [ 'tag3' ], category: 4 } })
402 for (const path of paths) {
404 const videos = await listVideos({ server: servers[0], path, tagsAllOf: [ 'tag1', 'tag2' ] })
405 expect(videos).to.have.lengthOf(1)
406 expect(videos[0].name).to.equal('tag filter')
410 const videos = await listVideos({ server: servers[0], path, tagsAllOf: [ 'tag1', 'tag3' ] })
411 expect(videos).to.have.lengthOf(0)
415 const { data, total } = await servers[0].videos.list({ tagsAllOf: [ 'tag3' ], categoryOneOf: [ 4 ] })
416 expect(total).to.equal(1)
417 expect(data[0].name).to.equal('tag filter with category')
421 const { total } = await servers[0].videos.list({ tagsAllOf: [ 'tag4' ], categoryOneOf: [ 4 ] })
422 expect(total).to.equal(0)
427 it('Should filter by HLS or WebTorrent files', async function () {
430 const finderFactory = (name: string) => (videos: Video[]) => videos.some(v => v.name === name)
432 await servers[0].config.enableTranscoding(true, false)
433 await servers[0].videos.upload({ attributes: { name: 'webtorrent video' } })
434 const hasWebtorrent = finderFactory('webtorrent video')
436 await waitJobs(servers)
438 await servers[0].config.enableTranscoding(false, true)
439 await servers[0].videos.upload({ attributes: { name: 'hls video' } })
440 const hasHLS = finderFactory('hls video')
442 await waitJobs(servers)
444 await servers[0].config.enableTranscoding(true, true)
445 await servers[0].videos.upload({ attributes: { name: 'hls and webtorrent video' } })
446 const hasBoth = finderFactory('hls and webtorrent video')
448 await waitJobs(servers)
450 for (const path of paths) {
452 const videos = await listVideos({ server: servers[0], path, hasWebtorrentFiles: true })
454 expect(hasWebtorrent(videos)).to.be.true
455 expect(hasHLS(videos)).to.be.false
456 expect(hasBoth(videos)).to.be.true
460 const videos = await listVideos({ server: servers[0], path, hasWebtorrentFiles: false })
462 expect(hasWebtorrent(videos)).to.be.false
463 expect(hasHLS(videos)).to.be.true
464 expect(hasBoth(videos)).to.be.false
468 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: true })
470 expect(hasWebtorrent(videos)).to.be.false
471 expect(hasHLS(videos)).to.be.true
472 expect(hasBoth(videos)).to.be.true
476 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: false })
478 expect(hasWebtorrent(videos)).to.be.true
479 expect(hasHLS(videos)).to.be.false
480 expect(hasBoth(videos)).to.be.false
484 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: false, hasWebtorrentFiles: false })
486 expect(hasWebtorrent(videos)).to.be.false
487 expect(hasHLS(videos)).to.be.false
488 expect(hasBoth(videos)).to.be.false
492 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: true, hasWebtorrentFiles: true })
494 expect(hasWebtorrent(videos)).to.be.false
495 expect(hasHLS(videos)).to.be.false
496 expect(hasBoth(videos)).to.be.true
502 after(async function () {
503 await cleanupTests(servers)