X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftests%2Fapi%2Fmoderation%2Fblocklist.ts;h=27592e8c5723079a0fd7b697742789dde35c394c;hb=863477173efa82a4412d2e522aa46462249f9199;hp=00cb6c65ce77ca26cd460abb6f5576509de5df3d;hpb=12edc1495a36b2199f1bf1ba37f50c7b694be382;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tests/api/moderation/blocklist.ts b/server/tests/api/moderation/blocklist.ts index 00cb6c65c..27592e8c5 100644 --- a/server/tests/api/moderation/blocklist.ts +++ b/server/tests/api/moderation/blocklist.ts @@ -1,67 +1,56 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ -import 'mocha' -import * as chai from 'chai' +import { expect } from 'chai' +import { UserNotificationType } from '@shared/models' import { BlocklistCommand, cleanupTests, CommentsCommand, - createUser, + createMultipleServers, doubleFollow, - flushAndRunMultipleServers, - getUserNotifications, - getVideosList, - getVideosListWithToken, - ServerInfo, + PeerTubeServer, setAccessTokensToServers, - uploadVideo, - userLogin, + setDefaultAccountAvatar, waitJobs -} from '@shared/extra-utils' -import { UserNotification, UserNotificationType, Video } from '@shared/models' +} from '@shared/server-commands' -const expect = chai.expect - -async function checkAllVideos (server: ServerInfo, token: string) { +async function checkAllVideos (server: PeerTubeServer, token: string) { { - const res = await getVideosListWithToken(server.url, token) - - expect(res.body.data).to.have.lengthOf(5) + const { data } = await server.videos.listWithToken({ token }) + expect(data).to.have.lengthOf(5) } { - const res = await getVideosList(server.url) - - expect(res.body.data).to.have.lengthOf(5) + const { data } = await server.videos.list() + expect(data).to.have.lengthOf(5) } } -async function checkAllComments (server: ServerInfo, token: string, videoUUID: string) { - const { data } = await server.commentsCommand.listThreads({ videoId: videoUUID, start: 0, count: 25, sort: '-createdAt', token }) +async function checkAllComments (server: PeerTubeServer, token: string, videoUUID: string) { + const { data } = await server.comments.listThreads({ videoId: videoUUID, start: 0, count: 25, sort: '-createdAt', token }) const threads = data.filter(t => t.isDeleted === false) expect(threads).to.have.lengthOf(2) for (const thread of threads) { - const tree = await server.commentsCommand.getThread({ videoId: videoUUID, threadId: thread.id, token }) + const tree = await server.comments.getThread({ videoId: videoUUID, threadId: thread.id, token }) expect(tree.children).to.have.lengthOf(1) } } async function checkCommentNotification ( - mainServer: ServerInfo, - comment: { server: ServerInfo, token: string, videoUUID: string, text: string }, + mainServer: PeerTubeServer, + comment: { server: PeerTubeServer, token: string, videoUUID: string, text: string }, check: 'presence' | 'absence' ) { - const command = comment.server.commentsCommand + const command = comment.server.comments const { threadId, createdAt } = await command.createThread({ token: comment.token, videoId: comment.videoUUID, text: comment.text }) await waitJobs([ mainServer, comment.server ]) - const res = await getUserNotifications(mainServer.url, mainServer.accessToken, 0, 30) - const commentNotifications = (res.body.data as UserNotification[]) - .filter(n => n.comment && n.comment.video.uuid === comment.videoUUID && n.createdAt >= createdAt) + const { data } = await mainServer.notifications.list({ start: 0, count: 30 }) + const commentNotifications = data.filter(n => n.comment && n.comment.video.uuid === comment.videoUUID && n.createdAt >= createdAt) if (check === 'presence') expect(commentNotifications).to.have.lengthOf(1) else expect(commentNotifications).to.have.lengthOf(0) @@ -72,7 +61,7 @@ async function checkCommentNotification ( } describe('Test blocklist', function () { - let servers: ServerInfo[] + let servers: PeerTubeServer[] let videoUUID1: string let videoUUID2: string let videoUUID3: string @@ -86,48 +75,49 @@ describe('Test blocklist', function () { before(async function () { this.timeout(120000) - servers = await flushAndRunMultipleServers(3) + servers = await createMultipleServers(3) await setAccessTokensToServers(servers) + await setDefaultAccountAvatar(servers) - command = servers[0].blocklistCommand - commentsCommand = servers.map(s => s.commentsCommand) + command = servers[0].blocklist + commentsCommand = servers.map(s => s.comments) { const user = { username: 'user1', password: 'password' } - await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password }) + await servers[0].users.create({ username: user.username, password: user.password }) - userToken1 = await userLogin(servers[0], user) - await uploadVideo(servers[0].url, userToken1, { name: 'video user 1' }) + userToken1 = await servers[0].login.getAccessToken(user) + await servers[0].videos.upload({ token: userToken1, attributes: { name: 'video user 1' } }) } { const user = { username: 'moderator', password: 'password' } - await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password }) + await servers[0].users.create({ username: user.username, password: user.password }) - userModeratorToken = await userLogin(servers[0], user) + userModeratorToken = await servers[0].login.getAccessToken(user) } { const user = { username: 'user2', password: 'password' } - await createUser({ url: servers[1].url, accessToken: servers[1].accessToken, username: user.username, password: user.password }) + await servers[1].users.create({ username: user.username, password: user.password }) - userToken2 = await userLogin(servers[1], user) - await uploadVideo(servers[1].url, userToken2, { name: 'video user 2' }) + userToken2 = await servers[1].login.getAccessToken(user) + await servers[1].videos.upload({ token: userToken2, attributes: { name: 'video user 2' } }) } { - const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video server 1' }) - videoUUID1 = res.body.video.uuid + const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video server 1' } }) + videoUUID1 = uuid } { - const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video server 2' }) - videoUUID2 = res.body.video.uuid + const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video server 2' } }) + videoUUID2 = uuid } { - const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 2 server 1' }) - videoUUID3 = res.body.video.uuid + const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 2 server 1' } }) + videoUUID3 = uuid } await doubleFollow(servers[0], servers[1]) @@ -168,12 +158,11 @@ describe('Test blocklist', function () { }) it('Should hide its videos', async function () { - const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken) + const { data } = await servers[0].videos.listWithToken() - const videos: Video[] = res.body.data - expect(videos).to.have.lengthOf(4) + expect(data).to.have.lengthOf(4) - const v = videos.find(v => v.name === 'video user 2') + const v = data.find(v => v.name === 'video user 2') expect(v).to.be.undefined }) @@ -182,12 +171,11 @@ describe('Test blocklist', function () { }) it('Should hide its videos', async function () { - const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken) + const { data } = await servers[0].videos.listWithToken() - const videos: Video[] = res.body.data - expect(videos).to.have.lengthOf(3) + expect(data).to.have.lengthOf(3) - const v = videos.find(v => v.name === 'video user 1') + const v = data.find(v => v.name === 'video user 1') expect(v).to.be.undefined }) @@ -265,6 +253,52 @@ describe('Test blocklist', function () { } }) + it('Should search blocked accounts', async function () { + const body = await command.listMyAccountBlocklist({ start: 0, count: 10, search: 'user2' }) + expect(body.total).to.equal(1) + + expect(body.data[0].blockedAccount.name).to.equal('user2') + }) + + it('Should get blocked status', async function () { + const remoteHandle = 'user2@' + servers[1].host + const localHandle = 'user1@' + servers[0].host + const unknownHandle = 'user5@' + servers[0].host + + { + const status = await command.getStatus({ accounts: [ remoteHandle ] }) + expect(Object.keys(status.accounts)).to.have.lengthOf(1) + expect(status.accounts[remoteHandle].blockedByUser).to.be.false + expect(status.accounts[remoteHandle].blockedByServer).to.be.false + + expect(Object.keys(status.hosts)).to.have.lengthOf(0) + } + + { + const status = await command.getStatus({ token: servers[0].accessToken, accounts: [ remoteHandle ] }) + expect(Object.keys(status.accounts)).to.have.lengthOf(1) + expect(status.accounts[remoteHandle].blockedByUser).to.be.true + expect(status.accounts[remoteHandle].blockedByServer).to.be.false + + expect(Object.keys(status.hosts)).to.have.lengthOf(0) + } + + { + const status = await command.getStatus({ token: servers[0].accessToken, accounts: [ localHandle, remoteHandle, unknownHandle ] }) + expect(Object.keys(status.accounts)).to.have.lengthOf(3) + + for (const handle of [ localHandle, remoteHandle ]) { + expect(status.accounts[handle].blockedByUser).to.be.true + expect(status.accounts[handle].blockedByServer).to.be.false + } + + expect(status.accounts[unknownHandle].blockedByUser).to.be.false + expect(status.accounts[unknownHandle].blockedByServer).to.be.false + + expect(Object.keys(status.hosts)).to.have.lengthOf(0) + } + }) + it('Should not allow a remote blocked user to comment my videos', async function () { this.timeout(60000) @@ -300,12 +334,12 @@ describe('Test blocklist', function () { // Server 1 and 3 should only have uploader comments for (const server of [ servers[0], servers[2] ]) { - const { data } = await server.commentsCommand.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' }) + const { data } = await server.comments.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' }) expect(data).to.have.lengthOf(1) expect(data[0].text).to.equal('uploader') - const tree = await server.commentsCommand.getThread({ videoId: videoUUID3, threadId: data[0].id }) + const tree = await server.comments.getThread({ videoId: videoUUID3, threadId: data[0].id }) if (server.serverNumber === 1) expect(tree.children).to.have.lengthOf(0) else expect(tree.children).to.have.lengthOf(1) @@ -317,18 +351,16 @@ describe('Test blocklist', function () { }) it('Should display its videos', async function () { - const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken) + const { data } = await servers[0].videos.listWithToken() + expect(data).to.have.lengthOf(4) - const videos: Video[] = res.body.data - expect(videos).to.have.lengthOf(4) - - const v = videos.find(v => v.name === 'video user 2') + const v = data.find(v => v.name === 'video user 2') expect(v).not.to.be.undefined }) it('Should display its comments on my video', async function () { for (const server of servers) { - const { data } = await server.commentsCommand.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' }) + const { data } = await server.comments.listThreads({ videoId: videoUUID3, count: 25, sort: '-createdAt' }) // Server 3 should not have 2 comment threads, because server 1 did not forward the server 2 comment if (server.serverNumber === 3) { @@ -340,7 +372,7 @@ describe('Test blocklist', function () { expect(data[0].text).to.equal('uploader') expect(data[1].text).to.equal('comment user 2') - const tree = await server.commentsCommand.getThread({ videoId: videoUUID3, threadId: data[0].id }) + const tree = await server.comments.getThread({ videoId: videoUUID3, threadId: data[0].id }) expect(tree.children).to.have.lengthOf(1) expect(tree.children[0].comment.text).to.equal('reply by user 2') expect(tree.children[0].children).to.have.lengthOf(1) @@ -391,13 +423,12 @@ describe('Test blocklist', function () { }) it('Should hide its videos', async function () { - const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken) + const { data } = await servers[0].videos.listWithToken() - const videos: Video[] = res.body.data - expect(videos).to.have.lengthOf(3) + expect(data).to.have.lengthOf(3) - const v1 = videos.find(v => v.name === 'video user 2') - const v2 = videos.find(v => v.name === 'video server 2') + const v1 = data.find(v => v.name === 'video user 2') + const v2 = data.find(v => v.name === 'video server 2') expect(v1).to.be.undefined expect(v2).to.be.undefined @@ -448,6 +479,42 @@ describe('Test blocklist', function () { expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port) }) + it('Should search blocked servers', async function () { + const body = await command.listMyServerBlocklist({ start: 0, count: 10, search: servers[1].host }) + expect(body.total).to.equal(1) + + expect(body.data[0].blockedServer.host).to.equal(servers[1].host) + }) + + it('Should get blocklist status', async function () { + const blockedServer = servers[1].host + const notBlockedServer = 'example.com' + + { + const status = await command.getStatus({ hosts: [ blockedServer, notBlockedServer ] }) + expect(Object.keys(status.accounts)).to.have.lengthOf(0) + + expect(Object.keys(status.hosts)).to.have.lengthOf(2) + expect(status.hosts[blockedServer].blockedByUser).to.be.false + expect(status.hosts[blockedServer].blockedByServer).to.be.false + + expect(status.hosts[notBlockedServer].blockedByUser).to.be.false + expect(status.hosts[notBlockedServer].blockedByServer).to.be.false + } + + { + const status = await command.getStatus({ token: servers[0].accessToken, hosts: [ blockedServer, notBlockedServer ] }) + expect(Object.keys(status.accounts)).to.have.lengthOf(0) + + expect(Object.keys(status.hosts)).to.have.lengthOf(2) + expect(status.hosts[blockedServer].blockedByUser).to.be.true + expect(status.hosts[blockedServer].blockedByServer).to.be.false + + expect(status.hosts[notBlockedServer].blockedByUser).to.be.false + expect(status.hosts[notBlockedServer].blockedByServer).to.be.false + } + }) + it('Should unblock the remote server', async function () { await command.removeFromMyBlocklist({ server: 'localhost:' + servers[1].port }) }) @@ -502,12 +569,11 @@ describe('Test blocklist', function () { it('Should hide its videos', async function () { for (const token of [ userModeratorToken, servers[0].accessToken ]) { - const res = await getVideosListWithToken(servers[0].url, token) + const { data } = await servers[0].videos.listWithToken({ token }) - const videos: Video[] = res.body.data - expect(videos).to.have.lengthOf(4) + expect(data).to.have.lengthOf(4) - const v = videos.find(v => v.name === 'video user 2') + const v = data.find(v => v.name === 'video user 2') expect(v).to.be.undefined } }) @@ -518,12 +584,11 @@ describe('Test blocklist', function () { it('Should hide its videos', async function () { for (const token of [ userModeratorToken, servers[0].accessToken ]) { - const res = await getVideosListWithToken(servers[0].url, token) + const { data } = await servers[0].videos.listWithToken({ token }) - const videos: Video[] = res.body.data - expect(videos).to.have.lengthOf(3) + expect(data).to.have.lengthOf(3) - const v = videos.find(v => v.name === 'video user 1') + const v = data.find(v => v.name === 'video user 1') expect(v).to.be.undefined } }) @@ -591,18 +656,44 @@ describe('Test blocklist', function () { } }) + it('Should search blocked accounts', async function () { + const body = await command.listServerAccountBlocklist({ start: 0, count: 10, search: 'user2' }) + expect(body.total).to.equal(1) + + expect(body.data[0].blockedAccount.name).to.equal('user2') + }) + + it('Should get blocked status', async function () { + const remoteHandle = 'user2@' + servers[1].host + const localHandle = 'user1@' + servers[0].host + const unknownHandle = 'user5@' + servers[0].host + + for (const token of [ undefined, servers[0].accessToken ]) { + const status = await command.getStatus({ token, accounts: [ localHandle, remoteHandle, unknownHandle ] }) + expect(Object.keys(status.accounts)).to.have.lengthOf(3) + + for (const handle of [ localHandle, remoteHandle ]) { + expect(status.accounts[handle].blockedByUser).to.be.false + expect(status.accounts[handle].blockedByServer).to.be.true + } + + expect(status.accounts[unknownHandle].blockedByUser).to.be.false + expect(status.accounts[unknownHandle].blockedByServer).to.be.false + + expect(Object.keys(status.hosts)).to.have.lengthOf(0) + } + }) + it('Should unblock the remote account', async function () { await command.removeFromServerBlocklist({ account: 'user2@localhost:' + servers[1].port }) }) it('Should display its videos', async function () { for (const token of [ userModeratorToken, servers[0].accessToken ]) { - const res = await getVideosListWithToken(servers[0].url, token) + const { data } = await servers[0].videos.listWithToken({ token }) + expect(data).to.have.lengthOf(4) - const videos: Video[] = res.body.data - expect(videos).to.have.lengthOf(4) - - const v = videos.find(v => v.name === 'video user 2') + const v = data.find(v => v.name === 'video user 2') expect(v).not.to.be.undefined } }) @@ -638,6 +729,7 @@ describe('Test blocklist', function () { }) describe('When managing server blocklist', function () { + it('Should list all videos', async function () { for (const token of [ userModeratorToken, servers[0].accessToken ]) { await checkAllVideos(servers[0], token) @@ -656,15 +748,17 @@ describe('Test blocklist', function () { it('Should hide its videos', async function () { for (const token of [ userModeratorToken, servers[0].accessToken ]) { - const res1 = await getVideosList(servers[0].url) - const res2 = await getVideosListWithToken(servers[0].url, token) + const requests = [ + servers[0].videos.list(), + servers[0].videos.listWithToken({ token }) + ] - for (const res of [ res1, res2 ]) { - const videos: Video[] = res.body.data - expect(videos).to.have.lengthOf(3) + for (const req of requests) { + const { data } = await req + expect(data).to.have.lengthOf(3) - const v1 = videos.find(v => v.name === 'video user 2') - const v2 = videos.find(v => v.name === 'video server 2') + const v1 = data.find(v => v.name === 'video user 2') + const v2 = data.find(v => v.name === 'video server 2') expect(v1).to.be.undefined expect(v2).to.be.undefined @@ -704,18 +798,16 @@ describe('Test blocklist', function () { { const now = new Date() - await servers[1].followsCommand.unfollow({ target: servers[0] }) + await servers[1].follows.unfollow({ target: servers[0] }) await waitJobs(servers) - await servers[1].followsCommand.follow({ targets: [ servers[0].host ] }) + await servers[1].follows.follow({ hosts: [ servers[0].host ] }) await waitJobs(servers) - const res = await getUserNotifications(servers[0].url, servers[0].accessToken, 0, 30) - const commentNotifications = (res.body.data as UserNotification[]) - .filter(n => { - return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER && - n.createdAt >= now.toISOString() - }) + const { data } = await servers[0].notifications.list({ start: 0, count: 30 }) + const commentNotifications = data.filter(n => { + return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER && n.createdAt >= now.toISOString() + }) expect(commentNotifications).to.have.lengthOf(0) } @@ -731,6 +823,30 @@ describe('Test blocklist', function () { expect(block.blockedServer.host).to.equal('localhost:' + servers[1].port) }) + it('Should search blocked servers', async function () { + const body = await command.listServerServerBlocklist({ start: 0, count: 10, search: servers[1].host }) + expect(body.total).to.equal(1) + + expect(body.data[0].blockedServer.host).to.equal(servers[1].host) + }) + + it('Should get blocklist status', async function () { + const blockedServer = servers[1].host + const notBlockedServer = 'example.com' + + for (const token of [ undefined, servers[0].accessToken ]) { + const status = await command.getStatus({ token, hosts: [ blockedServer, notBlockedServer ] }) + expect(Object.keys(status.accounts)).to.have.lengthOf(0) + + expect(Object.keys(status.hosts)).to.have.lengthOf(2) + expect(status.hosts[blockedServer].blockedByUser).to.be.false + expect(status.hosts[blockedServer].blockedByServer).to.be.true + + expect(status.hosts[notBlockedServer].blockedByUser).to.be.false + expect(status.hosts[notBlockedServer].blockedByServer).to.be.false + } + }) + it('Should unblock the remote server', async function () { await command.removeFromServerBlocklist({ server: 'localhost:' + servers[1].port }) }) @@ -767,18 +883,16 @@ describe('Test blocklist', function () { { const now = new Date() - await servers[1].followsCommand.unfollow({ target: servers[0] }) + await servers[1].follows.unfollow({ target: servers[0] }) await waitJobs(servers) - await servers[1].followsCommand.follow({ targets: [ servers[0].host ] }) + await servers[1].follows.follow({ hosts: [ servers[0].host ] }) await waitJobs(servers) - const res = await getUserNotifications(servers[0].url, servers[0].accessToken, 0, 30) - const commentNotifications = (res.body.data as UserNotification[]) - .filter(n => { - return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER && - n.createdAt >= now.toISOString() - }) + const { data } = await servers[0].notifications.list({ start: 0, count: 30 }) + const commentNotifications = data.filter(n => { + return n.type === UserNotificationType.NEW_INSTANCE_FOLLOWER && n.createdAt >= now.toISOString() + }) expect(commentNotifications).to.have.lengthOf(1) }