]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/requests.js
933ed29b466ee585c2291c01140bb250fb3cb3da
[github/Chocobozzz/PeerTube.git] / server / tests / api / requests.js
1 'use strict'
2
3 const chai = require('chai')
4 const each = require('async/each')
5 const expect = chai.expect
6 const request = require('supertest')
7
8 const loginUtils = require('../utils/login')
9 const podsUtils = require('../utils/pods')
10 const serversUtils = require('../utils/servers')
11 const videosUtils = require('../utils/videos')
12
13 describe('Test requests stats', function () {
14 const path = '/api/v1/requests/stats'
15 let servers = []
16
17 function uploadVideo (server, callback) {
18 const name = 'my super video'
19 const description = 'my super description'
20 const tags = [ 'tag1', 'tag2' ]
21 const fixture = 'video_short.webm'
22
23 videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, fixture, callback)
24 }
25
26 function getRequestsStats (server, callback) {
27 request(server.url)
28 .get(path)
29 .set('Accept', 'application/json')
30 .set('Authorization', 'Bearer ' + server.accessToken)
31 .expect(200)
32 .end(callback)
33 }
34
35 // ---------------------------------------------------------------
36
37 before(function (done) {
38 this.timeout(20000)
39 serversUtils.flushAndRunMultipleServers(2, function (serversRun, urlsRun) {
40 servers = serversRun
41
42 each(servers, function (server, callbackEach) {
43 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
44 if (err) return callbackEach(err)
45
46 server.accessToken = accessToken
47 callbackEach()
48 })
49 }, function (err) {
50 if (err) throw err
51
52 const server1 = servers[0]
53 podsUtils.makeFriends(server1.url, server1.accessToken, done)
54 })
55 })
56 })
57
58 it('Should have a correct timer', function (done) {
59 const server = servers[0]
60
61 getRequestsStats(server, function (err, res) {
62 if (err) throw err
63
64 const body = res.body
65 expect(body.remainingMilliSeconds).to.be.at.least(0)
66 expect(body.remainingMilliSeconds).to.be.at.most(10000)
67
68 done()
69 })
70 })
71
72 it('Should have the correct total request', function (done) {
73 this.timeout(15000)
74
75 const server = servers[0]
76 // Ensure the requests of pod 1 won't be made
77 servers[1].app.kill()
78
79 uploadVideo(server, function (err) {
80 if (err) throw err
81
82 setTimeout(function () {
83 getRequestsStats(server, function (err, res) {
84 if (err) throw err
85
86 const body = res.body
87 expect(body.totalRequests).to.equal(1)
88
89 done()
90 })
91 }, 1000)
92 })
93 })
94
95 after(function (done) {
96 process.kill(-servers[0].app.pid)
97
98 if (this.ok) {
99 serversUtils.flushTests(done)
100 } else {
101 done()
102 }
103 })
104 })