diff options
Diffstat (limited to 'server/tests/api/check-params/pods.ts')
-rw-r--r-- | server/tests/api/check-params/pods.ts | 259 |
1 files changed, 259 insertions, 0 deletions
diff --git a/server/tests/api/check-params/pods.ts b/server/tests/api/check-params/pods.ts new file mode 100644 index 000000000..27c080931 --- /dev/null +++ b/server/tests/api/check-params/pods.ts | |||
@@ -0,0 +1,259 @@ | |||
1 | /* tslint:disable:no-unused-expression */ | ||
2 | |||
3 | import * as request from 'supertest' | ||
4 | import 'mocha' | ||
5 | |||
6 | import { | ||
7 | ServerInfo, | ||
8 | flushTests, | ||
9 | runServer, | ||
10 | createUser, | ||
11 | loginAndGetAccessToken, | ||
12 | setAccessTokensToServers, | ||
13 | killallServers, | ||
14 | makePostBodyRequest | ||
15 | } from '../../utils' | ||
16 | |||
17 | describe('Test pods API validators', function () { | ||
18 | const path = '/api/v1/pods/' | ||
19 | let server: ServerInfo | ||
20 | |||
21 | // --------------------------------------------------------------- | ||
22 | |||
23 | before(async function () { | ||
24 | this.timeout(45000) | ||
25 | |||
26 | await flushTests() | ||
27 | server = await runServer(1) | ||
28 | |||
29 | await setAccessTokensToServers([ server ]) | ||
30 | }) | ||
31 | |||
32 | describe('When managing friends', function () { | ||
33 | let userAccessToken = null | ||
34 | |||
35 | before(async function () { | ||
36 | await createUser(server.url, server.accessToken, 'user1', 'password') | ||
37 | server.user = { | ||
38 | username: 'user1', | ||
39 | password: 'password' | ||
40 | } | ||
41 | |||
42 | userAccessToken = await loginAndGetAccessToken(server) | ||
43 | }) | ||
44 | |||
45 | describe('When making friends', function () { | ||
46 | const body = { | ||
47 | hosts: [ 'localhost:9002' ] | ||
48 | } | ||
49 | |||
50 | it('Should fail without hosts', async function () { | ||
51 | await request(server.url) | ||
52 | .post(path + '/makefriends') | ||
53 | .set('Authorization', 'Bearer ' + server.accessToken) | ||
54 | .set('Accept', 'application/json') | ||
55 | .expect(400) | ||
56 | }) | ||
57 | |||
58 | it('Should fail if hosts is not an array', async function () { | ||
59 | await request(server.url) | ||
60 | .post(path + '/makefriends') | ||
61 | .send({ hosts: 'localhost:9002' }) | ||
62 | .set('Authorization', 'Bearer ' + server.accessToken) | ||
63 | .set('Accept', 'application/json') | ||
64 | .expect(400) | ||
65 | }) | ||
66 | |||
67 | it('Should fail if the array is not composed by hosts', async function () { | ||
68 | await request(server.url) | ||
69 | .post(path + '/makefriends') | ||
70 | .send({ hosts: [ 'localhost:9002', 'localhost:coucou' ] }) | ||
71 | .set('Authorization', 'Bearer ' + server.accessToken) | ||
72 | .set('Accept', 'application/json') | ||
73 | .expect(400) | ||
74 | }) | ||
75 | |||
76 | it('Should fail if the array is composed with http schemes', async function () { | ||
77 | await request(server.url) | ||
78 | .post(path + '/makefriends') | ||
79 | .send({ hosts: [ 'localhost:9002', 'http://localhost:9003' ] }) | ||
80 | .set('Authorization', 'Bearer ' + server.accessToken) | ||
81 | .set('Accept', 'application/json') | ||
82 | .expect(400) | ||
83 | }) | ||
84 | |||
85 | it('Should fail if hosts are not unique', async function () { | ||
86 | await request(server.url) | ||
87 | .post(path + '/makefriends') | ||
88 | .send({ urls: [ 'localhost:9002', 'localhost:9002' ] }) | ||
89 | .set('Authorization', 'Bearer ' + server.accessToken) | ||
90 | .set('Accept', 'application/json') | ||
91 | .expect(400) | ||
92 | }) | ||
93 | |||
94 | it('Should fail with an invalid token', async function () { | ||
95 | await request(server.url) | ||
96 | .post(path + '/makefriends') | ||
97 | .send(body) | ||
98 | .set('Authorization', 'Bearer faketoken') | ||
99 | .set('Accept', 'application/json') | ||
100 | .expect(401) | ||
101 | }) | ||
102 | |||
103 | it('Should fail if the user is not an administrator', async function () { | ||
104 | await request(server.url) | ||
105 | .post(path + '/makefriends') | ||
106 | .send(body) | ||
107 | .set('Authorization', 'Bearer ' + userAccessToken) | ||
108 | .set('Accept', 'application/json') | ||
109 | .expect(403) | ||
110 | }) | ||
111 | }) | ||
112 | |||
113 | describe('When quitting friends', function () { | ||
114 | it('Should fail with an invalid token', async function () { | ||
115 | await request(server.url) | ||
116 | .get(path + '/quitfriends') | ||
117 | .query({ start: 'hello' }) | ||
118 | .set('Authorization', 'Bearer faketoken') | ||
119 | .set('Accept', 'application/json') | ||
120 | .expect(401) | ||
121 | }) | ||
122 | |||
123 | it('Should fail if the user is not an administrator', async function () { | ||
124 | await request(server.url) | ||
125 | .get(path + '/quitfriends') | ||
126 | .query({ start: 'hello' }) | ||
127 | .set('Authorization', 'Bearer ' + userAccessToken) | ||
128 | .set('Accept', 'application/json') | ||
129 | .expect(403) | ||
130 | }) | ||
131 | }) | ||
132 | |||
133 | describe('When removing one friend', function () { | ||
134 | it('Should fail with an invalid token', async function () { | ||
135 | await request(server.url) | ||
136 | .delete(path + '/1') | ||
137 | .set('Authorization', 'Bearer faketoken') | ||
138 | .set('Accept', 'application/json') | ||
139 | .expect(401) | ||
140 | }) | ||
141 | |||
142 | it('Should fail if the user is not an administrator', async function () { | ||
143 | await request(server.url) | ||
144 | .delete(path + '/1') | ||
145 | .set('Authorization', 'Bearer ' + userAccessToken) | ||
146 | .set('Accept', 'application/json') | ||
147 | .expect(403) | ||
148 | }) | ||
149 | |||
150 | it('Should fail with an undefined id', async function () { | ||
151 | await request(server.url) | ||
152 | .delete(path + '/' + undefined) | ||
153 | .set('Authorization', 'Bearer ' + server.accessToken) | ||
154 | .set('Accept', 'application/json') | ||
155 | .expect(400) | ||
156 | }) | ||
157 | |||
158 | it('Should fail with an invalid id', async function () { | ||
159 | await request(server.url) | ||
160 | .delete(path + '/foobar') | ||
161 | .set('Authorization', 'Bearer ' + server.accessToken) | ||
162 | .set('Accept', 'application/json') | ||
163 | .expect(400) | ||
164 | }) | ||
165 | |||
166 | it('Should fail if the pod is not a friend', async function () { | ||
167 | await request(server.url) | ||
168 | .delete(path + '/-1') | ||
169 | .set('Authorization', 'Bearer ' + server.accessToken) | ||
170 | .set('Accept', 'application/json') | ||
171 | .expect(404) | ||
172 | }) | ||
173 | |||
174 | it('Should succeed with the correct parameters') | ||
175 | }) | ||
176 | }) | ||
177 | |||
178 | describe('When adding a pod', function () { | ||
179 | it('Should fail with nothing', async function () { | ||
180 | const fields = {} | ||
181 | await makePostBodyRequest({ url: server.url, path, fields }) | ||
182 | }) | ||
183 | |||
184 | it('Should fail without public key', async function () { | ||
185 | const fields = { | ||
186 | email: 'test.example.com', | ||
187 | host: 'coucou.com' | ||
188 | } | ||
189 | await makePostBodyRequest({ url: server.url, path, fields }) | ||
190 | }) | ||
191 | |||
192 | it('Should fail without an email', async function () { | ||
193 | const fields = { | ||
194 | host: 'coucou.com', | ||
195 | publicKey: 'my super public key' | ||
196 | } | ||
197 | await makePostBodyRequest({ url: server.url, path, fields }) | ||
198 | }) | ||
199 | |||
200 | it('Should fail without an invalid email', async function () { | ||
201 | const fields = { | ||
202 | host: 'coucou.com', | ||
203 | email: 'test.example.com', | ||
204 | publicKey: 'my super public key' | ||
205 | } | ||
206 | await makePostBodyRequest({ url: server.url, path, fields }) | ||
207 | }) | ||
208 | |||
209 | it('Should fail without a host', async function () { | ||
210 | const fields = { | ||
211 | email: 'test.example.com', | ||
212 | publicKey: 'my super public key' | ||
213 | } | ||
214 | await makePostBodyRequest({ url: server.url, path, fields }) | ||
215 | }) | ||
216 | |||
217 | it('Should fail with an incorrect host', async function () { | ||
218 | const fields = { | ||
219 | host: 'http://coucou.com', | ||
220 | email: 'test.example.com', | ||
221 | publicKey: 'my super public key' | ||
222 | } | ||
223 | await makePostBodyRequest({ url: server.url, path, fields }) | ||
224 | |||
225 | fields.host = 'http://coucou' | ||
226 | await makePostBodyRequest({ url: server.url, path, fields }) | ||
227 | |||
228 | fields.host = 'coucou' | ||
229 | await makePostBodyRequest({ url: server.url, path, fields }) | ||
230 | }) | ||
231 | |||
232 | it('Should succeed with the correct parameters', async function () { | ||
233 | const fields = { | ||
234 | host: 'coucou.com', | ||
235 | email: 'test@example.com', | ||
236 | publicKey: 'my super public key' | ||
237 | } | ||
238 | await makePostBodyRequest({ url: server.url, path, fields, statusCodeExpected: 200 }) | ||
239 | }) | ||
240 | |||
241 | it('Should fail with a host that already exists', async function () { | ||
242 | const fields = { | ||
243 | host: 'coucou.com', | ||
244 | email: 'test@example.com', | ||
245 | publicKey: 'my super public key' | ||
246 | } | ||
247 | await makePostBodyRequest({ url: server.url, path, fields, statusCodeExpected: 409 }) | ||
248 | }) | ||
249 | }) | ||
250 | |||
251 | after(async function () { | ||
252 | killallServers([ server ]) | ||
253 | |||
254 | // Keep the logs if the test failed | ||
255 | if (this['ok']) { | ||
256 | await flushTests() | ||
257 | } | ||
258 | }) | ||
259 | }) | ||