Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

fixes security test order #456

Merged
merged 1 commit into from
Dec 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion middleware/swagger-security.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ exports = module.exports = function (options) {
req.swagger.operation.security || req.swagger.swaggerObject.security;

if (securityReqs && securityReqs.length > 0) {
async.map(securityReqs, function (secReq, cb) { // logical OR - any one can allow
async.mapSeries(securityReqs, function (secReq, cb) { // logical OR - any one can allow
var secName;

async.map(Object.keys(secReq), function (name, cb) { // logical AND - all must allow
Expand Down
34 changes: 32 additions & 2 deletions test/1.2/test-middleware-swagger-security.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,27 @@ var rlJson = _.cloneDeep(require('../../samples/1.2/resource-listing.json'));
var storeJson = _.cloneDeep(require('../../samples/1.2/store.json'));
var userJson = _.cloneDeep(require('../../samples/1.2/user.json'));

var SecurityDef = function (allow) {
var SecurityDef = function (allow, delay) {
var self = this;

if (allow === undefined) {
allow = true;
}

if (delay === undefined) {
delay = 0;
}

this.called = false;

this.func = function (request, securityDefinition, scopes, cb) {
assert(Array.isArray(scopes));

self.called = true;

cb(allow ? null : new Error('disallowed'));
setTimeout(function() {
cb(allow ? null : new Error('disallowed'));
}, delay);
};
};
var ApiKeySecurityDef = function() {
Expand Down Expand Up @@ -532,6 +538,30 @@ describe('Swagger Security Middleware v1.2', function () {
});
});

it('should authorize first if both are true', function(done) {
var local = new SecurityDef(true, 400);
var local2 = new SecurityDef(true);

helpers.createServer([rlJson, [petJson, storeJson, userJson]], {
swaggerRouterOptions: swaggerRouterOptions,
swaggerSecurityOptions: {
local: local.func,
local2: local2.func
}
}, function (app) {
request(app)
.get('/api/securedOr')
.expect(200)
.end(function(err, res) {
helpers.expectContent('OK')(err, res);

assert(!local2.called);

done();
});
});
});

it('should authorize if first is true', function(done) {
var local = new SecurityDef(true);
var local2 = new SecurityDef(false);
Expand Down
34 changes: 32 additions & 2 deletions test/2.0/test-middleware-swagger-security.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,27 @@ var request = require('supertest');

var petStoreJson = _.cloneDeep(require('../../samples/2.0/petstore.json'));

var SecurityDef = function (allow) {
var SecurityDef = function (allow, delay) {
var self = this;

if (allow === undefined) {
allow = true;
}

if (delay === undefined) {
delay = 0;
}

this.called = false;

this.func = function (request, securityDefinition, scopes, cb) {
assert(Array.isArray(scopes));

self.called = true;

cb(allow ? null : new Error('disallowed'));
setTimeout(function() {
cb(allow ? null : new Error('disallowed'));
}, delay);
};
};
var ApiKeySecurityDef = function() {
Expand Down Expand Up @@ -469,6 +475,30 @@ describe('Swagger Security Middleware v2.0', function () {
});
});

it('should authorize first if both are true', function(done) {
var local = new SecurityDef(true, 400);
var local2 = new SecurityDef(true);

helpers.createServer([petStoreJson], {
swaggerRouterOptions: swaggerRouterOptions,
swaggerSecurityOptions: {
local: local.func,
local2: local2.func
}
}, function (app) {
request(app)
.get('/api/securedOr')
.expect(200)
.end(function(err, res) {
helpers.expectContent('OK')(err, res);

assert(!local2.called);

done();
});
});
});

it('should authorize if first is true', function(done) {
var local = new SecurityDef(true);
var local2 = new SecurityDef(false);
Expand Down