test(contact): added contact route with mock

This commit is contained in:
Stan Girard 2023-11-13 17:58:11 +01:00
parent ec60d3dbff
commit 6017fa2e9c
5 changed files with 29 additions and 2 deletions

View File

@ -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

View File

@ -37,4 +37,5 @@ boto3==1.28.46
botocore==1.31.46
celery[sqs]
python-dotenv
pytest-mock

View File

@ -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):

View File

@ -0,0 +1,6 @@
import pytest
@pytest.fixture(autouse=True)
def no_requests(monkeypatch):
monkeypatch.delattr("requests.sessions.Session.request")

View File

@ -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()