]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - test/api/friendsBasic.js
Tests refractoring
[github/Chocobozzz/PeerTube.git] / test / api / friendsBasic.js
1 ;(function () {
2 'use strict'
3
4 var async = require('async')
5 var chai = require('chai')
6 var expect = chai.expect
7 var request = require('supertest')
8
9 var utils = require('./utils')
10
11 describe('Test basic friends', function () {
12 var apps = []
13 var urls = []
14
15 before(function (done) {
16 this.timeout(20000)
17 utils.runMultipleServers(3, function (apps_run, urls_run) {
18 apps = apps_run
19 urls = urls_run
20 done()
21 })
22 })
23
24 it('Should not have friends', function (done) {
25 async.each(urls, function (url, callback) {
26 utils.getFriendsList(url, function (err, res) {
27 if (err) throw err
28
29 var result = res.body
30 expect(result).to.be.an('array')
31 expect(result.length).to.equal(0)
32 callback()
33 })
34 }, function (err) {
35 if (err) throw err
36
37 done()
38 })
39 })
40
41 it('Should make friends', function (done) {
42 this.timeout(10000)
43
44 function testMadeFriends (urls, url_to_test, callback) {
45 var friends = []
46 for (var i = 0; i < urls.length; i++) {
47 if (urls[i] === url_to_test) continue
48 friends.push(urls[i])
49 }
50
51 utils.getFriendsList(url_to_test, function (err, res) {
52 if (err) throw err
53
54 var result = res.body
55 var result_urls = [ result[0].url, result[1].url ]
56 expect(result).to.be.an('array')
57 expect(result.length).to.equal(2)
58 expect(result_urls[0]).to.not.equal(result_urls[1])
59
60 var error_string = 'Friends url do not correspond for ' + url_to_test
61 expect(friends).to.contain(result_urls[0], error_string)
62 expect(friends).to.contain(result_urls[1], error_string)
63 callback()
64 })
65 }
66
67 var path = '/api/v1/pods/makefriends'
68
69 // The second pod make friend with the third
70 request(urls[1])
71 .get(path)
72 .set('Accept', 'application/json')
73 .expect(204)
74 .end(function (err, res) {
75 if (err) throw err
76
77 // Wait for the request between pods
78 setTimeout(function () {
79 // The second pod should have the third as a friend
80 utils.getFriendsList(urls[1], function (err, res) {
81 if (err) throw err
82
83 var result = res.body
84 expect(result).to.be.an('array')
85 expect(result.length).to.equal(1)
86 expect(result[0].url).to.be.equal(urls[2])
87
88 // Same here, the third pod should have the second pod as a friend
89 utils.getFriendsList(urls[2], function (err, res) {
90 if (err) throw err
91
92 var result = res.body
93 expect(result).to.be.an('array')
94 expect(result.length).to.equal(1)
95 expect(result[0].url).to.be.equal(urls[1])
96
97 // Finally the first pod make friend with the second pod
98 request(urls[0])
99 .get(path)
100 .set('Accept', 'application/json')
101 .expect(204)
102 .end(function (err, res) {
103 if (err) throw err
104
105 setTimeout(function () {
106 // Now each pod should be friend with the other ones
107 async.each(urls, function (url, callback) {
108 testMadeFriends(urls, url, callback)
109 }, function (err) {
110 if (err) throw err
111 done()
112 })
113 }, 1000)
114 })
115 })
116 })
117 }, 1000)
118 })
119 })
120
121 // TODO
122 it('Should not be able to make friends again')
123
124 after(function (done) {
125 apps.forEach(function (app) {
126 process.kill(-app.pid)
127 })
128
129 if (this.ok) {
130 utils.flushTests(function () {
131 done()
132 })
133 } else {
134 done()
135 }
136 })
137 })
138 })()