Skip to content

Commit

Permalink
Add thorough tests for all application endpoints
Browse files Browse the repository at this point in the history
This commit includes the creation of a new file, test_endpoints.py, specifically dedicated to thoroughly testing the functionality of all application endpoints. It utilizes Pytest to ensure that each endpoint correctly calls (or doesn’t call) the appropriate methods in their supporting classes, as per the original design. The test checks both allowed and not allowed methods for each endpoint.
  • Loading branch information
maxachis committed May 25, 2024
1 parent ddc9d26 commit 80b55e3
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions tests/test_endpoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"""

Check warning on line 1 in tests/test_endpoints.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/test_endpoints.py#L1 <205>

1 blank line required between summary line and description
Raw output
./tests/test_endpoints.py:1:1: D205 1 blank line required between summary line and description

Check warning on line 1 in tests/test_endpoints.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/test_endpoints.py#L1 <400>

First line should end with a period
Raw output
./tests/test_endpoints.py:1:1: D400 First line should end with a period
This module tests the functionality of all endpoints, ensuring that, as designed, they call (or don't call)
the appropriate methods in their supporting classes
"""

from collections import namedtuple
from typing import Type, Any, List

Check warning on line 7 in tests/test_endpoints.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/test_endpoints.py#L7 <401>

'typing.Type' imported but unused
Raw output
./tests/test_endpoints.py:7:1: F401 'typing.Type' imported but unused

Check warning on line 7 in tests/test_endpoints.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/test_endpoints.py#L7 <401>

'typing.Any' imported but unused
Raw output
./tests/test_endpoints.py:7:1: F401 'typing.Any' imported but unused

Check warning on line 7 in tests/test_endpoints.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/test_endpoints.py#L7 <401>

'typing.List' imported but unused
Raw output
./tests/test_endpoints.py:7:1: F401 'typing.List' imported but unused

import pytest
from unittest.mock import patch

from flask.testing import FlaskClient
from flask_restful import Resource

from app import create_app
from resources.Agencies import Agencies
from resources.ApiKey import ApiKey
from resources.Archives import Archives
from resources.DataSources import (
DataSources,
DataSourcesMap,
DataSourcesNeedsIdentification,
DataSourceById,
)
from resources.Login import Login
from resources.QuickSearch import QuickSearch
from resources.RefreshSession import RefreshSession
from resources.RequestResetPassword import RequestResetPassword
from resources.ResetPassword import ResetPassword
from resources.ResetTokenValidation import ResetTokenValidation
from resources.SearchTokens import SearchTokens
from resources.User import User


# Define constants for HTTP methods
GET = "get"
POST = "post"
PUT = "put"
DELETE = "delete"


@pytest.fixture
def client(mocker) -> FlaskClient:
"""

Check warning on line 44 in tests/test_endpoints.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/test_endpoints.py#L44 <205>

1 blank line required between summary line and description
Raw output
./tests/test_endpoints.py:44:1: D205 1 blank line required between summary line and description

Check warning on line 44 in tests/test_endpoints.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/test_endpoints.py#L44 <400>

First line should end with a period
Raw output
./tests/test_endpoints.py:44:1: D400 First line should end with a period
Create a client with a mocked database connection
:param mocker:
:return:
"""
app = create_app(mocker.MagicMock())
with app.test_client() as client:
yield client


def run_endpoint_tests(

Check warning on line 54 in tests/test_endpoints.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/test_endpoints.py#L54 <103>

Missing docstring in public function
Raw output
./tests/test_endpoints.py:54:1: D103 Missing docstring in public function
client: FlaskClient, endpoint: str, class_type: Resource, allowed_methods: list[str]
):
methods = [GET, POST, PUT, DELETE]
for method in methods:
if method in allowed_methods:
with patch.object(
class_type, method, return_value="Mocked response"
) as mock_method:
response = getattr(client, method)(endpoint)
assert (
response.status_code == 200
), f"{method.upper()} {endpoint} failed with status code {response.status_code}, expected 200"
mock_method.assert_called_once(), f"{method.upper()} {endpoint} should have called the {method} method on {class_type.__name__}"
else:
response = getattr(client, method)(endpoint)
assert (
response.status_code == 405
), f"{method.upper()} {endpoint} failed with status code {response.status_code}, expected 405"


TestParameters = namedtuple("Resource", ["class_type", "endpoint", "allowed_methods"])
test_parameters = [
TestParameters(User, "/user", [POST, PUT]),
TestParameters(Login, "/login", [POST]),
TestParameters(RefreshSession, "/refresh-session", [POST]),
TestParameters(ApiKey, "/api_key", [GET]),
TestParameters(RequestResetPassword, "/request-reset-password", [POST]),
TestParameters(ResetPassword, "/reset-password", [POST]),
TestParameters(ResetTokenValidation, "/reset-token-validation", [POST]),
TestParameters(QuickSearch, "/quick-search/<search>/<location>", [GET]),
TestParameters(Archives, "/archives", [GET, PUT]),
TestParameters(DataSources, "/data-sources", [GET, POST]),
TestParameters(DataSourcesMap, "/data-sources-map", [GET]),
TestParameters(
DataSourcesNeedsIdentification, "/data-sources-needs-identification", [GET]
),
TestParameters(DataSourceById, "/data-sources-by-id/<data_source_id>", [GET, PUT]),
TestParameters(Agencies, "/agencies/<page>", [GET]),
TestParameters(SearchTokens, "/search-tokens", [GET]),
]


@pytest.mark.parametrize("test_parameter", test_parameters)
def test_endpoints(client: FlaskClient, test_parameter) -> None:
"""

Check warning on line 99 in tests/test_endpoints.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/test_endpoints.py#L99 <205>

1 blank line required between summary line and description
Raw output
./tests/test_endpoints.py:99:1: D205 1 blank line required between summary line and description

Check warning on line 99 in tests/test_endpoints.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] tests/test_endpoints.py#L99 <400>

First line should end with a period
Raw output
./tests/test_endpoints.py:99:1: D400 First line should end with a period
Using the test_parameters list, this tests all endpoints to ensure that
only the appropriate methods can be called from the endpoints
:param client: the client fixture
:param class_type:
:param endpoint:
:param allowed_methods:
:return:
"""
run_endpoint_tests(
client,
test_parameter.endpoint,
test_parameter.class_type,
test_parameter.allowed_methods,
)

0 comments on commit 80b55e3

Please sign in to comment.