Tests for totalRepliesFromVideoAuthor
[oweals/peertube.git] / server / tests / api / videos / video-abuse.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { VideoAbuse, VideoAbuseState } from '../../../../shared/models/videos'
6 import {
7   cleanupTests,
8   deleteVideoAbuse,
9   flushAndRunMultipleServers,
10   getVideoAbusesList,
11   getVideosList,
12   reportVideoAbuse,
13   ServerInfo,
14   setAccessTokensToServers,
15   updateVideoAbuse,
16   uploadVideo
17 } from '../../../../shared/extra-utils/index'
18 import { doubleFollow } from '../../../../shared/extra-utils/server/follows'
19 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
20 import {
21   addAccountToServerBlocklist,
22   addServerToServerBlocklist,
23   removeAccountFromServerBlocklist,
24   removeServerFromServerBlocklist
25 } from '../../../../shared/extra-utils/users/blocklist'
26
27 const expect = chai.expect
28
29 describe('Test video abuses', function () {
30   let servers: ServerInfo[] = []
31   let abuseServer2: VideoAbuse
32
33   before(async function () {
34     this.timeout(50000)
35
36     // Run servers
37     servers = await flushAndRunMultipleServers(2)
38
39     // Get the access tokens
40     await setAccessTokensToServers(servers)
41
42     // Server 1 and server 2 follow each other
43     await doubleFollow(servers[0], servers[1])
44
45     // Upload some videos on each servers
46     const video1Attributes = {
47       name: 'my super name for server 1',
48       description: 'my super description for server 1'
49     }
50     await uploadVideo(servers[0].url, servers[0].accessToken, video1Attributes)
51
52     const video2Attributes = {
53       name: 'my super name for server 2',
54       description: 'my super description for server 2'
55     }
56     await uploadVideo(servers[1].url, servers[1].accessToken, video2Attributes)
57
58     // Wait videos propagation, server 2 has transcoding enabled
59     await waitJobs(servers)
60
61     const res = await getVideosList(servers[0].url)
62     const videos = res.body.data
63
64     expect(videos.length).to.equal(2)
65
66     servers[0].video = videos.find(video => video.name === 'my super name for server 1')
67     servers[1].video = videos.find(video => video.name === 'my super name for server 2')
68   })
69
70   it('Should not have video abuses', async function () {
71     const res = await getVideoAbusesList(servers[0].url, servers[0].accessToken)
72
73     expect(res.body.total).to.equal(0)
74     expect(res.body.data).to.be.an('array')
75     expect(res.body.data.length).to.equal(0)
76   })
77
78   it('Should report abuse on a local video', async function () {
79     this.timeout(15000)
80
81     const reason = 'my super bad reason'
82     await reportVideoAbuse(servers[0].url, servers[0].accessToken, servers[0].video.id, reason)
83
84     // We wait requests propagation, even if the server 1 is not supposed to make a request to server 2
85     await waitJobs(servers)
86   })
87
88   it('Should have 1 video abuses on server 1 and 0 on server 2', async function () {
89     const res1 = await getVideoAbusesList(servers[0].url, servers[0].accessToken)
90
91     expect(res1.body.total).to.equal(1)
92     expect(res1.body.data).to.be.an('array')
93     expect(res1.body.data.length).to.equal(1)
94
95     const abuse: VideoAbuse = res1.body.data[0]
96     expect(abuse.reason).to.equal('my super bad reason')
97     expect(abuse.reporterAccount.name).to.equal('root')
98     expect(abuse.reporterAccount.host).to.equal('localhost:' + servers[0].port)
99     expect(abuse.video.id).to.equal(servers[0].video.id)
100
101     const res2 = await getVideoAbusesList(servers[1].url, servers[1].accessToken)
102     expect(res2.body.total).to.equal(0)
103     expect(res2.body.data).to.be.an('array')
104     expect(res2.body.data.length).to.equal(0)
105   })
106
107   it('Should report abuse on a remote video', async function () {
108     this.timeout(10000)
109
110     const reason = 'my super bad reason 2'
111     await reportVideoAbuse(servers[0].url, servers[0].accessToken, servers[1].video.id, reason)
112
113     // We wait requests propagation
114     await waitJobs(servers)
115   })
116
117   it('Should have 2 video abuses on server 1 and 1 on server 2', async function () {
118     const res1 = await getVideoAbusesList(servers[0].url, servers[0].accessToken)
119     expect(res1.body.total).to.equal(2)
120     expect(res1.body.data).to.be.an('array')
121     expect(res1.body.data.length).to.equal(2)
122
123     const abuse1: VideoAbuse = res1.body.data[0]
124     expect(abuse1.reason).to.equal('my super bad reason')
125     expect(abuse1.reporterAccount.name).to.equal('root')
126     expect(abuse1.reporterAccount.host).to.equal('localhost:' + servers[0].port)
127     expect(abuse1.video.id).to.equal(servers[0].video.id)
128     expect(abuse1.state.id).to.equal(VideoAbuseState.PENDING)
129     expect(abuse1.state.label).to.equal('Pending')
130     expect(abuse1.moderationComment).to.be.null
131
132     const abuse2: VideoAbuse = res1.body.data[1]
133     expect(abuse2.reason).to.equal('my super bad reason 2')
134     expect(abuse2.reporterAccount.name).to.equal('root')
135     expect(abuse2.reporterAccount.host).to.equal('localhost:' + servers[0].port)
136     expect(abuse2.video.id).to.equal(servers[1].video.id)
137     expect(abuse2.state.id).to.equal(VideoAbuseState.PENDING)
138     expect(abuse2.state.label).to.equal('Pending')
139     expect(abuse2.moderationComment).to.be.null
140
141     const res2 = await getVideoAbusesList(servers[1].url, servers[1].accessToken)
142     expect(res2.body.total).to.equal(1)
143     expect(res2.body.data).to.be.an('array')
144     expect(res2.body.data.length).to.equal(1)
145
146     abuseServer2 = res2.body.data[0]
147     expect(abuseServer2.reason).to.equal('my super bad reason 2')
148     expect(abuseServer2.reporterAccount.name).to.equal('root')
149     expect(abuseServer2.reporterAccount.host).to.equal('localhost:' + servers[0].port)
150     expect(abuseServer2.state.id).to.equal(VideoAbuseState.PENDING)
151     expect(abuseServer2.state.label).to.equal('Pending')
152     expect(abuseServer2.moderationComment).to.be.null
153   })
154
155   it('Should update the state of a video abuse', async function () {
156     const body = { state: VideoAbuseState.REJECTED }
157     await updateVideoAbuse(servers[1].url, servers[1].accessToken, abuseServer2.video.uuid, abuseServer2.id, body)
158
159     const res = await getVideoAbusesList(servers[1].url, servers[1].accessToken)
160     expect(res.body.data[0].state.id).to.equal(VideoAbuseState.REJECTED)
161   })
162
163   it('Should add a moderation comment', async function () {
164     const body = { state: VideoAbuseState.ACCEPTED, moderationComment: 'It is valid' }
165     await updateVideoAbuse(servers[1].url, servers[1].accessToken, abuseServer2.video.uuid, abuseServer2.id, body)
166
167     const res = await getVideoAbusesList(servers[1].url, servers[1].accessToken)
168     expect(res.body.data[0].state.id).to.equal(VideoAbuseState.ACCEPTED)
169     expect(res.body.data[0].moderationComment).to.equal('It is valid')
170   })
171
172   it('Should hide video abuses from blocked accounts', async function () {
173     this.timeout(10000)
174
175     {
176       await reportVideoAbuse(servers[1].url, servers[1].accessToken, servers[0].video.uuid, 'will mute this')
177       await waitJobs(servers)
178
179       const res = await getVideoAbusesList(servers[0].url, servers[0].accessToken)
180       expect(res.body.total).to.equal(3)
181     }
182
183     const accountToBlock = 'root@localhost:' + servers[1].port
184
185     {
186       await addAccountToServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, accountToBlock)
187
188       const res = await getVideoAbusesList(servers[ 0 ].url, servers[ 0 ].accessToken)
189       expect(res.body.total).to.equal(2)
190
191       const abuse = res.body.data.find(a => a.reason === 'will mute this')
192       expect(abuse).to.be.undefined
193     }
194
195     {
196       await removeAccountFromServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, accountToBlock)
197
198       const res = await getVideoAbusesList(servers[ 0 ].url, servers[ 0 ].accessToken)
199       expect(res.body.total).to.equal(3)
200     }
201   })
202
203   it('Should hide video abuses from blocked servers', async function () {
204     const serverToBlock = servers[1].host
205
206     {
207       await addServerToServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, servers[1].host)
208
209       const res = await getVideoAbusesList(servers[ 0 ].url, servers[ 0 ].accessToken)
210       expect(res.body.total).to.equal(2)
211
212       const abuse = res.body.data.find(a => a.reason === 'will mute this')
213       expect(abuse).to.be.undefined
214     }
215
216     {
217       await removeServerFromServerBlocklist(servers[ 0 ].url, servers[ 0 ].accessToken, serverToBlock)
218
219       const res = await getVideoAbusesList(servers[ 0 ].url, servers[ 0 ].accessToken)
220       expect(res.body.total).to.equal(3)
221     }
222   })
223
224   it('Should delete the video abuse', async function () {
225     this.timeout(10000)
226
227     await deleteVideoAbuse(servers[1].url, servers[1].accessToken, abuseServer2.video.uuid, abuseServer2.id)
228
229     await waitJobs(servers)
230
231     {
232       const res = await getVideoAbusesList(servers[1].url, servers[1].accessToken)
233       expect(res.body.total).to.equal(1)
234       expect(res.body.data.length).to.equal(1)
235       expect(res.body.data[0].id).to.not.equal(abuseServer2.id)
236     }
237
238     {
239       const res = await getVideoAbusesList(servers[0].url, servers[0].accessToken)
240       expect(res.body.total).to.equal(3)
241     }
242   })
243
244   after(async function () {
245     await cleanupTests(servers)
246   })
247 })