1 /* tslint:disable:no-unused-expression */
3 import * as request from 'supertest'
11 loginAndGetAccessToken,
12 setAccessTokensToServers,
17 describe('Test pods API validators', function () {
18 const path = '/api/v1/pods/'
19 let server: ServerInfo
21 // ---------------------------------------------------------------
23 before(async function () {
27 server = await runServer(1)
29 await setAccessTokensToServers([ server ])
32 describe('When managing friends', function () {
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 + '/makefriends')
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 + '/makefriends')
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 + '/makefriends')
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 + '/makefriends')
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 + '/makefriends')
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 + '/makefriends')
98 .set('Authorization', 'Bearer faketoken')
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 + '/makefriends')
107 .set('Authorization', 'Bearer ' + userAccessToken)
108 .set('Accept', 'application/json')
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')
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')
133 describe('When removing one friend', function () {
134 it('Should fail with an invalid token', async function () {
135 await request(server.url)
137 .set('Authorization', 'Bearer faketoken')
138 .set('Accept', 'application/json')
142 it('Should fail if the user is not an administrator', async function () {
143 await request(server.url)
145 .set('Authorization', 'Bearer ' + userAccessToken)
146 .set('Accept', 'application/json')
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')
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')
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')
174 it('Should succeed with the correct parameters')
178 describe('When adding a pod', function () {
179 it('Should fail with nothing', async function () {
181 await makePostBodyRequest({ url: server.url, path, fields })
184 it('Should fail without public key', async function () {
186 email: 'test.example.com',
189 await makePostBodyRequest({ url: server.url, path, fields })
192 it('Should fail without an email', async function () {
195 publicKey: 'my super public key'
197 await makePostBodyRequest({ url: server.url, path, fields })
200 it('Should fail without an invalid email', async function () {
203 email: 'test.example.com',
204 publicKey: 'my super public key'
206 await makePostBodyRequest({ url: server.url, path, fields })
209 it('Should fail without a host', async function () {
211 email: 'test.example.com',
212 publicKey: 'my super public key'
214 await makePostBodyRequest({ url: server.url, path, fields })
217 it('Should fail with an incorrect host', async function () {
219 host: 'http://coucou.com',
220 email: 'test.example.com',
221 publicKey: 'my super public key'
223 await makePostBodyRequest({ url: server.url, path, fields })
225 fields.host = 'http://coucou'
226 await makePostBodyRequest({ url: server.url, path, fields })
228 fields.host = 'coucou'
229 await makePostBodyRequest({ url: server.url, path, fields })
232 it('Should succeed with the correct parameters', async function () {
235 email: 'test@example.com',
236 publicKey: 'my super public key'
238 await makePostBodyRequest({ url: server.url, path, fields, statusCodeExpected: 200 })
241 it('Should fail with a host that already exists', async function () {
244 email: 'test@example.com',
245 publicKey: 'my super public key'
247 await makePostBodyRequest({ url: server.url, path, fields, statusCodeExpected: 409 })
251 after(async function () {
252 killallServers([ server ])
254 // Keep the logs if the test failed