Skip to content

Commit

Permalink
- tests updated
Browse files Browse the repository at this point in the history
  • Loading branch information
hamzabinkhalid committed Jan 8, 2025
1 parent 672e164 commit 684e7e8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
36 changes: 36 additions & 0 deletions tests/authenticator_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from contextlib import contextmanager
from typing import Any, Generator
from unittest import mock

import jwt as jwt
import pytest
Expand Down Expand Up @@ -42,6 +43,41 @@ def create_magic_link(mocker, mock_notification_service_calls):
yield link_key


def patch_get_applications_for_account(path):
return mock.patch(
path,
return_value=[
ApplicationSummary(
id="00000000-0000-0000-0000-000000000000",
reference="TEST-REFERENCE",
status="NOT_STARTED",
round_id="00000000-0000-0000-0000-000000000000",
fund_id="00000000-0000-0000-0000-000000000000",
started_at="2025-01-07T15:22:08.422538+00:00",
project_name=None,
language="English",
last_edited="2025-01-07T15:22:08.422538+00:00",
)
],
)


@pytest.fixture
def mock_get_applications_for_auth_api():
with patch_get_applications_for_account(
"authenticator.api.magic_links.routes.get_applications_for_account"
) as mock_get_applications:
yield mock_get_applications


@pytest.fixture
def mock_get_applications_for_auth_frontend():
with patch_get_applications_for_account(
"authenticator.frontend.magic_links.routes.get_applications_for_account"
) as mock_get_applications:
yield mock_get_applications


@pytest.fixture
def mock_get_applications_for_account():
from unittest import mock
Expand Down
16 changes: 15 additions & 1 deletion tests/authenticator_tests/test_magic_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test_reused_magic_link_redirects_for_active_session(
assert second_response.status_code == 302

def test_reused_magic_link_with_active_session_shows_landing(
self, authenticator_test_client, create_magic_link, mock_get_applications_for_account
self, authenticator_test_client, create_magic_link, mock_get_applications_for_auth_frontend
):
"""
GIVEN a running Flask client, redis instance and
Expand All @@ -133,6 +133,9 @@ def test_reused_magic_link_with_active_session_shows_landing(
with (
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.patch(
"authenticator.frontend.magic_links.routes.MagicLinkMethods.redis_mlinks", create=True
) as mock_redis_mlinks,
):
# Mock get_fund() called in get_magic_link()
mock_fund = mock.MagicMock()
Expand All @@ -148,8 +151,13 @@ def test_reused_magic_link_with_active_session_shows_landing(
mock_round.configure_mock(contact_email="[email protected]")
mock_round.configure_mock(reference_contact_page_over_email=False)
mock_round.configure_mock(is_expression_of_interest=False)
mock_round.configure_mock(has_eligibility=True)
mock_get_round_data.return_value = mock_round

mock_redis_mlinks.get.return_value = json.dumps(
{"accountId": "usera", "iat": 1736312454, "exp": 1736316114}
).encode("utf-8")

# use magic link landing but unauthorised
landing_response = authenticator_test_client.get(landing_endpoint)

Expand Down Expand Up @@ -177,10 +185,16 @@ def test_reused_magic_link_with_active_session_shows_landing(
== 1
)

mock_redis_mlinks.get.return_value = None

# use link
use_link_response = authenticator_test_client.get(use_endpoint)
assert use_link_response.status_code == 302

mock_redis_mlinks.get.return_value = json.dumps(
{"accountId": "usera", "iat": 1736312454, "exp": 1736316114}
).encode("utf-8")

# re-use magic link landing but now authorised (cookie present)
second_landing_response = authenticator_test_client.get(landing_endpoint)
assert second_landing_response.status_code == 200
Expand Down

0 comments on commit 684e7e8

Please sign in to comment.