Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
9155398
Show difference between mock and real
adamtheturtle Sep 29, 2018
f1948d1
Stub for query difference
adamtheturtle Sep 29, 2018
d920660
A different erroring test
adamtheturtle Sep 29, 2018
d1d8e36
Add docstrings
adamtheturtle Sep 29, 2018
759194f
Passing tests on real
adamtheturtle Sep 29, 2018
72cbd3c
Progress
adamtheturtle Sep 29, 2018
9141606
Progress
adamtheturtle Sep 29, 2018
d9f8d98
Progress
adamtheturtle Sep 29, 2018
5a3b6dc
Progress
adamtheturtle Sep 29, 2018
d31e271
Progress
adamtheturtle Sep 29, 2018
6fb88bf
Progress
adamtheturtle Sep 29, 2018
5795d4b
Progress
adamtheturtle Sep 29, 2018
464d702
Progress
adamtheturtle Sep 29, 2018
23204e7
Add query out of bounds response
adamtheturtle Sep 29, 2018
b74f2ee
Merge remote-tracking branch 'origin/master' into auth-test
adamtheturtle Sep 29, 2018
aec1196
Progress
adamtheturtle Sep 29, 2018
625ff3f
Progress
adamtheturtle Sep 29, 2018
0b5dfc6
Progress
adamtheturtle Sep 29, 2018
b5fe3ac
Progress
adamtheturtle Sep 29, 2018
27b56f5
Merge remote-tracking branch 'origin/master' into auth-test
adamtheturtle Sep 30, 2018
5ef178e
Merge remote-tracking branch 'origin/master' into auth-test
adamtheturtle Sep 30, 2018
01f128d
Progress
adamtheturtle Sep 30, 2018
07b2e7d
Progress
adamtheturtle Sep 30, 2018
5cb859e
Fixing one test
adamtheturtle Sep 30, 2018
eae698f
Merge remote-tracking branch 'origin/master' into auth-test
adamtheturtle Sep 30, 2018
2d29dbf
Merge remote-tracking branch 'origin/master' into auth-test
adamtheturtle Sep 30, 2018
4c42d82
Merge remote-tracking branch 'origin/master' into auth-test
adamtheturtle Sep 30, 2018
ceb681d
Remove unnecessary changes
adamtheturtle Sep 30, 2018
06cd51c
Merge remote-tracking branch 'origin/master' into auth-test
adamtheturtle Sep 30, 2018
4715e43
One more passing test
adamtheturtle Sep 30, 2018
4552b67
Comment out part done code
adamtheturtle Sep 30, 2018
ec70514
Merge remote-tracking branch 'origin/master' into auth-test
adamtheturtle Oct 1, 2018
7dcb2c3
Merge remote-tracking branch 'origin/master' into auth-test
adamtheturtle Oct 1, 2018
d44383e
Uncomment code
adamtheturtle Oct 1, 2018
80dd1b7
Tests working for server keys [skip ci]
adamtheturtle Oct 1, 2018
cb20e6e
Empty for CI [skip ci]
adamtheturtle Oct 1, 2018
8685237
Improve docstrings [skip ci]
adamtheturtle Oct 1, 2018
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
4 changes: 4 additions & 0 deletions src/mock_vws/_mock_web_services_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
validate_width,
)
from ._services_validators.auth_validators import (
validate_access_key_exists,
validate_auth_header_exists,
validate_auth_header_has_signature,
validate_authorization,
)
from ._services_validators.date_validators import (
Expand Down Expand Up @@ -222,6 +224,8 @@ def decorator(method: Callable[..., str]) -> Callable[..., str]:
]

common_decorators = [
validate_access_key_exists,
validate_auth_header_has_signature,
validate_auth_header_exists,
set_content_length_header,
update_request_count,
Expand Down
2 changes: 1 addition & 1 deletion src/mock_vws/_query_validators/auth_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def validate_client_key_exists(

Returns:
The result of calling the endpoint.
An ``UNAUTHORIZED`` FOOBAR.
An ``UNAUTHORIZED`` response if the client key is unknown.
"""
request, context = args

Expand Down
77 changes: 75 additions & 2 deletions src/mock_vws/_services_validators/auth_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,79 @@ def validate_auth_header_exists(
return json_dump(body)


@wrapt.decorator
def validate_access_key_exists(
wrapped: Callable[..., str],
instance: Any,
args: Tuple[_RequestObjectProxy, _Context],
kwargs: Dict,
) -> str:
"""
Validate the authorization header includes an access key for a database.

Args:
wrapped: An endpoint function for `requests_mock`.
instance: The class that the endpoint function is in.
args: The arguments given to the endpoint function.
kwargs: The keyword arguments given to the endpoint function.

Returns:
The result of calling the endpoint.
An ``UNAUTHORIZED`` response if the access key is unknown.
"""
request, context = args

header = request.headers['Authorization']
first_part, _ = header.split(b':')
_, access_key = first_part.split(b' ')
for database in instance.databases:
if access_key == database.server_access_key:
return wrapped(*args, **kwargs)

context.status_code = codes.BAD_REQUEST

body = {
'transaction_id': uuid.uuid4().hex,
'result_code': ResultCodes.FAIL.value,
}
return json_dump(body)


@wrapt.decorator
def validate_auth_header_has_signature(
wrapped: Callable[..., str],
instance: Any, # pylint: disable=unused-argument
args: Tuple[_RequestObjectProxy, _Context],
kwargs: Dict,
) -> str:
"""
Validate the authorization header includes a signature.

Args:
wrapped: An endpoint function for `requests_mock`.
instance: The class that the endpoint function is in.
args: The arguments given to the endpoint function.
kwargs: The keyword arguments given to the endpoint function.

Returns:
The result of calling the endpoint.
An ``UNAUTHORIZED`` response if the "Authorization" header is not as
expected.
"""
request, context = args

header = request.headers['Authorization']
if header.count(b':') == 1 and header.split(b':')[1]:
return wrapped(*args, **kwargs)

context.status_code = codes.BAD_REQUEST
body = {
'transaction_id': uuid.uuid4().hex,
'result_code': ResultCodes.FAIL.value,
}
return json_dump(body)


@wrapt.decorator
def validate_authorization(
wrapped: Callable[..., str],
Expand Down Expand Up @@ -78,9 +151,9 @@ def validate_authorization(
if database is not None:
return wrapped(*args, **kwargs)

context.status_code = codes.BAD_REQUEST
context.status_code = codes.UNAUTHORIZED
body = {
'transaction_id': uuid.uuid4().hex,
'result_code': ResultCodes.FAIL.value,
'result_code': ResultCodes.AUTHENTICATION_FAILURE.value,
}
return json_dump(body)
21 changes: 21 additions & 0 deletions tests/mock_vws/test_authorization_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,27 @@ def test_bad_access_key_query(
)
assert response.text == expected_text

def test_bad_secret_key_services(
self,
vuforia_database: VuforiaDatabase,
) -> None:
"""
If the server secret key given is incorrect, an
``AuthenticationFailure`` response is returned.
"""
keys = vuforia_database
keys.server_secret_key = b'example'
response = get_vws_target(
target_id=uuid.uuid4().hex,
vuforia_database=keys,
)

assert_vws_failure(
response=response,
status_code=codes.UNAUTHORIZED,
result_code=ResultCodes.AUTHENTICATION_FAILURE,
)

def test_bad_secret_key_query(
self,
vuforia_database: VuforiaDatabase,
Expand Down
2 changes: 1 addition & 1 deletion tests/mock_vws/test_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def request_mocked_address() -> None:
url='https://vws.vuforia.com/summary',
headers={
'Date': rfc_1123_date(),
'Authorization': 'bad_auth_token',
'Authorization': b'bad_auth_token',
},
data=b'',
)
Expand Down