Browse Source

Adding example mocking tests

merge-requests/1/head
Drew Short 6 years ago
parent
commit
5f792429a6
  1. 1
      server/Pipfile
  2. 17
      server/Pipfile.lock
  3. 33
      server/tests/api/test_decorators.py
  4. 132
      server/tests/middleware/test_authentication_middleware.py

1
server/Pipfile

@ -17,6 +17,7 @@ pytest = "*"
coverage = "*" coverage = "*"
pycodestyle = "*" pycodestyle = "*"
mypy = "*" mypy = "*"
mock = "*"
[requires] [requires]
python_version = "3.6" python_version = "3.6"

17
server/Pipfile.lock

@ -1,7 +1,7 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "5286b9a3df03bd77ad7efe758fb498cad5e666806a24092c906cc3e2c8b77779"
"sha256": "a8ad1b3822122643e380c48cefa0ab6356d268b7b9756f55d8040466825fea25"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
@ -258,6 +258,14 @@
"index": "pypi", "index": "pypi",
"version": "==4.5.1" "version": "==4.5.1"
}, },
"mock": {
"hashes": [
"sha256:5ce3c71c5545b472da17b72268978914d0252980348636840bd34a00b5cc96c1",
"sha256:b158b6df76edd239b8208d481dc46b6afd45a846b7812ff0ce58971cf5bc8bba"
],
"index": "pypi",
"version": "==2.0.0"
},
"more-itertools": { "more-itertools": {
"hashes": [ "hashes": [
"sha256:2b6b9893337bfd9166bee6a62c2b0c9fe7735dcf85948b387ec8cba30e85d8e8", "sha256:2b6b9893337bfd9166bee6a62c2b0c9fe7735dcf85948b387ec8cba30e85d8e8",
@ -274,6 +282,13 @@
"index": "pypi", "index": "pypi",
"version": "==0.610" "version": "==0.610"
}, },
"pbr": {
"hashes": [
"sha256:3747c6f017f2dc099986c325239661948f9f5176f6880d9fdef164cb664cd665",
"sha256:a9c27eb8f0e24e786e544b2dbaedb729c9d8546342b5a6818d8eda098ad4340d"
],
"version": "==4.0.4"
},
"pluggy": { "pluggy": {
"hashes": [ "hashes": [
"sha256:7f8ae7f5bdf75671a718d2daf0a64b7885f74510bcd98b1a0bb420eb9a9d0cff", "sha256:7f8ae7f5bdf75671a718d2daf0a64b7885f74510bcd98b1a0bb420eb9a9d0cff",

33
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

132
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()
Loading…
Cancel
Save