Drew Short
6 years ago
4 changed files with 182 additions and 1 deletions
-
1server/Pipfile
-
17server/Pipfile.lock
-
33server/tests/api/test_decorators.py
-
132server/tests/middleware/test_authentication_middleware.py
@ -0,0 +1,33 @@ |
|||
from typing import Any |
|||
|
|||
from flask import Response, Flask |
|||
|
|||
from atheneum.api.decorators import return_json |
|||
from atheneum.api.model import APIResponse |
|||
|
|||
|
|||
@return_json |
|||
def return_jsonified_result(obj: Any) -> Any: |
|||
return obj |
|||
|
|||
|
|||
def test_return_json_response(): |
|||
result = return_jsonified_result(Response(status=200)) |
|||
assert isinstance(result, Response) |
|||
assert result.status_code == 200 |
|||
|
|||
|
|||
def test_return_json_apiresponse(app: Flask): |
|||
with app.app_context(): |
|||
result = return_jsonified_result(APIResponse(payload={}, status=200)) |
|||
assert len(result) == 2 |
|||
assert isinstance(result[0], Response) |
|||
assert isinstance(result[1], int) |
|||
assert result[0].status_code == 200 |
|||
|
|||
|
|||
def test_return_json_dict(app: Flask): |
|||
with app.app_context(): |
|||
result = return_jsonified_result({'status': 200}) |
|||
assert isinstance(result, Response) |
|||
assert result.status_code == 200 |
@ -0,0 +1,132 @@ |
|||
from mock import patch, MagicMock, Mock |
|||
|
|||
from atheneum.middleware.authentication_middleware import \ |
|||
authenticate_with_password, authenticate_with_token |
|||
|
|||
middleware_module = 'atheneum.middleware.authentication_middleware' |
|||
|
|||
|
|||
@patch(middleware_module + '.g') |
|||
@patch(middleware_module + '.authentication_service.is_valid_password') |
|||
@patch(middleware_module + '.user_service.find_by_name') |
|||
def test_authenticate_with_password_happy_path( |
|||
mock_user_service: MagicMock, |
|||
mock_authentication_service: MagicMock, |
|||
mock_g: MagicMock): |
|||
mock_g.user = Mock() |
|||
mock_user_service.return_value = Mock() |
|||
mock_authentication_service.return_value = True |
|||
assert authenticate_with_password('test', 'test') |
|||
mock_user_service.assert_called_once() |
|||
mock_authentication_service.assert_called_once() |
|||
mock_g.user.assert_not_called() |
|||
|
|||
|
|||
@patch(middleware_module + '.g') |
|||
@patch(middleware_module + '.authentication_service.is_valid_password') |
|||
@patch(middleware_module + '.user_service.find_by_name') |
|||
def test_authenticate_with_password_no_user( |
|||
mock_user_service: MagicMock, |
|||
mock_authentication_service: MagicMock, |
|||
mock_g: MagicMock): |
|||
mock_g.user = Mock() |
|||
mock_user_service.return_value = None |
|||
mock_authentication_service.return_value = True |
|||
assert not authenticate_with_password('test', 'test') |
|||
mock_user_service.assert_called_once() |
|||
mock_authentication_service.assert_not_called() |
|||
mock_g.user.assert_not_called() |
|||
|
|||
|
|||
@patch(middleware_module + '.g') |
|||
@patch(middleware_module + '.authentication_service.is_valid_password') |
|||
@patch(middleware_module + '.user_service.find_by_name') |
|||
def test_authenticate_with_password_invalid_password( |
|||
mock_user_service: MagicMock, |
|||
mock_authentication_service: MagicMock, |
|||
mock_g: MagicMock): |
|||
mock_g.user = Mock() |
|||
mock_user_service.return_value = Mock() |
|||
mock_authentication_service.return_value = False |
|||
assert not authenticate_with_password('test', 'test') |
|||
mock_user_service.assert_called_once() |
|||
mock_authentication_service.assert_called_once() |
|||
mock_g.user.assert_not_called() |
|||
|
|||
|
|||
@patch(middleware_module + '.g') |
|||
@patch(middleware_module + '.authentication_service.is_valid_token') |
|||
@patch(middleware_module + '.user_token_service.find_by_user_and_token') |
|||
@patch(middleware_module + '.user_service.find_by_name') |
|||
def test_authenticate_with_token_happy_path( |
|||
mock_user_service: MagicMock, |
|||
mock_user_token_service: MagicMock, |
|||
mock_authentication_service: MagicMock, |
|||
mock_g: MagicMock): |
|||
mock_g.user = Mock() |
|||
mock_user_service.return_value = Mock() |
|||
mock_user_token_service.return_value = Mock() |
|||
mock_authentication_service.return_value = True |
|||
assert authenticate_with_token('test', 'test') |
|||
mock_user_service.assert_called_once() |
|||
mock_user_token_service.assert_called_once() |
|||
mock_authentication_service.assert_called_once() |
|||
mock_g.user.assert_not_called() |
|||
|
|||
|
|||
@patch(middleware_module + '.g') |
|||
@patch(middleware_module + '.authentication_service.is_valid_token') |
|||
@patch(middleware_module + '.user_token_service.find_by_user_and_token') |
|||
@patch(middleware_module + '.user_service.find_by_name') |
|||
def test_authenticate_with_token_no_user( |
|||
mock_user_service: MagicMock, |
|||
mock_user_token_service: MagicMock, |
|||
mock_authentication_service: MagicMock, |
|||
mock_g: MagicMock): |
|||
mock_g.user = Mock() |
|||
mock_user_service.return_value = None |
|||
assert not authenticate_with_token('test', 'test') |
|||
mock_user_service.assert_called_once() |
|||
mock_user_token_service.assert_not_called() |
|||
mock_authentication_service.assert_not_called() |
|||
mock_g.user.assert_not_called() |
|||
|
|||
|
|||
@patch(middleware_module + '.g') |
|||
@patch(middleware_module + '.authentication_service.is_valid_token') |
|||
@patch(middleware_module + '.user_token_service.find_by_user_and_token') |
|||
@patch(middleware_module + '.user_service.find_by_name') |
|||
def test_authenticate_with_token_no_user_token( |
|||
mock_user_service: MagicMock, |
|||
mock_user_token_service: MagicMock, |
|||
mock_authentication_service: MagicMock, |
|||
mock_g: MagicMock): |
|||
mock_g.user = Mock() |
|||
mock_user_service.return_value = Mock() |
|||
mock_user_token_service.return_value = None |
|||
mock_authentication_service.return_value = False |
|||
assert not authenticate_with_token('test', 'test') |
|||
mock_user_service.assert_called_once() |
|||
mock_user_token_service.assert_called_once() |
|||
mock_authentication_service.assert_called_once() |
|||
mock_g.user.assert_not_called() |
|||
|
|||
|
|||
@patch(middleware_module + '.g') |
|||
@patch(middleware_module + '.authentication_service.is_valid_token') |
|||
@patch(middleware_module + '.user_token_service.find_by_user_and_token') |
|||
@patch(middleware_module + '.user_service.find_by_name') |
|||
def test_authenticate_with_token_invalid_token( |
|||
mock_user_service: MagicMock, |
|||
mock_user_token_service: MagicMock, |
|||
mock_authentication_service: MagicMock, |
|||
mock_g: MagicMock): |
|||
mock_g.user = Mock() |
|||
mock_user_service.return_value = Mock() |
|||
mock_user_token_service.return_value = Mock() |
|||
mock_authentication_service.return_value = False |
|||
assert not authenticate_with_token('test', 'test') |
|||
mock_user_service.assert_called_once() |
|||
mock_user_token_service.assert_called_once() |
|||
mock_authentication_service.assert_called_once() |
|||
mock_g.user.assert_not_called() |
Write
Preview
Loading…
Cancel
Save
Reference in new issue