diff options
Diffstat (limited to 'server/tests/api/friendsBasic.js')
-rw-r--r-- | server/tests/api/friendsBasic.js | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/server/tests/api/friendsBasic.js b/server/tests/api/friendsBasic.js new file mode 100644 index 000000000..328724936 --- /dev/null +++ b/server/tests/api/friendsBasic.js | |||
@@ -0,0 +1,185 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | var async = require('async') | ||
4 | var chai = require('chai') | ||
5 | var expect = chai.expect | ||
6 | var request = require('supertest') | ||
7 | |||
8 | var utils = require('./utils') | ||
9 | |||
10 | describe('Test basic friends', function () { | ||
11 | var apps = [] | ||
12 | var urls = [] | ||
13 | |||
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]) | ||
29 | |||
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) { | ||
51 | if (err) throw err | ||
52 | |||
53 | var result = res.body | ||
54 | expect(result).to.be.an('array') | ||
55 | expect(result.length).to.equal(0) | ||
56 | callback() | ||
57 | }) | ||
58 | }, done) | ||
59 | }) | ||
60 | |||
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 | ||
83 | |||
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]) | ||
88 | |||
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) { | ||
95 | if (err) throw err | ||
96 | |||
97 | var result = res.body | ||
98 | expect(result).to.be.an('array') | ||
99 | expect(result.length).to.equal(1) | ||
100 | expect(result[0].url).to.be.equal(urls[1]) | ||
101 | |||
102 | next() | ||
103 | }) | ||
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) | ||
123 | }, done) | ||
124 | }) | ||
125 | }) | ||
126 | |||
127 | it('Should not be allowed to make friend again', function (done) { | ||
128 | utils.makeFriends(urls[1], 409, done) | ||
129 | }) | ||
130 | |||
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 | ||
141 | |||
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) { | ||
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) | ||
158 | expect(result[0].url).not.to.be.equal(urls[1]) | ||
159 | callback() | ||
160 | }) | ||
161 | }, next) | ||
162 | } | ||
163 | ], done) | ||
164 | }) | ||
165 | |||
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) | ||
171 | }) | ||
172 | }) | ||
173 | |||
174 | after(function (done) { | ||
175 | apps.forEach(function (app) { | ||
176 | process.kill(-app.pid) | ||
177 | }) | ||
178 | |||
179 | if (this.ok) { | ||
180 | utils.flushTests(done) | ||
181 | } else { | ||
182 | done() | ||
183 | } | ||
184 | }) | ||
185 | }) | ||