]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/video-abuse.js
871054788dec75f121f0671389c18d94867a5af4
[github/Chocobozzz/PeerTube.git] / server / tests / api / video-abuse.js
1 /* eslint-disable no-unused-expressions */
2
3 'use strict'
4
5 const chai = require('chai')
6 const each = require('async/each')
7 const expect = chai.expect
8 const series = require('async/series')
9
10 const loginUtils = require('../utils/login')
11 const podsUtils = require('../utils/pods')
12 const serversUtils = require('../utils/servers')
13 const videosUtils = require('../utils/videos')
14 const videoAbusesUtils = require('../utils/video-abuses')
15
16 describe('Test video abuses', function () {
17 let servers = []
18
19 before(function (done) {
20 this.timeout(30000)
21
22 series([
23 // Run servers
24 function (next) {
25 serversUtils.flushAndRunMultipleServers(2, function (serversRun) {
26 servers = serversRun
27 next()
28 })
29 },
30 // Get the access tokens
31 function (next) {
32 each(servers, function (server, callbackEach) {
33 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
34 if (err) return callbackEach(err)
35
36 server.accessToken = accessToken
37 callbackEach()
38 })
39 }, next)
40 },
41 // Pod 1 make friends too
42 function (next) {
43 const server = servers[0]
44 podsUtils.makeFriends(server.url, server.accessToken, next)
45 },
46 // Upload some videos on each pods
47 function (next) {
48 const name = 'my super name for pod 1'
49 const category = 5
50 const description = 'my super description for pod 1'
51 const tags = [ 'tag' ]
52 const file = 'video_short2.webm'
53 videosUtils.uploadVideo(servers[0].url, servers[0].accessToken, name, category, description, tags, file, next)
54 },
55 function (next) {
56 const name = 'my super name for pod 2'
57 const category = 5
58 const description = 'my super description for pod 2'
59 const tags = [ 'tag' ]
60 const file = 'video_short2.webm'
61 videosUtils.uploadVideo(servers[1].url, servers[1].accessToken, name, category, description, tags, file, next)
62 },
63 // Wait videos propagation
64 function (next) {
65 setTimeout(next, 11000)
66 },
67 function (next) {
68 videosUtils.getVideosList(servers[0].url, function (err, res) {
69 if (err) throw err
70
71 const videos = res.body.data
72
73 expect(videos.length).to.equal(2)
74
75 servers[0].video = videos.find(function (video) { return video.name === 'my super name for pod 1' })
76 servers[1].video = videos.find(function (video) { return video.name === 'my super name for pod 2' })
77
78 next()
79 })
80 }
81 ], done)
82 })
83
84 it('Should not have video abuses', function (done) {
85 videoAbusesUtils.getVideoAbusesList(servers[0].url, servers[0].accessToken, function (err, res) {
86 if (err) throw err
87
88 expect(res.body.total).to.equal(0)
89 expect(res.body.data).to.be.an('array')
90 expect(res.body.data.length).to.equal(0)
91
92 done()
93 })
94 })
95
96 it('Should report abuse on a local video', function (done) {
97 this.timeout(15000)
98
99 const reason = 'my super bad reason'
100 videoAbusesUtils.reportVideoAbuse(servers[0].url, servers[0].accessToken, servers[0].video.id, reason, function (err) {
101 if (err) throw err
102
103 // We wait requests propagation, even if the pod 1 is not supposed to make a request to pod 2
104 setTimeout(done, 11000)
105 })
106 })
107
108 it('Should have 1 video abuses on pod 1 and 0 on pod 2', function (done) {
109 videoAbusesUtils.getVideoAbusesList(servers[0].url, servers[0].accessToken, function (err, res) {
110 if (err) throw err
111
112 expect(res.body.total).to.equal(1)
113 expect(res.body.data).to.be.an('array')
114 expect(res.body.data.length).to.equal(1)
115
116 const abuse = res.body.data[0]
117 expect(abuse.reason).to.equal('my super bad reason')
118 expect(abuse.reporterUsername).to.equal('root')
119 expect(abuse.reporterPodHost).to.equal('localhost:9001')
120 expect(abuse.videoId).to.equal(servers[0].video.id)
121
122 videoAbusesUtils.getVideoAbusesList(servers[1].url, servers[1].accessToken, function (err, res) {
123 if (err) throw err
124
125 expect(res.body.total).to.equal(0)
126 expect(res.body.data).to.be.an('array')
127 expect(res.body.data.length).to.equal(0)
128
129 done()
130 })
131 })
132 })
133
134 it('Should report abuse on a remote video', function (done) {
135 this.timeout(15000)
136
137 const reason = 'my super bad reason 2'
138 videoAbusesUtils.reportVideoAbuse(servers[0].url, servers[0].accessToken, servers[1].video.id, reason, function (err) {
139 if (err) throw err
140
141 // We wait requests propagation
142 setTimeout(done, 11000)
143 })
144 })
145
146 it('Should have 2 video abuse on pod 1 and 1 on pod 2', function (done) {
147 videoAbusesUtils.getVideoAbusesList(servers[0].url, servers[0].accessToken, function (err, res) {
148 if (err) throw err
149
150 expect(res.body.total).to.equal(2)
151 expect(res.body.data).to.be.an('array')
152 expect(res.body.data.length).to.equal(2)
153
154 let abuse = res.body.data[0]
155 expect(abuse.reason).to.equal('my super bad reason')
156 expect(abuse.reporterUsername).to.equal('root')
157 expect(abuse.reporterPodHost).to.equal('localhost:9001')
158 expect(abuse.videoId).to.equal(servers[0].video.id)
159
160 abuse = res.body.data[1]
161 expect(abuse.reason).to.equal('my super bad reason 2')
162 expect(abuse.reporterUsername).to.equal('root')
163 expect(abuse.reporterPodHost).to.equal('localhost:9001')
164 expect(abuse.videoId).to.equal(servers[1].video.id)
165
166 videoAbusesUtils.getVideoAbusesList(servers[1].url, servers[1].accessToken, function (err, res) {
167 if (err) throw err
168
169 expect(res.body.total).to.equal(1)
170 expect(res.body.data).to.be.an('array')
171 expect(res.body.data.length).to.equal(1)
172
173 let abuse = res.body.data[0]
174 expect(abuse.reason).to.equal('my super bad reason 2')
175 expect(abuse.reporterUsername).to.equal('root')
176 expect(abuse.reporterPodHost).to.equal('localhost:9001')
177
178 done()
179 })
180 })
181 })
182
183 after(function (done) {
184 servers.forEach(function (server) {
185 process.kill(-server.app.pid)
186 })
187
188 // Keep the logs if the test failed
189 if (this.ok) {
190 serversUtils.flushTests(done)
191 } else {
192 done()
193 }
194 })
195 })