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