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/extra-utils'
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
144 expectedStatus?: HttpStatusCode
146 const res = await makeGetRequest({
147 url: options.server.url,
149 token: options.token ?? options.server.accessToken,
151 ...pick(options, [ 'isLocal', 'include', 'category', 'tagsAllOf', 'hasWebtorrentFiles', 'hasHLSFiles' ]),
155 expectedStatus: options.expectedStatus ?? HttpStatusCode.OK_200
158 return res.body.data as Video[]
161 async function getVideosNames (options: {
162 server: PeerTubeServer
164 include?: VideoInclude
166 expectedStatus?: HttpStatusCode
168 const videosResults: string[][] = []
170 for (const path of paths) {
171 const videos = await listVideos({ ...options, path })
173 videosResults.push(videos.map(v => v.name))
179 it('Should display local videos', async function () {
180 for (const server of servers) {
181 const namesResults = await getVideosNames({ server, isLocal: true })
183 for (const names of namesResults) {
184 expect(names).to.have.lengthOf(1)
185 expect(names[0]).to.equal('public ' + server.serverNumber)
190 it('Should display local videos with hidden privacy by the admin or the moderator', async function () {
191 for (const server of servers) {
192 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
194 const namesResults = await getVideosNames({
198 include: VideoInclude.HIDDEN_PRIVACY
201 for (const names of namesResults) {
202 expect(names).to.have.lengthOf(3)
204 expect(names[0]).to.equal('public ' + server.serverNumber)
205 expect(names[1]).to.equal('unlisted ' + server.serverNumber)
206 expect(names[2]).to.equal('private ' + server.serverNumber)
212 it('Should display all videos by the admin or the moderator', async function () {
213 for (const server of servers) {
214 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
216 const [ channelVideos, accountVideos, videos, searchVideos ] = await getVideosNames({
219 include: VideoInclude.HIDDEN_PRIVACY
222 expect(channelVideos).to.have.lengthOf(3)
223 expect(accountVideos).to.have.lengthOf(3)
225 expect(videos).to.have.lengthOf(5)
226 expect(searchVideos).to.have.lengthOf(5)
231 it('Should display only remote videos', async function () {
234 await servers[1].videos.upload({ attributes: { name: 'remote video' } })
236 await waitJobs(servers)
238 const finder = (videos: Video[]) => videos.find(v => v.name === 'remote video')
240 for (const path of remotePaths) {
242 const videos = await listVideos({ server: servers[0], path })
243 const video = finder(videos)
244 expect(video).to.exist
248 const videos = await listVideos({ server: servers[0], path, isLocal: false })
249 const video = finder(videos)
250 expect(video).to.exist
254 const videos = await listVideos({ server: servers[0], path, isLocal: true })
255 const video = finder(videos)
256 expect(video).to.not.exist
261 it('Should include not published videos', async function () {
262 await servers[0].config.enableLive({ allowReplay: false, transcoding: false })
263 await servers[0].live.create({ fields: { name: 'live video', channelId: servers[0].store.channel.id, privacy: VideoPrivacy.PUBLIC } })
265 const finder = (videos: Video[]) => videos.find(v => v.name === 'live video')
267 for (const path of paths) {
269 const videos = await listVideos({ server: servers[0], path })
270 const video = finder(videos)
271 expect(video).to.not.exist
272 expect(videos[0].state).to.not.exist
273 expect(videos[0].waitTranscoding).to.not.exist
277 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.NOT_PUBLISHED_STATE })
278 const video = finder(videos)
279 expect(video).to.exist
280 expect(video.state).to.exist
285 it('Should include blacklisted videos', async function () {
286 const { id } = await servers[0].videos.upload({ attributes: { name: 'blacklisted' } })
288 await servers[0].blacklist.add({ videoId: id })
290 const finder = (videos: Video[]) => videos.find(v => v.name === 'blacklisted')
292 for (const path of paths) {
294 const videos = await listVideos({ server: servers[0], path })
295 const video = finder(videos)
296 expect(video).to.not.exist
297 expect(videos[0].blacklisted).to.not.exist
301 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.BLACKLISTED })
302 const video = finder(videos)
303 expect(video).to.exist
304 expect(video.blacklisted).to.be.true
309 it('Should include videos from muted account', async function () {
310 const finder = (videos: Video[]) => videos.find(v => v.name === 'remote video')
312 await servers[0].blocklist.addToServerBlocklist({ account: 'root@' + servers[1].host })
314 for (const path of remotePaths) {
316 const videos = await listVideos({ server: servers[0], path })
317 const video = finder(videos)
318 expect(video).to.not.exist
320 // Some paths won't have videos
322 expect(videos[0].blockedOwner).to.not.exist
323 expect(videos[0].blockedServer).to.not.exist
328 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.BLOCKED_OWNER })
330 const video = finder(videos)
331 expect(video).to.exist
332 expect(video.blockedServer).to.be.false
333 expect(video.blockedOwner).to.be.true
337 await servers[0].blocklist.removeFromServerBlocklist({ account: 'root@' + servers[1].host })
340 it('Should include videos from muted server', async function () {
341 const finder = (videos: Video[]) => videos.find(v => v.name === 'remote video')
343 await servers[0].blocklist.addToServerBlocklist({ server: servers[1].host })
345 for (const path of remotePaths) {
347 const videos = await listVideos({ server: servers[0], path })
348 const video = finder(videos)
349 expect(video).to.not.exist
351 // Some paths won't have videos
353 expect(videos[0].blockedOwner).to.not.exist
354 expect(videos[0].blockedServer).to.not.exist
359 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.BLOCKED_OWNER })
360 const video = finder(videos)
361 expect(video).to.exist
362 expect(video.blockedServer).to.be.true
363 expect(video.blockedOwner).to.be.false
367 await servers[0].blocklist.removeFromServerBlocklist({ server: servers[1].host })
370 it('Should include video files', async function () {
371 for (const path of paths) {
373 const videos = await listVideos({ server: servers[0], path })
375 for (const video of videos) {
376 const videoWithFiles = video as VideoDetails
378 expect(videoWithFiles.files).to.not.exist
379 expect(videoWithFiles.streamingPlaylists).to.not.exist
384 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.FILES })
386 for (const video of videos) {
387 const videoWithFiles = video as VideoDetails
389 expect(videoWithFiles.files).to.exist
390 expect(videoWithFiles.files).to.have.length.at.least(1)
396 it('Should filter by tags and category', async function () {
397 await servers[0].videos.upload({ attributes: { name: 'tag filter', tags: [ 'tag1', 'tag2' ] } })
398 await servers[0].videos.upload({ attributes: { name: 'tag filter with category', tags: [ 'tag3' ], category: 4 } })
400 for (const path of paths) {
402 const videos = await listVideos({ server: servers[0], path, tagsAllOf: [ 'tag1', 'tag2' ] })
403 expect(videos).to.have.lengthOf(1)
404 expect(videos[0].name).to.equal('tag filter')
408 const videos = await listVideos({ server: servers[0], path, tagsAllOf: [ 'tag1', 'tag3' ] })
409 expect(videos).to.have.lengthOf(0)
413 const { data, total } = await servers[0].videos.list({ tagsAllOf: [ 'tag3' ], categoryOneOf: [ 4 ] })
414 expect(total).to.equal(1)
415 expect(data[0].name).to.equal('tag filter with category')
419 const { total } = await servers[0].videos.list({ tagsAllOf: [ 'tag4' ], categoryOneOf: [ 4 ] })
420 expect(total).to.equal(0)
425 it('Should filter by HLS or WebTorrent files', async function () {
428 const finderFactory = (name: string) => (videos: Video[]) => videos.some(v => v.name === name)
430 await servers[0].config.enableTranscoding(true, false)
431 await servers[0].videos.upload({ attributes: { name: 'webtorrent video' } })
432 const hasWebtorrent = finderFactory('webtorrent video')
434 await waitJobs(servers)
436 await servers[0].config.enableTranscoding(false, true)
437 await servers[0].videos.upload({ attributes: { name: 'hls video' } })
438 const hasHLS = finderFactory('hls video')
440 await waitJobs(servers)
442 await servers[0].config.enableTranscoding(true, true)
443 await servers[0].videos.upload({ attributes: { name: 'hls and webtorrent video' } })
444 const hasBoth = finderFactory('hls and webtorrent video')
446 await waitJobs(servers)
448 for (const path of paths) {
450 const videos = await listVideos({ server: servers[0], path, hasWebtorrentFiles: true })
452 expect(hasWebtorrent(videos)).to.be.true
453 expect(hasHLS(videos)).to.be.false
454 expect(hasBoth(videos)).to.be.true
458 const videos = await listVideos({ server: servers[0], path, hasWebtorrentFiles: false })
460 expect(hasWebtorrent(videos)).to.be.false
461 expect(hasHLS(videos)).to.be.true
462 expect(hasBoth(videos)).to.be.false
466 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: true })
468 expect(hasWebtorrent(videos)).to.be.false
469 expect(hasHLS(videos)).to.be.true
470 expect(hasBoth(videos)).to.be.true
474 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: false })
476 expect(hasWebtorrent(videos)).to.be.true
477 expect(hasHLS(videos)).to.be.false
478 expect(hasBoth(videos)).to.be.false
482 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: false, hasWebtorrentFiles: false })
484 expect(hasWebtorrent(videos)).to.be.false
485 expect(hasHLS(videos)).to.be.false
486 expect(hasBoth(videos)).to.be.false
490 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: true, hasWebtorrentFiles: true })
492 expect(hasWebtorrent(videos)).to.be.false
493 expect(hasHLS(videos)).to.be.false
494 expect(hasBoth(videos)).to.be.true
500 after(async function () {
501 await cleanupTests(servers)