From e3d15a6a9aed97a004d9dac1b7a6499d794e080a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 8 Jul 2021 11:17:55 +0200 Subject: Introduce blacklist command --- server/tests/api/check-params/video-blacklist.ts | 47 +++---- server/tests/api/live/live-save-replay.ts | 5 +- server/tests/api/live/live.ts | 3 +- server/tests/api/moderation/video-blacklist.ts | 144 ++++++++------------- .../api/notifications/moderation-notifications.ts | 12 +- server/tests/api/server/email.ts | 6 +- server/tests/api/users/users.ts | 3 +- server/tests/api/videos/video-playlists.ts | 6 +- server/tests/external-plugins/auto-block-videos.ts | 11 +- shared/extra-utils/server/servers.ts | 4 +- shared/extra-utils/videos/blacklist-command.ts | 77 +++++++++++ shared/extra-utils/videos/index.ts | 2 +- shared/extra-utils/videos/video-blacklist.ts | 79 ----------- 13 files changed, 173 insertions(+), 226 deletions(-) create mode 100644 shared/extra-utils/videos/blacklist-command.ts delete mode 100644 shared/extra-utils/videos/video-blacklist.ts diff --git a/server/tests/api/check-params/video-blacklist.ts b/server/tests/api/check-params/video-blacklist.ts index ce7f5fa17..98cf2e11a 100644 --- a/server/tests/api/check-params/video-blacklist.ts +++ b/server/tests/api/check-params/video-blacklist.ts @@ -1,32 +1,28 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ import 'mocha' - +import { expect } from 'chai' +import { HttpStatusCode } from '@shared/core-utils' import { + BlacklistCommand, + checkBadCountPagination, + checkBadSortPagination, + checkBadStartPagination, cleanupTests, createUser, doubleFollow, flushAndRunMultipleServers, - getBlacklistedVideosList, getVideo, getVideoWithToken, makePostBodyRequest, makePutBodyRequest, - removeVideoFromBlacklist, ServerInfo, setAccessTokensToServers, uploadVideo, userLogin, waitJobs -} from '../../../../shared/extra-utils' -import { - checkBadCountPagination, - checkBadSortPagination, - checkBadStartPagination -} from '../../../../shared/extra-utils/requests/check-api-params' -import { VideoBlacklistType, VideoDetails } from '../../../../shared/models/videos' -import { expect } from 'chai' -import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' +} from '@shared/extra-utils' +import { VideoBlacklistType, VideoDetails } from '@shared/models' describe('Test video blacklist API validators', function () { let servers: ServerInfo[] @@ -34,6 +30,7 @@ describe('Test video blacklist API validators', function () { let remoteVideoUUID: string let userAccessToken1 = '' let userAccessToken2 = '' + let command: BlacklistCommand // --------------------------------------------------------------- @@ -75,6 +72,8 @@ describe('Test video blacklist API validators', function () { } await waitJobs(servers) + + command = servers[0].blacklistCommand }) describe('When adding a video in blacklist', function () { @@ -234,25 +233,26 @@ describe('Test video blacklist API validators', function () { }) describe('When removing a video in blacklist', function () { + it('Should fail with a non authenticated user', async function () { - await removeVideoFromBlacklist(servers[0].url, 'fake token', servers[0].video.uuid, HttpStatusCode.UNAUTHORIZED_401) + await command.remove({ token: 'fake token', videoId: servers[0].video.uuid, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) }) it('Should fail with a non admin user', async function () { - await removeVideoFromBlacklist(servers[0].url, userAccessToken2, servers[0].video.uuid, HttpStatusCode.FORBIDDEN_403) + await command.remove({ token: userAccessToken2, videoId: servers[0].video.uuid, expectedStatus: HttpStatusCode.FORBIDDEN_403 }) }) it('Should fail with an incorrect id', async function () { - await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, 'hello', HttpStatusCode.BAD_REQUEST_400) + await command.remove({ videoId: 'hello', expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) }) it('Should fail with a not blacklisted video', async function () { // The video was not added to the blacklist so it should fail - await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, notBlacklistedVideoId, HttpStatusCode.NOT_FOUND_404) + await command.remove({ videoId: notBlacklistedVideoId, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) }) it('Should succeed with the correct params', async function () { - await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, servers[0].video.uuid, HttpStatusCode.NO_CONTENT_204) + await command.remove({ videoId: servers[0].video.uuid, expectedStatus: HttpStatusCode.NO_CONTENT_204 }) }) }) @@ -260,11 +260,11 @@ describe('Test video blacklist API validators', function () { const basePath = '/api/v1/videos/blacklist/' it('Should fail with a non authenticated user', async function () { - await getBlacklistedVideosList({ url: servers[0].url, token: 'fake token', specialStatus: HttpStatusCode.UNAUTHORIZED_401 }) + await servers[0].blacklistCommand.list({ token: 'fake token', expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) }) it('Should fail with a non admin user', async function () { - await getBlacklistedVideosList({ url: servers[0].url, token: userAccessToken2, specialStatus: HttpStatusCode.FORBIDDEN_403 }) + await servers[0].blacklistCommand.list({ token: userAccessToken2, expectedStatus: HttpStatusCode.FORBIDDEN_403 }) }) it('Should fail with a bad start pagination', async function () { @@ -280,16 +280,11 @@ describe('Test video blacklist API validators', function () { }) it('Should fail with an invalid type', async function () { - await getBlacklistedVideosList({ - url: servers[0].url, - token: servers[0].accessToken, - type: 0, - specialStatus: HttpStatusCode.BAD_REQUEST_400 - }) + await servers[0].blacklistCommand.list({ type: 0, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) }) it('Should succeed with the correct parameters', async function () { - await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, type: VideoBlacklistType.MANUAL }) + await servers[0].blacklistCommand.list({ type: VideoBlacklistType.MANUAL }) }) }) diff --git a/server/tests/api/live/live-save-replay.ts b/server/tests/api/live/live-save-replay.ts index 9acd5601d..363fb561c 100644 --- a/server/tests/api/live/live-save-replay.ts +++ b/server/tests/api/live/live-save-replay.ts @@ -6,7 +6,6 @@ import { FfmpegCommand } from 'fluent-ffmpeg' import { LiveVideoCreate, VideoDetails, VideoPrivacy, VideoState } from '@shared/models' import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' import { - addVideoToBlacklist, checkLiveCleanup, cleanupTests, ConfigCommand, @@ -172,7 +171,7 @@ describe('Save replay setting', function () { await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200) await Promise.all([ - addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideoUUID, 'bad live', true), + servers[0].blacklistCommand.add({ videoId: liveVideoUUID, reason: 'bad live', unfederate: true }), testFfmpegStreamError(ffmpegCommand, true) ]) @@ -280,7 +279,7 @@ describe('Save replay setting', function () { await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200) await Promise.all([ - addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideoUUID, 'bad live', true), + servers[0].blacklistCommand.add({ videoId: liveVideoUUID, reason: 'bad live', unfederate: true }), testFfmpegStreamError(ffmpegCommand, true) ]) diff --git a/server/tests/api/live/live.ts b/server/tests/api/live/live.ts index 5d70d8513..cb52e4431 100644 --- a/server/tests/api/live/live.ts +++ b/server/tests/api/live/live.ts @@ -7,7 +7,6 @@ import { ffprobePromise, getVideoStreamFromFile } from '@server/helpers/ffprobe- import { LiveVideo, LiveVideoCreate, Video, VideoDetails, VideoPrivacy, VideoState, VideoStreamingPlaylistType } from '@shared/models' import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' import { - addVideoToBlacklist, buildServerDirectory, checkLiveCleanup, checkLiveSegmentHash, @@ -347,7 +346,7 @@ describe('Test live', function () { liveVideo = await createLiveWrapper() - await addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideo.uuid) + await servers[0].blacklistCommand.add({ videoId: liveVideo.uuid }) const command = sendRTMPStream(rtmpUrl + '/live', liveVideo.streamKey) await testFfmpegStreamError(command, true) diff --git a/server/tests/api/moderation/video-blacklist.ts b/server/tests/api/moderation/video-blacklist.ts index 4a4930c98..17a68e4a6 100644 --- a/server/tests/api/moderation/video-blacklist.ts +++ b/server/tests/api/moderation/video-blacklist.ts @@ -4,22 +4,19 @@ import 'mocha' import * as chai from 'chai' import { orderBy } from 'lodash' import { - addVideoToBlacklist, + BlacklistCommand, cleanupTests, createUser, doubleFollow, flushAndRunMultipleServers, - getBlacklistedVideosList, getMyUserInformation, getMyVideos, getVideosList, killallServers, - removeVideoFromBlacklist, reRunServer, ServerInfo, setAccessTokensToServers, updateVideo, - updateVideoBlacklist, uploadVideo, userLogin, waitJobs @@ -32,13 +29,14 @@ const expect = chai.expect describe('Test video blacklist', function () { let servers: ServerInfo[] = [] let videoId: number + let command: BlacklistCommand async function blacklistVideosOnServer (server: ServerInfo) { const res = await getVideosList(server.url) const videos = res.body.data for (const video of videos) { - await addVideoToBlacklist(server.url, server.accessToken, video.id, 'super reason') + await server.blacklistCommand.add({ videoId: video.id, reason: 'super reason' }) } } @@ -61,6 +59,8 @@ describe('Test video blacklist', function () { // Wait videos propagation, server 2 has transcoding enabled await waitJobs(servers) + command = servers[0].blacklistCommand + // Blacklist the two videos on server 1 await blacklistVideosOnServer(servers[0]) }) @@ -77,7 +77,7 @@ describe('Test video blacklist', function () { } { - const body = await servers[0].searchCommand.searchVideos({ search: 'name' }) + const body = await servers[0].searchCommand.searchVideos({ search: 'video' }) expect(body.total).to.equal(0) expect(body.data).to.be.an('array') @@ -95,7 +95,7 @@ describe('Test video blacklist', function () { } { - const body = await servers[1].searchCommand.searchVideos({ search: 'name' }) + const body = await servers[1].searchCommand.searchVideos({ search: 'video' }) expect(body.total).to.equal(2) expect(body.data).to.be.an('array') @@ -106,11 +106,10 @@ describe('Test video blacklist', function () { describe('When listing manually blacklisted videos', function () { it('Should display all the blacklisted videos', async function () { - const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken }) - - expect(res.body.total).to.equal(2) + const body = await command.list() + expect(body.total).to.equal(2) - const blacklistedVideos = res.body.data + const blacklistedVideos = body.data expect(blacklistedVideos).to.be.an('array') expect(blacklistedVideos.length).to.equal(2) @@ -121,79 +120,66 @@ describe('Test video blacklist', function () { }) it('Should display all the blacklisted videos when applying manual type filter', async function () { - const res = await getBlacklistedVideosList({ - url: servers[0].url, - token: servers[0].accessToken, - type: VideoBlacklistType.MANUAL - }) + const body = await command.list({ type: VideoBlacklistType.MANUAL }) + expect(body.total).to.equal(2) - expect(res.body.total).to.equal(2) - - const blacklistedVideos = res.body.data + const blacklistedVideos = body.data expect(blacklistedVideos).to.be.an('array') expect(blacklistedVideos.length).to.equal(2) }) it('Should display nothing when applying automatic type filter', async function () { - const res = await getBlacklistedVideosList({ - url: servers[0].url, - token: servers[0].accessToken, - type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED - }) + const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED }) + expect(body.total).to.equal(0) - expect(res.body.total).to.equal(0) - - const blacklistedVideos = res.body.data + const blacklistedVideos = body.data expect(blacklistedVideos).to.be.an('array') expect(blacklistedVideos.length).to.equal(0) }) it('Should get the correct sort when sorting by descending id', async function () { - const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: '-id' }) - expect(res.body.total).to.equal(2) + const body = await command.list({ sort: '-id' }) + expect(body.total).to.equal(2) - const blacklistedVideos = res.body.data + const blacklistedVideos = body.data expect(blacklistedVideos).to.be.an('array') expect(blacklistedVideos.length).to.equal(2) - const result = orderBy(res.body.data, [ 'id' ], [ 'desc' ]) - + const result = orderBy(body.data, [ 'id' ], [ 'desc' ]) expect(blacklistedVideos).to.deep.equal(result) }) it('Should get the correct sort when sorting by descending video name', async function () { - const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: '-name' }) - expect(res.body.total).to.equal(2) + const body = await command.list({ sort: '-name' }) + expect(body.total).to.equal(2) - const blacklistedVideos = res.body.data + const blacklistedVideos = body.data expect(blacklistedVideos).to.be.an('array') expect(blacklistedVideos.length).to.equal(2) - const result = orderBy(res.body.data, [ 'name' ], [ 'desc' ]) - + const result = orderBy(body.data, [ 'name' ], [ 'desc' ]) expect(blacklistedVideos).to.deep.equal(result) }) it('Should get the correct sort when sorting by ascending creation date', async function () { - const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: 'createdAt' }) - expect(res.body.total).to.equal(2) + const body = await command.list({ sort: 'createdAt' }) + expect(body.total).to.equal(2) - const blacklistedVideos = res.body.data + const blacklistedVideos = body.data expect(blacklistedVideos).to.be.an('array') expect(blacklistedVideos.length).to.equal(2) - const result = orderBy(res.body.data, [ 'createdAt' ]) - + const result = orderBy(body.data, [ 'createdAt' ]) expect(blacklistedVideos).to.deep.equal(result) }) }) describe('When updating blacklisted videos', function () { it('Should change the reason', async function () { - await updateVideoBlacklist(servers[0].url, servers[0].accessToken, videoId, 'my super reason updated') + await command.update({ videoId, reason: 'my super reason updated' }) - const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: '-name' }) - const video = res.body.data.find(b => b.video.id === videoId) + const body = await command.list({ sort: '-name' }) + const video = body.data.find(b => b.video.id === videoId) expect(video.reason).to.equal('my super reason updated') }) @@ -228,12 +214,12 @@ describe('Test video blacklist', function () { it('Should remove a video from the blacklist on server 1', async function () { // Get one video in the blacklist - const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: '-name' }) - videoToRemove = res.body.data[0] - blacklist = res.body.data.slice(1) + const body = await command.list({ sort: '-name' }) + videoToRemove = body.data[0] + blacklist = body.data.slice(1) // Remove it - await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoToRemove.video.id) + await command.remove({ videoId: videoToRemove.video.id }) }) it('Should have the ex-blacklisted video in videos list on server 1', async function () { @@ -249,10 +235,10 @@ describe('Test video blacklist', function () { }) it('Should not have the ex-blacklisted video in videos blacklist list on server 1', async function () { - const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: '-name' }) - expect(res.body.total).to.equal(1) + const body = await command.list({ sort: '-name' }) + expect(body.total).to.equal(1) - const videos = res.body.data + const videos = body.data expect(videos).to.be.an('array') expect(videos.length).to.equal(1) expect(videos).to.deep.equal(blacklist) @@ -281,7 +267,7 @@ describe('Test video blacklist', function () { it('Should blacklist video 3 and keep it federated', async function () { this.timeout(10000) - await addVideoToBlacklist(servers[0].url, servers[0].accessToken, video3UUID, 'super reason', false) + await command.add({ videoId: video3UUID, reason: 'super reason', unfederate: false }) await waitJobs(servers) @@ -299,7 +285,7 @@ describe('Test video blacklist', function () { it('Should unfederate the video', async function () { this.timeout(10000) - await addVideoToBlacklist(servers[0].url, servers[0].accessToken, video4UUID, 'super reason', true) + await command.add({ videoId: video4UUID, reason: 'super reason', unfederate: true }) await waitJobs(servers) @@ -323,9 +309,9 @@ describe('Test video blacklist', function () { }) it('Should have the correct video blacklist unfederate attribute', async function () { - const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, sort: 'createdAt' }) + const body = await command.list({ sort: 'createdAt' }) - const blacklistedVideos: VideoBlacklist[] = res.body.data + const blacklistedVideos = body.data const video3Blacklisted = blacklistedVideos.find(b => b.video.uuid === video3UUID) const video4Blacklisted = blacklistedVideos.find(b => b.video.uuid === video4UUID) @@ -336,7 +322,7 @@ describe('Test video blacklist', function () { it('Should remove the video from blacklist and refederate the video', async function () { this.timeout(10000) - await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, video4UUID) + await command.remove({ videoId: video4UUID }) await waitJobs(servers) @@ -407,14 +393,9 @@ describe('Test video blacklist', function () { it('Should auto blacklist a video on upload', async function () { await uploadVideo(servers[0].url, userWithoutFlag, { name: 'blacklisted' }) - const res = await getBlacklistedVideosList({ - url: servers[0].url, - token: servers[0].accessToken, - type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED - }) - - expect(res.body.total).to.equal(1) - expect(res.body.data[0].video.name).to.equal('blacklisted') + const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED }) + expect(body.total).to.equal(1) + expect(body.data[0].video.name).to.equal('blacklisted') }) it('Should auto blacklist a video on URL import', async function () { @@ -427,15 +408,9 @@ describe('Test video blacklist', function () { } await importVideo(servers[0].url, userWithoutFlag, attributes) - const res = await getBlacklistedVideosList({ - url: servers[0].url, - token: servers[0].accessToken, - sort: 'createdAt', - type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED - }) - - expect(res.body.total).to.equal(2) - expect(res.body.data[1].video.name).to.equal('URL import') + const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED }) + expect(body.total).to.equal(2) + expect(body.data[1].video.name).to.equal('URL import') }) it('Should auto blacklist a video on torrent import', async function () { @@ -446,27 +421,16 @@ describe('Test video blacklist', function () { } await importVideo(servers[0].url, userWithoutFlag, attributes) - const res = await getBlacklistedVideosList({ - url: servers[0].url, - token: servers[0].accessToken, - sort: 'createdAt', - type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED - }) - - expect(res.body.total).to.equal(3) - expect(res.body.data[2].video.name).to.equal('Torrent import') + const body = await command.list({ sort: 'createdAt', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED }) + expect(body.total).to.equal(3) + expect(body.data[2].video.name).to.equal('Torrent import') }) it('Should not auto blacklist a video on upload if the user has the bypass blacklist flag', async function () { await uploadVideo(servers[0].url, userWithFlag, { name: 'not blacklisted' }) - const res = await getBlacklistedVideosList({ - url: servers[0].url, - token: servers[0].accessToken, - type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED - }) - - expect(res.body.total).to.equal(3) + const body = await command.list({ type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED }) + expect(body.total).to.equal(3) }) }) diff --git a/server/tests/api/notifications/moderation-notifications.ts b/server/tests/api/notifications/moderation-notifications.ts index 605b41947..52ade0548 100644 --- a/server/tests/api/notifications/moderation-notifications.ts +++ b/server/tests/api/notifications/moderation-notifications.ts @@ -4,7 +4,6 @@ import 'mocha' import { buildUUID } from '@server/helpers/uuid' import { addVideoCommentThread, - addVideoToBlacklist, checkAbuseStateChange, checkAutoInstanceFollowing, CheckerBaseParams, @@ -28,7 +27,6 @@ import { MockSmtpServer, prepareNotificationsTest, registerUser, - removeVideoFromBlacklist, ServerInfo, uploadVideo, wait, @@ -297,7 +295,7 @@ describe('Test moderation notifications', function () { const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) const uuid = resVideo.body.video.uuid - await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid) + await servers[0].blacklistCommand.add({ videoId: uuid }) await waitJobs(servers) await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist') @@ -310,10 +308,10 @@ describe('Test moderation notifications', function () { const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) const uuid = resVideo.body.video.uuid - await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid) + await servers[0].blacklistCommand.add({ videoId: uuid }) await waitJobs(servers) - await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid) + await servers[0].blacklistCommand.remove({ videoId: uuid }) await waitJobs(servers) await wait(500) @@ -517,7 +515,7 @@ describe('Test moderation notifications', function () { it('Should send video published and unblacklist after video unblacklisted', async function () { this.timeout(40000) - await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoUUID) + await servers[0].blacklistCommand.remove({ videoId: videoUUID }) await waitJobs(servers) @@ -554,7 +552,7 @@ describe('Test moderation notifications', function () { const resVideo = await uploadVideo(servers[0].url, userAccessToken, data) const uuid = resVideo.body.video.uuid - await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid) + await servers[0].blacklistCommand.remove({ videoId: uuid }) await waitJobs(servers) await checkNewBlacklistOnMyVideo(userBaseParams, uuid, name, 'unblacklist') diff --git a/server/tests/api/server/email.ts b/server/tests/api/server/email.ts index 85844ae9d..5d997713b 100644 --- a/server/tests/api/server/email.ts +++ b/server/tests/api/server/email.ts @@ -4,14 +4,12 @@ import 'mocha' import * as chai from 'chai' import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' import { - addVideoToBlacklist, askResetPassword, askSendVerifyEmail, blockUser, cleanupTests, createUser, flushAndRunServer, - removeVideoFromBlacklist, resetPassword, ServerInfo, setAccessTokensToServers, @@ -248,7 +246,7 @@ describe('Test emails', function () { this.timeout(10000) const reason = 'my super reason' - await addVideoToBlacklist(server.url, server.accessToken, videoUserUUID, reason) + await server.blacklistCommand.add({ videoId: videoUserUUID, reason }) await waitJobs(server) expect(emails).to.have.lengthOf(6) @@ -266,7 +264,7 @@ describe('Test emails', function () { it('Should send the notification email', async function () { this.timeout(10000) - await removeVideoFromBlacklist(server.url, server.accessToken, videoUserUUID) + await server.blacklistCommand.remove({ videoId: videoUserUUID }) await waitJobs(server) expect(emails).to.have.lengthOf(7) diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.ts index 3beea5d50..4b9056306 100644 --- a/server/tests/api/users/users.ts +++ b/server/tests/api/users/users.ts @@ -11,7 +11,6 @@ import { createUser, deleteMe, flushAndRunServer, - getBlacklistedVideosList, getMyUserInformation, getMyUserVideoQuotaUsed, getMyUserVideoRating, @@ -808,7 +807,7 @@ describe('Test users', function () { describe('Video blacklists', function () { it('Should be able to list video blacklist by a moderator', async function () { - await getBlacklistedVideosList({ url: server.url, token: accessTokenUser }) + await server.blacklistCommand.list({ token: accessTokenUser }) }) }) diff --git a/server/tests/api/videos/video-playlists.ts b/server/tests/api/videos/video-playlists.ts index 90189721a..069450453 100644 --- a/server/tests/api/videos/video-playlists.ts +++ b/server/tests/api/videos/video-playlists.ts @@ -6,7 +6,6 @@ import { HttpStatusCode } from '@shared/core-utils' import { addVideoChannel, addVideoInPlaylist, - addVideoToBlacklist, checkPlaylistFilesWereRemoved, cleanupTests, createUser, @@ -28,7 +27,6 @@ import { getVideoPlaylistsList, getVideoPlaylistWithToken, removeUser, - removeVideoFromBlacklist, removeVideoFromPlaylist, reorderVideosPlaylist, ServerInfo, @@ -728,7 +726,7 @@ describe('Test video playlists', function () { const position = 1 { - await addVideoToBlacklist(servers[0].url, servers[0].accessToken, video1, 'reason', true) + await servers[0].blacklistCommand.add({ videoId: video1, reason: 'reason', unfederate: true }) await waitJobs(servers) await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3) @@ -738,7 +736,7 @@ describe('Test video playlists', function () { } { - await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, video1) + await servers[0].blacklistCommand.remove({ videoId: video1 }) await waitJobs(servers) await checkPlaylistElementType(groupUser1, playlistServer1UUID2, VideoPlaylistElementType.REGULAR, position, name, 3) diff --git a/server/tests/external-plugins/auto-block-videos.ts b/server/tests/external-plugins/auto-block-videos.ts index 6baf37566..6659b6f39 100644 --- a/server/tests/external-plugins/auto-block-videos.ts +++ b/server/tests/external-plugins/auto-block-videos.ts @@ -2,13 +2,11 @@ import 'mocha' import { expect } from 'chai' -import { Video, VideoBlacklist } from '@shared/models' +import { Video } from '@shared/models' import { doubleFollow, - getBlacklistedVideosList, getVideosList, MockBlocklist, - removeVideoFromBlacklist, setAccessTokensToServers, uploadVideoAndGetId, wait @@ -97,10 +95,9 @@ describe('Official plugin auto-block videos', function () { }) it('Should have video in blacklists', async function () { - const res = await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken }) - - const videoBlacklists = res.body.data as VideoBlacklist[] + const body = await servers[0].blacklistCommand.list() + const videoBlacklists = body.data expect(videoBlacklists).to.have.lengthOf(1) expect(videoBlacklists[0].reason).to.contains('Automatically blocked from auto block plugin') expect(videoBlacklists[0].video.name).to.equal(server2Videos[0].name) @@ -163,7 +160,7 @@ describe('Official plugin auto-block videos', function () { await check(servers[0], video.uuid, false) - await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, video.uuid) + await servers[0].blacklistCommand.remove({ videoId: video.uuid }) await check(servers[0], video.uuid, true) diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index cb2a8814b..a4432902f 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts @@ -18,7 +18,7 @@ import { makeGetRequest } from '../requests/requests' import { SearchCommand } from '../search' import { SocketIOCommand } from '../socket' import { AccountsCommand, BlocklistCommand, SubscriptionsCommand } from '../users' -import { LiveCommand, ServicesCommand } from '../videos' +import { LiveCommand, ServicesCommand, BlacklistCommand } from '../videos' import { ConfigCommand } from './config-command' import { ContactFormCommand } from './contact-form-command' import { DebugCommand } from './debug-command' @@ -102,6 +102,7 @@ interface ServerInfo { subscriptionsCommand?: SubscriptionsCommand liveCommand?: LiveCommand servicesCommand?: ServicesCommand + blacklistCommand?: BlacklistCommand } function parallelTests () { @@ -329,6 +330,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = [] server.subscriptionsCommand = new SubscriptionsCommand(server) server.liveCommand = new LiveCommand(server) server.servicesCommand = new ServicesCommand(server) + server.blacklistCommand = new BlacklistCommand(server) res(server) }) diff --git a/shared/extra-utils/videos/blacklist-command.ts b/shared/extra-utils/videos/blacklist-command.ts new file mode 100644 index 000000000..fdae6b469 --- /dev/null +++ b/shared/extra-utils/videos/blacklist-command.ts @@ -0,0 +1,77 @@ + +import { ResultList } from '@shared/models' +import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { VideoBlacklist, VideoBlacklistType } from '../../models/videos' +import { AbstractCommand, OverrideCommandOptions } from '../shared' + +export class BlacklistCommand extends AbstractCommand { + + add (options: OverrideCommandOptions & { + videoId: number | string + reason?: string + unfederate?: boolean + }) { + const { videoId, reason, unfederate } = options + const path = '/api/v1/videos/' + videoId + '/blacklist' + + return this.postBodyRequest({ + ...options, + + path, + fields: { reason, unfederate }, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + + update (options: OverrideCommandOptions & { + videoId: number | string + reason?: string + }) { + const { videoId, reason } = options + const path = '/api/v1/videos/' + videoId + '/blacklist' + + return this.putBodyRequest({ + ...options, + + path, + fields: { reason }, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + + remove (options: OverrideCommandOptions & { + videoId: number | string + }) { + const { videoId } = options + const path = '/api/v1/videos/' + videoId + '/blacklist' + + return this.deleteRequest({ + ...options, + + path, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + + list (options: OverrideCommandOptions & { + sort?: string + type?: VideoBlacklistType + } = {}) { + const { sort, type } = options + const path = '/api/v1/videos/blacklist/' + + const query = { sort, type } + + return this.getRequestBody>({ + ...options, + + path, + query, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } +} diff --git a/shared/extra-utils/videos/index.ts b/shared/extra-utils/videos/index.ts index fe5dc6655..67f5faf54 100644 --- a/shared/extra-utils/videos/index.ts +++ b/shared/extra-utils/videos/index.ts @@ -1,7 +1,7 @@ +export * from './blacklist-command' export * from './live-command' export * from './live' export * from './services-command' -export * from './video-blacklist' export * from './video-captions' export * from './video-change-ownership' export * from './video-channels' diff --git a/shared/extra-utils/videos/video-blacklist.ts b/shared/extra-utils/videos/video-blacklist.ts deleted file mode 100644 index aa1548537..000000000 --- a/shared/extra-utils/videos/video-blacklist.ts +++ /dev/null @@ -1,79 +0,0 @@ -import * as request from 'supertest' -import { VideoBlacklistType } from '../../models/videos' -import { makeGetRequest } from '..' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' - -function addVideoToBlacklist ( - url: string, - token: string, - videoId: number | string, - reason?: string, - unfederate?: boolean, - specialStatus = HttpStatusCode.NO_CONTENT_204 -) { - const path = '/api/v1/videos/' + videoId + '/blacklist' - - return request(url) - .post(path) - .send({ reason, unfederate }) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + token) - .expect(specialStatus) -} - -function updateVideoBlacklist ( - url: string, - token: string, - videoId: number, - reason?: string, - specialStatus = HttpStatusCode.NO_CONTENT_204 -) { - const path = '/api/v1/videos/' + videoId + '/blacklist' - - return request(url) - .put(path) - .send({ reason }) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + token) - .expect(specialStatus) -} - -function removeVideoFromBlacklist (url: string, token: string, videoId: number | string, specialStatus = HttpStatusCode.NO_CONTENT_204) { - const path = '/api/v1/videos/' + videoId + '/blacklist' - - return request(url) - .delete(path) - .set('Accept', 'application/json') - .set('Authorization', 'Bearer ' + token) - .expect(specialStatus) -} - -function getBlacklistedVideosList (parameters: { - url: string - token: string - sort?: string - type?: VideoBlacklistType - specialStatus?: HttpStatusCode -}) { - const { url, token, sort, type, specialStatus = HttpStatusCode.OK_200 } = parameters - const path = '/api/v1/videos/blacklist/' - - const query = { sort, type } - - return makeGetRequest({ - url, - path, - query, - token, - statusCodeExpected: specialStatus - }) -} - -// --------------------------------------------------------------------------- - -export { - addVideoToBlacklist, - removeVideoFromBlacklist, - getBlacklistedVideosList, - updateVideoBlacklist -} -- cgit v1.2.3