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 for (const server of servers) {
35 const moderator = { username: 'moderator', password: 'my super password' }
36 await server.users.create({ username: moderator.username, password: moderator.password, role: UserRole.MODERATOR })
37 server['moderatorAccessToken'] = await server.login.getAccessToken(moderator)
39 await server.videos.upload({ attributes: { name: 'public ' + server.serverNumber } })
42 const attributes = { name: 'unlisted ' + server.serverNumber, privacy: VideoPrivacy.UNLISTED }
43 await server.videos.upload({ attributes })
47 const attributes = { name: 'private ' + server.serverNumber, privacy: VideoPrivacy.PRIVATE }
48 await server.videos.upload({ attributes })
52 await doubleFollow(servers[0], servers[1])
55 `/api/v1/video-channels/root_channel/videos`,
56 `/api/v1/accounts/root/videos`,
58 '/api/v1/search/videos'
62 `/api/v1/video-channels/root_channel@${servers[1].host}/videos`,
63 `/api/v1/accounts/root@${servers[1].host}/videos`,
65 '/api/v1/search/videos'
69 describe('Check deprecated videos filter', function () {
71 async function getVideosNames (server: PeerTubeServer, token: string, filter: string, expectedStatus = HttpStatusCode.OK_200) {
72 const videosResults: Video[][] = []
74 for (const path of paths) {
75 const res = await makeGetRequest({
86 videosResults.push(res.body.data.map(v => v.name))
92 it('Should display local videos', async function () {
93 for (const server of servers) {
94 const namesResults = await getVideosNames(server, server.accessToken, 'local')
95 for (const names of namesResults) {
96 expect(names).to.have.lengthOf(1)
97 expect(names[0]).to.equal('public ' + server.serverNumber)
102 it('Should display all local videos by the admin or the moderator', async function () {
103 for (const server of servers) {
104 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
106 const namesResults = await getVideosNames(server, token, 'all-local')
107 for (const names of namesResults) {
108 expect(names).to.have.lengthOf(3)
110 expect(names[0]).to.equal('public ' + server.serverNumber)
111 expect(names[1]).to.equal('unlisted ' + server.serverNumber)
112 expect(names[2]).to.equal('private ' + server.serverNumber)
118 it('Should display all videos by the admin or the moderator', async function () {
119 for (const server of servers) {
120 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
122 const [ channelVideos, accountVideos, videos, searchVideos ] = await getVideosNames(server, token, 'all')
123 expect(channelVideos).to.have.lengthOf(3)
124 expect(accountVideos).to.have.lengthOf(3)
126 expect(videos).to.have.lengthOf(5)
127 expect(searchVideos).to.have.lengthOf(5)
133 describe('Check videos filters', function () {
135 async function listVideos (options: {
136 server: PeerTubeServer
139 hasWebtorrentFiles?: boolean
140 hasHLSFiles?: boolean
141 include?: VideoInclude
142 privacyOneOf?: VideoPrivacy[]
146 expectedStatus?: HttpStatusCode
148 const res = await makeGetRequest({
149 url: options.server.url,
151 token: options.token ?? options.server.accessToken,
153 ...pick(options, [ 'isLocal', 'include', 'category', 'tagsAllOf', 'hasWebtorrentFiles', 'hasHLSFiles', 'privacyOneOf' ]),
157 expectedStatus: options.expectedStatus ?? HttpStatusCode.OK_200
160 return res.body.data as Video[]
163 async function getVideosNames (options: {
164 server: PeerTubeServer
166 include?: VideoInclude
167 privacyOneOf?: VideoPrivacy[]
169 expectedStatus?: HttpStatusCode
171 const videosResults: string[][] = []
173 for (const path of paths) {
174 const videos = await listVideos({ ...options, path })
176 videosResults.push(videos.map(v => v.name))
182 it('Should display local videos', async function () {
183 for (const server of servers) {
184 const namesResults = await getVideosNames({ server, isLocal: true })
186 for (const names of namesResults) {
187 expect(names).to.have.lengthOf(1)
188 expect(names[0]).to.equal('public ' + server.serverNumber)
193 it('Should display local videos with hidden privacy by the admin or the moderator', async function () {
194 for (const server of servers) {
195 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
197 const namesResults = await getVideosNames({
201 privacyOneOf: [ VideoPrivacy.UNLISTED, VideoPrivacy.PUBLIC, VideoPrivacy.PRIVATE ]
204 for (const names of namesResults) {
205 expect(names).to.have.lengthOf(3)
207 expect(names[0]).to.equal('public ' + server.serverNumber)
208 expect(names[1]).to.equal('unlisted ' + server.serverNumber)
209 expect(names[2]).to.equal('private ' + server.serverNumber)
215 it('Should display all videos by the admin or the moderator', async function () {
216 for (const server of servers) {
217 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
219 const [ channelVideos, accountVideos, videos, searchVideos ] = await getVideosNames({
222 privacyOneOf: [ VideoPrivacy.UNLISTED, VideoPrivacy.PUBLIC, VideoPrivacy.PRIVATE ]
225 expect(channelVideos).to.have.lengthOf(3)
226 expect(accountVideos).to.have.lengthOf(3)
228 expect(videos).to.have.lengthOf(5)
229 expect(searchVideos).to.have.lengthOf(5)
234 it('Should display only remote videos', async function () {
237 await servers[1].videos.upload({ attributes: { name: 'remote video' } })
239 await waitJobs(servers)
241 const finder = (videos: Video[]) => videos.find(v => v.name === 'remote video')
243 for (const path of remotePaths) {
245 const videos = await listVideos({ server: servers[0], path })
246 const video = finder(videos)
247 expect(video).to.exist
251 const videos = await listVideos({ server: servers[0], path, isLocal: false })
252 const video = finder(videos)
253 expect(video).to.exist
257 const videos = await listVideos({ server: servers[0], path, isLocal: true })
258 const video = finder(videos)
259 expect(video).to.not.exist
264 it('Should include not published videos', async function () {
265 await servers[0].config.enableLive({ allowReplay: false, transcoding: false })
266 await servers[0].live.create({ fields: { name: 'live video', channelId: servers[0].store.channel.id, privacy: VideoPrivacy.PUBLIC } })
268 const finder = (videos: Video[]) => videos.find(v => v.name === 'live video')
270 for (const path of paths) {
272 const videos = await listVideos({ server: servers[0], path })
273 const video = finder(videos)
274 expect(video).to.not.exist
275 expect(videos[0].state).to.not.exist
276 expect(videos[0].waitTranscoding).to.not.exist
280 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.NOT_PUBLISHED_STATE })
281 const video = finder(videos)
282 expect(video).to.exist
283 expect(video.state).to.exist
288 it('Should include blacklisted videos', async function () {
289 const { id } = await servers[0].videos.upload({ attributes: { name: 'blacklisted' } })
291 await servers[0].blacklist.add({ videoId: id })
293 const finder = (videos: Video[]) => videos.find(v => v.name === 'blacklisted')
295 for (const path of paths) {
297 const videos = await listVideos({ server: servers[0], path })
298 const video = finder(videos)
299 expect(video).to.not.exist
300 expect(videos[0].blacklisted).to.not.exist
304 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.BLACKLISTED })
305 const video = finder(videos)
306 expect(video).to.exist
307 expect(video.blacklisted).to.be.true
312 it('Should include videos from muted account', async function () {
313 const finder = (videos: Video[]) => videos.find(v => v.name === 'remote video')
315 await servers[0].blocklist.addToServerBlocklist({ account: 'root@' + servers[1].host })
317 for (const path of remotePaths) {
319 const videos = await listVideos({ server: servers[0], path })
320 const video = finder(videos)
321 expect(video).to.not.exist
323 // Some paths won't have videos
325 expect(videos[0].blockedOwner).to.not.exist
326 expect(videos[0].blockedServer).to.not.exist
331 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.BLOCKED_OWNER })
333 const video = finder(videos)
334 expect(video).to.exist
335 expect(video.blockedServer).to.be.false
336 expect(video.blockedOwner).to.be.true
340 await servers[0].blocklist.removeFromServerBlocklist({ account: 'root@' + servers[1].host })
343 it('Should include videos from muted server', async function () {
344 const finder = (videos: Video[]) => videos.find(v => v.name === 'remote video')
346 await servers[0].blocklist.addToServerBlocklist({ server: servers[1].host })
348 for (const path of remotePaths) {
350 const videos = await listVideos({ server: servers[0], path })
351 const video = finder(videos)
352 expect(video).to.not.exist
354 // Some paths won't have videos
356 expect(videos[0].blockedOwner).to.not.exist
357 expect(videos[0].blockedServer).to.not.exist
362 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.BLOCKED_OWNER })
363 const video = finder(videos)
364 expect(video).to.exist
365 expect(video.blockedServer).to.be.true
366 expect(video.blockedOwner).to.be.false
370 await servers[0].blocklist.removeFromServerBlocklist({ server: servers[1].host })
373 it('Should include video files', async function () {
374 for (const path of paths) {
376 const videos = await listVideos({ server: servers[0], path })
378 for (const video of videos) {
379 const videoWithFiles = video as VideoDetails
381 expect(videoWithFiles.files).to.not.exist
382 expect(videoWithFiles.streamingPlaylists).to.not.exist
387 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.FILES })
389 for (const video of videos) {
390 const videoWithFiles = video as VideoDetails
392 expect(videoWithFiles.files).to.exist
393 expect(videoWithFiles.files).to.have.length.at.least(1)
399 it('Should filter by tags and category', async function () {
400 await servers[0].videos.upload({ attributes: { name: 'tag filter', tags: [ 'tag1', 'tag2' ] } })
401 await servers[0].videos.upload({ attributes: { name: 'tag filter with category', tags: [ 'tag3' ], category: 4 } })
403 for (const path of paths) {
405 const videos = await listVideos({ server: servers[0], path, tagsAllOf: [ 'tag1', 'tag2' ] })
406 expect(videos).to.have.lengthOf(1)
407 expect(videos[0].name).to.equal('tag filter')
411 const videos = await listVideos({ server: servers[0], path, tagsAllOf: [ 'tag1', 'tag3' ] })
412 expect(videos).to.have.lengthOf(0)
416 const { data, total } = await servers[0].videos.list({ tagsAllOf: [ 'tag3' ], categoryOneOf: [ 4 ] })
417 expect(total).to.equal(1)
418 expect(data[0].name).to.equal('tag filter with category')
422 const { total } = await servers[0].videos.list({ tagsAllOf: [ 'tag4' ], categoryOneOf: [ 4 ] })
423 expect(total).to.equal(0)
428 it('Should filter by HLS or WebTorrent files', async function () {
431 const finderFactory = (name: string) => (videos: Video[]) => videos.some(v => v.name === name)
433 await servers[0].config.enableTranscoding(true, false)
434 await servers[0].videos.upload({ attributes: { name: 'webtorrent video' } })
435 const hasWebtorrent = finderFactory('webtorrent video')
437 await waitJobs(servers)
439 await servers[0].config.enableTranscoding(false, true)
440 await servers[0].videos.upload({ attributes: { name: 'hls video' } })
441 const hasHLS = finderFactory('hls video')
443 await waitJobs(servers)
445 await servers[0].config.enableTranscoding(true, true)
446 await servers[0].videos.upload({ attributes: { name: 'hls and webtorrent video' } })
447 const hasBoth = finderFactory('hls and webtorrent video')
449 await waitJobs(servers)
451 for (const path of paths) {
453 const videos = await listVideos({ server: servers[0], path, hasWebtorrentFiles: true })
455 expect(hasWebtorrent(videos)).to.be.true
456 expect(hasHLS(videos)).to.be.false
457 expect(hasBoth(videos)).to.be.true
461 const videos = await listVideos({ server: servers[0], path, hasWebtorrentFiles: false })
463 expect(hasWebtorrent(videos)).to.be.false
464 expect(hasHLS(videos)).to.be.true
465 expect(hasBoth(videos)).to.be.false
469 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: true })
471 expect(hasWebtorrent(videos)).to.be.false
472 expect(hasHLS(videos)).to.be.true
473 expect(hasBoth(videos)).to.be.true
477 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: false })
479 expect(hasWebtorrent(videos)).to.be.true
480 expect(hasHLS(videos)).to.be.false
481 expect(hasBoth(videos)).to.be.false
485 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: false, hasWebtorrentFiles: false })
487 expect(hasWebtorrent(videos)).to.be.false
488 expect(hasHLS(videos)).to.be.false
489 expect(hasBoth(videos)).to.be.false
493 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: true, hasWebtorrentFiles: true })
495 expect(hasWebtorrent(videos)).to.be.false
496 expect(hasHLS(videos)).to.be.false
497 expect(hasBoth(videos)).to.be.true
503 after(async function () {
504 await cleanupTests(servers)