]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/videos/video-playlists.ts
Introduce blocklist command
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-playlists.ts
index 52b32998df9836088b4820893abafdd8e079261f..90189721a6de0e57b472c31fa1a965c483f5661f 100644 (file)
@@ -1,7 +1,8 @@
 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
 
-import * as chai from 'chai'
 import 'mocha'
+import * as chai from 'chai'
+import { HttpStatusCode } from '@shared/core-utils'
 import {
   addVideoChannel,
   addVideoInPlaylist,
@@ -34,32 +35,26 @@ import {
   setAccessTokensToServers,
   setDefaultVideoChannel,
   testImage,
-  unfollow,
   updateVideo,
   updateVideoPlaylist,
   updateVideoPlaylistElement,
   uploadVideo,
   uploadVideoAndGetId,
   userLogin,
+  wait,
   waitJobs
-} from '../../../../shared/extra-utils'
-import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
-import { VideoPlaylist } from '../../../../shared/models/videos/playlist/video-playlist.model'
-import { VideoPrivacy } from '../../../../shared/models/videos'
-import { VideoPlaylistType } from '../../../../shared/models/videos/playlist/video-playlist-type.model'
-import { VideoExistInPlaylist } from '../../../../shared/models/videos/playlist/video-exist-in-playlist.model'
-import { User } from '../../../../shared/models/users'
-import { VideoPlaylistElement, VideoPlaylistElementType } from '../../../../shared/models/videos/playlist/video-playlist-element.model'
+} from '@shared/extra-utils'
 import {
-  addAccountToAccountBlocklist,
-  addAccountToServerBlocklist,
-  addServerToAccountBlocklist,
-  addServerToServerBlocklist,
-  removeAccountFromAccountBlocklist,
-  removeAccountFromServerBlocklist,
-  removeServerFromAccountBlocklist,
-  removeServerFromServerBlocklist
-} from '../../../../shared/extra-utils/users/blocklist'
+  User,
+  VideoExistInPlaylist,
+  VideoPlaylist,
+  VideoPlaylistCreateResult,
+  VideoPlaylistElement,
+  VideoPlaylistElementType,
+  VideoPlaylistPrivacy,
+  VideoPlaylistType,
+  VideoPrivacy
+} from '@shared/models'
 
 const expect = chai.expect
 
@@ -121,24 +116,18 @@ describe('Test video playlists', function () {
     await doubleFollow(servers[0], servers[2])
 
     {
-      const serverPromises: Promise<any>[][] = []
+      servers[0].videos = []
+      servers[1].videos = []
+      servers[2].videos = []
 
       for (const server of servers) {
-        const videoPromises: Promise<any>[] = []
-
         for (let i = 0; i < 7; i++) {
-          videoPromises.push(
-            uploadVideo(server.url, server.accessToken, { name: `video ${i} server ${server.serverNumber}`, nsfw: false })
-              .then(res => res.body.video)
-          )
-        }
+          const name = `video ${i} server ${server.serverNumber}`
+          const resVideo = await uploadVideo(server.url, server.accessToken, { name, nsfw: false })
 
-        serverPromises.push(videoPromises)
+          server.videos.push(resVideo.body.video)
+        }
       }
-
-      servers[0].videos = await Promise.all(serverPromises[0])
-      servers[1].videos = await Promise.all(serverPromises[1])
-      servers[2].videos = await Promise.all(serverPromises[2])
     }
 
     nsfwVideoServer1 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'NSFW video', nsfw: true })).id
@@ -227,6 +216,8 @@ describe('Test video playlists', function () {
       })
 
       await waitJobs(servers)
+      // Processing a playlist by the receiver could be long
+      await wait(3000)
 
       for (const server of servers) {
         const res = await getVideoPlaylistsList(server.url, 0, 5)
@@ -310,6 +301,7 @@ describe('Test video playlists', function () {
       }
 
       await waitJobs(servers)
+      await wait(3000)
 
       for (const server of [ servers[0], servers[1] ]) {
         const res = await getVideoPlaylistsList(server.url, 0, 5)
@@ -428,30 +420,45 @@ describe('Test video playlists', function () {
         expect(data).to.have.lengthOf(0)
       }
     })
+  })
 
-    it('Should not list unlisted or private playlists', async function () {
+  describe('Playlist rights', function () {
+    let unlistedPlaylist: VideoPlaylistCreateResult
+    let privatePlaylist: VideoPlaylistCreateResult
+
+    before(async function () {
       this.timeout(30000)
 
-      await createVideoPlaylist({
-        url: servers[1].url,
-        token: servers[1].accessToken,
-        playlistAttrs: {
-          displayName: 'playlist unlisted',
-          privacy: VideoPlaylistPrivacy.UNLISTED
-        }
-      })
+      {
+        const res = await createVideoPlaylist({
+          url: servers[1].url,
+          token: servers[1].accessToken,
+          playlistAttrs: {
+            displayName: 'playlist unlisted',
+            privacy: VideoPlaylistPrivacy.UNLISTED,
+            videoChannelId: servers[1].videoChannel.id
+          }
+        })
+        unlistedPlaylist = res.body.videoPlaylist
+      }
 
-      await createVideoPlaylist({
-        url: servers[1].url,
-        token: servers[1].accessToken,
-        playlistAttrs: {
-          displayName: 'playlist private',
-          privacy: VideoPlaylistPrivacy.PRIVATE
-        }
-      })
+      {
+        const res = await createVideoPlaylist({
+          url: servers[1].url,
+          token: servers[1].accessToken,
+          playlistAttrs: {
+            displayName: 'playlist private',
+            privacy: VideoPlaylistPrivacy.PRIVATE
+          }
+        })
+        privatePlaylist = res.body.videoPlaylist
+      }
 
       await waitJobs(servers)
+      await wait(3000)
+    })
 
+    it('Should not list unlisted or private playlists', async function () {
       for (const server of servers) {
         const results = [
           await getAccountPlaylistsList(server.url, 'root@localhost:' + servers[1].port, 0, 5, '-createdAt'),
@@ -469,6 +476,27 @@ describe('Test video playlists', function () {
         }
       }
     })
+
+    it('Should not get unlisted playlist using only the id', async function () {
+      await getVideoPlaylist(servers[1].url, unlistedPlaylist.id, 404)
+    })
+
+    it('Should get unlisted plyaylist using uuid or shortUUID', async function () {
+      await getVideoPlaylist(servers[1].url, unlistedPlaylist.uuid)
+      await getVideoPlaylist(servers[1].url, unlistedPlaylist.shortUUID)
+    })
+
+    it('Should not get private playlist without token', async function () {
+      for (const id of [ privatePlaylist.id, privatePlaylist.uuid, privatePlaylist.shortUUID ]) {
+        await getVideoPlaylist(servers[1].url, id, 401)
+      }
+    })
+
+    it('Should get private playlist with a token', async function () {
+      for (const id of [ privatePlaylist.id, privatePlaylist.uuid, privatePlaylist.shortUUID ]) {
+        await getVideoPlaylistWithToken(servers[1].url, servers[1].accessToken, id)
+      }
+    })
   })
 
   describe('Update playlists', function () {
@@ -552,6 +580,9 @@ describe('Test video playlists', function () {
       {
         const res = await addVideo({ videoId: nsfwVideoServer1, startTimestamp: 5 })
         playlistElementNSFW = res.body.videoPlaylistElement.id
+
+        await addVideo({ videoId: nsfwVideoServer1, startTimestamp: 4 })
+        await addVideo({ videoId: nsfwVideoServer1 })
       }
 
       await waitJobs(servers)
@@ -563,10 +594,10 @@ describe('Test video playlists', function () {
       for (const server of servers) {
         const res = await getPlaylistVideos(server.url, server.accessToken, playlistServer1UUID, 0, 10)
 
-        expect(res.body.total).to.equal(6)
+        expect(res.body.total).to.equal(8)
 
         const videoElements: VideoPlaylistElement[] = res.body.data
-        expect(videoElements).to.have.lengthOf(6)
+        expect(videoElements).to.have.lengthOf(8)
 
         expect(videoElements[0].video.name).to.equal('video 0 server 1')
         expect(videoElements[0].position).to.equal(1)
@@ -598,6 +629,16 @@ describe('Test video playlists', function () {
         expect(videoElements[5].startTimestamp).to.equal(5)
         expect(videoElements[5].stopTimestamp).to.be.null
 
+        expect(videoElements[6].video.name).to.equal('NSFW video')
+        expect(videoElements[6].position).to.equal(7)
+        expect(videoElements[6].startTimestamp).to.equal(4)
+        expect(videoElements[6].stopTimestamp).to.be.null
+
+        expect(videoElements[7].video.name).to.equal('NSFW video')
+        expect(videoElements[7].position).to.equal(8)
+        expect(videoElements[7].startTimestamp).to.be.null
+        expect(videoElements[7].stopTimestamp).to.be.null
+
         const res3 = await getPlaylistVideos(server.url, server.accessToken, playlistServer1UUID, 0, 2)
         expect(res3.body.data).to.have.lengthOf(2)
       }
@@ -615,7 +656,7 @@ describe('Test video playlists', function () {
     let video3: string
 
     before(async function () {
-      this.timeout(30000)
+      this.timeout(60000)
 
       groupUser1 = [ Object.assign({}, servers[0], { accessToken: userAccessTokenServer1 }) ]
       groupWithoutToken1 = [ Object.assign({}, servers[0], { accessToken: undefined }) ]
@@ -643,6 +684,8 @@ describe('Test video playlists', function () {
       video2 = (await uploadVideoAndGetId({ server: servers[1], videoName: 'video 90' })).uuid
       video3 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video 91', nsfw: true })).uuid
 
+      await waitJobs(servers)
+
       await addVideo({ videoId: video1, startTimestamp: 15, stopTimestamp: 28 })
       await addVideo({ videoId: video2, startTimestamp: 35 })
       await addVideo({ videoId: video3 })
@@ -709,56 +752,58 @@ describe('Test video playlists', function () {
     it('Should update the element type if the account or server of the video is blocked', async function () {
       this.timeout(90000)
 
+      const command = servers[0].blocklistCommand
+
       const name = 'video 90'
       const position = 2
 
       {
-        await addAccountToAccountBlocklist(servers[0].url, userAccessTokenServer1, 'root@localhost:' + servers[1].port)
+        await command.addToMyBlocklist({ token: userAccessTokenServer1, account: 'root@localhost:' + servers[1].port })
         await waitJobs(servers)
 
         await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.UNAVAILABLE, position, name, 3)
         await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
 
-        await removeAccountFromAccountBlocklist(servers[0].url, userAccessTokenServer1, 'root@localhost:' + servers[1].port)
+        await command.removeFromMyBlocklist({ token: userAccessTokenServer1, account: 'root@localhost:' + servers[1].port })
         await waitJobs(servers)
 
         await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
       }
 
       {
-        await addServerToAccountBlocklist(servers[0].url, userAccessTokenServer1, 'localhost:' + servers[1].port)
+        await command.addToMyBlocklist({ token: userAccessTokenServer1, server: 'localhost:' + servers[1].port })
         await waitJobs(servers)
 
         await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.UNAVAILABLE, position, name, 3)
         await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
 
-        await removeServerFromAccountBlocklist(servers[0].url, userAccessTokenServer1, 'localhost:' + servers[1].port)
+        await command.removeFromMyBlocklist({ token: userAccessTokenServer1, server: 'localhost:' + servers[1].port })
         await waitJobs(servers)
 
         await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
       }
 
       {
-        await addAccountToServerBlocklist(servers[0].url, servers[0].accessToken, 'root@localhost:' + servers[1].port)
+        await command.addToServerBlocklist({ account: 'root@localhost:' + servers[1].port })
         await waitJobs(servers)
 
         await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.UNAVAILABLE, position, name, 3)
         await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
 
-        await removeAccountFromServerBlocklist(servers[0].url, servers[0].accessToken, 'root@localhost:' + servers[1].port)
+        await command.removeFromServerBlocklist({ account: 'root@localhost:' + servers[1].port })
         await waitJobs(servers)
 
         await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
       }
 
       {
-        await addServerToServerBlocklist(servers[0].url, servers[0].accessToken, 'localhost:' + servers[1].port)
+        await command.addToServerBlocklist({ server: 'localhost:' + servers[1].port })
         await waitJobs(servers)
 
         await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.UNAVAILABLE, position, name, 3)
         await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
 
-        await removeServerFromServerBlocklist(servers[0].url, servers[0].accessToken, 'localhost:' + servers[1].port)
+        await command.removeFromServerBlocklist({ server: 'localhost:' + servers[1].port })
         await waitJobs(servers)
 
         await checkPlaylistElementType(group2, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3)
@@ -807,6 +852,8 @@ describe('Test video playlists', function () {
             'video 1 server 3',
             'video 3 server 1',
             'video 4 server 1',
+            'NSFW video',
+            'NSFW video',
             'NSFW video'
           ])
         }
@@ -836,6 +883,8 @@ describe('Test video playlists', function () {
             'video 2 server 3',
             'video 1 server 3',
             'video 4 server 1',
+            'NSFW video',
+            'NSFW video',
             'NSFW video'
           ])
         }
@@ -865,7 +914,9 @@ describe('Test video playlists', function () {
             'video 2 server 3',
             'NSFW video',
             'video 1 server 3',
-            'video 4 server 1'
+            'video 4 server 1',
+            'NSFW video',
+            'NSFW video'
           ])
 
           for (let i = 1; i <= elements.length; i++) {
@@ -1023,10 +1074,10 @@ describe('Test video playlists', function () {
       for (const server of servers) {
         const res = await getPlaylistVideos(server.url, server.accessToken, playlistServer1UUID, 0, 10)
 
-        expect(res.body.total).to.equal(4)
+        expect(res.body.total).to.equal(6)
 
         const elements: VideoPlaylistElement[] = res.body.data
-        expect(elements).to.have.lengthOf(4)
+        expect(elements).to.have.lengthOf(6)
 
         expect(elements[0].video.name).to.equal('video 0 server 1')
         expect(elements[0].position).to.equal(1)
@@ -1039,6 +1090,12 @@ describe('Test video playlists', function () {
 
         expect(elements[3].video.name).to.equal('video 4 server 1')
         expect(elements[3].position).to.equal(4)
+
+        expect(elements[4].video.name).to.equal('NSFW video')
+        expect(elements[4].position).to.equal(5)
+
+        expect(elements[5].video.name).to.equal('NSFW video')
+        expect(elements[5].position).to.equal(6)
       }
     })
 
@@ -1059,7 +1116,7 @@ describe('Test video playlists', function () {
       await waitJobs(servers)
 
       for (const server of servers) {
-        await getVideoPlaylist(server.url, videoPlaylistIds.uuid, 200)
+        await getVideoPlaylist(server.url, videoPlaylistIds.uuid, HttpStatusCode.OK_200)
       }
 
       const playlistAttrs = { privacy: VideoPlaylistPrivacy.PRIVATE }
@@ -1068,11 +1125,11 @@ describe('Test video playlists', function () {
       await waitJobs(servers)
 
       for (const server of [ servers[1], servers[2] ]) {
-        await getVideoPlaylist(server.url, videoPlaylistIds.uuid, 404)
+        await getVideoPlaylist(server.url, videoPlaylistIds.uuid, HttpStatusCode.NOT_FOUND_404)
       }
-      await getVideoPlaylist(servers[0].url, videoPlaylistIds.uuid, 401)
+      await getVideoPlaylist(servers[0].url, videoPlaylistIds.uuid, HttpStatusCode.UNAUTHORIZED_401)
 
-      await getVideoPlaylistWithToken(servers[0].url, servers[0].accessToken, videoPlaylistIds.uuid, 200)
+      await getVideoPlaylistWithToken(servers[0].url, servers[0].accessToken, videoPlaylistIds.uuid, HttpStatusCode.OK_200)
     })
   })
 
@@ -1086,7 +1143,7 @@ describe('Test video playlists', function () {
       await waitJobs(servers)
 
       for (const server of servers) {
-        await getVideoPlaylist(server.url, playlistServer1UUID, 404)
+        await getVideoPlaylist(server.url, playlistServer1UUID, HttpStatusCode.NOT_FOUND_404)
       }
     })
 
@@ -1109,7 +1166,7 @@ describe('Test video playlists', function () {
         expect(finder(res.body.data)).to.not.be.undefined
       }
 
-      await unfollow(servers[2].url, servers[2].accessToken, servers[0])
+      await servers[2].followsCommand.unfollow({ target: servers[0] })
 
       {
         const res = await getVideoPlaylistsList(servers[2].url, 0, 5)
@@ -1146,7 +1203,7 @@ describe('Test video playlists', function () {
       expect(res3.body.displayName).to.equal('channel playlist')
       expect(res3.body.privacy.id).to.equal(VideoPlaylistPrivacy.PRIVATE)
 
-      await getVideoPlaylist(servers[1].url, videoPlaylistUUID, 404)
+      await getVideoPlaylist(servers[1].url, videoPlaylistUUID, HttpStatusCode.NOT_FOUND_404)
     })
 
     it('Should delete an account and delete its playlists', async function () {