Authenticate before make/quit friends (server + tests)

This commit is contained in:
Chocobozzz 2016-05-13 16:31:14 +02:00
parent 5dda52c924
commit b3b9264742
10 changed files with 68 additions and 38 deletions

View File

@ -4,11 +4,12 @@ const express = require('express')
const logger = require('../../../helpers/logger') const logger = require('../../../helpers/logger')
const friends = require('../../../lib/friends') const friends = require('../../../lib/friends')
const middleware = require('../../../middlewares') const middlewares = require('../../../middlewares')
const Pods = require('../../../models/pods') const Pods = require('../../../models/pods')
const reqValidator = middleware.reqValidators.pods const oAuth2 = middlewares.oauth2
const secureMiddleware = middleware.secure const reqValidator = middlewares.reqValidators.pods
const secureRequest = middleware.reqValidators.remote.secureRequest const secureMiddleware = middlewares.secure
const secureRequest = middlewares.reqValidators.remote.secureRequest
const videos = require('../../../lib/videos') const videos = require('../../../lib/videos')
const Videos = require('../../../models/videos') const Videos = require('../../../models/videos')
@ -16,8 +17,8 @@ const router = express.Router()
router.get('/', listPods) router.get('/', listPods)
router.post('/', reqValidator.podsAdd, addPods) router.post('/', reqValidator.podsAdd, addPods)
router.get('/makefriends', reqValidator.makeFriends, makeFriends) router.get('/makefriends', oAuth2.authenticate, reqValidator.makeFriends, makeFriends)
router.get('/quitfriends', quitFriends) router.get('/quitfriends', oAuth2.authenticate, quitFriends)
// Post because this is a secured request // Post because this is a secured request
router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods) router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods)

View File

@ -3,9 +3,9 @@
const express = require('express') const express = require('express')
const map = require('lodash/map') const map = require('lodash/map')
const middleware = require('../../../middlewares') const middlewares = require('../../../middlewares')
const secureMiddleware = middleware.secure const secureMiddleware = middlewares.secure
const reqValidator = middleware.reqValidators.remote const reqValidator = middlewares.reqValidators.remote
const logger = require('../../../helpers/logger') const logger = require('../../../helpers/logger')
const Videos = require('../../../models/videos') const Videos = require('../../../models/videos')
const videos = require('../../../lib/videos') const videos = require('../../../lib/videos')

View File

@ -2,7 +2,7 @@
const config = require('config') const config = require('config')
const express = require('express') const express = require('express')
const oAuth2 = require('../../../middlewares/oauth2') const oAuth2 = require('../../../middlewares').oauth2
const Users = require('../../../models/users') const Users = require('../../../models/users')

View File

@ -9,9 +9,9 @@ const multer = require('multer')
const constants = require('../../../initializers/constants') const constants = require('../../../initializers/constants')
const logger = require('../../../helpers/logger') const logger = require('../../../helpers/logger')
const friends = require('../../../lib/friends') const friends = require('../../../lib/friends')
const middleware = require('../../../middlewares') const middlewares = require('../../../middlewares')
const oAuth2 = require('../../../middlewares/oauth2') const oAuth2 = middlewares.oauth2
const reqValidator = middleware.reqValidators.videos const reqValidator = middlewares.reqValidators.videos
const utils = require('../../../helpers/utils') const utils = require('../../../helpers/utils')
const Videos = require('../../../models/videos') // model const Videos = require('../../../models/videos') // model
const videos = require('../../../lib/videos') const videos = require('../../../lib/videos')

View File

@ -1,9 +1,11 @@
'use strict' 'use strict'
const oauth2 = require('./oauth2')
const reqValidatorsMiddleware = require('./reqValidators') const reqValidatorsMiddleware = require('./reqValidators')
const secureMiddleware = require('./secure') const secureMiddleware = require('./secure')
const middlewares = { const middlewares = {
oauth2: oauth2,
reqValidators: reqValidatorsMiddleware, reqValidators: reqValidatorsMiddleware,
secure: secureMiddleware secure: secureMiddleware
} }

View File

@ -10,15 +10,18 @@ describe('Test advanced friends', function () {
let servers = [] let servers = []
function makeFriends (podNumber, callback) { function makeFriends (podNumber, callback) {
return utils.makeFriends(servers[podNumber - 1].url, callback) const server = servers[podNumber - 1]
return utils.makeFriends(server.url, server.accessToken, callback)
} }
function quitFriends (podNumber, callback) { function quitFriends (podNumber, callback) {
return utils.quitFriends(servers[podNumber - 1].url, callback) const server = servers[podNumber - 1]
return utils.quitFriends(server.url, server.accessToken, callback)
} }
function getFriendsList (podNumber, end) { function getFriendsList (podNumber, end) {
return utils.getFriendsList(servers[podNumber - 1].url, end) const server = servers[podNumber - 1]
return utils.getFriendsList(server.url, end)
} }
function uploadVideo (podNumber, callback) { function uploadVideo (podNumber, callback) {

View File

@ -3,13 +3,17 @@
const async = require('async') const async = require('async')
const chai = require('chai') const chai = require('chai')
const expect = chai.expect const expect = chai.expect
const request = require('supertest')
const utils = require('./utils') const utils = require('./utils')
describe('Test basic friends', function () { describe('Test basic friends', function () {
let servers = [] let servers = []
function makeFriends (podNumber, callback) {
const server = servers[podNumber - 1]
return utils.makeFriends(server.url, server.accessToken, callback)
}
function testMadeFriends (servers, serverToTest, callback) { function testMadeFriends (servers, serverToTest, callback) {
const friends = [] const friends = []
for (let i = 0; i < servers.length; i++) { for (let i = 0; i < servers.length; i++) {
@ -39,7 +43,15 @@ describe('Test basic friends', function () {
this.timeout(20000) this.timeout(20000)
utils.flushAndRunMultipleServers(3, function (serversRun, urlsRun) { utils.flushAndRunMultipleServers(3, function (serversRun, urlsRun) {
servers = serversRun servers = serversRun
done()
async.each(servers, function (server, callbackEach) {
utils.loginAndGetAccessToken(server, function (err, accessToken) {
if (err) return callbackEach(err)
server.accessToken = accessToken
callbackEach()
})
}, done)
}) })
}) })
@ -59,16 +71,10 @@ describe('Test basic friends', function () {
it('Should make friends', function (done) { it('Should make friends', function (done) {
this.timeout(10000) this.timeout(10000)
const path = '/api/v1/pods/makefriends'
async.series([ async.series([
// The second pod make friend with the third // The second pod make friend with the third
function (next) { function (next) {
request(servers[1].url) makeFriends(2, next)
.get(path)
.set('Accept', 'application/json')
.expect(204)
.end(next)
}, },
// Wait for the request between pods // Wait for the request between pods
function (next) { function (next) {
@ -102,11 +108,7 @@ describe('Test basic friends', function () {
}, },
// Finally the first pod make friend with the second pod // Finally the first pod make friend with the second pod
function (next) { function (next) {
request(servers[0].url) makeFriends(1, next)
.get(path)
.set('Accept', 'application/json')
.expect(204)
.end(next)
}, },
// Wait for the request between pods // Wait for the request between pods
function (next) { function (next) {
@ -123,14 +125,16 @@ describe('Test basic friends', function () {
}) })
it('Should not be allowed to make friend again', function (done) { it('Should not be allowed to make friend again', function (done) {
utils.makeFriends(servers[1].url, 409, done) const server = servers[1]
utils.makeFriends(server.url, server.accessToken, 409, done)
}) })
it('Should quit friends of pod 2', function (done) { it('Should quit friends of pod 2', function (done) {
async.series([ async.series([
// Pod 1 quit friends // Pod 1 quit friends
function (next) { function (next) {
utils.quitFriends(servers[1].url, next) const server = servers[1]
utils.quitFriends(server.url, server.accessToken, next)
}, },
// Pod 1 should not have friends anymore // Pod 1 should not have friends anymore
function (next) { function (next) {
@ -162,7 +166,8 @@ describe('Test basic friends', function () {
}) })
it('Should allow pod 2 to make friend again', function (done) { it('Should allow pod 2 to make friend again', function (done) {
utils.makeFriends(servers[1].url, function () { const server = servers[1]
utils.makeFriends(server.url, server.accessToken, function () {
async.each(servers, function (server, callback) { async.each(servers, function (server, callback) {
testMadeFriends(servers, server, callback) testMadeFriends(servers, server, callback)
}, done) }, done)

View File

@ -37,7 +37,8 @@ describe('Test multiple pods', function () {
}, },
// The second pod make friend with the third // The second pod make friend with the third
function (next) { function (next) {
utils.makeFriends(servers[1].url, next) const server = servers[1]
utils.makeFriends(server.url, server.accessToken, next)
}, },
// Wait for the request between pods // Wait for the request between pods
function (next) { function (next) {
@ -45,7 +46,8 @@ describe('Test multiple pods', function () {
}, },
// Pod 1 make friends too // Pod 1 make friends too
function (next) { function (next) {
utils.makeFriends(servers[0].url, next) const server = servers[0]
utils.makeFriends(server.url, server.accessToken, next)
}, },
function (next) { function (next) {
webtorrent.create({ host: 'client', port: '1' }, next) webtorrent.create({ host: 'client', port: '1' }, next)

View File

@ -82,6 +82,16 @@ describe('Test users', function () {
utils.uploadVideo(server.url, accessToken, 'my super name', 'my super description', 'video_short.webm', 401, done) utils.uploadVideo(server.url, accessToken, 'my super name', 'my super description', 'video_short.webm', 401, done)
}) })
it('Should not be able to make friends', function (done) {
accessToken = 'mysupertoken'
utils.makeFriends(server.url, accessToken, 401, done)
})
it('Should not be able to quit friends', function (done) {
accessToken = 'mysupertoken'
utils.quitFriends(server.url, accessToken, 401, done)
})
it('Should be able to login', function (done) { it('Should be able to login', function (done) {
utils.login(server.url, server.client, server.user, 200, function (err, res) { utils.login(server.url, server.client, server.user, 200, function (err, res) {
if (err) throw err if (err) throw err

View File

@ -97,7 +97,7 @@ function loginAndGetAccessToken (server, callback) {
}) })
} }
function makeFriends (url, expectedStatus, callback) { function makeFriends (url, accessToken, expectedStatus, callback) {
if (!callback) { if (!callback) {
callback = expectedStatus callback = expectedStatus
expectedStatus = 204 expectedStatus = 204
@ -109,6 +109,7 @@ function makeFriends (url, expectedStatus, callback) {
request(url) request(url)
.get(path) .get(path)
.set('Accept', 'application/json') .set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + accessToken)
.expect(expectedStatus) .expect(expectedStatus)
.end(function (err, res) { .end(function (err, res) {
if (err) throw err if (err) throw err
@ -118,14 +119,20 @@ function makeFriends (url, expectedStatus, callback) {
}) })
} }
function quitFriends (url, callback) { function quitFriends (url, accessToken, expectedStatus, callback) {
if (!callback) {
callback = expectedStatus
expectedStatus = 204
}
const path = '/api/v1/pods/quitfriends' const path = '/api/v1/pods/quitfriends'
// The first pod make friend with the third // The first pod make friend with the third
request(url) request(url)
.get(path) .get(path)
.set('Accept', 'application/json') .set('Accept', 'application/json')
.expect(204) .set('Authorization', 'Bearer ' + accessToken)
.expect(expectedStatus)
.end(function (err, res) { .end(function (err, res) {
if (err) throw err if (err) throw err