Skip to content

Commit

Permalink
Merge pull request #950 from MikeSoft007/bugfix/waitlist_email
Browse files Browse the repository at this point in the history
Bugfix: email notifcation during waiylist signup
  • Loading branch information
johnson-oragui authored Aug 23, 2024
2 parents fd78c63 + 9023e94 commit 9071502
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 90 deletions.
59 changes: 48 additions & 11 deletions api/v1/routes/waitlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@
waitlist = APIRouter(prefix="/waitlist", tags=["Waitlist"])

def process_waitlist_signup(user: WaitlistAddUserSchema, db: Session):
"""
Process a waitlist signup request.
Args:
- user (WaitlistAddUserSchema): The user details to be added to the waitlist.
- db (Session): The database session.
Returns:
- db_user: The added user object.
Raises:
- HTTPException: If the full name is not provided or if the email is already registered.
"""
if not user.full_name:
logger.error("Full name is required")
raise HTTPException(
Expand Down Expand Up @@ -55,14 +68,32 @@ async def waitlist_signup(
user: WaitlistAddUserSchema,
db: Session = Depends(get_db)
):
"""
Add a user to the waitlist.
Args:
- user (WaitlistAddUserSchema): The user details to be added to the waitlist.
Returns:
- success_response: A success response with a message and status code.
Example:
```
curl -X POST \
http://localhost:8000/waitlist/ \
-H 'Content-Type: application/json' \
-d '{"email": "[email protected]", "full_name": "John Doe"}'
```
"""

db_user = process_waitlist_signup(user, db)
if db_user:
cta_link = 'https://anchor-python.teams.hng.tech/about-us'
# Send email in the background
background_tasks.add_task(
send_email,
recipient=user.email,
template_name='waitlist.html',
template_name='waitlists.html',
subject='Welcome to HNG Waitlist',
context={
'name': user.full_name,
Expand All @@ -82,19 +113,25 @@ def admin_add_user_to_waitlist(
db: Session = Depends(get_db),
):
"""
Manually adds a user to the waitlist.
This endpoint allows an admin to add a user to the waitlist.
Manually add a user to the waitlist as an admin.
Parameters:
- item: WaitlistAddUserSchema
The details of the user to be added to the waitlist.
- admin: User (Depends on get_super_admin)
The current admin making the request. This is a dependency that provides the current admin context.
Args:
- item (WaitlistAddUserSchema): The user details to be added to the waitlist.
- admin (User): The current admin making the request.
Returns:
- 201: User added successfully
- 400: Validation error
- 403: Forbidden
- success_response: A success response with a message and status code.
Raises:
- HTTPException: If the full name is not provided or if the email is already registered.
Example:
```
curl -X POST \
http://localhost:8000/waitlist/admin \
-H 'Content-Type: application/json' \
-d '{"email": "[email protected]", "full_name": "John Doe"}'
```
"""

try:
Expand Down
79 changes: 0 additions & 79 deletions tests/v1/auth/test_magic_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,82 +60,3 @@ def test_request_magic_link(mock_user_service, mock_db_session):
response = client.post(MAGIC_ENDPOINT, json={"email": "[email protected]"})
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.json().get("message") == "User not found"



# import pytest
# from fastapi.testclient import TestClient
# from unittest.mock import patch, MagicMock
# from main import app
# from api.v1.models.user import User
# from api.v1.services.user import user_service
# from uuid_extensions import uuid7
# from api.db.database import get_db
# from fastapi import status
# from datetime import datetime, timezone


# client = TestClient(app)
# MAGIC_ENDPOINT = '/api/v1/auth/magic-link'


# @pytest.fixture
# def mock_db_session():
# """Fixture to create a mock database session."""

# with patch("api.v1.services.user.get_db", autospec=True) as mock_get_db:
# mock_db = MagicMock()
# # mock_get_db.return_value.__enter__.return_value = mock_db
# app.dependency_overrides[get_db] = lambda: mock_db
# yield mock_db
# app.dependency_overrides = {}


# @pytest.fixture
# def mock_user_service():
# """Fixture to create a mock user service."""

# with patch("api.v1.services.user.user_service", autospec=True) as mock_service:
# yield mock_service

# @pytest.mark.usefixtures("mock_db_session", "mock_user_service")
# def test_request_magic_link(mock_user_service, mock_db_session):
# """Test for requesting magic link"""

# # Create a mock user
# mock_user = User(
# id=str(uuid7()),
# email="[email protected]",
# password=user_service.hash_password("Testpassword@123"),
# first_name='Test',
# last_name='User',
# is_active=False,
# is_superadmin=False,
# created_at=datetime.now(timezone.utc),
# updated_at=datetime.now(timezone.utc)
# )
# mock_db_session.query.return_value.filter.return_value.first.return_value = mock_user

# with patch("api.utils.send_mail.smtplib.SMTP_SSL") as mock_smtp:
# # Configure the mock SMTP server
# mock_smtp_instance = MagicMock()
# mock_smtp.return_value = mock_smtp_instance


# # Test for requesting magic link for an existing user
# magic_login = client.post(MAGIC_ENDPOINT, json={
# "email": mock_user.email
# })
# assert magic_login.status_code == status.HTTP_200_OK
# response = magic_login.json()
# #assert response.get("status_code") == status.HTTP_200_OK # check for the right response before proceeding
# assert response.get("message") == f"Magic link sent to {mock_user.email}"

# # Test for requesting magic link for a non-existing user
# mock_db_session.query.return_value.filter.return_value.first.return_value = None
# magic_login = client.post(MAGIC_ENDPOINT, json={
# "email": "[email protected]"
# })
# response = magic_login.json()
# assert response.get("status_code") == status.HTTP_404_NOT_FOUND # check for the right response before proceeding
# assert response.get("message") == "User not found"

0 comments on commit 9071502

Please sign in to comment.