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