diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-04-14 22:06:11 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-04-14 22:06:11 +0200 |
commit | 0c1cbbfe29d91c95f9c574b57adf067654b8b5b4 (patch) | |
tree | b61e48caedea8881cbfd1159295f6c311b35be90 | |
parent | c9bf7d30bd688e84b12dca03029dde6e9645a997 (diff) | |
download | PeerTube-0c1cbbfe29d91c95f9c574b57adf067654b8b5b4.tar.gz PeerTube-0c1cbbfe29d91c95f9c574b57adf067654b8b5b4.tar.zst PeerTube-0c1cbbfe29d91c95f9c574b57adf067654b8b5b4.zip |
Add authentications for routes that need it and adapts the tests
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | server/controllers/api/v1/users.js | 7 | ||||
-rw-r--r-- | server/controllers/api/v1/videos.js | 11 | ||||
-rw-r--r-- | server/initializers/installer.js | 6 | ||||
-rw-r--r-- | server/middlewares/oauth2.js | 26 | ||||
-rw-r--r-- | server/models/users.js | 2 | ||||
-rw-r--r-- | server/models/videos.js | 3 | ||||
-rw-r--r-- | server/tests/api/checkParams.js | 68 | ||||
-rw-r--r-- | server/tests/api/friendsAdvanced.js | 42 | ||||
-rw-r--r-- | server/tests/api/friendsBasic.js | 60 | ||||
-rw-r--r-- | server/tests/api/index.js | 1 | ||||
-rw-r--r-- | server/tests/api/multiplePods.js | 67 | ||||
-rw-r--r-- | server/tests/api/singlePod.js | 33 | ||||
-rw-r--r-- | server/tests/api/users.js | 133 | ||||
-rw-r--r-- | server/tests/api/utils.js | 99 |
15 files changed, 416 insertions, 144 deletions
diff --git a/package.json b/package.json index 076cf6fdb..678804ed2 100644 --- a/package.json +++ b/package.json | |||
@@ -48,7 +48,7 @@ | |||
48 | "dezalgo": "^1.0.3", | 48 | "dezalgo": "^1.0.3", |
49 | "electron-spawn": "https://github.com/Chocobozzz/electron-spawn", | 49 | "electron-spawn": "https://github.com/Chocobozzz/electron-spawn", |
50 | "express": "^4.12.4", | 50 | "express": "^4.12.4", |
51 | "express-oauth-server": "https://github.com/oauthjs/express-oauth-server", | 51 | "express-oauth-server": "https://github.com/Chocobozzz/express-oauth-server", |
52 | "express-validator": "^2.11.0", | 52 | "express-validator": "^2.11.0", |
53 | "js-yaml": "^3.5.4", | 53 | "js-yaml": "^3.5.4", |
54 | "lodash-node": "^3.10.2", | 54 | "lodash-node": "^3.10.2", |
diff --git a/server/controllers/api/v1/users.js b/server/controllers/api/v1/users.js index acb860c66..f45b47077 100644 --- a/server/controllers/api/v1/users.js +++ b/server/controllers/api/v1/users.js | |||
@@ -1,15 +1,14 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | var express = require('express') | 3 | const express = require('express') |
4 | var oAuth2 = require('../../../middlewares/oauth2') | 4 | const oAuth2 = require('../../../middlewares/oauth2') |
5 | 5 | ||
6 | const middleware = require('../../../middlewares') | 6 | const middleware = require('../../../middlewares') |
7 | const cacheMiddleware = middleware.cache | 7 | const cacheMiddleware = middleware.cache |
8 | 8 | ||
9 | const router = express.Router() | 9 | const router = express.Router() |
10 | 10 | ||
11 | router.post('/token', cacheMiddleware.cache(false), oAuth2.token(), success) | 11 | router.post('/token', cacheMiddleware.cache(false), oAuth2.token, success) |
12 | router.get('/authenticate', cacheMiddleware.cache(false), oAuth2.authenticate(), success) | ||
13 | 12 | ||
14 | // --------------------------------------------------------------------------- | 13 | // --------------------------------------------------------------------------- |
15 | 14 | ||
diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js index 97d3e6b5a..d25ca95f7 100644 --- a/server/controllers/api/v1/videos.js +++ b/server/controllers/api/v1/videos.js | |||
@@ -8,6 +8,7 @@ const multer = require('multer') | |||
8 | const logger = require('../../../helpers/logger') | 8 | const logger = require('../../../helpers/logger') |
9 | const friends = require('../../../lib/friends') | 9 | const friends = require('../../../lib/friends') |
10 | const middleware = require('../../../middlewares') | 10 | const middleware = require('../../../middlewares') |
11 | const oAuth2 = require('../../../middlewares/oauth2') | ||
11 | const cacheMiddleware = middleware.cache | 12 | const cacheMiddleware = middleware.cache |
12 | const reqValidator = middleware.reqValidators.videos | 13 | const reqValidator = middleware.reqValidators.videos |
13 | const Videos = require('../../../models/videos') // model | 14 | const Videos = require('../../../models/videos') // model |
@@ -38,9 +39,9 @@ const storage = multer.diskStorage({ | |||
38 | const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }]) | 39 | const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }]) |
39 | 40 | ||
40 | router.get('/', cacheMiddleware.cache(false), listVideos) | 41 | router.get('/', cacheMiddleware.cache(false), listVideos) |
41 | router.post('/', reqFiles, reqValidator.videosAdd, cacheMiddleware.cache(false), addVideo) | 42 | router.post('/', oAuth2.authenticate, reqFiles, reqValidator.videosAdd, cacheMiddleware.cache(false), addVideo) |
42 | router.get('/:id', reqValidator.videosGet, cacheMiddleware.cache(false), getVideos) | 43 | router.get('/:id', reqValidator.videosGet, cacheMiddleware.cache(false), getVideos) |
43 | router.delete('/:id', reqValidator.videosRemove, cacheMiddleware.cache(false), removeVideo) | 44 | router.delete('/:id', oAuth2.authenticate, reqValidator.videosRemove, cacheMiddleware.cache(false), removeVideo) |
44 | router.get('/search/:name', reqValidator.videosSearch, cacheMiddleware.cache(false), searchVideos) | 45 | router.get('/search/:name', reqValidator.videosSearch, cacheMiddleware.cache(false), searchVideos) |
45 | 46 | ||
46 | // --------------------------------------------------------------------------- | 47 | // --------------------------------------------------------------------------- |
@@ -63,7 +64,8 @@ function addVideo (req, res, next) { | |||
63 | name: video_infos.name, | 64 | name: video_infos.name, |
64 | namePath: video_file.filename, | 65 | namePath: video_file.filename, |
65 | description: video_infos.description, | 66 | description: video_infos.description, |
66 | magnetUri: torrent.magnetURI | 67 | magnetUri: torrent.magnetURI, |
68 | author: res.locals.oauth.token.user.username | ||
67 | } | 69 | } |
68 | 70 | ||
69 | Videos.add(video_data, function (err) { | 71 | Videos.add(video_data, function (err) { |
@@ -141,7 +143,8 @@ function getFormatedVideo (video_obj) { | |||
141 | description: video_obj.description, | 143 | description: video_obj.description, |
142 | podUrl: video_obj.podUrl, | 144 | podUrl: video_obj.podUrl, |
143 | isLocal: videos.getVideoState(video_obj).owned, | 145 | isLocal: videos.getVideoState(video_obj).owned, |
144 | magnetUri: video_obj.magnetUri | 146 | magnetUri: video_obj.magnetUri, |
147 | author: video_obj.author | ||
145 | } | 148 | } |
146 | 149 | ||
147 | return formated_video | 150 | return formated_video |
diff --git a/server/initializers/installer.js b/server/initializers/installer.js index 750eb2c59..ec9175f34 100644 --- a/server/initializers/installer.js +++ b/server/initializers/installer.js | |||
@@ -57,12 +57,12 @@ function createOAuthClientIfNotExist (callback) { | |||
57 | logger.info('Creating a default OAuth Client.') | 57 | logger.info('Creating a default OAuth Client.') |
58 | 58 | ||
59 | // TODO: generate password | 59 | // TODO: generate password |
60 | const password = 'megustalabanana' | 60 | const secret = 'megustalabanana' |
61 | Users.createClient(password, [ 'password' ], function (err, id) { | 61 | Users.createClient(secret, [ 'password' ], function (err, id) { |
62 | if (err) return callback(err) | 62 | if (err) return callback(err) |
63 | 63 | ||
64 | logger.info('Client id: ' + id) | 64 | logger.info('Client id: ' + id) |
65 | logger.info('Client password: ' + password) | 65 | logger.info('Client secret: ' + secret) |
66 | 66 | ||
67 | return callback(null) | 67 | return callback(null) |
68 | }) | 68 | }) |
diff --git a/server/middlewares/oauth2.js b/server/middlewares/oauth2.js index a1fa61fbb..1defdc02e 100644 --- a/server/middlewares/oauth2.js +++ b/server/middlewares/oauth2.js | |||
@@ -2,10 +2,34 @@ | |||
2 | 2 | ||
3 | const OAuthServer = require('express-oauth-server') | 3 | const OAuthServer = require('express-oauth-server') |
4 | 4 | ||
5 | const oAuth2 = new OAuthServer({ | 5 | const logger = require('../helpers/logger') |
6 | |||
7 | const oAuthServer = new OAuthServer({ | ||
6 | model: require('../models/users') | 8 | model: require('../models/users') |
7 | }) | 9 | }) |
8 | 10 | ||
11 | const oAuth2 = { | ||
12 | authenticate: authenticate, | ||
13 | token: token | ||
14 | } | ||
15 | |||
16 | function authenticate (req, res, next) { | ||
17 | oAuthServer.authenticate()(req, res, function (err) { | ||
18 | if (err) { | ||
19 | logger.error('Cannot authenticate.', { error: err }) | ||
20 | return res.sendStatus(500) | ||
21 | } | ||
22 | |||
23 | if (res.statusCode === 401 || res.statusCode === 400) return res.end() | ||
24 | |||
25 | return next() | ||
26 | }) | ||
27 | } | ||
28 | |||
29 | function token (req, res, next) { | ||
30 | return oAuthServer.token()(req, res, next) | ||
31 | } | ||
32 | |||
9 | // --------------------------------------------------------------------------- | 33 | // --------------------------------------------------------------------------- |
10 | 34 | ||
11 | module.exports = oAuth2 | 35 | module.exports = oAuth2 |
diff --git a/server/models/users.js b/server/models/users.js index 367c206ab..046fe462d 100644 --- a/server/models/users.js +++ b/server/models/users.js | |||
@@ -104,7 +104,7 @@ function saveToken (token, client, user) { | |||
104 | } | 104 | } |
105 | 105 | ||
106 | return OAuthTokensDB.create(token_to_create, function (err, token_created) { | 106 | return OAuthTokensDB.create(token_to_create, function (err, token_created) { |
107 | if (err) throw err // node-oauth2-server library use Promise.try | 107 | if (err) throw err // node-oauth2-server library uses Promise.try |
108 | 108 | ||
109 | token_created.client = client | 109 | token_created.client = client |
110 | token_created.user = user | 110 | token_created.user = user |
diff --git a/server/models/videos.js b/server/models/videos.js index 0141cbb7f..13ef2295a 100644 --- a/server/models/videos.js +++ b/server/models/videos.js | |||
@@ -21,7 +21,8 @@ const videosSchema = mongoose.Schema({ | |||
21 | namePath: String, | 21 | namePath: String, |
22 | description: String, | 22 | description: String, |
23 | magnetUri: String, | 23 | magnetUri: String, |
24 | podUrl: String | 24 | podUrl: String, |
25 | author: String | ||
25 | }) | 26 | }) |
26 | const VideosDB = mongoose.model('videos', videosSchema) | 27 | const VideosDB = mongoose.model('videos', videosSchema) |
27 | 28 | ||
diff --git a/server/tests/api/checkParams.js b/server/tests/api/checkParams.js index dcc190e97..59ee0bfc3 100644 --- a/server/tests/api/checkParams.js +++ b/server/tests/api/checkParams.js | |||
@@ -9,17 +9,18 @@ const request = require('supertest') | |||
9 | const utils = require('./utils') | 9 | const utils = require('./utils') |
10 | 10 | ||
11 | describe('Test parameters validator', function () { | 11 | describe('Test parameters validator', function () { |
12 | let app = null | 12 | let server = null |
13 | let url = '' | ||
14 | 13 | ||
15 | function makePostRequest (path, fields, attach, done, fail) { | 14 | function makePostRequest (path, token, fields, attach, done, fail) { |
16 | let status_code = 400 | 15 | let status_code = 400 |
17 | if (fail !== undefined && fail === false) status_code = 200 | 16 | if (fail !== undefined && fail === false) status_code = 200 |
18 | 17 | ||
19 | const req = request(url) | 18 | const req = request(server.url) |
20 | .post(path) | 19 | .post(path) |
21 | .set('Accept', 'application/json') | 20 | .set('Accept', 'application/json') |
22 | 21 | ||
22 | if (token) req.set('Authorization', 'Bearer ' + token) | ||
23 | |||
23 | Object.keys(fields).forEach(function (field) { | 24 | Object.keys(fields).forEach(function (field) { |
24 | const value = fields[field] | 25 | const value = fields[field] |
25 | req.field(field, value) | 26 | req.field(field, value) |
@@ -32,7 +33,7 @@ describe('Test parameters validator', function () { | |||
32 | let status_code = 400 | 33 | let status_code = 400 |
33 | if (fail !== undefined && fail === false) status_code = 200 | 34 | if (fail !== undefined && fail === false) status_code = 200 |
34 | 35 | ||
35 | request(url) | 36 | request(server.url) |
36 | .post(path) | 37 | .post(path) |
37 | .set('Accept', 'application/json') | 38 | .set('Accept', 'application/json') |
38 | .send(fields) | 39 | .send(fields) |
@@ -49,9 +50,17 @@ describe('Test parameters validator', function () { | |||
49 | utils.flushTests(next) | 50 | utils.flushTests(next) |
50 | }, | 51 | }, |
51 | function (next) { | 52 | function (next) { |
52 | utils.runServer(1, function (app1, url1) { | 53 | utils.runServer(1, function (server1) { |
53 | app = app1 | 54 | server = server1 |
54 | url = url1 | 55 | |
56 | next() | ||
57 | }) | ||
58 | }, | ||
59 | function (next) { | ||
60 | utils.loginAndGetAccessToken(server, function (err, token) { | ||
61 | if (err) throw err | ||
62 | server.access_token = token | ||
63 | |||
55 | next() | 64 | next() |
56 | }) | 65 | }) |
57 | } | 66 | } |
@@ -118,7 +127,7 @@ describe('Test parameters validator', function () { | |||
118 | 127 | ||
119 | describe('When searching a video', function () { | 128 | describe('When searching a video', function () { |
120 | it('Should fail with nothing', function (done) { | 129 | it('Should fail with nothing', function (done) { |
121 | request(url) | 130 | request(server.url) |
122 | .get(pathUtils.join(path, 'search')) | 131 | .get(pathUtils.join(path, 'search')) |
123 | .set('Accept', 'application/json') | 132 | .set('Accept', 'application/json') |
124 | .expect(400, done) | 133 | .expect(400, done) |
@@ -129,7 +138,7 @@ describe('Test parameters validator', function () { | |||
129 | it('Should fail with nothing', function (done) { | 138 | it('Should fail with nothing', function (done) { |
130 | const data = {} | 139 | const data = {} |
131 | const attach = {} | 140 | const attach = {} |
132 | makePostRequest(path, data, attach, done) | 141 | makePostRequest(path, server.access_token, data, attach, done) |
133 | }) | 142 | }) |
134 | 143 | ||
135 | it('Should fail without name', function (done) { | 144 | it('Should fail without name', function (done) { |
@@ -139,7 +148,7 @@ describe('Test parameters validator', function () { | |||
139 | const attach = { | 148 | const attach = { |
140 | 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') | 149 | 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') |
141 | } | 150 | } |
142 | makePostRequest(path, data, attach, done) | 151 | makePostRequest(path, server.access_token, data, attach, done) |
143 | }) | 152 | }) |
144 | 153 | ||
145 | it('Should fail with a long name', function (done) { | 154 | it('Should fail with a long name', function (done) { |
@@ -150,7 +159,7 @@ describe('Test parameters validator', function () { | |||
150 | const attach = { | 159 | const attach = { |
151 | 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') | 160 | 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') |
152 | } | 161 | } |
153 | makePostRequest(path, data, attach, done) | 162 | makePostRequest(path, server.access_token, data, attach, done) |
154 | }) | 163 | }) |
155 | 164 | ||
156 | it('Should fail without description', function (done) { | 165 | it('Should fail without description', function (done) { |
@@ -160,7 +169,7 @@ describe('Test parameters validator', function () { | |||
160 | const attach = { | 169 | const attach = { |
161 | 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') | 170 | 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') |
162 | } | 171 | } |
163 | makePostRequest(path, data, attach, done) | 172 | makePostRequest(path, server.access_token, data, attach, done) |
164 | }) | 173 | }) |
165 | 174 | ||
166 | it('Should fail with a long description', function (done) { | 175 | it('Should fail with a long description', function (done) { |
@@ -173,7 +182,7 @@ describe('Test parameters validator', function () { | |||
173 | const attach = { | 182 | const attach = { |
174 | 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') | 183 | 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') |
175 | } | 184 | } |
176 | makePostRequest(path, data, attach, done) | 185 | makePostRequest(path, server.access_token, data, attach, done) |
177 | }) | 186 | }) |
178 | 187 | ||
179 | it('Should fail without an input file', function (done) { | 188 | it('Should fail without an input file', function (done) { |
@@ -182,7 +191,7 @@ describe('Test parameters validator', function () { | |||
182 | description: 'my super description' | 191 | description: 'my super description' |
183 | } | 192 | } |
184 | const attach = {} | 193 | const attach = {} |
185 | makePostRequest(path, data, attach, done) | 194 | makePostRequest(path, server.access_token, data, attach, done) |
186 | }) | 195 | }) |
187 | 196 | ||
188 | it('Should fail without an incorrect input file', function (done) { | 197 | it('Should fail without an incorrect input file', function (done) { |
@@ -193,7 +202,7 @@ describe('Test parameters validator', function () { | |||
193 | const attach = { | 202 | const attach = { |
194 | 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short_fake.webm') | 203 | 'videofile': pathUtils.join(__dirname, '..', 'fixtures', 'video_short_fake.webm') |
195 | } | 204 | } |
196 | makePostRequest(path, data, attach, done) | 205 | makePostRequest(path, server.access_token, data, attach, done) |
197 | }) | 206 | }) |
198 | 207 | ||
199 | it('Should succeed with the correct parameters', function (done) { | 208 | it('Should succeed with the correct parameters', function (done) { |
@@ -204,11 +213,11 @@ describe('Test parameters validator', function () { | |||
204 | const attach = { | 213 | const attach = { |
205 | 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') | 214 | 'videofile': pathUtils.join(__dirname, 'fixtures', 'video_short.webm') |
206 | } | 215 | } |
207 | makePostRequest(path, data, attach, function () { | 216 | makePostRequest(path, server.access_token, data, attach, function () { |
208 | attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4') | 217 | attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.mp4') |
209 | makePostRequest(path, data, attach, function () { | 218 | makePostRequest(path, server.access_token, data, attach, function () { |
210 | attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv') | 219 | attach.videofile = pathUtils.join(__dirname, 'fixtures', 'video_short.ogv') |
211 | makePostRequest(path, data, attach, done, true) | 220 | makePostRequest(path, server.access_token, data, attach, done, true) |
212 | }, true) | 221 | }, true) |
213 | }, true) | 222 | }, true) |
214 | }) | 223 | }) |
@@ -216,7 +225,7 @@ describe('Test parameters validator', function () { | |||
216 | 225 | ||
217 | describe('When getting a video', function () { | 226 | describe('When getting a video', function () { |
218 | it('Should return the list of the videos with nothing', function (done) { | 227 | it('Should return the list of the videos with nothing', function (done) { |
219 | request(url) | 228 | request(server.url) |
220 | .get(path) | 229 | .get(path) |
221 | .set('Accept', 'application/json') | 230 | .set('Accept', 'application/json') |
222 | .expect(200) | 231 | .expect(200) |
@@ -232,14 +241,14 @@ describe('Test parameters validator', function () { | |||
232 | }) | 241 | }) |
233 | 242 | ||
234 | it('Should fail without a mongodb id', function (done) { | 243 | it('Should fail without a mongodb id', function (done) { |
235 | request(url) | 244 | request(server.url) |
236 | .get(path + 'coucou') | 245 | .get(path + 'coucou') |
237 | .set('Accept', 'application/json') | 246 | .set('Accept', 'application/json') |
238 | .expect(400, done) | 247 | .expect(400, done) |
239 | }) | 248 | }) |
240 | 249 | ||
241 | it('Should return 404 with an incorrect video', function (done) { | 250 | it('Should return 404 with an incorrect video', function (done) { |
242 | request(url) | 251 | request(server.url) |
243 | .get(path + '123456789012345678901234') | 252 | .get(path + '123456789012345678901234') |
244 | .set('Accept', 'application/json') | 253 | .set('Accept', 'application/json') |
245 | .expect(404, done) | 254 | .expect(404, done) |
@@ -250,20 +259,23 @@ describe('Test parameters validator', function () { | |||
250 | 259 | ||
251 | describe('When removing a video', function () { | 260 | describe('When removing a video', function () { |
252 | it('Should have 404 with nothing', function (done) { | 261 | it('Should have 404 with nothing', function (done) { |
253 | request(url) | 262 | request(server.url) |
254 | .delete(path) | 263 | .delete(path) |
255 | .expect(400, done) | 264 | .set('Authorization', 'Bearer ' + server.access_token) |
265 | .expect(400, done) | ||
256 | }) | 266 | }) |
257 | 267 | ||
258 | it('Should fail without a mongodb id', function (done) { | 268 | it('Should fail without a mongodb id', function (done) { |
259 | request(url) | 269 | request(server.url) |
260 | .delete(path + 'hello') | 270 | .delete(path + 'hello') |
271 | .set('Authorization', 'Bearer ' + server.access_token) | ||
261 | .expect(400, done) | 272 | .expect(400, done) |
262 | }) | 273 | }) |
263 | 274 | ||
264 | it('Should fail with a video which does not exist', function (done) { | 275 | it('Should fail with a video which does not exist', function (done) { |
265 | request(url) | 276 | request(server.url) |
266 | .delete(path + '123456789012345678901234') | 277 | .delete(path + '123456789012345678901234') |
278 | .set('Authorization', 'Bearer ' + server.access_token) | ||
267 | .expect(404, done) | 279 | .expect(404, done) |
268 | }) | 280 | }) |
269 | 281 | ||
@@ -288,7 +300,7 @@ describe('Test parameters validator', function () { | |||
288 | }) | 300 | }) |
289 | 301 | ||
290 | after(function (done) { | 302 | after(function (done) { |
291 | process.kill(-app.pid) | 303 | process.kill(-server.app.pid) |
292 | 304 | ||
293 | // Keep the logs if the test failed | 305 | // Keep the logs if the test failed |
294 | if (this.ok) { | 306 | if (this.ok) { |
diff --git a/server/tests/api/friendsAdvanced.js b/server/tests/api/friendsAdvanced.js index 6c4b7567f..6f18648d7 100644 --- a/server/tests/api/friendsAdvanced.js +++ b/server/tests/api/friendsAdvanced.js | |||
@@ -7,41 +7,48 @@ const expect = chai.expect | |||
7 | const utils = require('./utils') | 7 | const utils = require('./utils') |
8 | 8 | ||
9 | describe('Test advanced friends', function () { | 9 | describe('Test advanced friends', function () { |
10 | let apps = [] | 10 | let servers = [] |
11 | let urls = [] | ||
12 | 11 | ||
13 | function makeFriends (pod_number, callback) { | 12 | function makeFriends (pod_number, callback) { |
14 | return utils.makeFriends(urls[pod_number - 1], callback) | 13 | return utils.makeFriends(servers[pod_number - 1].url, callback) |
15 | } | 14 | } |
16 | 15 | ||
17 | function quitFriends (pod_number, callback) { | 16 | function quitFriends (pod_number, callback) { |
18 | return utils.quitFriends(urls[pod_number - 1], callback) | 17 | return utils.quitFriends(servers[pod_number - 1].url, callback) |
19 | } | 18 | } |
20 | 19 | ||
21 | function getFriendsList (pod_number, end) { | 20 | function getFriendsList (pod_number, end) { |
22 | return utils.getFriendsList(urls[pod_number - 1], end) | 21 | return utils.getFriendsList(servers[pod_number - 1].url, end) |
23 | } | 22 | } |
24 | 23 | ||
25 | function uploadVideo (pod_number, callback) { | 24 | function uploadVideo (pod_number, callback) { |
26 | const name = 'my super video' | 25 | const name = 'my super video' |
27 | const description = 'my super description' | 26 | const description = 'my super description' |
28 | const fixture = 'video_short.webm' | 27 | const fixture = 'video_short.webm' |
28 | const server = servers[pod_number - 1] | ||
29 | 29 | ||
30 | return utils.uploadVideo(urls[pod_number - 1], name, description, fixture, callback) | 30 | return utils.uploadVideo(server.url, server.access_token, name, description, fixture, callback) |
31 | } | 31 | } |
32 | 32 | ||
33 | function getVideos (pod_number, callback) { | 33 | function getVideos (pod_number, callback) { |
34 | return utils.getVideosList(urls[pod_number - 1], callback) | 34 | return utils.getVideosList(servers[pod_number - 1].url, callback) |
35 | } | 35 | } |
36 | 36 | ||
37 | // --------------------------------------------------------------- | 37 | // --------------------------------------------------------------- |
38 | 38 | ||
39 | before(function (done) { | 39 | before(function (done) { |
40 | this.timeout(30000) | 40 | this.timeout(30000) |
41 | utils.flushAndRunMultipleServers(6, function (apps_run, urls_run) { | 41 | utils.flushAndRunMultipleServers(6, function (servers_run, urls_run) { |
42 | apps = apps_run | 42 | servers = servers_run |
43 | urls = urls_run | 43 | |
44 | done() | 44 | async.each(servers, function (server, callback_each) { |
45 | utils.loginAndGetAccessToken(server, function (err, access_token) { | ||
46 | if (err) return callback_each(err) | ||
47 | |||
48 | server.access_token = access_token | ||
49 | callback_each() | ||
50 | }) | ||
51 | }, done) | ||
45 | }) | 52 | }) |
46 | }) | 53 | }) |
47 | 54 | ||
@@ -121,7 +128,7 @@ describe('Test advanced friends', function () { | |||
121 | }, | 128 | }, |
122 | // Kill pod 4 | 129 | // Kill pod 4 |
123 | function (next) { | 130 | function (next) { |
124 | apps[3].kill() | 131 | servers[3].app.kill() |
125 | next() | 132 | next() |
126 | }, | 133 | }, |
127 | // Expulse pod 4 from pod 1 and 2 | 134 | // Expulse pod 4 from pod 1 and 2 |
@@ -145,8 +152,8 @@ describe('Test advanced friends', function () { | |||
145 | }, | 152 | }, |
146 | // Rerun server 4 | 153 | // Rerun server 4 |
147 | function (next) { | 154 | function (next) { |
148 | utils.runServer(4, function (app, url) { | 155 | utils.runServer(4, function (server) { |
149 | apps[3] = app | 156 | servers[3].app = server.app |
150 | next() | 157 | next() |
151 | }) | 158 | }) |
152 | }, | 159 | }, |
@@ -156,7 +163,6 @@ describe('Test advanced friends', function () { | |||
156 | 163 | ||
157 | // Pod 4 didn't know pod 1 and 2 removed it | 164 | // Pod 4 didn't know pod 1 and 2 removed it |
158 | expect(res.body.length).to.equal(3) | 165 | expect(res.body.length).to.equal(3) |
159 | |||
160 | next() | 166 | next() |
161 | }) | 167 | }) |
162 | }, | 168 | }, |
@@ -174,7 +180,7 @@ describe('Test advanced friends', function () { | |||
174 | const result = res.body | 180 | const result = res.body |
175 | expect(result.length).to.equal(3) | 181 | expect(result.length).to.equal(3) |
176 | for (const pod of result) { | 182 | for (const pod of result) { |
177 | expect(pod.url).not.equal(urls[3]) | 183 | expect(pod.url).not.equal(servers[3].url) |
178 | } | 184 | } |
179 | 185 | ||
180 | done() | 186 | done() |
@@ -237,8 +243,8 @@ describe('Test advanced friends', function () { | |||
237 | }) | 243 | }) |
238 | 244 | ||
239 | after(function (done) { | 245 | after(function (done) { |
240 | apps.forEach(function (app) { | 246 | servers.forEach(function (server) { |
241 | process.kill(-app.pid) | 247 | process.kill(-server.app.pid) |
242 | }) | 248 | }) |
243 | 249 | ||
244 | if (this.ok) { | 250 | if (this.ok) { |
diff --git a/server/tests/api/friendsBasic.js b/server/tests/api/friendsBasic.js index 62eac51ec..49e51804f 100644 --- a/server/tests/api/friendsBasic.js +++ b/server/tests/api/friendsBasic.js | |||
@@ -8,17 +8,16 @@ const request = require('supertest') | |||
8 | const utils = require('./utils') | 8 | const utils = require('./utils') |
9 | 9 | ||
10 | describe('Test basic friends', function () { | 10 | describe('Test basic friends', function () { |
11 | let apps = [] | 11 | let servers = [] |
12 | let urls = [] | ||
13 | 12 | ||
14 | function testMadeFriends (urls, url_to_test, callback) { | 13 | function testMadeFriends (servers, server_to_test, callback) { |
15 | const friends = [] | 14 | const friends = [] |
16 | for (let i = 0; i < urls.length; i++) { | 15 | for (let i = 0; i < servers.length; i++) { |
17 | if (urls[i] === url_to_test) continue | 16 | if (servers[i].url === server_to_test.url) continue |
18 | friends.push(urls[i]) | 17 | friends.push(servers[i].url) |
19 | } | 18 | } |
20 | 19 | ||
21 | utils.getFriendsList(url_to_test, function (err, res) { | 20 | utils.getFriendsList(server_to_test.url, function (err, res) { |
22 | if (err) throw err | 21 | if (err) throw err |
23 | 22 | ||
24 | const result = res.body | 23 | const result = res.body |
@@ -27,7 +26,7 @@ describe('Test basic friends', function () { | |||
27 | expect(result.length).to.equal(2) | 26 | expect(result.length).to.equal(2) |
28 | expect(result_urls[0]).to.not.equal(result_urls[1]) | 27 | expect(result_urls[0]).to.not.equal(result_urls[1]) |
29 | 28 | ||
30 | const error_string = 'Friends url do not correspond for ' + url_to_test | 29 | const error_string = 'Friends url do not correspond for ' + server_to_test.url |
31 | expect(friends).to.contain(result_urls[0], error_string) | 30 | expect(friends).to.contain(result_urls[0], error_string) |
32 | expect(friends).to.contain(result_urls[1], error_string) | 31 | expect(friends).to.contain(result_urls[1], error_string) |
33 | callback() | 32 | callback() |
@@ -38,16 +37,15 @@ describe('Test basic friends', function () { | |||
38 | 37 | ||
39 | before(function (done) { | 38 | before(function (done) { |
40 | this.timeout(20000) | 39 | this.timeout(20000) |
41 | utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) { | 40 | utils.flushAndRunMultipleServers(3, function (servers_run, urls_run) { |
42 | apps = apps_run | 41 | servers = servers_run |
43 | urls = urls_run | ||
44 | done() | 42 | done() |
45 | }) | 43 | }) |
46 | }) | 44 | }) |
47 | 45 | ||
48 | it('Should not have friends', function (done) { | 46 | it('Should not have friends', function (done) { |
49 | async.each(urls, function (url, callback) { | 47 | async.each(servers, function (server, callback) { |
50 | utils.getFriendsList(url, function (err, res) { | 48 | utils.getFriendsList(server.url, function (err, res) { |
51 | if (err) throw err | 49 | if (err) throw err |
52 | 50 | ||
53 | const result = res.body | 51 | const result = res.body |
@@ -66,7 +64,7 @@ describe('Test basic friends', function () { | |||
66 | async.series([ | 64 | async.series([ |
67 | // The second pod make friend with the third | 65 | // The second pod make friend with the third |
68 | function (next) { | 66 | function (next) { |
69 | request(urls[1]) | 67 | request(servers[1].url) |
70 | .get(path) | 68 | .get(path) |
71 | .set('Accept', 'application/json') | 69 | .set('Accept', 'application/json') |
72 | .expect(204) | 70 | .expect(204) |
@@ -78,33 +76,33 @@ describe('Test basic friends', function () { | |||
78 | }, | 76 | }, |
79 | // The second pod should have the third as a friend | 77 | // The second pod should have the third as a friend |
80 | function (next) { | 78 | function (next) { |
81 | utils.getFriendsList(urls[1], function (err, res) { | 79 | utils.getFriendsList(servers[1].url, function (err, res) { |
82 | if (err) throw err | 80 | if (err) throw err |
83 | 81 | ||
84 | const result = res.body | 82 | const result = res.body |
85 | expect(result).to.be.an('array') | 83 | expect(result).to.be.an('array') |
86 | expect(result.length).to.equal(1) | 84 | expect(result.length).to.equal(1) |
87 | expect(result[0].url).to.be.equal(urls[2]) | 85 | expect(result[0].url).to.be.equal(servers[2].url) |
88 | 86 | ||
89 | next() | 87 | next() |
90 | }) | 88 | }) |
91 | }, | 89 | }, |
92 | // Same here, the third pod should have the second pod as a friend | 90 | // Same here, the third pod should have the second pod as a friend |
93 | function (next) { | 91 | function (next) { |
94 | utils.getFriendsList(urls[2], function (err, res) { | 92 | utils.getFriendsList(servers[2].url, function (err, res) { |
95 | if (err) throw err | 93 | if (err) throw err |
96 | 94 | ||
97 | const result = res.body | 95 | const result = res.body |
98 | expect(result).to.be.an('array') | 96 | expect(result).to.be.an('array') |
99 | expect(result.length).to.equal(1) | 97 | expect(result.length).to.equal(1) |
100 | expect(result[0].url).to.be.equal(urls[1]) | 98 | expect(result[0].url).to.be.equal(servers[1].url) |
101 | 99 | ||
102 | next() | 100 | next() |
103 | }) | 101 | }) |
104 | }, | 102 | }, |
105 | // Finally the first pod make friend with the second pod | 103 | // Finally the first pod make friend with the second pod |
106 | function (next) { | 104 | function (next) { |
107 | request(urls[0]) | 105 | request(servers[0].url) |
108 | .get(path) | 106 | .get(path) |
109 | .set('Accept', 'application/json') | 107 | .set('Accept', 'application/json') |
110 | .expect(204) | 108 | .expect(204) |
@@ -118,25 +116,25 @@ describe('Test basic friends', function () { | |||
118 | // Now each pod should be friend with the other ones | 116 | // Now each pod should be friend with the other ones |
119 | function (err) { | 117 | function (err) { |
120 | if (err) throw err | 118 | if (err) throw err |
121 | async.each(urls, function (url, callback) { | 119 | async.each(servers, function (server, callback) { |
122 | testMadeFriends(urls, url, callback) | 120 | testMadeFriends(servers, server, callback) |
123 | }, done) | 121 | }, done) |
124 | }) | 122 | }) |
125 | }) | 123 | }) |
126 | 124 | ||
127 | it('Should not be allowed to make friend again', function (done) { | 125 | it('Should not be allowed to make friend again', function (done) { |
128 | utils.makeFriends(urls[1], 409, done) | 126 | utils.makeFriends(servers[1].url, 409, done) |
129 | }) | 127 | }) |
130 | 128 | ||
131 | it('Should quit friends of pod 2', function (done) { | 129 | it('Should quit friends of pod 2', function (done) { |
132 | async.series([ | 130 | async.series([ |
133 | // Pod 1 quit friends | 131 | // Pod 1 quit friends |
134 | function (next) { | 132 | function (next) { |
135 | utils.quitFriends(urls[1], next) | 133 | utils.quitFriends(servers[1].url, next) |
136 | }, | 134 | }, |
137 | // Pod 1 should not have friends anymore | 135 | // Pod 1 should not have friends anymore |
138 | function (next) { | 136 | function (next) { |
139 | utils.getFriendsList(urls[1], function (err, res) { | 137 | utils.getFriendsList(servers[1].url, function (err, res) { |
140 | if (err) throw err | 138 | if (err) throw err |
141 | 139 | ||
142 | const result = res.body | 140 | const result = res.body |
@@ -148,14 +146,14 @@ describe('Test basic friends', function () { | |||
148 | }, | 146 | }, |
149 | // Other pods shouldn't have pod 1 too | 147 | // Other pods shouldn't have pod 1 too |
150 | function (next) { | 148 | function (next) { |
151 | async.each([ urls[0], urls[2] ], function (url, callback) { | 149 | async.each([ servers[0].url, servers[2].url ], function (url, callback) { |
152 | utils.getFriendsList(url, function (err, res) { | 150 | utils.getFriendsList(url, function (err, res) { |
153 | if (err) throw err | 151 | if (err) throw err |
154 | 152 | ||
155 | const result = res.body | 153 | const result = res.body |
156 | expect(result).to.be.an('array') | 154 | expect(result).to.be.an('array') |
157 | expect(result.length).to.equal(1) | 155 | expect(result.length).to.equal(1) |
158 | expect(result[0].url).not.to.be.equal(urls[1]) | 156 | expect(result[0].url).not.to.be.equal(servers[1].url) |
159 | callback() | 157 | callback() |
160 | }) | 158 | }) |
161 | }, next) | 159 | }, next) |
@@ -164,16 +162,16 @@ describe('Test basic friends', function () { | |||
164 | }) | 162 | }) |
165 | 163 | ||
166 | it('Should allow pod 2 to make friend again', function (done) { | 164 | it('Should allow pod 2 to make friend again', function (done) { |
167 | utils.makeFriends(urls[1], function () { | 165 | utils.makeFriends(servers[1].url, function () { |
168 | async.each(urls, function (url, callback) { | 166 | async.each(servers, function (server, callback) { |
169 | testMadeFriends(urls, url, callback) | 167 | testMadeFriends(servers, server, callback) |
170 | }, done) | 168 | }, done) |
171 | }) | 169 | }) |
172 | }) | 170 | }) |
173 | 171 | ||
174 | after(function (done) { | 172 | after(function (done) { |
175 | apps.forEach(function (app) { | 173 | servers.forEach(function (server) { |
176 | process.kill(-app.pid) | 174 | process.kill(-server.app.pid) |
177 | }) | 175 | }) |
178 | 176 | ||
179 | if (this.ok) { | 177 | if (this.ok) { |
diff --git a/server/tests/api/index.js b/server/tests/api/index.js index 9c4fdd48a..61c9a7aca 100644 --- a/server/tests/api/index.js +++ b/server/tests/api/index.js | |||
@@ -3,6 +3,7 @@ | |||
3 | // Order of the tests we want to execute | 3 | // Order of the tests we want to execute |
4 | require('./checkParams') | 4 | require('./checkParams') |
5 | require('./friendsBasic') | 5 | require('./friendsBasic') |
6 | require('./users') | ||
6 | require('./singlePod') | 7 | require('./singlePod') |
7 | require('./multiplePods') | 8 | require('./multiplePods') |
8 | require('./friendsAdvanced') | 9 | require('./friendsAdvanced') |
diff --git a/server/tests/api/multiplePods.js b/server/tests/api/multiplePods.js index 0e2355a55..b6545ca60 100644 --- a/server/tests/api/multiplePods.js +++ b/server/tests/api/multiplePods.js | |||
@@ -10,8 +10,7 @@ const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent')) | |||
10 | webtorrent.silent = true | 10 | webtorrent.silent = true |
11 | 11 | ||
12 | describe('Test multiple pods', function () { | 12 | describe('Test multiple pods', function () { |
13 | let apps = [] | 13 | let servers = [] |
14 | let urls = [] | ||
15 | const to_remove = [] | 14 | const to_remove = [] |
16 | 15 | ||
17 | before(function (done) { | 16 | before(function (done) { |
@@ -20,15 +19,25 @@ describe('Test multiple pods', function () { | |||
20 | async.series([ | 19 | async.series([ |
21 | // Run servers | 20 | // Run servers |
22 | function (next) { | 21 | function (next) { |
23 | utils.flushAndRunMultipleServers(3, function (apps_run, urls_run) { | 22 | utils.flushAndRunMultipleServers(3, function (servers_run) { |
24 | apps = apps_run | 23 | servers = servers_run |
25 | urls = urls_run | ||
26 | next() | 24 | next() |
27 | }) | 25 | }) |
28 | }, | 26 | }, |
27 | // Get the access tokens | ||
28 | function (next) { | ||
29 | async.each(servers, function (server, callback_each) { | ||
30 | utils.loginAndGetAccessToken(server, function (err, access_token) { | ||
31 | if (err) return callback_each(err) | ||
32 | |||
33 | server.access_token = access_token | ||
34 | callback_each() | ||
35 | }) | ||
36 | }, next) | ||
37 | }, | ||
29 | // The second pod make friend with the third | 38 | // The second pod make friend with the third |
30 | function (next) { | 39 | function (next) { |
31 | utils.makeFriends(urls[1], next) | 40 | utils.makeFriends(servers[1].url, next) |
32 | }, | 41 | }, |
33 | // Wait for the request between pods | 42 | // Wait for the request between pods |
34 | function (next) { | 43 | function (next) { |
@@ -36,7 +45,7 @@ describe('Test multiple pods', function () { | |||
36 | }, | 45 | }, |
37 | // Pod 1 make friends too | 46 | // Pod 1 make friends too |
38 | function (next) { | 47 | function (next) { |
39 | utils.makeFriends(urls[0], next) | 48 | utils.makeFriends(servers[0].url, next) |
40 | }, | 49 | }, |
41 | function (next) { | 50 | function (next) { |
42 | webtorrent.create({ host: 'client', port: '1' }, next) | 51 | webtorrent.create({ host: 'client', port: '1' }, next) |
@@ -45,8 +54,8 @@ describe('Test multiple pods', function () { | |||
45 | }) | 54 | }) |
46 | 55 | ||
47 | it('Should not have videos for all pods', function (done) { | 56 | it('Should not have videos for all pods', function (done) { |
48 | async.each(urls, function (url, callback) { | 57 | async.each(servers, function (server, callback) { |
49 | utils.getVideosList(url, function (err, res) { | 58 | utils.getVideosList(server.url, function (err, res) { |
50 | if (err) throw err | 59 | if (err) throw err |
51 | 60 | ||
52 | expect(res.body).to.be.an('array') | 61 | expect(res.body).to.be.an('array') |
@@ -63,7 +72,7 @@ describe('Test multiple pods', function () { | |||
63 | 72 | ||
64 | async.series([ | 73 | async.series([ |
65 | function (next) { | 74 | function (next) { |
66 | utils.uploadVideo(urls[0], 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', next) | 75 | utils.uploadVideo(servers[0].url, servers[0].access_token, 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', next) |
67 | }, | 76 | }, |
68 | function (next) { | 77 | function (next) { |
69 | setTimeout(next, 11000) | 78 | setTimeout(next, 11000) |
@@ -72,10 +81,10 @@ describe('Test multiple pods', function () { | |||
72 | function (err) { | 81 | function (err) { |
73 | if (err) throw err | 82 | if (err) throw err |
74 | 83 | ||
75 | async.each(urls, function (url, callback) { | 84 | async.each(servers, function (server, callback) { |
76 | let base_magnet = null | 85 | let base_magnet = null |
77 | 86 | ||
78 | utils.getVideosList(url, function (err, res) { | 87 | utils.getVideosList(server.url, function (err, res) { |
79 | if (err) throw err | 88 | if (err) throw err |
80 | 89 | ||
81 | const videos = res.body | 90 | const videos = res.body |
@@ -106,7 +115,7 @@ describe('Test multiple pods', function () { | |||
106 | 115 | ||
107 | async.series([ | 116 | async.series([ |
108 | function (next) { | 117 | function (next) { |
109 | utils.uploadVideo(urls[1], 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', next) | 118 | utils.uploadVideo(servers[1].url, servers[1].access_token, 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', next) |
110 | }, | 119 | }, |
111 | function (next) { | 120 | function (next) { |
112 | setTimeout(next, 11000) | 121 | setTimeout(next, 11000) |
@@ -115,10 +124,10 @@ describe('Test multiple pods', function () { | |||
115 | function (err) { | 124 | function (err) { |
116 | if (err) throw err | 125 | if (err) throw err |
117 | 126 | ||
118 | async.each(urls, function (url, callback) { | 127 | async.each(servers, function (server, callback) { |
119 | let base_magnet = null | 128 | let base_magnet = null |
120 | 129 | ||
121 | utils.getVideosList(url, function (err, res) { | 130 | utils.getVideosList(server.url, function (err, res) { |
122 | if (err) throw err | 131 | if (err) throw err |
123 | 132 | ||
124 | const videos = res.body | 133 | const videos = res.body |
@@ -149,10 +158,10 @@ describe('Test multiple pods', function () { | |||
149 | 158 | ||
150 | async.series([ | 159 | async.series([ |
151 | function (next) { | 160 | function (next) { |
152 | utils.uploadVideo(urls[2], 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', next) | 161 | utils.uploadVideo(servers[2].url, servers[2].access_token, 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', next) |
153 | }, | 162 | }, |
154 | function (next) { | 163 | function (next) { |
155 | utils.uploadVideo(urls[2], 'my super name for pod 3-2', 'my super description for pod 3-2', 'video_short.webm', next) | 164 | utils.uploadVideo(servers[2].url, servers[2].access_token, 'my super name for pod 3-2', 'my super description for pod 3-2', 'video_short.webm', next) |
156 | }, | 165 | }, |
157 | function (next) { | 166 | function (next) { |
158 | setTimeout(next, 22000) | 167 | setTimeout(next, 22000) |
@@ -162,8 +171,8 @@ describe('Test multiple pods', function () { | |||
162 | 171 | ||
163 | let base_magnet = null | 172 | let base_magnet = null |
164 | // All pods should have this video | 173 | // All pods should have this video |
165 | async.each(urls, function (url, callback) { | 174 | async.each(servers, function (server, callback) { |
166 | utils.getVideosList(url, function (err, res) { | 175 | utils.getVideosList(server.url, function (err, res) { |
167 | if (err) throw err | 176 | if (err) throw err |
168 | 177 | ||
169 | const videos = res.body | 178 | const videos = res.body |
@@ -201,7 +210,7 @@ describe('Test multiple pods', function () { | |||
201 | // Yes, this could be long | 210 | // Yes, this could be long |
202 | this.timeout(200000) | 211 | this.timeout(200000) |
203 | 212 | ||
204 | utils.getVideosList(urls[2], function (err, res) { | 213 | utils.getVideosList(servers[2].url, function (err, res) { |
205 | if (err) throw err | 214 | if (err) throw err |
206 | 215 | ||
207 | const video = res.body[0] | 216 | const video = res.body[0] |
@@ -222,7 +231,7 @@ describe('Test multiple pods', function () { | |||
222 | // Yes, this could be long | 231 | // Yes, this could be long |
223 | this.timeout(200000) | 232 | this.timeout(200000) |
224 | 233 | ||
225 | utils.getVideosList(urls[0], function (err, res) { | 234 | utils.getVideosList(servers[0].url, function (err, res) { |
226 | if (err) throw err | 235 | if (err) throw err |
227 | 236 | ||
228 | const video = res.body[1] | 237 | const video = res.body[1] |
@@ -241,7 +250,7 @@ describe('Test multiple pods', function () { | |||
241 | // Yes, this could be long | 250 | // Yes, this could be long |
242 | this.timeout(200000) | 251 | this.timeout(200000) |
243 | 252 | ||
244 | utils.getVideosList(urls[1], function (err, res) { | 253 | utils.getVideosList(servers[1].url, function (err, res) { |
245 | if (err) throw err | 254 | if (err) throw err |
246 | 255 | ||
247 | const video = res.body[2] | 256 | const video = res.body[2] |
@@ -260,7 +269,7 @@ describe('Test multiple pods', function () { | |||
260 | // Yes, this could be long | 269 | // Yes, this could be long |
261 | this.timeout(200000) | 270 | this.timeout(200000) |
262 | 271 | ||
263 | utils.getVideosList(urls[0], function (err, res) { | 272 | utils.getVideosList(servers[0].url, function (err, res) { |
264 | if (err) throw err | 273 | if (err) throw err |
265 | 274 | ||
266 | const video = res.body[3] | 275 | const video = res.body[3] |
@@ -280,10 +289,10 @@ describe('Test multiple pods', function () { | |||
280 | 289 | ||
281 | async.series([ | 290 | async.series([ |
282 | function (next) { | 291 | function (next) { |
283 | utils.removeVideo(urls[2], to_remove[0], next) | 292 | utils.removeVideo(servers[2].url, servers[2].access_token, to_remove[0], next) |
284 | }, | 293 | }, |
285 | function (next) { | 294 | function (next) { |
286 | utils.removeVideo(urls[2], to_remove[1], next) | 295 | utils.removeVideo(servers[2].url, servers[2].access_token, to_remove[1], next) |
287 | }], | 296 | }], |
288 | function (err) { | 297 | function (err) { |
289 | if (err) throw err | 298 | if (err) throw err |
@@ -293,8 +302,8 @@ describe('Test multiple pods', function () { | |||
293 | }) | 302 | }) |
294 | 303 | ||
295 | it('Should have videos 1 and 3 on each pod', function (done) { | 304 | it('Should have videos 1 and 3 on each pod', function (done) { |
296 | async.each(urls, function (url, callback) { | 305 | async.each(servers, function (server, callback) { |
297 | utils.getVideosList(url, function (err, res) { | 306 | utils.getVideosList(server.url, function (err, res) { |
298 | if (err) throw err | 307 | if (err) throw err |
299 | 308 | ||
300 | const videos = res.body | 309 | const videos = res.body |
@@ -313,8 +322,8 @@ describe('Test multiple pods', function () { | |||
313 | }) | 322 | }) |
314 | 323 | ||
315 | after(function (done) { | 324 | after(function (done) { |
316 | apps.forEach(function (app) { | 325 | servers.forEach(function (server) { |
317 | process.kill(-app.pid) | 326 | process.kill(-server.app.pid) |
318 | }) | 327 | }) |
319 | process.kill(-webtorrent.app.pid) | 328 | process.kill(-webtorrent.app.pid) |
320 | 329 | ||
diff --git a/server/tests/api/singlePod.js b/server/tests/api/singlePod.js index 0b96f221a..64d5475dd 100644 --- a/server/tests/api/singlePod.js +++ b/server/tests/api/singlePod.js | |||
@@ -12,8 +12,7 @@ webtorrent.silent = true | |||
12 | const utils = require('./utils') | 12 | const utils = require('./utils') |
13 | 13 | ||
14 | describe('Test a single pod', function () { | 14 | describe('Test a single pod', function () { |
15 | let app = null | 15 | let server = null |
16 | let url = '' | ||
17 | let video_id = -1 | 16 | let video_id = -1 |
18 | 17 | ||
19 | before(function (done) { | 18 | before(function (done) { |
@@ -24,9 +23,15 @@ describe('Test a single pod', function () { | |||
24 | utils.flushTests(next) | 23 | utils.flushTests(next) |
25 | }, | 24 | }, |
26 | function (next) { | 25 | function (next) { |
27 | utils.runServer(1, function (app1, url1) { | 26 | utils.runServer(1, function (server1) { |
28 | app = app1 | 27 | server = server1 |
29 | url = url1 | 28 | next() |
29 | }) | ||
30 | }, | ||
31 | function (next) { | ||
32 | utils.loginAndGetAccessToken(server, function (err, token) { | ||
33 | if (err) throw err | ||
34 | server.access_token = token | ||
30 | next() | 35 | next() |
31 | }) | 36 | }) |
32 | }, | 37 | }, |
@@ -37,7 +42,7 @@ describe('Test a single pod', function () { | |||
37 | }) | 42 | }) |
38 | 43 | ||
39 | it('Should not have videos', function (done) { | 44 | it('Should not have videos', function (done) { |
40 | utils.getVideosList(url, function (err, res) { | 45 | utils.getVideosList(server.url, function (err, res) { |
41 | if (err) throw err | 46 | if (err) throw err |
42 | 47 | ||
43 | expect(res.body).to.be.an('array') | 48 | expect(res.body).to.be.an('array') |
@@ -49,14 +54,14 @@ describe('Test a single pod', function () { | |||
49 | 54 | ||
50 | it('Should upload the video', function (done) { | 55 | it('Should upload the video', function (done) { |
51 | this.timeout(5000) | 56 | this.timeout(5000) |
52 | utils.uploadVideo(url, 'my super name', 'my super description', 'video_short.webm', done) | 57 | utils.uploadVideo(server.url, server.access_token, 'my super name', 'my super description', 'video_short.webm', done) |
53 | }) | 58 | }) |
54 | 59 | ||
55 | it('Should seed the uploaded video', function (done) { | 60 | it('Should seed the uploaded video', function (done) { |
56 | // Yes, this could be long | 61 | // Yes, this could be long |
57 | this.timeout(60000) | 62 | this.timeout(60000) |
58 | 63 | ||
59 | utils.getVideosList(url, function (err, res) { | 64 | utils.getVideosList(server.url, function (err, res) { |
60 | if (err) throw err | 65 | if (err) throw err |
61 | 66 | ||
62 | expect(res.body).to.be.an('array') | 67 | expect(res.body).to.be.an('array') |
@@ -84,7 +89,7 @@ describe('Test a single pod', function () { | |||
84 | // Yes, this could be long | 89 | // Yes, this could be long |
85 | this.timeout(60000) | 90 | this.timeout(60000) |
86 | 91 | ||
87 | utils.getVideo(url, video_id, function (err, res) { | 92 | utils.getVideo(server.url, video_id, function (err, res) { |
88 | if (err) throw err | 93 | if (err) throw err |
89 | 94 | ||
90 | const video = res.body | 95 | const video = res.body |
@@ -104,7 +109,7 @@ describe('Test a single pod', function () { | |||
104 | }) | 109 | }) |
105 | 110 | ||
106 | it('Should search the video', function (done) { | 111 | it('Should search the video', function (done) { |
107 | utils.searchVideo(url, 'my', function (err, res) { | 112 | utils.searchVideo(server.url, 'my', function (err, res) { |
108 | if (err) throw err | 113 | if (err) throw err |
109 | 114 | ||
110 | expect(res.body).to.be.an('array') | 115 | expect(res.body).to.be.an('array') |
@@ -120,7 +125,7 @@ describe('Test a single pod', function () { | |||
120 | }) | 125 | }) |
121 | 126 | ||
122 | it('Should not find a search', function (done) { | 127 | it('Should not find a search', function (done) { |
123 | utils.searchVideo(url, 'hello', function (err, res) { | 128 | utils.searchVideo(server.url, 'hello', function (err, res) { |
124 | if (err) throw err | 129 | if (err) throw err |
125 | 130 | ||
126 | expect(res.body).to.be.an('array') | 131 | expect(res.body).to.be.an('array') |
@@ -131,7 +136,7 @@ describe('Test a single pod', function () { | |||
131 | }) | 136 | }) |
132 | 137 | ||
133 | it('Should remove the video', function (done) { | 138 | it('Should remove the video', function (done) { |
134 | utils.removeVideo(url, video_id, function (err) { | 139 | utils.removeVideo(server.url, server.access_token, video_id, function (err) { |
135 | if (err) throw err | 140 | if (err) throw err |
136 | 141 | ||
137 | fs.readdir(pathUtils.join(__dirname, '../../../test1/uploads/'), function (err, files) { | 142 | fs.readdir(pathUtils.join(__dirname, '../../../test1/uploads/'), function (err, files) { |
@@ -144,7 +149,7 @@ describe('Test a single pod', function () { | |||
144 | }) | 149 | }) |
145 | 150 | ||
146 | it('Should not have videos', function (done) { | 151 | it('Should not have videos', function (done) { |
147 | utils.getVideosList(url, function (err, res) { | 152 | utils.getVideosList(server.url, function (err, res) { |
148 | if (err) throw err | 153 | if (err) throw err |
149 | 154 | ||
150 | expect(res.body).to.be.an('array') | 155 | expect(res.body).to.be.an('array') |
@@ -155,7 +160,7 @@ describe('Test a single pod', function () { | |||
155 | }) | 160 | }) |
156 | 161 | ||
157 | after(function (done) { | 162 | after(function (done) { |
158 | process.kill(-app.pid) | 163 | process.kill(-server.app.pid) |
159 | process.kill(-webtorrent.app.pid) | 164 | process.kill(-webtorrent.app.pid) |
160 | 165 | ||
161 | // Keep the logs if the test failed | 166 | // Keep the logs if the test failed |
diff --git a/server/tests/api/users.js b/server/tests/api/users.js new file mode 100644 index 000000000..506b19299 --- /dev/null +++ b/server/tests/api/users.js | |||
@@ -0,0 +1,133 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const async = require('async') | ||
4 | const chai = require('chai') | ||
5 | const expect = chai.expect | ||
6 | const pathUtils = require('path') | ||
7 | |||
8 | const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent')) | ||
9 | webtorrent.silent = true | ||
10 | |||
11 | const utils = require('./utils') | ||
12 | |||
13 | describe('Test users', function () { | ||
14 | let server = null | ||
15 | let access_token = null | ||
16 | let video_id | ||
17 | |||
18 | before(function (done) { | ||
19 | this.timeout(20000) | ||
20 | |||
21 | async.series([ | ||
22 | function (next) { | ||
23 | utils.flushTests(next) | ||
24 | }, | ||
25 | function (next) { | ||
26 | utils.runServer(1, function (server1) { | ||
27 | server = server1 | ||
28 | next() | ||
29 | }) | ||
30 | } | ||
31 | ], done) | ||
32 | }) | ||
33 | |||
34 | it('Should not login with an invalid client id', function (done) { | ||
35 | const client = { id: 'client', password: server.client.secret } | ||
36 | utils.login(server.url, client, server.user, 400, function (err, res) { | ||
37 | if (err) throw err | ||
38 | |||
39 | expect(res.body.error).to.equal('invalid_client') | ||
40 | done() | ||
41 | }) | ||
42 | }) | ||
43 | |||
44 | it('Should not login with an invalid client password', function (done) { | ||
45 | const client = { id: server.client.id, password: 'coucou' } | ||
46 | utils.login(server.url, client, server.user, 400, function (err, res) { | ||
47 | if (err) throw err | ||
48 | |||
49 | expect(res.body.error).to.equal('invalid_client') | ||
50 | done() | ||
51 | }) | ||
52 | }) | ||
53 | |||
54 | it('Should not login with an invalid username', function (done) { | ||
55 | const user = { username: 'captain crochet', password: server.user.password } | ||
56 | utils.login(server.url, server.client, user, 400, function (err, res) { | ||
57 | if (err) throw err | ||
58 | |||
59 | expect(res.body.error).to.equal('invalid_grant') | ||
60 | done() | ||
61 | }) | ||
62 | }) | ||
63 | |||
64 | it('Should not login with an invalid password', function (done) { | ||
65 | const user = { username: server.user.username, password: 'mewthree' } | ||
66 | utils.login(server.url, server.client, user, 400, function (err, res) { | ||
67 | if (err) throw err | ||
68 | |||
69 | expect(res.body.error).to.equal('invalid_grant') | ||
70 | done() | ||
71 | }) | ||
72 | }) | ||
73 | |||
74 | it('Should not be able to upload a video', function (done) { | ||
75 | access_token = 'mysupertoken' | ||
76 | utils.uploadVideo(server.url, access_token, 'my super name', 'my super description', 'video_short.webm', 401, done) | ||
77 | }) | ||
78 | |||
79 | it('Should be able to login', function (done) { | ||
80 | utils.login(server.url, server.client, server.user, 200, function (err, res) { | ||
81 | if (err) throw err | ||
82 | |||
83 | access_token = res.body.access_token | ||
84 | done() | ||
85 | }) | ||
86 | }) | ||
87 | |||
88 | it('Should upload the video with the correct token', function (done) { | ||
89 | utils.uploadVideo(server.url, access_token, 'my super name', 'my super description', 'video_short.webm', 204, function (err, res) { | ||
90 | if (err) throw err | ||
91 | |||
92 | utils.getVideosList(server.url, function (err, res) { | ||
93 | if (err) throw err | ||
94 | |||
95 | video_id = res.body[0].id | ||
96 | done() | ||
97 | }) | ||
98 | }) | ||
99 | }) | ||
100 | |||
101 | it('Should upload the video again with the correct token', function (done) { | ||
102 | utils.uploadVideo(server.url, access_token, 'my super name 2', 'my super description 2', 'video_short.webm', 204, done) | ||
103 | }) | ||
104 | |||
105 | it('Should not be able to remove the video with an incorrect token', function (done) { | ||
106 | utils.removeVideo(server.url, 'bad_token', video_id, 401, done) | ||
107 | }) | ||
108 | |||
109 | it('Should not be able to remove the video with the token of another account') | ||
110 | |||
111 | it('Should be able to remove the video with the correct token', function (done) { | ||
112 | utils.removeVideo(server.url, access_token, video_id, done) | ||
113 | }) | ||
114 | |||
115 | it('Should logout') | ||
116 | |||
117 | it('Should not be able to upload a video') | ||
118 | |||
119 | it('Should not be able to remove a video') | ||
120 | |||
121 | it('Should be able to login again') | ||
122 | |||
123 | after(function (done) { | ||
124 | process.kill(-server.app.pid) | ||
125 | |||
126 | // Keep the logs if the test failed | ||
127 | if (this.ok) { | ||
128 | utils.flushTests(done) | ||
129 | } else { | ||
130 | done() | ||
131 | } | ||
132 | }) | ||
133 | }) | ||
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 | ||
64 | function 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 | |||
90 | function 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 | |||
62 | function makeFriends (url, expected_status, callback) { | 98 | function 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 | ||
99 | function removeVideo (url, id, end) { | 135 | function 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 | ||
135 | function runServer (number, callback) { | 177 | function 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 | ||
180 | function uploadVideo (url, name, description, fixture, end) { | 255 | function 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 | ||