Files
contact.allucanget.biz/tests/test_newsletter_api.py
zwitschi 56840ac313
All checks were successful
CI / test (3.11) (push) Successful in 9m26s
CI / build-image (push) Successful in 49s
feat(newsletter): Add subscription confirmation email functionality
- Implemented `send_subscription_confirmation` function to send a confirmation email upon subscription.
- Added a default HTML template for the confirmation email in settings.
- Updated the newsletter management page to handle subscription and unsubscription actions.
- Created new templates for embedding contact and newsletter forms.
- Added styles for the new templates and unified CSS styles across the application.
- Updated tests to reflect changes in the newsletter management API endpoints.
2025-10-30 12:38:26 +01:00

119 lines
4.0 KiB
Python

import sqlite3
import importlib
import pytest
server_app_module = importlib.import_module("server.app")
# Expose app and init_db from the imported module
app = server_app_module.app
init_db = server_app_module.init_db
@pytest.fixture
def client():
with app.test_client() as client:
yield client
def test_newsletter_subscription_creates_record(client):
resp = client.post("/api/newsletter", json={"email": "test@example.com"})
assert resp.status_code == 201
body = resp.get_json()
assert body["status"] == "ok"
# Note: The API doesn't return an ID in the response
def test_newsletter_duplicate_subscription_returns_conflict(client):
# First subscription
client.post("/api/newsletter", json={"email": "test@example.com"})
# Duplicate subscription
resp = client.post("/api/newsletter", json={"email": "test@example.com"})
assert resp.status_code == 409
body = resp.get_json()
assert body["status"] == "error"
assert "already subscribed" in body["message"].lower()
def test_newsletter_unsubscribe(client):
# Subscribe first
client.post("/api/newsletter", json={"email": "test@example.com"})
# Unsubscribe
resp = client.delete("/api/newsletter", json={"email": "test@example.com"})
assert resp.status_code == 200
body = resp.get_json()
assert body["status"] == "ok"
assert "unsubscribed" in body["message"].lower()
def test_newsletter_unsubscribe_not_subscribed(client):
resp = client.delete("/api/newsletter", json={"email": "test@example.com"})
assert resp.status_code == 404
body = resp.get_json()
assert body["status"] == "error"
assert "not subscribed" in body["message"].lower()
def test_newsletter_update_email(client):
# Subscribe first
client.post("/api/newsletter", json={"email": "old@example.com"})
# Update email
resp = client.put(
"/api/newsletter", json={"old_email": "old@example.com", "new_email": "new@example.com"})
assert resp.status_code == 200
body = resp.get_json()
assert body["status"] == "ok"
assert "updated" in body["message"].lower()
def test_newsletter_update_email_not_found(client):
resp = client.put(
"/api/newsletter", json={"old_email": "nonexistent@example.com", "new_email": "new@example.com"})
assert resp.status_code == 404
body = resp.get_json()
assert body["status"] == "error"
assert "not found" in body["message"].lower()
def test_newsletter_manage_page_get(client):
resp = client.get("/newsletter/manage")
assert resp.status_code == 200
assert b"Newsletter Subscription Management" in resp.data
def test_newsletter_manage_subscribe(client):
resp = client.post("/newsletter/manage",
data={"email": "manage@example.com", "action": "subscribe"})
assert resp.status_code == 200
assert b"Successfully subscribed" in resp.data
def test_newsletter_manage_unsubscribe(client):
# Subscribe first
client.post("/newsletter/manage",
data={"email": "manage@example.com", "action": "subscribe"})
# Unsubscribe
resp = client.post("/newsletter/manage",
data={"email": "manage@example.com", "action": "unsubscribe"})
assert resp.status_code == 200
assert b"Unsubscription Confirmed" in resp.data
def test_newsletter_manage_update(client):
# Subscribe first
client.post("/newsletter/manage",
data={"email": "old@example.com", "action": "subscribe"})
# Update
resp = client.post("/newsletter/manage", data={
"old_email": "old@example.com", "email": "updated@example.com", "action": "update"})
assert resp.status_code == 200
# Check that some success message is displayed
assert b"updated successfully" in resp.data.lower()
def test_newsletter_manage_invalid_email(client):
resp = client.post("/newsletter/manage",
data={"email": "invalid-email", "action": "subscribe"})
assert resp.status_code == 200
assert b"Please enter a valid email address" in resp.data