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