-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from i4Trust/apikey-tests
Adding optional API-Key requirement
- Loading branch information
Showing
15 changed files
with
988 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from api.exceptions.as_exception import ActivationServiceException | ||
|
||
class ApiKeyException(ActivationServiceException): | ||
|
||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from api.exceptions.apikey_exception import ApiKeyException | ||
|
||
# Check API-Key in request | ||
def check_api_key(request, header_name, api_key): | ||
|
||
# Get header | ||
auth_header = request.headers.get(header_name) | ||
if not auth_header: | ||
message = "Missing API-Key header" | ||
internal_msg = message + " ('{}')".format(header_name) | ||
raise ApiKeyException(message, internal_msg, 400) | ||
|
||
# Check API-Keys | ||
if auth_header != api_key: | ||
msg = "Invalid API-Key" | ||
int_msg = msg + " (provided '{}' != expected '{}')".format(auth_header, api_key) | ||
raise ApiKeyException(msg, int_msg, 400) | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import pytest | ||
from api import app | ||
from tests.pytest.util.config_handler import load_config | ||
from api.util.apikey_handler import check_api_key | ||
|
||
from api.exceptions.apikey_exception import ApiKeyException | ||
|
||
# Get AS config | ||
as_config = load_config("tests/config/as.yml", app) | ||
app.config['as'] = as_config | ||
|
||
@pytest.fixture | ||
def mock_request_apikey_ok_ishare(mocker): | ||
def headers_get(attr): | ||
if attr == "AS-API-KEY": return "31f5247c-17e5-4969-95f0-928c8ab16504" | ||
else: return None | ||
request = mocker.Mock() | ||
request.headers.get.side_effect = headers_get | ||
return request | ||
|
||
@pytest.fixture | ||
def mock_request_apikey_ok_issuer(mocker): | ||
def headers_get(attr): | ||
if attr == "AS-API-KEY": return "eb4675ed-860e-4de1-a9a7-3e2e4356d08d" | ||
else: return None | ||
request = mocker.Mock() | ||
request.headers.get.side_effect = headers_get | ||
return request | ||
|
||
@pytest.fixture | ||
def mock_request_apikey_invalid_header(mocker): | ||
def headers_get(attr): | ||
if attr == "AS-API-KEY": return "abc" | ||
else: return None | ||
request = mocker.Mock() | ||
request.headers.get.side_effect = headers_get | ||
return request | ||
|
||
@pytest.fixture | ||
def mock_request_apikey_no_headers(mocker): | ||
def headers_get(attr): | ||
return None | ||
request = mocker.Mock() | ||
request.headers.get.side_effect = headers_get | ||
return request | ||
|
||
@pytest.mark.ok | ||
@pytest.mark.it('should successfully check API-Key for iSHARE flow') | ||
def test_apikey_ok_ishare(mock_request_apikey_ok_ishare): | ||
|
||
# Call function with request mock | ||
try: | ||
check_api_key(mock_request_apikey_ok_ishare, "AS-API-KEY", "31f5247c-17e5-4969-95f0-928c8ab16504") | ||
except Exception as ex: | ||
pytest.fail("should throw no exception: {}".format(ex)) | ||
|
||
@pytest.mark.ok | ||
@pytest.mark.it('should successfully check API-Key for TIL flow') | ||
def test_apikey_ok_issuer(mock_request_apikey_ok_issuer): | ||
|
||
# Call function with request mock | ||
try: | ||
check_api_key(mock_request_apikey_ok_issuer, "AS-API-KEY", "eb4675ed-860e-4de1-a9a7-3e2e4356d08d") | ||
except Exception as ex: | ||
pytest.fail("should throw no exception: {}".format(ex)) | ||
|
||
@pytest.mark.failure | ||
@pytest.mark.it('should throw exception about missing API-Key header') | ||
def test_check_missing_header(mock_request_apikey_no_headers): | ||
|
||
# Call function | ||
with pytest.raises(ApiKeyException, match=r'Missing API-Key header') as ex: | ||
check_api_key(mock_request_apikey_no_headers, "AS-API-KEY", "eb4675ed-860e-4de1-a9a7-3e2e4356d08d") | ||
|
||
@pytest.mark.failure | ||
@pytest.mark.it('should throw exception about invalid API-Key') | ||
def test_check_invalid_header(mock_request_apikey_invalid_header): | ||
|
||
# Call function | ||
with pytest.raises(ApiKeyException, match=r'Invalid API-Key') as ex: | ||
check_api_key(mock_request_apikey_invalid_header, "AS-API-KEY", "eb4675ed-860e-4de1-a9a7-3e2e4356d08d") |
Oops, something went wrong.