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