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