diff --git a/api/core/dependencies/email/templates/squeeze.html b/api/core/dependencies/email/templates/squeeze.html new file mode 100644 index 000000000..aa289ba48 --- /dev/null +++ b/api/core/dependencies/email/templates/squeeze.html @@ -0,0 +1,56 @@ +{% extends 'base.html' %} + +{% block title %}Welcome{% endblock %} + +{% block content %} + + + + +
+
+

Welcome to Boilerplate Squeeze

+

Thanks for signing up

+
+ +
+

Hi {{name}}

+

Experience quality and innovation + like never before. Our product is made to fit your needs and make your + life easier.

+
+ +
+

Here's what you can look forward to.

+
+
    +
  • + Exclusive Offers: Enjoy special promotions and + discounts available only to our members. +
  • +
  • + Exclusive Offers: Enjoy special promotions and + discounts available only to our members. +
  • +
  • + Exclusive Offers: Enjoy special promotions and + discounts available only to our members. +
  • +
+
+
+ + + Learn more about us + + + + +
+

Regards,

+

Boilerplate

+
+
+{% endblock %} \ No newline at end of file diff --git a/api/v1/routes/squeeze.py b/api/v1/routes/squeeze.py index 1131e0edd..8256a030d 100644 --- a/api/v1/routes/squeeze.py +++ b/api/v1/routes/squeeze.py @@ -1,4 +1,4 @@ -from fastapi import APIRouter, Depends, status +from fastapi import APIRouter, Depends, status, BackgroundTasks from sqlalchemy.orm import Session from api.db.database import get_db from api.core.responses import SUCCESS @@ -13,6 +13,7 @@ @squeeze.post("", response_model=success_response, status_code=201) def create_squeeze( + background_tasks: BackgroundTasks, data: CreateSqueeze, db: Session = Depends(get_db), current_user: User = Depends(user_service.get_current_super_admin), @@ -23,7 +24,7 @@ def create_squeeze( return success_response(status.HTTP_404_NOT_FOUND, "User not found!") data.user_id = user.id data.full_name = f"{user.first_name} {user.last_name}" - new_squeeze = squeeze_service.create(db, data) + new_squeeze = squeeze_service.create(background_tasks, db, data) return success_response(status.HTTP_201_CREATED, SUCCESS, new_squeeze.to_dict()) diff --git a/api/v1/services/squeeze.py b/api/v1/services/squeeze.py index 0096a1a90..87138156e 100644 --- a/api/v1/services/squeeze.py +++ b/api/v1/services/squeeze.py @@ -1,14 +1,15 @@ -from fastapi import HTTPException +from fastapi import HTTPException, BackgroundTasks from sqlalchemy.orm import Session from api.core.base.services import Service from api.v1.models.squeeze import Squeeze +from api.core.dependencies.email_sender import send_email from api.v1.schemas.squeeze import CreateSqueeze, FilterSqueeze class SqueezeService(Service): """Squeeze service""" - def create(self, db: Session, data: CreateSqueeze): + def create(self, background_tasks: BackgroundTasks, db: Session, data: CreateSqueeze): """Create squeeze page""" new_squeeze = Squeeze( title=data.title, @@ -25,6 +26,18 @@ def create(self, db: Session, data: CreateSqueeze): db.add(new_squeeze) db.commit() db.refresh(new_squeeze) + cta_link = 'https://anchor-python.teams.hng.tech/about-us' + background_tasks.add_task( + send_email, + recipient=data.email, + template_name='squeeze.html', + subject='Welcome to HNG Squeeze', + context={ + 'name': data.full_name, + 'cta_link': cta_link + } + ) + return new_squeeze def fetch_all(self, db: Session, filter: FilterSqueeze = None): diff --git a/tests/v1/squeeze_page/test_create_squeeze.py b/tests/v1/squeeze_page/test_create_squeeze.py index 443f56785..de5d79540 100644 --- a/tests/v1/squeeze_page/test_create_squeeze.py +++ b/tests/v1/squeeze_page/test_create_squeeze.py @@ -1,9 +1,8 @@ import pytest from fastapi.testclient import TestClient from main import app -from api.db.database import get_db from unittest.mock import MagicMock, patch -from api.v1.models import * +from api.v1.models import User from api.v1.services.user import user_service from uuid_extensions import uuid7 from fastapi import status @@ -47,6 +46,14 @@ "status_code": 201, } +# Mock the BackgroundTasks and email sending function +@pytest.fixture(scope='module') +def mock_send_email(): + with patch("api.core.dependencies.email_sender.send_email", return_value=None) as mock_email_sending: + with patch("fastapi.BackgroundTasks.add_task") as add_task_mock: + # Override the add_task method to simulate direct function call + add_task_mock.side_effect = lambda func, *args, **kwargs: func(*args, **kwargs) + yield mock_email_sending @pytest.fixture def mock_db_session(_=MagicMock()): @@ -71,16 +78,17 @@ def create_mock_super_admin(_): @pytest.mark.parametrize("data", [squeeze1, squeeze2, squeeze3]) -@pytest.mark.usefixtures("mock_db_session") -def test_create_squeeze_page(mock_db_session, data): +@pytest.mark.usefixtures("mock_db_session", "mock_send_email") +def test_create_squeeze_page(mock_db_session, data, mock_send_email): """Test create squeeze page.""" create_mock_super_admin(mock_db_session) tok = client.post( LOGIN_URI, json={"email": "user1@gmail.com", "password": "P@ssw0rd"} ).json() + print(tok) assert tok["status_code"] == status.HTTP_200_OK token = tok["access_token"] res = client.post(URI, json=data, headers=theader(token)) assert res.status_code == data["status_code"] assert res.json()['data']['title'] == data["title"] - assert res.json()['data']['email'] == data["email"] \ No newline at end of file + assert res.json()['data']['email'] == data["email"] diff --git a/tests/v1/squeeze_page/test_fetch_squeeze.py b/tests/v1/squeeze_page/test_fetch_squeeze.py index cde09f70e..aab2bdedb 100644 --- a/tests/v1/squeeze_page/test_fetch_squeeze.py +++ b/tests/v1/squeeze_page/test_fetch_squeeze.py @@ -37,6 +37,16 @@ "status_code": 201, } + +# Mock the BackgroundTasks and email sending function +@pytest.fixture(scope='module') +def mock_send_email(): + with patch("api.core.dependencies.email_sender.send_email", return_value=None) as mock_email_sending: + with patch("fastapi.BackgroundTasks.add_task") as add_task_mock: + # Override the add_task method to simulate direct function call + add_task_mock.side_effect = lambda func, *args, **kwargs: func(*args, **kwargs) + yield mock_email_sending + @pytest.fixture def mock_db_session(_=MagicMock()): """Mock session""" @@ -60,8 +70,8 @@ def create_mock_super_admin(_): @pytest.mark.parametrize("data", [squeeze1]) -@pytest.mark.usefixtures("mock_db_session") -def test_fetch_squeeze_page(mock_db_session, data): +@pytest.mark.usefixtures("mock_db_session", "mock_send_email") +def test_fetch_squeeze_page(mock_db_session, data, mock_send_email): """Test create squeeze page.""" create_mock_super_admin(mock_db_session) tok = client.post( @@ -76,8 +86,8 @@ def test_fetch_squeeze_page(mock_db_session, data): @pytest.mark.parametrize("data", [squeeze1, squeeze2]) -@pytest.mark.usefixtures("mock_db_session") -def test_fetch_all_squeeze_page(mock_db_session, data): +@pytest.mark.usefixtures("mock_db_session", "mock_send_email") +def test_fetch_all_squeeze_page(mock_db_session, data, mock_send_email): """Test create squeeze page.""" create_mock_super_admin(mock_db_session) tok = client.post(