Skip to content

Commit

Permalink
Tests improvement
Browse files Browse the repository at this point in the history
validate_security_object have to raises only for apiKey security definitions
  • Loading branch information
macisamuele committed Oct 6, 2016
1 parent a5b0240 commit 0aa191a
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 12 deletions.
15 changes: 12 additions & 3 deletions bravado_core/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from bravado_core.exception import SwaggerMappingError, SwaggerSecurityValidationError
from bravado_core.schema import SWAGGER_PRIMITIVES
from bravado_core.swagger20_validator import get_validator_type
from six import itervalues


def validate_schema_object(swagger_spec, schema_object_spec, value):
Expand Down Expand Up @@ -82,14 +83,22 @@ def validate_security_object(op, request_data):
:type request_data: dict
:raise: SwaggerSecurityValidationError
"""
if len(op.security_requirements) > 0:

security_types = set([
definition.type
for security_requirement in op.security_requirements
for definition in itervalues(security_requirement.security_definitions)
])

# At the moment we are handling only apiKey securities
if 'apiKey' in security_types:
matched_security_indexes = []
for security_index, security_params_list in enumerate(op.security_requirements):
if all([request_data[security_param.name] is not None for security_param in security_params_list]):
if all([request_data.get(security_param.name) is not None for security_param in security_params_list]):
matched_security_indexes.append(security_index)

if len(matched_security_indexes) == 0:
raise SwaggerSecurityValidationError('No security definition used.') # TODO: improve error message
raise SwaggerSecurityValidationError('No security definition used.')

if len(matched_security_indexes) > 1:
# if more than one security defs are matched then check if one security definition contains all the others
Expand Down
17 changes: 17 additions & 0 deletions test-data/2.0/security/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@
}
},
"/example4": {
"get": {
"description": "Endpoint uses oauth2, no handling provided at the moment",
"security": [
{
"oauth2": [
"write:resource"
]
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/example5": {
"get": {
"description": "Endpoint not requires any header parameter",
"security": [],
Expand Down
3 changes: 2 additions & 1 deletion tests/operation/security_object_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ def test_wrong_request_with_apiKey_security(petstore_spec):
('example1', 'get_example1', (('apiKey1',), ('apiKey2',))),
('example2', 'get_example2', (('apiKey3',),)),
('example3', 'get_example3', (('apiKey1', 'apiKey2',), ('apiKey2',))),
('example4', 'get_example4', ()),
('example4', 'get_example4', (('oauth2',),)),
('example5', 'get_example5', ()),
]
)
def test_security_parameters_selection(
Expand Down
38 changes: 30 additions & 8 deletions tests/security_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
# from bravado_core.security_definition import SecurityDefinition
#
# def test_a(security_spec):
# oauth2 = security_spec.security_definitions['oauth2']
# example1 = security_spec.resources['example1'].operations['get_example1']
# example2 = security_spec.resources['example2'].operations['get_example2']
# example3 = security_spec.resources['example3'].operations['get_example3']
# example4 = security_spec.resources['example4'].operations['get_example4']
import pytest
from six import iteritems


def test_security_definition_property_extraction(security_dict, security_spec):
security_definitions = security_dict['securityDefinitions']
for security_name, security_spec_dict in iteritems(security_definitions):
security_object = security_spec.security_definitions[security_name]
for key, value in iteritems(security_spec_dict):
assert getattr(security_object, key if key != 'in' else 'location') == value


@pytest.mark.parametrize(
'resource, operation, expected_scopes',
[
('example1', 'get_example1', [{'apiKey1': []}, {'apiKey2': []}]),
('example2', 'get_example2', [{'apiKey3': []}]),
('example3', 'get_example3', [{'apiKey1': [], 'apiKey2': []}, {'apiKey2': []}]),
('example4', 'get_example4', [{'oauth2': ['write:resource']}]),
('example5', 'get_example5', []),
]
)
def test_security_scopes(security_spec, resource, operation, expected_scopes):
def _get_operation():
return security_spec.resources[resource].operations[operation]

assert [
security_requirement.security_scopes
for security_requirement in _get_operation().security_requirements
] == expected_scopes
35 changes: 35 additions & 0 deletions tests/validate/validate_security_object_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pytest

from bravado_core.exception import SwaggerValidationError
from bravado_core.validate import validate_security_object


@pytest.mark.parametrize(
'resource, operation, request_data',
[
('example1', 'get_example1', {'apiKey1': 'key'}),
('example1', 'get_example1', {'apiKey2': 'key'}),
('example2', 'get_example2', {'apiKey3': 'key'}),
('example3', 'get_example3', {'apiKey1': 'key', 'apiKey2': 'key'}),
('example3', 'get_example3', {'apiKey2': 'key'}),
('example4', 'get_example4', {}),
('example5', 'get_example5', {}),
]
)
def test_validate_correct_security_objects(security_spec, resource, operation, request_data):
op = security_spec.resources[resource].operations[operation]
validate_security_object(op, request_data)


@pytest.mark.parametrize(
'resource, operation, request_data',
[
('example1', 'get_example1', {}),
('example1', 'get_example1', {'apiKey1': 'key', 'apiKey2': 'key'}),
('example3', 'get_example3', {'apiKey1': 'key', 'apiKey3': 'key'}),
]
)
def test_validate_incorrect_security_objects(security_spec, resource, operation, request_data):
op = security_spec.resources[resource].operations[operation]
with pytest.raises(SwaggerValidationError):
validate_security_object(op, request_data)

0 comments on commit 0aa191a

Please sign in to comment.