From 2f2e6202979956235d0cbf316b001c8831797550 Mon Sep 17 00:00:00 2001 From: MarcUsher <99440441+MarcUsher@users.noreply.github.com> Date: Thu, 12 Dec 2024 11:54:00 +0000 Subject: [PATCH] FSPT-189: Update import paths and test mock paths --- authenticator/api/__init__.py | 6 ++--- authenticator/api/magic_links/routes.py | 2 +- authenticator/api/session/auth_session.py | 10 ++++---- authenticator/api/sso/routes.py | 4 ++-- authenticator/app.py | 12 +++++----- authenticator/frontend/magic_links/routes.py | 12 +++++----- authenticator/models/account.py | 8 +++---- authenticator/models/data.py | 2 +- authenticator/models/fund.py | 4 ++-- authenticator/models/magic_link.py | 4 ++-- authenticator/tests/conftest.py | 12 +++++----- authenticator/tests/test_account.py | 19 ++++++++-------- authenticator/tests/test_fund.py | 15 ++++++------ authenticator/tests/test_magic_links.py | 24 ++++++++++---------- authenticator/tests/test_notification.py | 7 +++--- authenticator/tests/test_security.py | 3 ++- authenticator/tests/test_signout.py | 4 ++-- authenticator/tests/test_signout_get.py | 4 ++-- authenticator/tests/test_sso.py | 2 +- 19 files changed, 78 insertions(+), 76 deletions(-) diff --git a/authenticator/api/__init__.py b/authenticator/api/__init__.py index aefe7ce9..6dab063b 100644 --- a/authenticator/api/__init__.py +++ b/authenticator/api/__init__.py @@ -1,3 +1,3 @@ -from api.magic_links.routes import MagicLinksView # noqa -from api.session.auth_session import AuthSessionUserView, AuthSessionSignOutView # noqa -from api.sso.routes import SsoLoginView, SsoLogoutView, SsoGraphCallView, SsoGetTokenView # noqa +from authenticator.api.magic_links.routes import MagicLinksView # noqa +from authenticator.api.session.auth_session import AuthSessionUserView, AuthSessionSignOutView # noqa +from authenticator.api.sso.routes import SsoLoginView, SsoLogoutView, SsoGraphCallView, SsoGetTokenView # noqa diff --git a/authenticator/api/magic_links/routes.py b/authenticator/api/magic_links/routes.py index 71f1f4c4..9ef39ebd 100644 --- a/authenticator/api/magic_links/routes.py +++ b/authenticator/api/magic_links/routes.py @@ -8,7 +8,7 @@ from models.account import AccountMethods from models.magic_link import MagicLinkMethods -from api.session.auth_session import AuthSessionBase +from authenticator.api.session.auth_session import AuthSessionBase from common.blueprints import Blueprint from config import Config diff --git a/authenticator/api/session/auth_session.py b/authenticator/api/session/auth_session.py index fd0111ba..5e417ccf 100644 --- a/authenticator/api/session/auth_session.py +++ b/authenticator/api/session/auth_session.py @@ -5,16 +5,16 @@ from flask import current_app, make_response, redirect, request, session, url_for from flask.views import MethodView from fsd_utils import clear_sentry -from models.magic_link import MagicLinkMethods -from security.utils import create_token, decode_with_options, validate_token -from api.responses import error_response -from api.session.exceptions import SessionCreateError +from authenticator.api.responses import error_response +from authenticator.api.session.exceptions import SessionCreateError +from authenticator.models.magic_link import MagicLinkMethods +from authenticator.security.utils import create_token, decode_with_options, validate_token from common.blueprints import Blueprint from config import Config if TYPE_CHECKING: - from models.account import Account as Account + from authenticator.models.account import Account as Account api_sessions_bp = Blueprint("api_sessions", __name__) diff --git a/authenticator/api/sso/routes.py b/authenticator/api/sso/routes.py index 1546495b..1040f2a6 100644 --- a/authenticator/api/sso/routes.py +++ b/authenticator/api/sso/routes.py @@ -5,9 +5,9 @@ from flask import abort, current_app, make_response, redirect, request, session from flask.views import MethodView from fsd_utils import clear_sentry -from models.account import AccountMethods +from authenticator.models.account import AccountMethods -from api.session.auth_session import AuthSessionBase +from authenticator.api.session.auth_session import AuthSessionBase from common.blueprints import Blueprint from config import Config diff --git a/authenticator/app.py b/authenticator/app.py index a44da530..6e1cc5a3 100644 --- a/authenticator/app.py +++ b/authenticator/app.py @@ -8,7 +8,6 @@ from flask_redis import FlaskRedis from flask_session import Session from flask_talisman import Talisman -from frontend.magic_links.filters import datetime_format from fsd_utils import LanguageSelector, init_sentry from fsd_utils.healthchecks.checkers import FlaskRunningChecker, RedisChecker from fsd_utils.healthchecks.healthcheck import Healthcheck @@ -16,8 +15,9 @@ from fsd_utils.logging import logging from fsd_utils.services.aws_extended_client import SQSExtendedClient from jinja2 import ChoiceLoader, PackageLoader, PrefixLoader -from models.fund import FundMethods +from authenticator.frontend.magic_links.filters import datetime_format +from authenticator.models.fund import FundMethods from config import Config redis_mlinks = FlaskRedis(config_prefix="REDIS_MLINKS") @@ -115,14 +115,14 @@ def _get_service_title(): return dict(get_service_title=_get_service_title) with flask_app.app_context(): - from frontend.default.routes import ( + from authenticator.frontend.default.routes import ( default_bp, internal_server_error, not_found, ) - from frontend.magic_links.routes import magic_links_bp - from frontend.sso.routes import sso_bp - from frontend.user.routes import user_bp + from authenticator.frontend.magic_links.routes import magic_links_bp + from authenticator.frontend.sso.routes import sso_bp + from authenticator.frontend.user.routes import user_bp flask_app.register_error_handler(404, not_found) flask_app.register_error_handler(500, internal_server_error) diff --git a/authenticator/frontend/magic_links/routes.py b/authenticator/frontend/magic_links/routes.py index 79ec3a99..e9685146 100644 --- a/authenticator/frontend/magic_links/routes.py +++ b/authenticator/frontend/magic_links/routes.py @@ -1,14 +1,14 @@ import uuid from flask import Blueprint, abort, current_app, g, redirect, render_template, request, url_for -from frontend.magic_links.forms import EmailForm from fsd_utils.authentication.decorators import login_requested -from models.account import AccountError, AccountMethods -from models.data import get_round_data -from models.fund import FundMethods -from models.magic_link import MagicLinkError, MagicLinkMethods -from models.notification import NotificationError +from authenticator.frontend.magic_links.forms import EmailForm +from authenticator.models.account import AccountError, AccountMethods +from authenticator.models.data import get_round_data +from authenticator.models.fund import FundMethods +from authenticator.models.magic_link import MagicLinkError, MagicLinkMethods +from authenticator.models.notification import NotificationError from config import Config magic_links_bp = Blueprint( diff --git a/authenticator/models/account.py b/authenticator/models/account.py index e780cd4d..4d873c65 100644 --- a/authenticator/models/account.py +++ b/authenticator/models/account.py @@ -5,11 +5,11 @@ from fsd_utils.config.notify_constants import NotifyConstants import config +from authenticator.models.data import get_account_data, get_data, get_round_data, post_data, put_data +from authenticator.models.fund import FundMethods +from authenticator.models.magic_link import MagicLinkMethods +from authenticator.models.notification import Notification from config import Config -from models.data import get_account_data, get_data, get_round_data, post_data, put_data -from models.fund import FundMethods -from models.magic_link import MagicLinkMethods -from models.notification import Notification @dataclass diff --git a/authenticator/models/data.py b/authenticator/models/data.py index acffb217..81dddd0d 100644 --- a/authenticator/models/data.py +++ b/authenticator/models/data.py @@ -6,8 +6,8 @@ from flask import current_app from fsd_utils.locale_selector.get_lang import get_lang +from authenticator.models.round import Round from config import Config -from models.round import Round def api_call(endpoint: str, method: str = "GET", params: dict = None): diff --git a/authenticator/models/fund.py b/authenticator/models/fund.py index a898d05c..1c11369e 100644 --- a/authenticator/models/fund.py +++ b/authenticator/models/fund.py @@ -4,9 +4,9 @@ from flask import request from fsd_utils.locale_selector.get_lang import get_lang +from authenticator.models.data import get_data +from authenticator.models.round import Round from config import Config -from models.data import get_data -from models.round import Round @dataclass diff --git a/authenticator/models/magic_link.py b/authenticator/models/magic_link.py index 0f80facb..2a141632 100644 --- a/authenticator/models/magic_link.py +++ b/authenticator/models/magic_link.py @@ -10,12 +10,12 @@ from flask import current_app from flask_redis import FlaskRedis -from security.utils import create_token +from authenticator.security.utils import create_token from config import Config if TYPE_CHECKING: - from models.account import Account as Account + from authenticator.models.account import Account as Account @dataclass diff --git a/authenticator/tests/conftest.py b/authenticator/tests/conftest.py index 9e102f91..d7e87642 100644 --- a/authenticator/tests/conftest.py +++ b/authenticator/tests/conftest.py @@ -1,9 +1,9 @@ import pytest from flask import current_app -from models.account import AccountMethods from testing.mocks.mocks import * # noqa from app import create_app +from authenticator.models.account import AccountMethods @pytest.fixture @@ -15,17 +15,17 @@ def app_context(): @pytest.fixture(scope="function") def create_magic_link(mocker): - from models.fund import Fund - from models.round import Round + from authenticator.models.fund import Fund + from authenticator.models.round import Round mocker.patch( - "models.account.FundMethods.get_fund", + "authenticator.models.account.FundMethods.get_fund", return_value=Fund( name="test fund", fund_title="hello", short_name="COF", identifier="asdfasdf", description="asdfasdfasdf" ), ) - mocker.patch("models.account.get_round_data", return_value=Round(contact_email="asdf@asdf.com")) - mocker.patch("models.account.Notification.send", return_value=True) + mocker.patch("authenticator.models.account.get_round_data", return_value=Round(contact_email="asdf@asdf.com")) + mocker.patch("authenticator.models.account.Notification.send", return_value=True) auth_landing = AccountMethods.get_magic_link("a@example.com", "cof", "r1w1") link_key_end = auth_landing.index("?fund=") link_key = auth_landing[link_key_end - 8 : link_key_end] # noqa:E203 diff --git a/authenticator/tests/test_account.py b/authenticator/tests/test_account.py index 0c911a19..314fe452 100644 --- a/authenticator/tests/test_account.py +++ b/authenticator/tests/test_account.py @@ -1,7 +1,8 @@ import pytest -from models.account import Account, AccountMethods -from models.fund import Fund -from models.round import Round + +from authenticator.models.account import Account, AccountMethods +from authenticator.models.fund import Fund +from authenticator.models.round import Round test_user_id = "test_id" test_user_email = "john@example.com" @@ -15,7 +16,7 @@ def mock_get_account(mocker, request): new_account = request.node.get_closest_marker("new_account") mocker.patch( - "models.account.AccountMethods.get_account", + "authenticator.models.account.AccountMethods.get_account", return_value=Account.from_json( { "account_id": test_user_id, @@ -34,7 +35,7 @@ def mock_get_account(mocker, request): @pytest.fixture(scope="function") def mock_create_account(mocker): mocker.patch( - "models.account.post_data", + "authenticator.models.account.post_data", return_value={ "account_id": test_user_id, "email_address": test_user_email, @@ -47,7 +48,7 @@ def mock_create_account(mocker): @pytest.fixture(scope="function") def mock_update_account(mocker): mocker.patch( - "models.account.put_data", + "authenticator.models.account.put_data", return_value={ "account_id": test_user_id, "email_address": "john.Doe@example.com", @@ -95,7 +96,7 @@ def test_create_account(self, mock_create_account): def test_create_magic_link(self, mocker, mock_get_account, flask_test_client, mock_redis_magic_links): mocker.patch( - "models.account.FundMethods.get_fund", + "authenticator.models.account.FundMethods.get_fund", return_value=Fund( name="test fund", fund_title="hello", @@ -104,8 +105,8 @@ def test_create_magic_link(self, mocker, mock_get_account, flask_test_client, mo description="asdfasdfasdf", ), ) - mocker.patch("models.account.get_round_data", return_value=Round(contact_email="asdf@asdf.com")) - mock_send_notification = mocker.patch("models.account.Notification.send", return_value=True) + mocker.patch("authenticator.models.account.get_round_data", return_value=Round(contact_email="asdf@asdf.com")) + mock_send_notification = mocker.patch("authenticator.models.account.Notification.send", return_value=True) result = AccountMethods.get_magic_link( email=test_user_email, diff --git a/authenticator/tests/test_fund.py b/authenticator/tests/test_fund.py index 24f9a0c6..248b9d76 100644 --- a/authenticator/tests/test_fund.py +++ b/authenticator/tests/test_fund.py @@ -1,7 +1,6 @@ from unittest import mock -from models.fund import Fund, FundMethods - +from authenticator.models.fund import Fund, FundMethods from config.envs.default import DefaultConfig from config.envs.unit_test import UnitTestConfig @@ -26,11 +25,11 @@ def test_get_service_name(self, app_context, mocker): } ) # Mock request object with query parameters - mock_request = mocker.patch("models.fund.request") + mock_request = mocker.patch("authenticator.models.fund.request") mock_request.args.get = lambda arg: fund_short_name # Call the method and check the output with mock.patch( - "models.fund.FundMethods.get_fund", + "authenticator.models.fund.FundMethods.get_fund", return_value=mock_fund, ) as mock_get_fund: FundMethods.get_service_name() @@ -39,11 +38,11 @@ def test_get_service_name(self, app_context, mocker): def test_get_service_name_for_none_short_name(self, app_context, mocker): fund_short_name = None # Mock request object with query parameters - mock_request = mocker.patch("models.fund.request") + mock_request = mocker.patch("authenticator.models.fund.request") mock_request.args.get = lambda arg: fund_short_name # Call the method and check the output with mock.patch( - "models.fund.FundMethods.get_fund", + "authenticator.models.fund.FundMethods.get_fund", return_value=None, ) as mock_get_fund: result = FundMethods.get_service_name() @@ -52,7 +51,7 @@ def test_get_service_name_for_none_short_name(self, app_context, mocker): def test_get_fund_success(self, app_context): # Mock the get_data function to return a response with a valid "id" - with mock.patch("models.fund.get_data") as mock_get_data: + with mock.patch("authenticator.models.fund.get_data") as mock_get_data: mock_get_data.return_value = { "id": 1234, "name": "Test Fund", @@ -76,7 +75,7 @@ def test_get_fund_success(self, app_context): def test_get_fund_failure(self, app_context): # Mock the get_data function to return a response with a valid "id" - with mock.patch("models.fund.get_data") as mock_get_data: + with mock.patch("authenticator.models.fund.get_data") as mock_get_data: mock_get_data.return_value = {} # Call the get_fund function with a valid fund short name diff --git a/authenticator/tests/test_magic_links.py b/authenticator/tests/test_magic_links.py index b083c0b4..3f80db1e 100644 --- a/authenticator/tests/test_magic_links.py +++ b/authenticator/tests/test_magic_links.py @@ -7,13 +7,13 @@ import frontend import pytest -from api.session.auth_session import AuthSessionBase from bs4 import BeautifulSoup -from frontend.magic_links.forms import EmailForm -from models.account import AccountMethods -from security.utils import validate_token from app import app +from authenticator.api.session.auth_session import AuthSessionBase +from authenticator.frontend.magic_links.forms import EmailForm +from authenticator.models.account import AccountMethods +from authenticator.security.utils import validate_token @pytest.mark.usefixtures("flask_test_client") @@ -122,8 +122,8 @@ def test_reused_magic_link_with_active_session_shows_landing(self, flask_test_cl landing_endpoint = f"/service/magic-links/landing/{link_key}?fund=cof&round=r2w3" with ( - mock.patch("models.fund.FundMethods.get_fund") as mock_get_fund, - mock.patch("frontend.magic_links.routes.get_round_data") as mock_get_round_data, + mock.patch("authenticator.models.fund.FundMethods.get_fund") as mock_get_fund, + mock.patch("authenticator.frontend.magic_links.routes.get_round_data") as mock_get_round_data, ): # Mock get_fund() called in get_magic_link() mock_fund = mock.MagicMock() @@ -265,13 +265,13 @@ def test_magic_link_route_new(self, flask_test_client): mock_account.get_magic_link.return_value = True # Test post request with fund and round short names: - with mock.patch("frontend.magic_links.routes.EmailForm", return_value=mock_form): + with mock.patch("authenticator.frontend.magic_links.routes.EmailForm", return_value=mock_form): with mock.patch( - "frontend.magic_links.routes.AccountMethods", + "authenticator.frontend.magic_links.routes.AccountMethods", return_value=mock_account, ): with mock.patch( - "frontend.magic_links.routes.get_round_data", + "authenticator.frontend.magic_links.routes.get_round_data", return_value=mock_account, ): response = flask_test_client.post( @@ -299,13 +299,13 @@ def test_magic_link_route_new_with_notify_reference(self, flask_test_client): mock_account.get_magic_link.return_value = True # Test post request with fund and round short names: - with mock.patch("frontend.magic_links.routes.EmailForm", return_value=mock_form): + with mock.patch("authenticator.frontend.magic_links.routes.EmailForm", return_value=mock_form): with mock.patch( - "frontend.magic_links.routes.AccountMethods", + "authenticator.frontend.magic_links.routes.AccountMethods", return_value=mock_account, ): with mock.patch( - "frontend.magic_links.routes.get_round_data", + "authenticator.frontend.magic_links.routes.get_round_data", return_value=mock_account, ): response = flask_test_client.post( diff --git a/authenticator/tests/test_notification.py b/authenticator/tests/test_notification.py index c905f539..e963289f 100644 --- a/authenticator/tests/test_notification.py +++ b/authenticator/tests/test_notification.py @@ -4,9 +4,10 @@ import boto3 import pytest from fsd_utils.services.aws_extended_client import SQSExtendedClient -from models.notification import Config, Notification, NotificationError from moto import mock_aws +from authenticator.models.notification import Config, Notification, NotificationError + @pytest.fixture def disable_notifications(monkeypatch): @@ -30,7 +31,7 @@ def test_notification_send_disabled(app_context, disable_notifications, caplog): @mock_aws def test_notification_send_success(app_context, monkeypatch, enable_notifications): - with mock.patch("models.notification.Notification._get_sqs_client") as mock_get_sqs_client: + with mock.patch("authenticator.models.notification.Notification._get_sqs_client") as mock_get_sqs_client: template_type = "welcome" template_type = "welcome" to_email = "test@example.com" @@ -64,7 +65,7 @@ def test_notification_send_success(app_context, monkeypatch, enable_notification @mock_aws def test_notification_send_failure(app_context, monkeypatch, enable_notifications): - with mock.patch("models.notification.Notification._get_sqs_client") as mock_get_sqs_client: + with mock.patch("authenticator.models.notification.Notification._get_sqs_client") as mock_get_sqs_client: template_type = "welcome" to_email = "test@example.com" content = {"name": "John"} diff --git a/authenticator/tests/test_security.py b/authenticator/tests/test_security.py index 09aba20f..d42c3a6d 100644 --- a/authenticator/tests/test_security.py +++ b/authenticator/tests/test_security.py @@ -7,7 +7,8 @@ import pytest from jwt import decode from jwt.exceptions import ExpiredSignatureError, InvalidSignatureError -from security.utils import create_token, validate_token + +from authenticator.security.utils import create_token, validate_token class TestSecurityUtils: diff --git a/authenticator/tests/test_signout.py b/authenticator/tests/test_signout.py index 4a4d2e1b..1fa1591f 100644 --- a/authenticator/tests/test_signout.py +++ b/authenticator/tests/test_signout.py @@ -6,8 +6,8 @@ import pytest from bs4 import BeautifulSoup -from security.utils import create_token, validate_token +from authenticator.security.utils import create_token, validate_token from config.envs.default import SafeAppConfig @@ -43,7 +43,7 @@ def test_signout_clears_cookie(self, flask_test_client, mock_redis_sessions): flask_test_client.set_cookie("/", "fsd_user_token", "invalid_token") flask_test_client.set_cookie("/", "user_fund_and_round", "fund_round") - with patch("api.session.auth_session.validate_token") as mock_validate_token: # noqa + with patch("authenticator.api.session.auth_session.validate_token") as mock_validate_token: # noqa mock_validate_token.return_value = { "fund": "test_fund", "round": "test_round", diff --git a/authenticator/tests/test_signout_get.py b/authenticator/tests/test_signout_get.py index 272acb73..0c5ec664 100644 --- a/authenticator/tests/test_signout_get.py +++ b/authenticator/tests/test_signout_get.py @@ -5,8 +5,8 @@ from unittest.mock import PropertyMock, patch import pytest -from security.utils import validate_token +from authenticator.security.utils import validate_token from config.envs.default import SafeAppConfig @@ -43,7 +43,7 @@ def test_signout_clears_cookie(self, flask_test_client, mock_redis_sessions): flask_test_client.set_cookie("/", "fsd_user_token", "invalid_token") flask_test_client.set_cookie("/", "user_fund_and_round", "fund_round") - with patch("api.session.auth_session.validate_token") as mock_validate_token: # noqa + with patch("authenticator.api.session.auth_session.validate_token") as mock_validate_token: # noqa mock_validate_token.return_value = { "fund": "test_fund", "round": "test_round", diff --git a/authenticator/tests/test_sso.py b/authenticator/tests/test_sso.py index 0b88fc8e..00298eb0 100644 --- a/authenticator/tests/test_sso.py +++ b/authenticator/tests/test_sso.py @@ -184,7 +184,7 @@ def test_sso_get_token_prevents_overwrite_of_existing_azure_subject_id(flask_tes def test_sso_get_token_500_when_error_in_auth_code_flow(flask_test_client, mocker, caplog): - mock_build_msal_app = mocker.patch("api.sso.routes.SsoBase._build_msal_app") + mock_build_msal_app = mocker.patch("authenticator.api.sso.routes.SsoBase._build_msal_app") mock_msal_app = mock_build_msal_app.return_value mock_msal_app.acquire_token_by_auth_code_flow.return_value = {"error": "some_error"}