Skip to content

Commit

Permalink
Merge pull request #949 from trevorjob/dev
Browse files Browse the repository at this point in the history
feat: added feature to send email when user unsubscribes from a newsletter
  • Loading branch information
johnson-oragui authored Aug 23, 2024
2 parents 885d3ed + 9c987db commit 50312de
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 32 deletions.
45 changes: 45 additions & 0 deletions api/core/dependencies/email/templates/unsubscribe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{% extends 'base.html' %} {% block title %}Welcome{% endblock %} {% block
content %}
<table role="presentation" width="100%" style="padding: 3.5rem">
<tr>
<td>
<div style="text-align: center; margin-bottom: 1.5rem">
<h1 style="font-size: 1.5rem; color: #0a0a0a; font-weight: 600">
Hello From The Boilerplate
</h1>
<p
style="
font-size: 1.125rem;
color: rgba(0, 0, 0, 0.8);
font-weight: 500;
"
>
Unsubscription Successful
</p>
</div>

<div>
<p style="color: #111; font-size: 1.125rem; font-weight: 600">
Hi Hope this find you well.
</p>
<p style="color: rgba(17, 17, 17, 0.9); font-weight: 400">
You have successfully Unsubscribed from our email newsletter.
</p>
<p style="color: rgba(17, 17, 17, 0.9); font-weight: 400">
This emai is a confirmation of that action. As you will not recieve
any newsletter updates from us anymore.
</p>
</div>

<!-- <div style="margin-top: 2rem;">
<p style="color: #111; font-size: 0.875rem; font-weight: 500;">Thank you for joining Boilerplate</p>
</div> -->

<div style="margin-top: 2rem">
<p>Regards,</p>
<p>Boilerplate</p>
</div>
</td>
</tr>
</table>
{% endblock %}
31 changes: 21 additions & 10 deletions api/v1/routes/newsletter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@


@news_sub.post("")
async def sub_newsletter(request: EmailSchema,
db: Annotated[Session, Depends(get_db)],
background_tasks: BackgroundTasks):
async def sub_newsletter(
request: EmailSchema,
db: Annotated[Session, Depends(get_db)],
background_tasks: BackgroundTasks,
):
"""
Newsletter subscription endpoint
"""
Expand All @@ -35,17 +37,15 @@ async def sub_newsletter(request: EmailSchema,
# Save user to the database
NewsletterService.create(db, request)

link = 'https://anchor-python.teams.hng.tech/'
link = "https://anchor-python.teams.hng.tech/"

# Send email in the background
background_tasks.add_task(
send_email,
recipient=request.email,
template_name='newsletter-subscription.html',
subject='Thank You for Subscribing to HNG Boilerplate Newsletters',
context={
'link': link
}
template_name="newsletter-subscription.html",
subject="Thank You for Subscribing to HNG Boilerplate Newsletters",
context={"link": link},
)

return success_response(
Expand Down Expand Up @@ -142,11 +142,22 @@ def get_all_newsletters(


@newsletter.post("/unsubscribe")
async def unsubscribe_newsletter(request: EmailSchema, db: Session = Depends(get_db)):
async def unsubscribe_newsletter(
background_tasks: BackgroundTasks,
request: EmailSchema,
db: Session = Depends(get_db),
):
"""
Newsletter unsubscription endpoint
"""
NewsletterService.unsubscribe(db, request)
background_tasks.add_task(
send_email,
recipient=request.email,
template_name="unsubscribe.html",
subject="Unsubscription from HNG Boilerplate Newsletter",
context={},
)
return success_response(
message="Unsubscribed successfully.",
status_code=status.HTTP_200_OK,
Expand Down
24 changes: 2 additions & 22 deletions tests/v1/newsletter/test_newsletter_unsubscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
from main import app



@pytest.fixture
def db_session_mock():
db_session = MagicMock(spec=Session)
return db_session


@pytest.fixture
def client(db_session_mock):
app.dependency_overrides[get_db] = lambda: db_session_mock
Expand All @@ -26,24 +26,6 @@ def client(db_session_mock):
app.dependency_overrides = {}


@patch("api.v1.services.newsletter.NewsletterService.unsubscribe")
def test_newsletter_subscribe(mock_unsubscribe, db_session_mock, client):
"""Tests the POST /api/v1/newsletter-subscription endpoint to ensure successful subscription with valid input."""

mock_unsubscribe.return_value = None

db_session_mock.add.return_value = None
db_session_mock.commit.return_value = None
db_session_mock.refresh.return_value = None

response = client.post('/api/v1/newsletters/unsubscribe', json={
"email": "[email protected]"
})

print('response', response.json())
assert response.status_code == 200


@patch("api.v1.services.newsletter.NewsletterService.unsubscribe")
def test_newsletter_subscribe_missing_fields(mock_unsubscribe, db_session_mock, client):
"""Tests the POST /api/v1/newsletter-subscription endpoint for missing required fields."""
Expand All @@ -54,7 +36,5 @@ def test_newsletter_subscribe_missing_fields(mock_unsubscribe, db_session_mock,
db_session_mock.commit.return_value = None
db_session_mock.refresh.return_value = None

response = client.post('/api/v1/newsletter-subscription', json={

})
response = client.post("/api/v1/newsletter-subscription", json={})
assert response.status_code == 422

0 comments on commit 50312de

Please sign in to comment.