diff --git a/.github/workflows/backend-tests.yml b/.github/workflows/backend-tests.yml index 184b4ae55..dc284825c 100644 --- a/.github/workflows/backend-tests.yml +++ b/.github/workflows/backend-tests.yml @@ -52,7 +52,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install pytest pytest-emoji pytest-md + pip install pytest pytest-emoji pytest-md pytest-mock pip install pyright if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Run pytest diff --git a/backend/requirements.txt b/backend/requirements.txt index 729a34a8b..1ca7565c9 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -37,4 +37,5 @@ boto3==1.28.46 botocore==1.31.46 celery[sqs] python-dotenv +pytest-mock diff --git a/backend/routes/contact_routes.py b/backend/routes/contact_routes.py index 9903a5327..ae1b677f2 100644 --- a/backend/routes/contact_routes.py +++ b/backend/routes/contact_routes.py @@ -5,13 +5,16 @@ from pydantic import BaseModel from utils.send_email import send_email + class ContactMessage(BaseModel): customer_email: str content: str + router = APIRouter() logger = get_logger(__name__) + def resend_contact_sales_email(customer_email: str, content: str): settings = ContactsSettings() mail_from = settings.resend_contact_sales_from @@ -27,7 +30,9 @@ def resend_contact_sales_email(customer_email: str, content: str): "reply_to": customer_email, "html": body, } - send_email(params) + + return send_email(params) + @router.post("/contact") def post_contact(message: ContactMessage): diff --git a/backend/tests/contact/conftest.py b/backend/tests/contact/conftest.py new file mode 100644 index 000000000..4f527facd --- /dev/null +++ b/backend/tests/contact/conftest.py @@ -0,0 +1,6 @@ +import pytest + + +@pytest.fixture(autouse=True) +def no_requests(monkeypatch): + monkeypatch.delattr("requests.sessions.Session.request") diff --git a/backend/tests/contact/test_contact.py b/backend/tests/contact/test_contact.py new file mode 100644 index 000000000..4e16d0bfe --- /dev/null +++ b/backend/tests/contact/test_contact.py @@ -0,0 +1,15 @@ +def test_post_contact(client, mocker): + # Mock the send_email function + mock_send_email = mocker.patch("routes.contact_routes.resend_contact_sales_email") + + # Define test data + test_data = {"customer_email": "test@example.com", "content": "Test message"} + + # Call the endpoint + response = client.post("/contact", json=test_data) + + # Assert that the response is as expected + assert response.status_code == 200 + + # Assert that send_email was called with the expected parameters + mock_send_email.assert_called_once()