1 /* tslint:disable:no-unused-expression */
3 import * as request from 'supertest'
11 loginAndGetAccessToken,
12 setAccessTokensToServers,
17 describe('Test pods API validators', function () {
18 let server: ServerInfo
20 // ---------------------------------------------------------------
22 before(async function () {
26 server = await runServer(1)
28 await setAccessTokensToServers([ server ])
31 describe('When managing friends', function () {
32 const path = '/api/v1/pods/'
33 let userAccessToken = null
35 before(async function () {
36 await createUser(server.url, server.accessToken, 'user1', 'password')
42 userAccessToken = await loginAndGetAccessToken(server)
45 describe('When making friends', function () {
47 hosts: [ 'localhost:9002' ]
50 it('Should fail without hosts', async function () {
51 await request(server.url)
52 .post(path + '/make-friends')
53 .set('Authorization', 'Bearer ' + server.accessToken)
54 .set('Accept', 'application/json')
58 it('Should fail if hosts is not an array', async function () {
59 await request(server.url)
60 .post(path + '/make-friends')
61 .send({ hosts: 'localhost:9002' })
62 .set('Authorization', 'Bearer ' + server.accessToken)
63 .set('Accept', 'application/json')
67 it('Should fail if the array is not composed by hosts', async function () {
68 await request(server.url)
69 .post(path + '/make-friends')
70 .send({ hosts: [ 'localhost:9002', 'localhost:coucou' ] })
71 .set('Authorization', 'Bearer ' + server.accessToken)
72 .set('Accept', 'application/json')
76 it('Should fail if the array is composed with http schemes', async function () {
77 await request(server.url)
78 .post(path + '/make-friends')
79 .send({ hosts: [ 'localhost:9002', 'http://localhost:9003' ] })
80 .set('Authorization', 'Bearer ' + server.accessToken)
81 .set('Accept', 'application/json')
85 it('Should fail if hosts are not unique', async function () {
86 await request(server.url)
87 .post(path + '/make-friends')
88 .send({ urls: [ 'localhost:9002', 'localhost:9002' ] })
89 .set('Authorization', 'Bearer ' + server.accessToken)
90 .set('Accept', 'application/json')
94 it('Should fail with an invalid token', async function () {
95 await request(server.url)
96 .post(path + '/make-friends')
98 .set('Authorization', 'Bearer fake_token')
99 .set('Accept', 'application/json')
103 it('Should fail if the user is not an administrator', async function () {
104 await request(server.url)
105 .post(path + '/make-friends')
107 .set('Authorization', 'Bearer ' + userAccessToken)
108 .set('Accept', 'application/json')
113 describe('When listing friends', function () {
114 it('Should fail with a bad start pagination', async function () {
115 await request(server.url)
117 .query({ start: 'hello' })
118 .set('Accept', 'application/json')
122 it('Should fail with a bad count pagination', async function () {
123 await request(server.url)
125 .query({ count: 'hello' })
126 .set('Accept', 'application/json')
130 it('Should fail with an incorrect sort', async function () {
131 await request(server.url)
133 .query({ sort: 'hello' })
134 .set('Accept', 'application/json')
139 describe('When quitting friends', function () {
140 it('Should fail with an invalid token', async function () {
141 await request(server.url)
142 .get(path + '/quit-friends')
143 .query({ start: 'hello' })
144 .set('Authorization', 'Bearer faketoken')
145 .set('Accept', 'application/json')
149 it('Should fail if the user is not an administrator', async function () {
150 await request(server.url)
151 .get(path + '/quit-friends')
152 .query({ start: 'hello' })
153 .set('Authorization', 'Bearer ' + userAccessToken)
154 .set('Accept', 'application/json')
159 describe('When removing one friend', function () {
160 it('Should fail with an invalid token', async function () {
161 await request(server.url)
163 .set('Authorization', 'Bearer faketoken')
164 .set('Accept', 'application/json')
168 it('Should fail if the user is not an administrator', async function () {
169 await request(server.url)
171 .set('Authorization', 'Bearer ' + userAccessToken)
172 .set('Accept', 'application/json')
176 it('Should fail with an undefined id', async function () {
177 await request(server.url)
178 .delete(path + '/' + undefined)
179 .set('Authorization', 'Bearer ' + server.accessToken)
180 .set('Accept', 'application/json')
184 it('Should fail with an invalid id', async function () {
185 await request(server.url)
186 .delete(path + '/foobar')
187 .set('Authorization', 'Bearer ' + server.accessToken)
188 .set('Accept', 'application/json')
192 it('Should fail if the pod is not a friend', async function () {
193 await request(server.url)
194 .delete(path + '/-1')
195 .set('Authorization', 'Bearer ' + server.accessToken)
196 .set('Accept', 'application/json')
200 it('Should succeed with the correct parameters')
204 describe('When adding a pod from remote', function () {
205 const path = '/api/v1/remote/pods/add'
207 it('Should fail with nothing', async function () {
209 await makePostBodyRequest({ url: server.url, path, fields })
212 it('Should fail without public key', async function () {
214 email: 'test.example.com',
217 await makePostBodyRequest({ url: server.url, path, fields })
220 it('Should fail without an email', async function () {
223 publicKey: 'my super public key'
225 await makePostBodyRequest({ url: server.url, path, fields })
228 it('Should fail without an invalid email', async function () {
231 email: 'test.example.com',
232 publicKey: 'my super public key'
234 await makePostBodyRequest({ url: server.url, path, fields })
237 it('Should fail without a host', async function () {
239 email: 'test.example.com',
240 publicKey: 'my super public key'
242 await makePostBodyRequest({ url: server.url, path, fields })
245 it('Should fail with an incorrect host', async function () {
247 host: 'http://coucou.com',
248 email: 'test.example.com',
249 publicKey: 'my super public key'
251 await makePostBodyRequest({ url: server.url, path, fields })
253 fields.host = 'http://coucou'
254 await makePostBodyRequest({ url: server.url, path, fields })
256 fields.host = 'coucou'
257 await makePostBodyRequest({ url: server.url, path, fields })
260 it('Should succeed with the correct parameters', async function () {
263 email: 'test@example.com',
264 publicKey: 'my super public key'
266 await makePostBodyRequest({ url: server.url, path, fields, statusCodeExpected: 200 })
269 it('Should fail with a host that already exists', async function () {
272 email: 'test@example.com',
273 publicKey: 'my super public key'
275 await makePostBodyRequest({ url: server.url, path, fields, statusCodeExpected: 409 })
279 after(async function () {
280 killallServers([ server ])
282 // Keep the logs if the test failed