]>
Commit | Line | Data |
---|---|---|
8c308c2b C |
1 | ;(function () { |
2 | 'use strict' | |
3 | ||
876d1bcf | 4 | var async = require('async') |
8c308c2b C |
5 | var chai = require('chai') |
6 | var expect = chai.expect | |
876d1bcf | 7 | var request = require('supertest') |
8c308c2b | 8 | |
876d1bcf | 9 | var utils = require('./utils') |
8c308c2b | 10 | |
3bcb78b3 | 11 | describe('Test basic friends', function () { |
ee66c593 C |
12 | var apps = [] |
13 | var urls = [] | |
14 | ||
45239549 C |
15 | function testMadeFriends (urls, url_to_test, callback) { |
16 | var friends = [] | |
17 | for (var i = 0; i < urls.length; i++) { | |
18 | if (urls[i] === url_to_test) continue | |
19 | friends.push(urls[i]) | |
20 | } | |
21 | ||
22 | utils.getFriendsList(url_to_test, function (err, res) { | |
23 | if (err) throw err | |
24 | ||
25 | var result = res.body | |
26 | var result_urls = [ result[0].url, result[1].url ] | |
27 | expect(result).to.be.an('array') | |
28 | expect(result.length).to.equal(2) | |
29 | expect(result_urls[0]).to.not.equal(result_urls[1]) | |
30 | ||
31 | var error_string = 'Friends url do not correspond for ' + url_to_test | |
32 | expect(friends).to.contain(result_urls[0], error_string) | |
33 | expect(friends).to.contain(result_urls[1], error_string) | |
34 | callback() | |
35 | }) | |
36 | } | |
37 | ||
ee66c593 | 38 | // --------------------------------------------------------------- |
8c308c2b C |
39 | |
40 | before(function (done) { | |
41 | this.timeout(20000) | |
ee66c593 | 42 | utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) { |
8c308c2b C |
43 | apps = apps_run |
44 | urls = urls_run | |
45 | done() | |
46 | }) | |
47 | }) | |
48 | ||
49 | it('Should not have friends', function (done) { | |
50 | async.each(urls, function (url, callback) { | |
876d1bcf | 51 | utils.getFriendsList(url, function (err, res) { |
8c308c2b C |
52 | if (err) throw err |
53 | ||
54 | var result = res.body | |
55 | expect(result).to.be.an('array') | |
56 | expect(result.length).to.equal(0) | |
57 | callback() | |
58 | }) | |
ee66c593 | 59 | }, done) |
8c308c2b C |
60 | }) |
61 | ||
62 | it('Should make friends', function (done) { | |
63 | this.timeout(10000) | |
64 | ||
f5a60a51 | 65 | var path = '/api/v1/pods/makefriends' |
8c308c2b | 66 | |
ee66c593 C |
67 | async.series([ |
68 | // The second pod make friend with the third | |
69 | function (next) { | |
70 | request(urls[1]) | |
71 | .get(path) | |
72 | .set('Accept', 'application/json') | |
73 | .expect(204) | |
74 | .end(next) | |
75 | }, | |
76 | // Wait for the request between pods | |
77 | function (next) { | |
78 | setTimeout(next, 1000) | |
79 | }, | |
80 | // The second pod should have the third as a friend | |
81 | function (next) { | |
82 | utils.getFriendsList(urls[1], function (err, res) { | |
83 | if (err) throw err | |
8c308c2b | 84 | |
ee66c593 C |
85 | var result = res.body |
86 | expect(result).to.be.an('array') | |
87 | expect(result.length).to.equal(1) | |
88 | expect(result[0].url).to.be.equal(urls[2]) | |
8c308c2b | 89 | |
ee66c593 C |
90 | next() |
91 | }) | |
92 | }, | |
93 | // Same here, the third pod should have the second pod as a friend | |
94 | function (next) { | |
95 | utils.getFriendsList(urls[2], function (err, res) { | |
96 | if (err) throw err | |
97 | ||
98 | var result = res.body | |
99 | expect(result).to.be.an('array') | |
100 | expect(result.length).to.equal(1) | |
101 | expect(result[0].url).to.be.equal(urls[1]) | |
102 | ||
103 | next() | |
104 | }) | |
105 | }, | |
106 | // Finally the first pod make friend with the second pod | |
107 | function (next) { | |
108 | request(urls[0]) | |
109 | .get(path) | |
110 | .set('Accept', 'application/json') | |
111 | .expect(204) | |
112 | .end(next) | |
113 | }, | |
114 | // Wait for the request between pods | |
115 | function (next) { | |
116 | setTimeout(next, 1000) | |
117 | } | |
118 | ], | |
119 | // Now each pod should be friend with the other ones | |
120 | function (err) { | |
121 | if (err) throw err | |
122 | async.each(urls, function (url, callback) { | |
123 | testMadeFriends(urls, url, callback) | |
124 | }, done) | |
125 | }) | |
8c308c2b C |
126 | }) |
127 | ||
45239549 C |
128 | it('Should not be allowed to make friend again', function (done) { |
129 | utils.makeFriends(urls[1], 409, done) | |
130 | }) | |
131 | ||
132 | it('Should quit friends of pod 2', function (done) { | |
ee66c593 C |
133 | async.series([ |
134 | // Pod 1 quit friends | |
135 | function (next) { | |
136 | utils.quitFriends(urls[1], next) | |
137 | }, | |
138 | // Pod 1 should not have friends anymore | |
139 | function (next) { | |
140 | utils.getFriendsList(urls[1], function (err, res) { | |
141 | if (err) throw err | |
45239549 | 142 | |
ee66c593 C |
143 | var result = res.body |
144 | expect(result).to.be.an('array') | |
145 | expect(result.length).to.equal(0) | |
45239549 | 146 | |
ee66c593 C |
147 | next() |
148 | }) | |
149 | }, | |
150 | // Other pods shouldn't have pod 1 too | |
151 | function (next) { | |
45239549 C |
152 | async.each([ urls[0], urls[2] ], function (url, callback) { |
153 | utils.getFriendsList(url, function (err, res) { | |
154 | if (err) throw err | |
155 | ||
156 | var result = res.body | |
157 | expect(result).to.be.an('array') | |
158 | expect(result.length).to.equal(1) | |
159 | expect(result[0].url).not.to.be.equal(urls[1]) | |
160 | callback() | |
161 | }) | |
ee66c593 C |
162 | }, next) |
163 | } | |
164 | ], done) | |
45239549 C |
165 | }) |
166 | ||
167 | it('Should allow pod 2 to make friend again', function (done) { | |
168 | utils.makeFriends(urls[1], function () { | |
169 | async.each(urls, function (url, callback) { | |
170 | testMadeFriends(urls, url, callback) | |
ee66c593 | 171 | }, done) |
45239549 C |
172 | }) |
173 | }) | |
8c308c2b C |
174 | |
175 | after(function (done) { | |
176 | apps.forEach(function (app) { | |
177 | process.kill(-app.pid) | |
178 | }) | |
179 | ||
180 | if (this.ok) { | |
ee66c593 | 181 | utils.flushTests(done) |
8c308c2b C |
182 | } else { |
183 | done() | |
184 | } | |
185 | }) | |
186 | }) | |
187 | })() |