aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/check-params/pods.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests/api/check-params/pods.js')
-rw-r--r--server/tests/api/check-params/pods.js204
1 files changed, 204 insertions, 0 deletions
diff --git a/server/tests/api/check-params/pods.js b/server/tests/api/check-params/pods.js
new file mode 100644
index 000000000..2f85af644
--- /dev/null
+++ b/server/tests/api/check-params/pods.js
@@ -0,0 +1,204 @@
1'use strict'
2
3const request = require('supertest')
4const series = require('async/series')
5
6const loginUtils = require('../../utils/login')
7const requestsUtils = require('../../utils/requests')
8const serversUtils = require('../../utils/servers')
9const usersUtils = require('../../utils/users')
10
11describe('Test pods API validators', function () {
12 const path = '/api/v1/pods/'
13 let server = null
14
15 // ---------------------------------------------------------------
16
17 before(function (done) {
18 this.timeout(20000)
19
20 series([
21 function (next) {
22 serversUtils.flushTests(next)
23 },
24 function (next) {
25 serversUtils.runServer(1, function (server1) {
26 server = server1
27
28 next()
29 })
30 },
31 function (next) {
32 loginUtils.loginAndGetAccessToken(server, function (err, token) {
33 if (err) throw err
34 server.accessToken = token
35
36 next()
37 })
38 }
39 ], done)
40 })
41
42 describe('When making friends', function () {
43 let userAccessToken = null
44
45 before(function (done) {
46 usersUtils.createUser(server.url, server.accessToken, 'user1', 'password', function () {
47 server.user = {
48 username: 'user1',
49 password: 'password'
50 }
51
52 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
53 if (err) throw err
54
55 userAccessToken = accessToken
56
57 done()
58 })
59 })
60 })
61
62 describe('When making friends', function () {
63 const body = {
64 hosts: [ 'localhost:9002' ]
65 }
66
67 it('Should fail without hosts', function (done) {
68 request(server.url)
69 .post(path + '/makefriends')
70 .set('Authorization', 'Bearer ' + server.accessToken)
71 .set('Accept', 'application/json')
72 .expect(400, done)
73 })
74
75 it('Should fail if hosts is not an array', function (done) {
76 request(server.url)
77 .post(path + '/makefriends')
78 .send({ hosts: 'localhost:9002' })
79 .set('Authorization', 'Bearer ' + server.accessToken)
80 .set('Accept', 'application/json')
81 .expect(400, done)
82 })
83
84 it('Should fail if the array is not composed by hosts', function (done) {
85 request(server.url)
86 .post(path + '/makefriends')
87 .send({ hosts: [ 'localhost:9002', 'localhost:coucou' ] })
88 .set('Authorization', 'Bearer ' + server.accessToken)
89 .set('Accept', 'application/json')
90 .expect(400, done)
91 })
92
93 it('Should fail if the array is composed with http schemes', function (done) {
94 request(server.url)
95 .post(path + '/makefriends')
96 .send({ hosts: [ 'localhost:9002', 'http://localhost:9003' ] })
97 .set('Authorization', 'Bearer ' + server.accessToken)
98 .set('Accept', 'application/json')
99 .expect(400, done)
100 })
101
102 it('Should fail if hosts are not unique', function (done) {
103 request(server.url)
104 .post(path + '/makefriends')
105 .send({ urls: [ 'localhost:9002', 'localhost:9002' ] })
106 .set('Authorization', 'Bearer ' + server.accessToken)
107 .set('Accept', 'application/json')
108 .expect(400, done)
109 })
110
111 it('Should fail with a invalid token', function (done) {
112 request(server.url)
113 .post(path + '/makefriends')
114 .send(body)
115 .set('Authorization', 'Bearer faketoken')
116 .set('Accept', 'application/json')
117 .expect(401, done)
118 })
119
120 it('Should fail if the user is not an administrator', function (done) {
121 request(server.url)
122 .post(path + '/makefriends')
123 .send(body)
124 .set('Authorization', 'Bearer ' + userAccessToken)
125 .set('Accept', 'application/json')
126 .expect(403, done)
127 })
128 })
129
130 describe('When quitting friends', function () {
131 it('Should fail with a invalid token', function (done) {
132 request(server.url)
133 .get(path + '/quitfriends')
134 .query({ start: 'hello' })
135 .set('Authorization', 'Bearer faketoken')
136 .set('Accept', 'application/json')
137 .expect(401, done)
138 })
139
140 it('Should fail if the user is not an administrator', function (done) {
141 request(server.url)
142 .get(path + '/quitfriends')
143 .query({ start: 'hello' })
144 .set('Authorization', 'Bearer ' + userAccessToken)
145 .set('Accept', 'application/json')
146 .expect(403, done)
147 })
148 })
149 })
150
151 describe('When adding a pod', function () {
152 it('Should fail with nothing', function (done) {
153 const data = {}
154 requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
155 })
156
157 it('Should fail without public key', function (done) {
158 const data = {
159 host: 'coucou.com'
160 }
161 requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
162 })
163
164 it('Should fail without an host', function (done) {
165 const data = {
166 publicKey: 'mysuperpublickey'
167 }
168 requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
169 })
170
171 it('Should fail with an incorrect host', function (done) {
172 const data = {
173 host: 'http://coucou.com',
174 publicKey: 'mysuperpublickey'
175 }
176 requestsUtils.makePostBodyRequest(server.url, path, null, data, function () {
177 data.host = 'http://coucou'
178 requestsUtils.makePostBodyRequest(server.url, path, null, data, function () {
179 data.host = 'coucou'
180 requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
181 })
182 })
183 })
184
185 it('Should succeed with the correct parameters', function (done) {
186 const data = {
187 host: 'coucou.com',
188 publicKey: 'mysuperpublickey'
189 }
190 requestsUtils.makePostBodyRequest(server.url, path, null, data, done, 200)
191 })
192 })
193
194 after(function (done) {
195 process.kill(-server.app.pid)
196
197 // Keep the logs if the test failed
198 if (this.ok) {
199 serversUtils.flushTests(done)
200 } else {
201 done()
202 }
203 })
204})