]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/pods.js
Add email to users
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / pods.js
CommitLineData
efe923bc
C
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
ad4a8a1c 42 describe('When managing friends', function () {
efe923bc
C
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 = {
ad4a8a1c 159 email: 'testexample.com',
efe923bc
C
160 host: 'coucou.com'
161 }
162 requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
163 })
164
ad4a8a1c
C
165 it('Should fail without an email', function (done) {
166 const data = {
167 host: 'coucou.com',
168 publicKey: 'mysuperpublickey'
169 }
170 requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
171 })
172
173 it('Should fail without an invalid email', function (done) {
174 const data = {
175 host: 'coucou.com',
176 email: 'testexample.com',
177 publicKey: 'mysuperpublickey'
178 }
179 requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
180 })
181
efe923bc
C
182 it('Should fail without an host', function (done) {
183 const data = {
ad4a8a1c 184 email: 'testexample.com',
efe923bc
C
185 publicKey: 'mysuperpublickey'
186 }
187 requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
188 })
189
190 it('Should fail with an incorrect host', function (done) {
191 const data = {
192 host: 'http://coucou.com',
ad4a8a1c 193 email: 'testexample.com',
efe923bc
C
194 publicKey: 'mysuperpublickey'
195 }
196 requestsUtils.makePostBodyRequest(server.url, path, null, data, function () {
197 data.host = 'http://coucou'
198 requestsUtils.makePostBodyRequest(server.url, path, null, data, function () {
199 data.host = 'coucou'
200 requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
201 })
202 })
203 })
204
205 it('Should succeed with the correct parameters', function (done) {
206 const data = {
207 host: 'coucou.com',
ad4a8a1c 208 email: 'test@example.com',
efe923bc
C
209 publicKey: 'mysuperpublickey'
210 }
211 requestsUtils.makePostBodyRequest(server.url, path, null, data, done, 200)
212 })
b09ce645
C
213
214 it('Should fail with a host that already exists', function (done) {
215 const data = {
216 host: 'coucou.com',
ad4a8a1c 217 email: 'test@example.com',
b09ce645
C
218 publicKey: 'mysuperpublickey'
219 }
220 requestsUtils.makePostBodyRequest(server.url, path, null, data, done, 409)
221 })
efe923bc
C
222 })
223
224 after(function (done) {
225 process.kill(-server.app.pid)
226
227 // Keep the logs if the test failed
228 if (this.ok) {
229 serversUtils.flushTests(done)
230 } else {
231 done()
232 }
233 })
234})