aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/utils.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-04-14 22:06:11 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-04-14 22:06:11 +0200
commit0c1cbbfe29d91c95f9c574b57adf067654b8b5b4 (patch)
treeb61e48caedea8881cbfd1159295f6c311b35be90 /server/tests/api/utils.js
parentc9bf7d30bd688e84b12dca03029dde6e9645a997 (diff)
downloadPeerTube-0c1cbbfe29d91c95f9c574b57adf067654b8b5b4.tar.gz
PeerTube-0c1cbbfe29d91c95f9c574b57adf067654b8b5b4.tar.zst
PeerTube-0c1cbbfe29d91c95f9c574b57adf067654b8b5b4.zip
Add authentications for routes that need it and adapts the tests
Diffstat (limited to 'server/tests/api/utils.js')
-rw-r--r--server/tests/api/utils.js99
1 files changed, 90 insertions, 9 deletions
diff --git a/server/tests/api/utils.js b/server/tests/api/utils.js
index 1b2f61059..d37e12cb2 100644
--- a/server/tests/api/utils.js
+++ b/server/tests/api/utils.js
@@ -11,6 +11,8 @@ const testUtils = {
11 getFriendsList: getFriendsList, 11 getFriendsList: getFriendsList,
12 getVideo: getVideo, 12 getVideo: getVideo,
13 getVideosList: getVideosList, 13 getVideosList: getVideosList,
14 login: login,
15 loginAndGetAccessToken: loginAndGetAccessToken,
14 makeFriends: makeFriends, 16 makeFriends: makeFriends,
15 quitFriends: quitFriends, 17 quitFriends: quitFriends,
16 removeVideo: removeVideo, 18 removeVideo: removeVideo,
@@ -59,6 +61,40 @@ function getVideosList (url, end) {
59 .end(end) 61 .end(end)
60} 62}
61 63
64function login (url, client, user, expected_status, end) {
65 if (!end) {
66 end = expected_status
67 expected_status = 200
68 }
69
70 const path = '/api/v1/users/token'
71
72 const body = {
73 client_id: client.id,
74 client_secret: client.secret,
75 username: user.username,
76 password: user.password,
77 response_type: 'code',
78 grant_type: 'password',
79 scope: 'upload'
80 }
81
82 request(url)
83 .post(path)
84 .type('form')
85 .send(body)
86 .expect(expected_status)
87 .end(end)
88}
89
90function loginAndGetAccessToken (server, callback) {
91 login(server.url, server.client, server.user, 200, function (err, res) {
92 if (err) return callback(err)
93
94 return callback(null, res.body.access_token)
95 })
96}
97
62function makeFriends (url, expected_status, callback) { 98function makeFriends (url, expected_status, callback) {
63 if (!callback) { 99 if (!callback) {
64 callback = expected_status 100 callback = expected_status
@@ -96,13 +132,19 @@ function quitFriends (url, callback) {
96 }) 132 })
97} 133}
98 134
99function removeVideo (url, id, end) { 135function removeVideo (url, token, id, expected_status, end) {
136 if (!end) {
137 end = expected_status
138 expected_status = 204
139 }
140
100 const path = '/api/v1/videos' 141 const path = '/api/v1/videos'
101 142
102 request(url) 143 request(url)
103 .delete(path + '/' + id) 144 .delete(path + '/' + id)
104 .set('Accept', 'application/json') 145 .set('Accept', 'application/json')
105 .expect(204) 146 .set('Authorization', 'Bearer ' + token)
147 .expect(expected_status)
106 .end(end) 148 .end(end)
107} 149}
108 150
@@ -133,12 +175,32 @@ function flushAndRunMultipleServers (total_servers, serversRun) {
133} 175}
134 176
135function runServer (number, callback) { 177function runServer (number, callback) {
136 const port = 9000 + number 178 const server = {
179 app: null,
180 url: `http://localhost:${9000 + number}`,
181 client: {
182 id: null,
183 secret: null
184 },
185 user: {
186 username: null,
187 password: null
188 }
189 }
190
191 // These actions are async so we need to be sure that they have both been done
137 const server_run_string = { 192 const server_run_string = {
138 'Connected to mongodb': false, 193 'Connected to mongodb': false,
139 'Server listening on port': false 194 'Server listening on port': false
140 } 195 }
141 196
197 const regexps = {
198 client_id: 'Client id: ([a-f0-9]+)',
199 client_secret: 'Client secret: (.+)',
200 user_username: 'Username: (.+)',
201 user_password: 'User password: (.+)'
202 }
203
142 // Share the environment 204 // Share the environment
143 const env = Object.create(process.env) 205 const env = Object.create(process.env)
144 env.NODE_ENV = 'test' 206 env.NODE_ENV = 'test'
@@ -149,9 +211,22 @@ function runServer (number, callback) {
149 detached: true 211 detached: true
150 } 212 }
151 213
152 const app = fork(pathUtils.join(__dirname, '../../../server.js'), [], options) 214 server.app = fork(pathUtils.join(__dirname, '../../../server.js'), [], options)
153 app.stdout.on('data', function onStdout (data) { 215 server.app.stdout.on('data', function onStdout (data) {
154 let dont_continue = false 216 let dont_continue = false
217
218 // Capture things if we want to
219 for (const key of Object.keys(regexps)) {
220 const regexp = regexps[key]
221 const matches = data.toString().match(regexp)
222 if (matches !== null) {
223 if (key === 'client_id') server.client.id = matches[1]
224 else if (key === 'client_secret') server.client.secret = matches[1]
225 else if (key === 'user_username') server.user.username = matches[1]
226 else if (key === 'user_password') server.user.password = matches[1]
227 }
228 }
229
155 // Check if all required sentences are here 230 // Check if all required sentences are here
156 for (const key of Object.keys(server_run_string)) { 231 for (const key of Object.keys(server_run_string)) {
157 if (data.toString().indexOf(key) !== -1) server_run_string[key] = true 232 if (data.toString().indexOf(key) !== -1) server_run_string[key] = true
@@ -161,8 +236,8 @@ function runServer (number, callback) {
161 // If no, there is maybe one thing not already initialized (mongodb...) 236 // If no, there is maybe one thing not already initialized (mongodb...)
162 if (dont_continue === true) return 237 if (dont_continue === true) return
163 238
164 app.stdout.removeListener('data', onStdout) 239 server.app.stdout.removeListener('data', onStdout)
165 callback(app, 'http://localhost:' + port) 240 callback(server)
166 }) 241 })
167} 242}
168 243
@@ -177,16 +252,22 @@ function searchVideo (url, search, end) {
177 .end(end) 252 .end(end)
178} 253}
179 254
180function uploadVideo (url, name, description, fixture, end) { 255function uploadVideo (url, access_token, name, description, fixture, special_status, end) {
256 if (!end) {
257 end = special_status
258 special_status = 204
259 }
260
181 const path = '/api/v1/videos' 261 const path = '/api/v1/videos'
182 262
183 request(url) 263 request(url)
184 .post(path) 264 .post(path)
185 .set('Accept', 'application/json') 265 .set('Accept', 'application/json')
266 .set('Authorization', 'Bearer ' + access_token)
186 .field('name', name) 267 .field('name', name)
187 .field('description', description) 268 .field('description', description)
188 .attach('videofile', pathUtils.join(__dirname, 'fixtures', fixture)) 269 .attach('videofile', pathUtils.join(__dirname, 'fixtures', fixture))
189 .expect(204) 270 .expect(special_status)
190 .end(end) 271 .end(end)
191} 272}
192 273