PeerTube/packages/tests/src/api/server/bulk.ts

186 lines
5.4 KiB
TypeScript
Raw Normal View History

2020-05-14 16:56:15 +02:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2022-08-17 15:44:32 +02:00
import { expect } from 'chai'
2020-05-14 16:56:15 +02:00
import {
2021-07-05 14:57:03 +02:00
BulkCommand,
2020-05-14 16:56:15 +02:00
cleanupTests,
2021-07-16 09:47:51 +02:00
createMultipleServers,
2021-07-16 14:27:30 +02:00
doubleFollow,
2021-07-16 09:47:51 +02:00
PeerTubeServer,
2020-05-14 16:56:15 +02:00
setAccessTokensToServers,
2021-05-11 11:27:40 +02:00
waitJobs
} from '@peertube/peertube-server-commands'
2020-05-14 16:56:15 +02:00
describe('Test bulk actions', function () {
const commentsUser3: { videoId: number, commentId: number }[] = []
2021-07-16 09:47:51 +02:00
let servers: PeerTubeServer[] = []
2021-07-09 14:15:11 +02:00
let user1Token: string
let user2Token: string
let user3Token: string
2020-05-14 16:56:15 +02:00
2021-07-05 14:57:03 +02:00
let bulkCommand: BulkCommand
2020-05-14 16:56:15 +02:00
before(async function () {
2022-09-12 08:29:01 +02:00
this.timeout(120000)
2020-05-14 16:56:15 +02:00
2021-07-16 09:47:51 +02:00
servers = await createMultipleServers(2)
2020-05-14 16:56:15 +02:00
// Get the access tokens
await setAccessTokensToServers(servers)
{
const user = { username: 'user1', password: 'password' }
2021-07-16 09:04:35 +02:00
await servers[0].users.create({ username: user.username, password: user.password })
2020-05-14 16:56:15 +02:00
2021-07-16 09:04:35 +02:00
user1Token = await servers[0].login.getAccessToken(user)
2020-05-14 16:56:15 +02:00
}
{
const user = { username: 'user2', password: 'password' }
2021-07-16 09:04:35 +02:00
await servers[0].users.create({ username: user.username, password: user.password })
2020-05-14 16:56:15 +02:00
2021-07-16 09:04:35 +02:00
user2Token = await servers[0].login.getAccessToken(user)
2020-05-14 16:56:15 +02:00
}
{
const user = { username: 'user3', password: 'password' }
2021-07-16 09:04:35 +02:00
await servers[1].users.create({ username: user.username, password: user.password })
2020-05-14 16:56:15 +02:00
2021-07-16 09:04:35 +02:00
user3Token = await servers[1].login.getAccessToken(user)
2020-05-14 16:56:15 +02:00
}
await doubleFollow(servers[0], servers[1])
2021-07-05 14:57:03 +02:00
bulkCommand = new BulkCommand(servers[0])
2020-05-14 16:56:15 +02:00
})
describe('Bulk remove comments', function () {
async function checkInstanceCommentsRemoved () {
{
2021-07-16 09:04:35 +02:00
const { data } = await servers[0].videos.list()
2020-05-14 16:56:15 +02:00
// Server 1 should not have these comments anymore
2021-07-15 10:02:54 +02:00
for (const video of data) {
2021-07-16 09:04:35 +02:00
const { data } = await servers[0].comments.listThreads({ videoId: video.id })
2021-07-09 14:15:11 +02:00
const comment = data.find(c => c.text === 'comment by user 3')
2020-05-14 16:56:15 +02:00
expect(comment).to.not.exist
}
}
{
2021-07-16 09:04:35 +02:00
const { data } = await servers[1].videos.list()
2020-05-14 16:56:15 +02:00
// Server 1 should not have these comments on videos of server 1
2021-07-15 10:02:54 +02:00
for (const video of data) {
2021-07-16 09:04:35 +02:00
const { data } = await servers[1].comments.listThreads({ videoId: video.id })
2021-07-09 14:15:11 +02:00
const comment = data.find(c => c.text === 'comment by user 3')
2020-05-14 16:56:15 +02:00
2022-12-09 11:14:47 +01:00
if (video.account.host === servers[0].host) {
2020-05-14 16:56:15 +02:00
expect(comment).to.not.exist
} else {
expect(comment).to.exist
}
}
}
}
before(async function () {
this.timeout(240000)
2020-05-14 16:56:15 +02:00
2021-07-16 09:04:35 +02:00
await servers[0].videos.upload({ attributes: { name: 'video 1 server 1' } })
await servers[0].videos.upload({ attributes: { name: 'video 2 server 1' } })
await servers[0].videos.upload({ token: user1Token, attributes: { name: 'video 3 server 1' } })
2020-05-14 16:56:15 +02:00
2021-07-16 09:04:35 +02:00
await servers[1].videos.upload({ attributes: { name: 'video 1 server 2' } })
2020-05-14 16:56:15 +02:00
await waitJobs(servers)
{
2021-07-16 09:04:35 +02:00
const { data } = await servers[0].videos.list()
2021-07-15 10:02:54 +02:00
for (const video of data) {
2021-07-16 09:04:35 +02:00
await servers[0].comments.createThread({ videoId: video.id, text: 'comment by root server 1' })
await servers[0].comments.createThread({ token: user1Token, videoId: video.id, text: 'comment by user 1' })
await servers[0].comments.createThread({ token: user2Token, videoId: video.id, text: 'comment by user 2' })
2020-05-14 16:56:15 +02:00
}
}
{
2021-07-16 09:04:35 +02:00
const { data } = await servers[1].videos.list()
2021-07-09 14:15:11 +02:00
2021-07-15 10:02:54 +02:00
for (const video of data) {
2021-07-16 09:04:35 +02:00
await servers[1].comments.createThread({ videoId: video.id, text: 'comment by root server 2' })
2020-05-14 16:56:15 +02:00
2021-07-16 09:04:35 +02:00
const comment = await servers[1].comments.createThread({ token: user3Token, videoId: video.id, text: 'comment by user 3' })
2021-07-09 14:15:11 +02:00
commentsUser3.push({ videoId: video.id, commentId: comment.id })
2020-05-14 16:56:15 +02:00
}
}
await waitJobs(servers)
})
it('Should delete comments of an account on my videos', async function () {
this.timeout(60000)
2021-07-05 14:57:03 +02:00
await bulkCommand.removeCommentsOf({
2021-07-09 14:15:11 +02:00
token: user1Token,
2020-05-14 16:56:15 +02:00
attributes: {
accountName: 'user2',
scope: 'my-videos'
}
})
await waitJobs(servers)
for (const server of servers) {
2021-07-16 09:04:35 +02:00
const { data } = await server.videos.list()
2020-05-14 16:56:15 +02:00
2021-07-15 10:02:54 +02:00
for (const video of data) {
2021-07-16 09:04:35 +02:00
const { data } = await server.comments.listThreads({ videoId: video.id })
2021-07-09 14:15:11 +02:00
const comment = data.find(c => c.text === 'comment by user 2')
2020-05-14 16:56:15 +02:00
2021-07-09 14:15:11 +02:00
if (video.name === 'video 3 server 1') expect(comment).to.not.exist
else expect(comment).to.exist
2020-05-14 16:56:15 +02:00
}
}
})
it('Should delete comments of an account on the instance', async function () {
this.timeout(60000)
2021-07-05 14:57:03 +02:00
await bulkCommand.removeCommentsOf({
2020-05-14 16:56:15 +02:00
attributes: {
2022-12-09 11:14:47 +01:00
accountName: 'user3@' + servers[1].host,
2020-05-14 16:56:15 +02:00
scope: 'instance'
}
})
await waitJobs(servers)
await checkInstanceCommentsRemoved()
})
it('Should not re create the comment on video update', async function () {
this.timeout(60000)
for (const obj of commentsUser3) {
2021-07-16 09:04:35 +02:00
await servers[1].comments.addReply({
2021-07-09 14:15:11 +02:00
token: user3Token,
videoId: obj.videoId,
toCommentId: obj.commentId,
text: 'comment by user 3 bis'
})
2020-05-14 16:56:15 +02:00
}
await waitJobs(servers)
await checkInstanceCommentsRemoved()
})
})
after(async function () {
await cleanupTests(servers)
})
})