]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/bulk.ts
Introduce server commands
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / bulk.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { Video } from '@shared/models'
6 import {
7 BulkCommand,
8 cleanupTests,
9 createUser,
10 doubleFollow,
11 flushAndRunMultipleServers,
12 getVideosList,
13 ServerInfo,
14 setAccessTokensToServers,
15 uploadVideo,
16 userLogin,
17 waitJobs
18 } from '../../../../shared/extra-utils/index'
19
20 const expect = chai.expect
21
22 describe('Test bulk actions', function () {
23 const commentsUser3: { videoId: number, commentId: number }[] = []
24
25 let servers: ServerInfo[] = []
26 let user1Token: string
27 let user2Token: string
28 let user3Token: string
29
30 let bulkCommand: BulkCommand
31
32 before(async function () {
33 this.timeout(30000)
34
35 servers = await flushAndRunMultipleServers(2)
36
37 // Get the access tokens
38 await setAccessTokensToServers(servers)
39
40 {
41 const user = { username: 'user1', password: 'password' }
42 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
43
44 user1Token = await userLogin(servers[0], user)
45 }
46
47 {
48 const user = { username: 'user2', password: 'password' }
49 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
50
51 user2Token = await userLogin(servers[0], user)
52 }
53
54 {
55 const user = { username: 'user3', password: 'password' }
56 await createUser({ url: servers[1].url, accessToken: servers[1].accessToken, username: user.username, password: user.password })
57
58 user3Token = await userLogin(servers[1], user)
59 }
60
61 await doubleFollow(servers[0], servers[1])
62
63 bulkCommand = new BulkCommand(servers[0])
64 })
65
66 describe('Bulk remove comments', function () {
67 async function checkInstanceCommentsRemoved () {
68 {
69 const res = await getVideosList(servers[0].url)
70 const videos = res.body.data as Video[]
71
72 // Server 1 should not have these comments anymore
73 for (const video of videos) {
74 const { data } = await servers[0].commentsCommand.listThreads({ videoId: video.id })
75 const comment = data.find(c => c.text === 'comment by user 3')
76
77 expect(comment).to.not.exist
78 }
79 }
80
81 {
82 const res = await getVideosList(servers[1].url)
83 const videos = res.body.data as Video[]
84
85 // Server 1 should not have these comments on videos of server 1
86 for (const video of videos) {
87 const { data } = await servers[1].commentsCommand.listThreads({ videoId: video.id })
88 const comment = data.find(c => c.text === 'comment by user 3')
89
90 if (video.account.host === 'localhost:' + servers[0].port) {
91 expect(comment).to.not.exist
92 } else {
93 expect(comment).to.exist
94 }
95 }
96 }
97 }
98
99 before(async function () {
100 this.timeout(120000)
101
102 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 1 server 1' })
103 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 2 server 1' })
104 await uploadVideo(servers[0].url, user1Token, { name: 'video 3 server 1' })
105
106 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video 1 server 2' })
107
108 await waitJobs(servers)
109
110 {
111 const res = await getVideosList(servers[0].url)
112 for (const video of res.body.data) {
113 await servers[0].commentsCommand.createThread({ videoId: video.id, text: 'comment by root server 1' })
114 await servers[0].commentsCommand.createThread({ token: user1Token, videoId: video.id, text: 'comment by user 1' })
115 await servers[0].commentsCommand.createThread({ token: user2Token, videoId: video.id, text: 'comment by user 2' })
116 }
117 }
118
119 {
120 const res = await getVideosList(servers[1].url)
121
122 for (const video of res.body.data) {
123 await servers[1].commentsCommand.createThread({ videoId: video.id, text: 'comment by root server 2' })
124
125 const comment = await servers[1].commentsCommand.createThread({ token: user3Token, videoId: video.id, text: 'comment by user 3' })
126 commentsUser3.push({ videoId: video.id, commentId: comment.id })
127 }
128 }
129
130 await waitJobs(servers)
131 })
132
133 it('Should delete comments of an account on my videos', async function () {
134 this.timeout(60000)
135
136 await bulkCommand.removeCommentsOf({
137 token: user1Token,
138 attributes: {
139 accountName: 'user2',
140 scope: 'my-videos'
141 }
142 })
143
144 await waitJobs(servers)
145
146 for (const server of servers) {
147 const res = await getVideosList(server.url)
148
149 for (const video of res.body.data) {
150 const { data } = await server.commentsCommand.listThreads({ videoId: video.id })
151 const comment = data.find(c => c.text === 'comment by user 2')
152
153 if (video.name === 'video 3 server 1') expect(comment).to.not.exist
154 else expect(comment).to.exist
155 }
156 }
157 })
158
159 it('Should delete comments of an account on the instance', async function () {
160 this.timeout(60000)
161
162 await bulkCommand.removeCommentsOf({
163 attributes: {
164 accountName: 'user3@localhost:' + servers[1].port,
165 scope: 'instance'
166 }
167 })
168
169 await waitJobs(servers)
170
171 await checkInstanceCommentsRemoved()
172 })
173
174 it('Should not re create the comment on video update', async function () {
175 this.timeout(60000)
176
177 for (const obj of commentsUser3) {
178 await servers[1].commentsCommand.addReply({
179 token: user3Token,
180 videoId: obj.videoId,
181 toCommentId: obj.commentId,
182 text: 'comment by user 3 bis'
183 })
184 }
185
186 await waitJobs(servers)
187
188 await checkInstanceCommentsRemoved()
189 })
190 })
191
192 after(async function () {
193 await cleanupTests(servers)
194 })
195 })