Use test wrapper exit function
[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   killallServers,
13   reportVideoAbuse,
14   ServerInfo,
15   setAccessTokensToServers,
16   updateVideoAbuse,
17   uploadVideo
18 } from '../../../../shared/extra-utils/index'
19 import { doubleFollow } from '../../../../shared/extra-utils/server/follows'
20 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
21
22 const expect = chai.expect
23
24 describe('Test video abuses', function () {
25   let servers: ServerInfo[] = []
26   let abuseServer2: VideoAbuse
27
28   before(async function () {
29     this.timeout(50000)
30
31     // Run servers
32     servers = await flushAndRunMultipleServers(2)
33
34     // Get the access tokens
35     await setAccessTokensToServers(servers)
36
37     // Server 1 and server 2 follow each other
38     await doubleFollow(servers[0], servers[1])
39
40     // Upload some videos on each servers
41     const video1Attributes = {
42       name: 'my super name for server 1',
43       description: 'my super description for server 1'
44     }
45     await uploadVideo(servers[0].url, servers[0].accessToken, video1Attributes)
46
47     const video2Attributes = {
48       name: 'my super name for server 2',
49       description: 'my super description for server 2'
50     }
51     await uploadVideo(servers[1].url, servers[1].accessToken, video2Attributes)
52
53     // Wait videos propagation, server 2 has transcoding enabled
54     await waitJobs(servers)
55
56     const res = await getVideosList(servers[0].url)
57     const videos = res.body.data
58
59     expect(videos.length).to.equal(2)
60
61     servers[0].video = videos.find(video => video.name === 'my super name for server 1')
62     servers[1].video = videos.find(video => video.name === 'my super name for server 2')
63   })
64
65   it('Should not have video abuses', async function () {
66     const res = await getVideoAbusesList(servers[0].url, servers[0].accessToken)
67
68     expect(res.body.total).to.equal(0)
69     expect(res.body.data).to.be.an('array')
70     expect(res.body.data.length).to.equal(0)
71   })
72
73   it('Should report abuse on a local video', async function () {
74     this.timeout(15000)
75
76     const reason = 'my super bad reason'
77     await reportVideoAbuse(servers[0].url, servers[0].accessToken, servers[0].video.id, reason)
78
79     // We wait requests propagation, even if the server 1 is not supposed to make a request to server 2
80     await waitJobs(servers)
81   })
82
83   it('Should have 1 video abuses on server 1 and 0 on server 2', async function () {
84     const res1 = await getVideoAbusesList(servers[0].url, servers[0].accessToken)
85
86     expect(res1.body.total).to.equal(1)
87     expect(res1.body.data).to.be.an('array')
88     expect(res1.body.data.length).to.equal(1)
89
90     const abuse: VideoAbuse = res1.body.data[0]
91     expect(abuse.reason).to.equal('my super bad reason')
92     expect(abuse.reporterAccount.name).to.equal('root')
93     expect(abuse.reporterAccount.host).to.equal('localhost:9001')
94     expect(abuse.video.id).to.equal(servers[0].video.id)
95
96     const res2 = await getVideoAbusesList(servers[1].url, servers[1].accessToken)
97     expect(res2.body.total).to.equal(0)
98     expect(res2.body.data).to.be.an('array')
99     expect(res2.body.data.length).to.equal(0)
100   })
101
102   it('Should report abuse on a remote video', async function () {
103     this.timeout(10000)
104
105     const reason = 'my super bad reason 2'
106     await reportVideoAbuse(servers[0].url, servers[0].accessToken, servers[1].video.id, reason)
107
108     // We wait requests propagation
109     await waitJobs(servers)
110   })
111
112   it('Should have 2 video abuses on server 1 and 1 on server 2', async function () {
113     const res1 = await getVideoAbusesList(servers[0].url, servers[0].accessToken)
114     expect(res1.body.total).to.equal(2)
115     expect(res1.body.data).to.be.an('array')
116     expect(res1.body.data.length).to.equal(2)
117
118     const abuse1: VideoAbuse = res1.body.data[0]
119     expect(abuse1.reason).to.equal('my super bad reason')
120     expect(abuse1.reporterAccount.name).to.equal('root')
121     expect(abuse1.reporterAccount.host).to.equal('localhost:9001')
122     expect(abuse1.video.id).to.equal(servers[0].video.id)
123     expect(abuse1.state.id).to.equal(VideoAbuseState.PENDING)
124     expect(abuse1.state.label).to.equal('Pending')
125     expect(abuse1.moderationComment).to.be.null
126
127     const abuse2: VideoAbuse = res1.body.data[1]
128     expect(abuse2.reason).to.equal('my super bad reason 2')
129     expect(abuse2.reporterAccount.name).to.equal('root')
130     expect(abuse2.reporterAccount.host).to.equal('localhost:9001')
131     expect(abuse2.video.id).to.equal(servers[1].video.id)
132     expect(abuse2.state.id).to.equal(VideoAbuseState.PENDING)
133     expect(abuse2.state.label).to.equal('Pending')
134     expect(abuse2.moderationComment).to.be.null
135
136     const res2 = await getVideoAbusesList(servers[1].url, servers[1].accessToken)
137     expect(res2.body.total).to.equal(1)
138     expect(res2.body.data).to.be.an('array')
139     expect(res2.body.data.length).to.equal(1)
140
141     abuseServer2 = res2.body.data[0]
142     expect(abuseServer2.reason).to.equal('my super bad reason 2')
143     expect(abuseServer2.reporterAccount.name).to.equal('root')
144     expect(abuseServer2.reporterAccount.host).to.equal('localhost:9001')
145     expect(abuseServer2.state.id).to.equal(VideoAbuseState.PENDING)
146     expect(abuseServer2.state.label).to.equal('Pending')
147     expect(abuseServer2.moderationComment).to.be.null
148   })
149
150   it('Should update the state of a video abuse', async function () {
151     const body = { state: VideoAbuseState.REJECTED }
152     await updateVideoAbuse(servers[1].url, servers[1].accessToken, abuseServer2.video.uuid, abuseServer2.id, body)
153
154     const res = await getVideoAbusesList(servers[1].url, servers[1].accessToken)
155     expect(res.body.data[0].state.id).to.equal(VideoAbuseState.REJECTED)
156   })
157
158   it('Should add a moderation comment', async function () {
159     const body = { state: VideoAbuseState.ACCEPTED, moderationComment: 'It is valid' }
160     await updateVideoAbuse(servers[1].url, servers[1].accessToken, abuseServer2.video.uuid, abuseServer2.id, body)
161
162     const res = await getVideoAbusesList(servers[1].url, servers[1].accessToken)
163     expect(res.body.data[0].state.id).to.equal(VideoAbuseState.ACCEPTED)
164     expect(res.body.data[0].moderationComment).to.equal('It is valid')
165   })
166
167   it('Should delete the video abuse', async function () {
168     await deleteVideoAbuse(servers[1].url, servers[1].accessToken, abuseServer2.video.uuid, abuseServer2.id)
169
170     const res = await getVideoAbusesList(servers[1].url, servers[1].accessToken)
171     expect(res.body.total).to.equal(0)
172     expect(res.body.data).to.be.an('array')
173     expect(res.body.data.length).to.equal(0)
174   })
175
176   after(async function () {
177     await cleanupTests(servers)
178   })
179 })