]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/bulk.ts
Merge branch 'release/3.2.0' into develop
[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, VideoComment } from '@shared/models'
6 import {
7 addVideoCommentReply,
8 addVideoCommentThread,
9 bulkRemoveCommentsOf,
10 cleanupTests,
11 createUser,
12 doubleFollow,
13 flushAndRunMultipleServers,
14 getVideoCommentThreads,
15 getVideosList,
16 ServerInfo,
17 setAccessTokensToServers,
18 uploadVideo,
19 userLogin,
20 waitJobs
21 } from '../../../../shared/extra-utils/index'
22
23 const expect = chai.expect
24
25 describe('Test bulk actions', function () {
26 const commentsUser3: { videoId: number, commentId: number }[] = []
27
28 let servers: ServerInfo[] = []
29 let user1AccessToken: string
30 let user2AccessToken: string
31 let user3AccessToken: string
32
33 before(async function () {
34 this.timeout(30000)
35
36 servers = await flushAndRunMultipleServers(2)
37
38 // Get the access tokens
39 await setAccessTokensToServers(servers)
40
41 {
42 const user = { username: 'user1', password: 'password' }
43 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
44
45 user1AccessToken = await userLogin(servers[0], user)
46 }
47
48 {
49 const user = { username: 'user2', password: 'password' }
50 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
51
52 user2AccessToken = await userLogin(servers[0], user)
53 }
54
55 {
56 const user = { username: 'user3', password: 'password' }
57 await createUser({ url: servers[1].url, accessToken: servers[1].accessToken, username: user.username, password: user.password })
58
59 user3AccessToken = await userLogin(servers[1], user)
60 }
61
62 await doubleFollow(servers[0], servers[1])
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 resThreads = await getVideoCommentThreads(servers[0].url, video.id, 0, 10)
74 const comments = resThreads.body.data as VideoComment[]
75 const comment = comments.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 resThreads = await getVideoCommentThreads(servers[1].url, video.id, 0, 10)
88 const comments = resThreads.body.data as VideoComment[]
89 const comment = comments.find(c => c.text === 'comment by user 3')
90
91 if (video.account.host === 'localhost:' + servers[0].port) {
92 expect(comment).to.not.exist
93 } else {
94 expect(comment).to.exist
95 }
96 }
97 }
98 }
99
100 before(async function () {
101 this.timeout(120000)
102
103 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 1 server 1' })
104 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 2 server 1' })
105 await uploadVideo(servers[0].url, user1AccessToken, { name: 'video 3 server 1' })
106
107 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video 1 server 2' })
108
109 await waitJobs(servers)
110
111 {
112 const res = await getVideosList(servers[0].url)
113 for (const video of res.body.data) {
114 await addVideoCommentThread(servers[0].url, servers[0].accessToken, video.id, 'comment by root server 1')
115 await addVideoCommentThread(servers[0].url, user1AccessToken, video.id, 'comment by user 1')
116 await addVideoCommentThread(servers[0].url, user2AccessToken, video.id, 'comment by user 2')
117 }
118 }
119
120 {
121 const res = await getVideosList(servers[1].url)
122 for (const video of res.body.data) {
123 await addVideoCommentThread(servers[1].url, servers[1].accessToken, video.id, 'comment by root server 2')
124
125 const res = await addVideoCommentThread(servers[1].url, user3AccessToken, video.id, 'comment by user 3')
126 commentsUser3.push({ videoId: video.id, commentId: res.body.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 bulkRemoveCommentsOf({
137 url: servers[0].url,
138 token: user1AccessToken,
139 attributes: {
140 accountName: 'user2',
141 scope: 'my-videos'
142 }
143 })
144
145 await waitJobs(servers)
146
147 for (const server of servers) {
148 const res = await getVideosList(server.url)
149
150 for (const video of res.body.data) {
151 const resThreads = await getVideoCommentThreads(server.url, video.id, 0, 10)
152 const comments = resThreads.body.data as VideoComment[]
153 const comment = comments.find(c => c.text === 'comment by user 2')
154
155 if (video.name === 'video 3 server 1') {
156 expect(comment).to.not.exist
157 } else {
158 expect(comment).to.exist
159 }
160 }
161 }
162 })
163
164 it('Should delete comments of an account on the instance', async function () {
165 this.timeout(60000)
166
167 await bulkRemoveCommentsOf({
168 url: servers[0].url,
169 token: servers[0].accessToken,
170 attributes: {
171 accountName: 'user3@localhost:' + servers[1].port,
172 scope: 'instance'
173 }
174 })
175
176 await waitJobs(servers)
177
178 await checkInstanceCommentsRemoved()
179 })
180
181 it('Should not re create the comment on video update', async function () {
182 this.timeout(60000)
183
184 for (const obj of commentsUser3) {
185 await addVideoCommentReply(servers[1].url, user3AccessToken, obj.videoId, obj.commentId, 'comment by user 3 bis')
186 }
187
188 await waitJobs(servers)
189
190 await checkInstanceCommentsRemoved()
191 })
192 })
193
194 after(async function () {
195 await cleanupTests(servers)
196 })
197 })