aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/requests.js
blob: af36f6e349b540275858cbf0cbfd35c5251cbf89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'use strict'

const chai = require('chai')
const each = require('async/each')
const expect = chai.expect
const request = require('supertest')

const loginUtils = require('../utils/login')
const podsUtils = require('../utils/pods')
const serversUtils = require('../utils/servers')
const videosUtils = require('../utils/videos')

describe('Test requests stats', function () {
  const path = '/api/v1/requests/stats'
  let servers = []

  function uploadVideo (server, callback) {
    const name = 'my super video'
    const description = 'my super description'
    const tags = [ 'tag1', 'tag2' ]
    const fixture = 'video_short.webm'

    videosUtils.uploadVideo(server.url, server.accessToken, name, description, tags, fixture, callback)
  }

  function getRequestsStats (server, callback) {
    request(server.url)
      .get(path)
      .set('Accept', 'application/json')
      .set('Authorization', 'Bearer ' + server.accessToken)
      .expect(200)
      .end(callback)
  }

  // ---------------------------------------------------------------

  before(function (done) {
    this.timeout(20000)
    serversUtils.flushAndRunMultipleServers(2, function (serversRun, urlsRun) {
      servers = serversRun

      each(servers, function (server, callbackEach) {
        loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
          if (err) return callbackEach(err)

          server.accessToken = accessToken
          callbackEach()
        })
      }, function (err) {
        if (err) throw err

        const server1 = servers[0]
        podsUtils.makeFriends(server1.url, server1.accessToken, done)
      })
    })
  })

  it('Should have a correct timer', function (done) {
    const server = servers[0]

    getRequestsStats(server, function (err, res) {
      if (err) throw err

      const body = res.body
      expect(body.remainingMilliSeconds).to.be.at.least(0)
      expect(body.remainingMilliSeconds).to.be.at.most(10000)

      done()
    })
  })

  it('Should have the correct request', function (done) {
    this.timeout(15000)

    const server = servers[0]
    // Ensure the requests of pod 1 won't be made
    servers[1].app.kill()

    uploadVideo(server, function (err) {
      if (err) throw err

      getRequestsStats(server, function (err, res) {
        if (err) throw err

        const body = res.body
        expect(body.requests).to.have.lengthOf(1)

        const request = body.requests[0]
        expect(request.to).to.have.lengthOf(1)
        expect(request.request.type).to.equal('add')

        // Wait one cycle
        setTimeout(done, 10000)
      })
    })
  })

  it('Should have the correct requests', function (done) {
    const server = servers[0]

    uploadVideo(server, function (err) {
      if (err) throw err

      getRequestsStats(server, function (err, res) {
        if (err) throw err

        const body = res.body
        expect(body.requests).to.have.lengthOf(2)

        const request = body.requests[1]
        expect(request.to).to.have.lengthOf(1)
        expect(request.request.type).to.equal('add')

        done()
      })
    })
  })

  after(function (done) {
    process.kill(-servers[0].app.pid)

    if (this.ok) {
      serversUtils.flushTests(done)
    } else {
      done()
    }
  })
})