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 const subscriptionVideosPath = '/api/v1/users/me/subscriptions/videos'
25 // ---------------------------------------------------------------
27 before(async function () {
30 servers = await createMultipleServers(2)
32 await setAccessTokensToServers(servers)
33 await setDefaultVideoChannel(servers)
34 await setDefaultAccountAvatar(servers)
36 await servers[1].config.enableMinimumTranscoding()
38 for (const server of servers) {
39 const moderator = { username: 'moderator', password: 'my super password' }
40 await server.users.create({ username: moderator.username, password: moderator.password, role: UserRole.MODERATOR })
41 server['moderatorAccessToken'] = await server.login.getAccessToken(moderator)
43 await server.videos.upload({ attributes: { name: 'public ' + server.serverNumber } })
46 const attributes = { name: 'unlisted ' + server.serverNumber, privacy: VideoPrivacy.UNLISTED }
47 await server.videos.upload({ attributes })
51 const attributes = { name: 'private ' + server.serverNumber, privacy: VideoPrivacy.PRIVATE }
52 await server.videos.upload({ attributes })
55 // Subscribing to itself
56 await server.subscriptions.add({ targetUri: 'root_channel@' + server.host })
59 await doubleFollow(servers[0], servers[1])
62 `/api/v1/video-channels/root_channel/videos`,
63 `/api/v1/accounts/root/videos`,
65 '/api/v1/search/videos',
66 subscriptionVideosPath
70 `/api/v1/video-channels/root_channel@${servers[1].host}/videos`,
71 `/api/v1/accounts/root@${servers[1].host}/videos`,
73 '/api/v1/search/videos'
77 describe('Check deprecated videos filter', function () {
79 async function getVideosNames (options: {
80 server: PeerTubeServer
83 skipSubscription?: boolean
84 expectedStatus?: HttpStatusCode
86 const { server, token, filter, skipSubscription = false, expectedStatus = HttpStatusCode.OK_200 } = options
88 const videosResults: Video[][] = []
90 for (const path of paths) {
91 if (skipSubscription && path === subscriptionVideosPath) continue
93 const res = await makeGetRequest({
104 videosResults.push(res.body.data.map(v => v.name))
110 it('Should display local videos', async function () {
111 for (const server of servers) {
112 const namesResults = await getVideosNames({ server, token: server.accessToken, filter: 'local' })
113 for (const names of namesResults) {
114 expect(names).to.have.lengthOf(1)
115 expect(names[0]).to.equal('public ' + server.serverNumber)
120 it('Should display all local 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 namesResults = await getVideosNames({ server, token, filter: 'all-local', skipSubscription: true })
125 for (const names of namesResults) {
126 expect(names).to.have.lengthOf(3)
128 expect(names[0]).to.equal('public ' + server.serverNumber)
129 expect(names[1]).to.equal('unlisted ' + server.serverNumber)
130 expect(names[2]).to.equal('private ' + server.serverNumber)
136 it('Should display all videos by the admin or the moderator', async function () {
137 for (const server of servers) {
138 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
140 const [ channelVideos, accountVideos, videos, searchVideos ] = await getVideosNames({ server, token, filter: 'all' })
141 expect(channelVideos).to.have.lengthOf(3)
142 expect(accountVideos).to.have.lengthOf(3)
144 expect(videos).to.have.lengthOf(5)
145 expect(searchVideos).to.have.lengthOf(5)
151 describe('Check videos filters', function () {
153 async function listVideos (options: {
154 server: PeerTubeServer
157 hasWebtorrentFiles?: boolean
158 hasHLSFiles?: boolean
159 include?: VideoInclude
160 privacyOneOf?: VideoPrivacy[]
164 expectedStatus?: HttpStatusCode
165 excludeAlreadyWatched?: boolean
167 const res = await makeGetRequest({
168 url: options.server.url,
170 token: options.token ?? options.server.accessToken,
177 'hasWebtorrentFiles',
180 'excludeAlreadyWatched'
185 expectedStatus: options.expectedStatus ?? HttpStatusCode.OK_200
188 return res.body.data as Video[]
191 async function getVideosNames (
193 server: PeerTubeServer
195 include?: VideoInclude
196 privacyOneOf?: VideoPrivacy[]
198 expectedStatus?: HttpStatusCode
199 skipSubscription?: boolean
200 excludeAlreadyWatched?: boolean
203 const { skipSubscription = false } = options
204 const videosResults: string[][] = []
206 for (const path of paths) {
207 if (skipSubscription && path === subscriptionVideosPath) continue
209 const videos = await listVideos({ ...options, path })
211 videosResults.push(videos.map(v => v.name))
217 it('Should display local videos', async function () {
218 for (const server of servers) {
219 const namesResults = await getVideosNames({ server, isLocal: true })
221 for (const names of namesResults) {
222 expect(names).to.have.lengthOf(1)
223 expect(names[0]).to.equal('public ' + server.serverNumber)
228 it('Should display local videos with hidden privacy by the admin or the moderator', async function () {
229 for (const server of servers) {
230 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
232 const namesResults = await getVideosNames(
237 privacyOneOf: [ VideoPrivacy.UNLISTED, VideoPrivacy.PUBLIC, VideoPrivacy.PRIVATE ],
238 skipSubscription: true
242 for (const names of namesResults) {
243 expect(names).to.have.lengthOf(3)
245 expect(names[0]).to.equal('public ' + server.serverNumber)
246 expect(names[1]).to.equal('unlisted ' + server.serverNumber)
247 expect(names[2]).to.equal('private ' + server.serverNumber)
253 it('Should display all videos by the admin or the moderator', async function () {
254 for (const server of servers) {
255 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
257 const [ channelVideos, accountVideos, videos, searchVideos ] = await getVideosNames({
260 privacyOneOf: [ VideoPrivacy.UNLISTED, VideoPrivacy.PUBLIC, VideoPrivacy.PRIVATE ]
263 expect(channelVideos).to.have.lengthOf(3)
264 expect(accountVideos).to.have.lengthOf(3)
266 expect(videos).to.have.lengthOf(5)
267 expect(searchVideos).to.have.lengthOf(5)
272 it('Should display only remote videos', async function () {
275 await servers[1].videos.upload({ attributes: { name: 'remote video' } })
277 await waitJobs(servers)
279 const finder = (videos: Video[]) => videos.find(v => v.name === 'remote video')
281 for (const path of remotePaths) {
283 const videos = await listVideos({ server: servers[0], path })
284 const video = finder(videos)
285 expect(video).to.exist
289 const videos = await listVideos({ server: servers[0], path, isLocal: false })
290 const video = finder(videos)
291 expect(video).to.exist
295 const videos = await listVideos({ server: servers[0], path, isLocal: true })
296 const video = finder(videos)
297 expect(video).to.not.exist
302 it('Should include not published videos', async function () {
303 await servers[0].config.enableLive({ allowReplay: false, transcoding: false })
304 await servers[0].live.create({ fields: { name: 'live video', channelId: servers[0].store.channel.id, privacy: VideoPrivacy.PUBLIC } })
306 const finder = (videos: Video[]) => videos.find(v => v.name === 'live video')
308 for (const path of paths) {
310 const videos = await listVideos({ server: servers[0], path })
311 const video = finder(videos)
312 expect(video).to.not.exist
313 expect(videos[0].state).to.not.exist
314 expect(videos[0].waitTranscoding).to.not.exist
318 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.NOT_PUBLISHED_STATE })
319 const video = finder(videos)
320 expect(video).to.exist
321 expect(video.state).to.exist
326 it('Should include blacklisted videos', async function () {
327 const { id } = await servers[0].videos.upload({ attributes: { name: 'blacklisted' } })
329 await servers[0].blacklist.add({ videoId: id })
331 const finder = (videos: Video[]) => videos.find(v => v.name === 'blacklisted')
333 for (const path of paths) {
335 const videos = await listVideos({ server: servers[0], path })
336 const video = finder(videos)
337 expect(video).to.not.exist
338 expect(videos[0].blacklisted).to.not.exist
342 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.BLACKLISTED })
343 const video = finder(videos)
344 expect(video).to.exist
345 expect(video.blacklisted).to.be.true
350 it('Should include videos from muted account', async function () {
351 const finder = (videos: Video[]) => videos.find(v => v.name === 'remote video')
353 await servers[0].blocklist.addToServerBlocklist({ account: 'root@' + servers[1].host })
355 for (const path of remotePaths) {
357 const videos = await listVideos({ server: servers[0], path })
358 const video = finder(videos)
359 expect(video).to.not.exist
361 // Some paths won't have videos
363 expect(videos[0].blockedOwner).to.not.exist
364 expect(videos[0].blockedServer).to.not.exist
369 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.BLOCKED_OWNER })
371 const video = finder(videos)
372 expect(video).to.exist
373 expect(video.blockedServer).to.be.false
374 expect(video.blockedOwner).to.be.true
378 await servers[0].blocklist.removeFromServerBlocklist({ account: 'root@' + servers[1].host })
381 it('Should include videos from muted server', async function () {
382 const finder = (videos: Video[]) => videos.find(v => v.name === 'remote video')
384 await servers[0].blocklist.addToServerBlocklist({ server: servers[1].host })
386 for (const path of remotePaths) {
388 const videos = await listVideos({ server: servers[0], path })
389 const video = finder(videos)
390 expect(video).to.not.exist
392 // Some paths won't have videos
394 expect(videos[0].blockedOwner).to.not.exist
395 expect(videos[0].blockedServer).to.not.exist
400 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.BLOCKED_OWNER })
401 const video = finder(videos)
402 expect(video).to.exist
403 expect(video.blockedServer).to.be.true
404 expect(video.blockedOwner).to.be.false
408 await servers[0].blocklist.removeFromServerBlocklist({ server: servers[1].host })
411 it('Should include video files', async function () {
412 for (const path of paths) {
414 const videos = await listVideos({ server: servers[0], path })
416 for (const video of videos) {
417 const videoWithFiles = video as VideoDetails
419 expect(videoWithFiles.files).to.not.exist
420 expect(videoWithFiles.streamingPlaylists).to.not.exist
425 const videos = await listVideos({ server: servers[0], path, include: VideoInclude.FILES })
427 for (const video of videos) {
428 const videoWithFiles = video as VideoDetails
430 expect(videoWithFiles.files).to.exist
431 expect(videoWithFiles.files).to.have.length.at.least(1)
437 it('Should filter by tags and category', async function () {
438 await servers[0].videos.upload({ attributes: { name: 'tag filter', tags: [ 'tag1', 'tag2' ] } })
439 await servers[0].videos.upload({ attributes: { name: 'tag filter with category', tags: [ 'tag3' ], category: 4 } })
441 for (const path of paths) {
443 const videos = await listVideos({ server: servers[0], path, tagsAllOf: [ 'tag1', 'tag2' ] })
444 expect(videos).to.have.lengthOf(1)
445 expect(videos[0].name).to.equal('tag filter')
449 const videos = await listVideos({ server: servers[0], path, tagsAllOf: [ 'tag1', 'tag3' ] })
450 expect(videos).to.have.lengthOf(0)
454 const { data, total } = await servers[0].videos.list({ tagsAllOf: [ 'tag3' ], categoryOneOf: [ 4 ] })
455 expect(total).to.equal(1)
456 expect(data[0].name).to.equal('tag filter with category')
460 const { total } = await servers[0].videos.list({ tagsAllOf: [ 'tag4' ], categoryOneOf: [ 4 ] })
461 expect(total).to.equal(0)
466 it('Should filter by HLS or WebTorrent files', async function () {
469 const finderFactory = (name: string) => (videos: Video[]) => videos.some(v => v.name === name)
471 await servers[0].config.enableTranscoding(true, false)
472 await servers[0].videos.upload({ attributes: { name: 'webtorrent video' } })
473 const hasWebtorrent = finderFactory('webtorrent video')
475 await waitJobs(servers)
477 await servers[0].config.enableTranscoding(false, true)
478 await servers[0].videos.upload({ attributes: { name: 'hls video' } })
479 const hasHLS = finderFactory('hls video')
481 await waitJobs(servers)
483 await servers[0].config.enableTranscoding(true, true)
484 await servers[0].videos.upload({ attributes: { name: 'hls and webtorrent video' } })
485 const hasBoth = finderFactory('hls and webtorrent video')
487 await waitJobs(servers)
489 for (const path of paths) {
491 const videos = await listVideos({ server: servers[0], path, hasWebtorrentFiles: true })
493 expect(hasWebtorrent(videos)).to.be.true
494 expect(hasHLS(videos)).to.be.false
495 expect(hasBoth(videos)).to.be.true
499 const videos = await listVideos({ server: servers[0], path, hasWebtorrentFiles: false })
501 expect(hasWebtorrent(videos)).to.be.false
502 expect(hasHLS(videos)).to.be.true
503 expect(hasBoth(videos)).to.be.false
507 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: true })
509 expect(hasWebtorrent(videos)).to.be.false
510 expect(hasHLS(videos)).to.be.true
511 expect(hasBoth(videos)).to.be.true
515 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: false })
517 expect(hasWebtorrent(videos)).to.be.true
518 expect(hasHLS(videos)).to.be.false
519 expect(hasBoth(videos)).to.be.false
523 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: false, hasWebtorrentFiles: false })
525 expect(hasWebtorrent(videos)).to.be.false
526 expect(hasHLS(videos)).to.be.false
527 expect(hasBoth(videos)).to.be.false
531 const videos = await listVideos({ server: servers[0], path, hasHLSFiles: true, hasWebtorrentFiles: true })
533 expect(hasWebtorrent(videos)).to.be.false
534 expect(hasHLS(videos)).to.be.false
535 expect(hasBoth(videos)).to.be.true
540 it('Should filter already watched videos by the user', async function () {
541 const { id } = await servers[0].videos.upload({ attributes: { name: 'video for history' } })
543 for (const path of paths) {
544 const videos = await listVideos({ server: servers[0], path, isLocal: true, excludeAlreadyWatched: true })
545 const foundVideo = videos.find(video => video.id === id)
547 expect(foundVideo).to.not.be.undefined
549 await servers[0].views.view({ id, token: servers[0].accessToken })
551 for (const path of paths) {
552 const videos = await listVideos({ server: servers[0], path, excludeAlreadyWatched: true })
553 const foundVideo = videos.find(video => video.id === id)
555 expect(foundVideo).to.be.undefined
560 after(async function () {
561 await cleanupTests(servers)