From 5f792429a66f7037e662de4a3f4863e15f2622fd Mon Sep 17 00:00:00 2001 From: Drew Short Date: Wed, 4 Jul 2018 22:37:43 -0500 Subject: [PATCH] Adding example mocking tests --- server/Pipfile | 1 + server/Pipfile.lock | 17 ++- server/tests/api/test_decorators.py | 33 +++++ .../test_authentication_middleware.py | 132 ++++++++++++++++++ 4 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 server/tests/api/test_decorators.py create mode 100644 server/tests/middleware/test_authentication_middleware.py diff --git a/server/Pipfile b/server/Pipfile index 91773e8..dbb7808 100644 --- a/server/Pipfile +++ b/server/Pipfile @@ -17,6 +17,7 @@ pytest = "*" coverage = "*" pycodestyle = "*" mypy = "*" +mock = "*" [requires] python_version = "3.6" diff --git a/server/Pipfile.lock b/server/Pipfile.lock index 3caa920..e79655f 100644 --- a/server/Pipfile.lock +++ b/server/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "5286b9a3df03bd77ad7efe758fb498cad5e666806a24092c906cc3e2c8b77779" + "sha256": "a8ad1b3822122643e380c48cefa0ab6356d268b7b9756f55d8040466825fea25" }, "pipfile-spec": 6, "requires": { @@ -258,6 +258,14 @@ "index": "pypi", "version": "==4.5.1" }, + "mock": { + "hashes": [ + "sha256:5ce3c71c5545b472da17b72268978914d0252980348636840bd34a00b5cc96c1", + "sha256:b158b6df76edd239b8208d481dc46b6afd45a846b7812ff0ce58971cf5bc8bba" + ], + "index": "pypi", + "version": "==2.0.0" + }, "more-itertools": { "hashes": [ "sha256:2b6b9893337bfd9166bee6a62c2b0c9fe7735dcf85948b387ec8cba30e85d8e8", @@ -274,6 +282,13 @@ "index": "pypi", "version": "==0.610" }, + "pbr": { + "hashes": [ + "sha256:3747c6f017f2dc099986c325239661948f9f5176f6880d9fdef164cb664cd665", + "sha256:a9c27eb8f0e24e786e544b2dbaedb729c9d8546342b5a6818d8eda098ad4340d" + ], + "version": "==4.0.4" + }, "pluggy": { "hashes": [ "sha256:7f8ae7f5bdf75671a718d2daf0a64b7885f74510bcd98b1a0bb420eb9a9d0cff", diff --git a/server/tests/api/test_decorators.py b/server/tests/api/test_decorators.py new file mode 100644 index 0000000..cf1ed57 --- /dev/null +++ b/server/tests/api/test_decorators.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 diff --git a/server/tests/middleware/test_authentication_middleware.py b/server/tests/middleware/test_authentication_middleware.py new file mode 100644 index 0000000..d962df0 --- /dev/null +++ b/server/tests/middleware/test_authentication_middleware.py @@ -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()