1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { pick } from '@shared/core-utils'
5 import { HttpStatusCode, UserRole, Video, VideoDetails, VideoInclude, VideoPrivacy } from '@shared/models'
12 setAccessTokensToServers,
13 setDefaultAccountAvatar,
14 setDefaultVideoChannel,
16 } from '@shared/server-commands'
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)
32 await setDefaultAccountAvatar(servers)
34 await servers[1].config.enableMinimumTranscoding()
36 for (const server of servers) {
37 const moderator = { username: 'moderator', password: 'my super password' }
38 await server.users.create({ username: moderator.username, password: moderator.password, role: UserRole.MODERATOR })
39 server['moderatorAccessToken'] = await server.login.getAccessToken(moderator)
41 await server.videos.upload({ attributes: { name: 'public ' + server.serverNumber } })
44 const attributes = { name: 'unlisted ' + server.serverNumber, privacy: VideoPrivacy.UNLISTED }
45 await server.videos.upload({ attributes })
49 const attributes = { name: 'private ' + server.serverNumber, privacy: VideoPrivacy.PRIVATE }
50 await server.videos.upload({ attributes })
54 await doubleFollow(servers[0], servers[1])
57 `/api/v1/video-channels/root_channel/videos`,
58 `/api/v1/accounts/root/videos`,
60 '/api/v1/search/videos'
64 `/api/v1/video-channels/root_channel@${servers[1].host}/videos`,
65 `/api/v1/accounts/root@${servers[1].host}/videos`,
67 '/api/v1/search/videos'
71 describe('Check deprecated videos filter', function () {
73 async function getVideosNames (server: PeerTubeServer, token: string, filter: string, expectedStatus = HttpStatusCode.OK_200) {
74 const videosResults: Video[][] = []
76 for (const path of paths) {
77 const res = await makeGetRequest({
88 videosResults.push(res.body.data.map(v => v.name))
94 it('Should display local videos', async function () {
95 for (const server of servers) {
96 const namesResults = await getVideosNames(server, server.accessToken, 'local')
97 for (const names of namesResults) {
98 expect(names).to.have.lengthOf(1)
99 expect(names[0]).to.equal('public ' + server.serverNumber)
104 it('Should display all local videos by the admin or the moderator', async function () {
105 for (const server of servers) {
106 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
108 const namesResults = await getVideosNames(server, token, 'all-local')
109 for (const names of namesResults) {
110 expect(names).to.have.lengthOf(3)
112 expect(names[0]).to.equal('public ' + server.serverNumber)
113 expect(names[1]).to.equal('unlisted ' + server.serverNumber)
114 expect(names[2]).to.equal('private ' + server.serverNumber)
120 it('Should display all videos by the admin or the moderator', async function () {
121 for (const server of servers) {
122 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
124 const [ channelVideos, accountVideos, videos, searchVideos ] = await getVideosNames(server, token, 'all')
125 expect(channelVideos).to.have.lengthOf(3)
126 expect(accountVideos).to.have.lengthOf(3)
128 expect(videos).to.have.lengthOf(5)
129 expect(searchVideos).to.have.lengthOf(5)
135 describe('Check videos filters', function () {
137 async function listVideos (options: {
138 server: PeerTubeServer
141 hasWebtorrentFiles?: boolean
142 hasHLSFiles?: boolean
143 include?: VideoInclude
144 privacyOneOf?: VideoPrivacy[]
148 expectedStatus?: HttpStatusCode
150 const res = await makeGetRequest({
151 url: options.server.url,
153 token: options.token ?? options.server.accessToken,
155 ...pick(options, [ 'isLocal', 'include', 'category', 'tagsAllOf', 'hasWebtorrentFiles', 'hasHLSFiles', 'privacyOneOf' ]),
159 expectedStatus: options.expectedStatus ?? HttpStatusCode.OK_200
162 return res.body.data as Video[]
165 async function getVideosNames (options: {
166 server: PeerTubeServer
168 include?: VideoInclude
169 privacyOneOf?: VideoPrivacy[]
171 expectedStatus?: HttpStatusCode
173 const videosResults: string[][] = []
175 for (const path of paths) {
176 const videos = await listVideos({ ...options, path })
178 videosResults.push(videos.map(v => v.name))
184 it('Should display local videos', async function () {
185 for (const server of servers) {
186 const namesResults = await getVideosNames({ server, isLocal: true })
188 for (const names of namesResults) {
189 expect(names).to.have.lengthOf(1)
190 expect(names[0]).to.equal('public ' + server.serverNumber)
195 it('Should display local videos with hidden privacy by the admin or the moderator', async function () {
196 for (const server of servers) {
197 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
199 const namesResults = await getVideosNames({
203 privacyOneOf: [ VideoPrivacy.UNLISTED, VideoPrivacy.PUBLIC, VideoPrivacy.PRIVATE ]
206 for (const names of namesResults) {
207 expect(names).to.have.lengthOf(3)
209 expect(names[0]).to.equal('public ' + server.serverNumber)
210 expect(names[1]).to.equal('unlisted ' + server.serverNumber)
211 expect(names[2]).to.equal('private ' + server.serverNumber)
217 it('Should display all videos by the admin or the moderator', async function () {
218 for (const server of servers) {
219 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
221 const [ channelVideos, accountVideos, videos, searchVideos ] = await getVideosNames({
224 privacyOneOf: [ VideoPrivacy.UNLISTED, VideoPrivacy.PUBLIC, VideoPrivacy.PRIVATE ]
227 expect(channelVideos).to.have.lengthOf(3)
228 expect(accountVideos).to.have.lengthOf(3)
230 expect(videos).to.have.lengthOf(5)
231 expect(searchVideos).to.have.lengthOf(5)
236 it('Should display only remote videos', async function () {
239 await servers[1].videos.upload({ attributes: { name: 'remote video' } })
241 await waitJobs(servers)
243 const finder = (videos: Video[]) => videos.find(v => v.name === 'remote video')
245 for (const path of remotePaths) {
247 const videos = await listVideos({ server: servers[0], path })
248 const video = finder(videos)
249 expect(video).to.exist
253 const videos = await listVideos({ server: servers[0], path, isLocal: false })
254 const video = finder(videos)
255 expect(video).to.exist
259 const videos = await listVideos({ server: servers[0], path, isLocal: true })
260 const video = finder(videos)
261 expect(video).to.not.exist
266 it('Should include not published videos', async function () {
267 await servers[0].config.enableLive({ allowReplay: false, transcoding: false })
268 await servers[0].live.create({ fields: { name: 'live video', channelId: servers[0].store.channel.id, privacy: VideoPrivacy.PUBLIC } })
270 const finder = (videos: Video[]) => videos.find(v => v.name === 'live video')
272 for (const path of paths) {
274 const videos = await listVideos({ server: servers[0], path })
275 const video = finder(videos)
276 expect(video).to.not.exist
277 expect(videos[0].state).to.not.exist
278 expect(videos[0].waitTranscoding).to.not.exist
282 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.NOT_PUBLISHED_STATE })
283 const video = finder(videos)
284 expect(video).to.exist
285 expect(video.state).to.exist
290 it('Should include blacklisted videos', async function () {
291 const { id } = await servers[0].videos.upload({ attributes: { name: 'blacklisted' } })
293 await servers[0].blacklist.add({ videoId: id })
295 const finder = (videos: Video[]) => videos.find(v => v.name === 'blacklisted')
297 for (const path of paths) {
299 const videos = await listVideos({ server: servers[0], path })
300 const video = finder(videos)
301 expect(video).to.not.exist
302 expect(videos[0].blacklisted).to.not.exist
306 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.BLACKLISTED })
307 const video = finder(videos)
308 expect(video).to.exist
309 expect(video.blacklisted).to.be.true
314 it('Should include videos from muted account', async function () {
315 const finder = (videos: Video[]) => videos.find(v => v.name === 'remote video')
317 await servers[0].blocklist.addToServerBlocklist({ account: 'root@' + servers[1].host })
319 for (const path of remotePaths) {
321 const videos = await listVideos({ server: servers[0], path })
322 const video = finder(videos)
323 expect(video).to.not.exist
325 // Some paths won't have videos
327 expect(videos[0].blockedOwner).to.not.exist
328 expect(videos[0].blockedServer).to.not.exist
333 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.BLOCKED_OWNER })
335 const video = finder(videos)
336 expect(video).to.exist
337 expect(video.blockedServer).to.be.false
338 expect(video.blockedOwner).to.be.true
342 await servers[0].blocklist.removeFromServerBlocklist({ account: 'root@' + servers[1].host })
345 it('Should include videos from muted server', async function () {
346 const finder = (videos: Video[]) => videos.find(v => v.name === 'remote video')
348 await servers[0].blocklist.addToServerBlocklist({ server: servers[1].host })
350 for (const path of remotePaths) {
352 const videos = await listVideos({ server: servers[0], path })
353 const video = finder(videos)
354 expect(video).to.not.exist
356 // Some paths won't have videos
358 expect(videos[0].blockedOwner).to.not.exist
359 expect(videos[0].blockedServer).to.not.exist
364 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.BLOCKED_OWNER })
365 const video = finder(videos)
366 expect(video).to.exist
367 expect(video.blockedServer).to.be.true
368 expect(video.blockedOwner).to.be.false
372 await servers[0].blocklist.removeFromServerBlocklist({ server: servers[1].host })
375 it('Should include video files', async function () {
376 for (const path of paths) {
378 const videos = await listVideos({ server: servers[0], path })
380 for (const video of videos) {
381 const videoWithFiles = video as VideoDetails
383 expect(videoWithFiles.files).to.not.exist
384 expect(videoWithFiles.streamingPlaylists).to.not.exist
389 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.FILES })
391 for (const video of videos) {
392 const videoWithFiles = video as VideoDetails
394 expect(videoWithFiles.files).to.exist
395 expect(videoWithFiles.files).to.have.length.at.least(1)
401 it('Should filter by tags and category', async function () {
402 await servers[0].videos.upload({ attributes: { name: 'tag filter', tags: [ 'tag1', 'tag2' ] } })
403 await servers[0].videos.upload({ attributes: { name: 'tag filter with category', tags: [ 'tag3' ], category: 4 } })
405 for (const path of paths) {
407 const videos = await listVideos({ server: servers[0], path, tagsAllOf: [ 'tag1', 'tag2' ] })
408 expect(videos).to.have.lengthOf(1)
409 expect(videos[0].name).to.equal('tag filter')
413 const videos = await listVideos({ server: servers[0], path, tagsAllOf: [ 'tag1', 'tag3' ] })
414 expect(videos).to.have.lengthOf(0)
418 const { data, total } = await servers[0].videos.list({ tagsAllOf: [ 'tag3' ], categoryOneOf: [ 4 ] })
419 expect(total).to.equal(1)
420 expect(data[0].name).to.equal('tag filter with category')
424 const { total } = await servers[0].videos.list({ tagsAllOf: [ 'tag4' ], categoryOneOf: [ 4 ] })
425 expect(total).to.equal(0)
430 it('Should filter by HLS or WebTorrent files', async function () {
433 const finderFactory = (name: string) => (videos: Video[]) => videos.some(v => v.name === name)
435 await servers[0].config.enableTranscoding(true, false)
436 await servers[0].videos.upload({ attributes: { name: 'webtorrent video' } })
437 const hasWebtorrent = finderFactory('webtorrent video')
439 await waitJobs(servers)
441 await servers[0].config.enableTranscoding(false, true)
442 await servers[0].videos.upload({ attributes: { name: 'hls video' } })
443 const hasHLS = finderFactory('hls video')
445 await waitJobs(servers)
447 await servers[0].config.enableTranscoding(true, true)
448 await servers[0].videos.upload({ attributes: { name: 'hls and webtorrent video' } })
449 const hasBoth = finderFactory('hls and webtorrent video')
451 await waitJobs(servers)
453 for (const path of paths) {
455 const videos = await listVideos({ server: servers[0], path, hasWebtorrentFiles: true })
457 expect(hasWebtorrent(videos)).to.be.true
458 expect(hasHLS(videos)).to.be.false
459 expect(hasBoth(videos)).to.be.true
463 const videos = await listVideos({ server: servers[0], path, hasWebtorrentFiles: false })
465 expect(hasWebtorrent(videos)).to.be.false
466 expect(hasHLS(videos)).to.be.true
467 expect(hasBoth(videos)).to.be.false
471 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: true })
473 expect(hasWebtorrent(videos)).to.be.false
474 expect(hasHLS(videos)).to.be.true
475 expect(hasBoth(videos)).to.be.true
479 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: false })
481 expect(hasWebtorrent(videos)).to.be.true
482 expect(hasHLS(videos)).to.be.false
483 expect(hasBoth(videos)).to.be.false
487 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: false, hasWebtorrentFiles: false })
489 expect(hasWebtorrent(videos)).to.be.false
490 expect(hasHLS(videos)).to.be.false
491 expect(hasBoth(videos)).to.be.false
495 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: true, hasWebtorrentFiles: true })
497 expect(hasWebtorrent(videos)).to.be.false
498 expect(hasHLS(videos)).to.be.false
499 expect(hasBoth(videos)).to.be.true
505 after(async function () {
506 await cleanupTests(servers)