]>
Commit | Line | Data |
---|---|---|
72329aaa C |
1 | /* eslint-disable no-unused-expressions */ |
2 | ||
efe923bc C |
3 | 'use strict' |
4 | ||
5 | const request = require('supertest') | |
6 | const series = require('async/series') | |
7 | ||
8 | const loginUtils = require('../../utils/login') | |
9 | const requestsUtils = require('../../utils/requests') | |
10 | const serversUtils = require('../../utils/servers') | |
11 | const usersUtils = require('../../utils/users') | |
12 | ||
13 | describe('Test pods API validators', function () { | |
14 | const path = '/api/v1/pods/' | |
15 | let server = null | |
16 | ||
17 | // --------------------------------------------------------------- | |
18 | ||
19 | before(function (done) { | |
20 | this.timeout(20000) | |
21 | ||
22 | series([ | |
23 | function (next) { | |
24 | serversUtils.flushTests(next) | |
25 | }, | |
26 | function (next) { | |
27 | serversUtils.runServer(1, function (server1) { | |
28 | server = server1 | |
29 | ||
30 | next() | |
31 | }) | |
32 | }, | |
33 | function (next) { | |
34 | loginUtils.loginAndGetAccessToken(server, function (err, token) { | |
35 | if (err) throw err | |
36 | server.accessToken = token | |
37 | ||
38 | next() | |
39 | }) | |
40 | } | |
41 | ], done) | |
42 | }) | |
43 | ||
ad4a8a1c | 44 | describe('When managing friends', function () { |
efe923bc C |
45 | let userAccessToken = null |
46 | ||
47 | before(function (done) { | |
48 | usersUtils.createUser(server.url, server.accessToken, 'user1', 'password', function () { | |
49 | server.user = { | |
50 | username: 'user1', | |
51 | password: 'password' | |
52 | } | |
53 | ||
54 | loginUtils.loginAndGetAccessToken(server, function (err, accessToken) { | |
55 | if (err) throw err | |
56 | ||
57 | userAccessToken = accessToken | |
58 | ||
59 | done() | |
60 | }) | |
61 | }) | |
62 | }) | |
63 | ||
64 | describe('When making friends', function () { | |
65 | const body = { | |
66 | hosts: [ 'localhost:9002' ] | |
67 | } | |
68 | ||
69 | it('Should fail without hosts', function (done) { | |
70 | request(server.url) | |
71 | .post(path + '/makefriends') | |
72 | .set('Authorization', 'Bearer ' + server.accessToken) | |
73 | .set('Accept', 'application/json') | |
74 | .expect(400, done) | |
75 | }) | |
76 | ||
77 | it('Should fail if hosts is not an array', function (done) { | |
78 | request(server.url) | |
79 | .post(path + '/makefriends') | |
80 | .send({ hosts: 'localhost:9002' }) | |
81 | .set('Authorization', 'Bearer ' + server.accessToken) | |
82 | .set('Accept', 'application/json') | |
83 | .expect(400, done) | |
84 | }) | |
85 | ||
86 | it('Should fail if the array is not composed by hosts', function (done) { | |
87 | request(server.url) | |
88 | .post(path + '/makefriends') | |
89 | .send({ hosts: [ 'localhost:9002', 'localhost:coucou' ] }) | |
90 | .set('Authorization', 'Bearer ' + server.accessToken) | |
91 | .set('Accept', 'application/json') | |
92 | .expect(400, done) | |
93 | }) | |
94 | ||
95 | it('Should fail if the array is composed with http schemes', function (done) { | |
96 | request(server.url) | |
97 | .post(path + '/makefriends') | |
98 | .send({ hosts: [ 'localhost:9002', 'http://localhost:9003' ] }) | |
99 | .set('Authorization', 'Bearer ' + server.accessToken) | |
100 | .set('Accept', 'application/json') | |
101 | .expect(400, done) | |
102 | }) | |
103 | ||
104 | it('Should fail if hosts are not unique', function (done) { | |
105 | request(server.url) | |
106 | .post(path + '/makefriends') | |
107 | .send({ urls: [ 'localhost:9002', 'localhost:9002' ] }) | |
108 | .set('Authorization', 'Bearer ' + server.accessToken) | |
109 | .set('Accept', 'application/json') | |
110 | .expect(400, done) | |
111 | }) | |
112 | ||
113 | it('Should fail with a invalid token', function (done) { | |
114 | request(server.url) | |
115 | .post(path + '/makefriends') | |
116 | .send(body) | |
117 | .set('Authorization', 'Bearer faketoken') | |
118 | .set('Accept', 'application/json') | |
119 | .expect(401, done) | |
120 | }) | |
121 | ||
122 | it('Should fail if the user is not an administrator', function (done) { | |
123 | request(server.url) | |
124 | .post(path + '/makefriends') | |
125 | .send(body) | |
126 | .set('Authorization', 'Bearer ' + userAccessToken) | |
127 | .set('Accept', 'application/json') | |
128 | .expect(403, done) | |
129 | }) | |
130 | }) | |
131 | ||
132 | describe('When quitting friends', function () { | |
133 | it('Should fail with a invalid token', function (done) { | |
134 | request(server.url) | |
135 | .get(path + '/quitfriends') | |
136 | .query({ start: 'hello' }) | |
137 | .set('Authorization', 'Bearer faketoken') | |
138 | .set('Accept', 'application/json') | |
139 | .expect(401, done) | |
140 | }) | |
141 | ||
142 | it('Should fail if the user is not an administrator', function (done) { | |
143 | request(server.url) | |
144 | .get(path + '/quitfriends') | |
145 | .query({ start: 'hello' }) | |
146 | .set('Authorization', 'Bearer ' + userAccessToken) | |
147 | .set('Accept', 'application/json') | |
148 | .expect(403, done) | |
149 | }) | |
150 | }) | |
151 | }) | |
152 | ||
153 | describe('When adding a pod', function () { | |
154 | it('Should fail with nothing', function (done) { | |
155 | const data = {} | |
156 | requestsUtils.makePostBodyRequest(server.url, path, null, data, done) | |
157 | }) | |
158 | ||
159 | it('Should fail without public key', function (done) { | |
160 | const data = { | |
ad4a8a1c | 161 | email: 'testexample.com', |
efe923bc C |
162 | host: 'coucou.com' |
163 | } | |
164 | requestsUtils.makePostBodyRequest(server.url, path, null, data, done) | |
165 | }) | |
166 | ||
ad4a8a1c C |
167 | it('Should fail without an email', function (done) { |
168 | const data = { | |
169 | host: 'coucou.com', | |
170 | publicKey: 'mysuperpublickey' | |
171 | } | |
172 | requestsUtils.makePostBodyRequest(server.url, path, null, data, done) | |
173 | }) | |
174 | ||
175 | it('Should fail without an invalid email', function (done) { | |
176 | const data = { | |
177 | host: 'coucou.com', | |
178 | email: 'testexample.com', | |
179 | publicKey: 'mysuperpublickey' | |
180 | } | |
181 | requestsUtils.makePostBodyRequest(server.url, path, null, data, done) | |
182 | }) | |
183 | ||
efe923bc C |
184 | it('Should fail without an host', function (done) { |
185 | const data = { | |
ad4a8a1c | 186 | email: 'testexample.com', |
efe923bc C |
187 | publicKey: 'mysuperpublickey' |
188 | } | |
189 | requestsUtils.makePostBodyRequest(server.url, path, null, data, done) | |
190 | }) | |
191 | ||
192 | it('Should fail with an incorrect host', function (done) { | |
193 | const data = { | |
194 | host: 'http://coucou.com', | |
ad4a8a1c | 195 | email: 'testexample.com', |
efe923bc C |
196 | publicKey: 'mysuperpublickey' |
197 | } | |
198 | requestsUtils.makePostBodyRequest(server.url, path, null, data, function () { | |
199 | data.host = 'http://coucou' | |
200 | requestsUtils.makePostBodyRequest(server.url, path, null, data, function () { | |
201 | data.host = 'coucou' | |
202 | requestsUtils.makePostBodyRequest(server.url, path, null, data, done) | |
203 | }) | |
204 | }) | |
205 | }) | |
206 | ||
207 | it('Should succeed with the correct parameters', function (done) { | |
208 | const data = { | |
209 | host: 'coucou.com', | |
ad4a8a1c | 210 | email: 'test@example.com', |
efe923bc C |
211 | publicKey: 'mysuperpublickey' |
212 | } | |
213 | requestsUtils.makePostBodyRequest(server.url, path, null, data, done, 200) | |
214 | }) | |
b09ce645 C |
215 | |
216 | it('Should fail with a host that already exists', function (done) { | |
217 | const data = { | |
218 | host: 'coucou.com', | |
ad4a8a1c | 219 | email: 'test@example.com', |
b09ce645 C |
220 | publicKey: 'mysuperpublickey' |
221 | } | |
222 | requestsUtils.makePostBodyRequest(server.url, path, null, data, done, 409) | |
223 | }) | |
efe923bc C |
224 | }) |
225 | ||
226 | after(function (done) { | |
227 | process.kill(-server.app.pid) | |
228 | ||
229 | // Keep the logs if the test failed | |
230 | if (this.ok) { | |
231 | serversUtils.flushTests(done) | |
232 | } else { | |
233 | done() | |
234 | } | |
235 | }) | |
236 | }) |