v1
This commit is contained in:
118
tests/test_newsletter_api.py
Normal file
118
tests/test_newsletter_api.py
Normal file
@@ -0,0 +1,118 @@
|
||||
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("/api/newsletter/manage")
|
||||
assert resp.status_code == 200
|
||||
assert b"Newsletter Subscription Management" in resp.data
|
||||
|
||||
|
||||
def test_newsletter_manage_subscribe(client):
|
||||
resp = client.post("/api/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("/api/newsletter/manage",
|
||||
data={"email": "manage@example.com", "action": "subscribe"})
|
||||
# Unsubscribe
|
||||
resp = client.post("/api/newsletter/manage",
|
||||
data={"email": "manage@example.com", "action": "unsubscribe"})
|
||||
assert resp.status_code == 200
|
||||
assert b"Successfully unsubscribed" in resp.data
|
||||
|
||||
|
||||
def test_newsletter_manage_update(client):
|
||||
# Subscribe first
|
||||
client.post("/api/newsletter/manage",
|
||||
data={"email": "old@example.com", "action": "subscribe"})
|
||||
# Update
|
||||
resp = client.post("/api/newsletter/manage", data={
|
||||
"old_email": "old@example.com", "new_email": "updated@example.com", "action": "update"})
|
||||
assert resp.status_code == 200
|
||||
# Check that some success message is displayed
|
||||
assert b"success" in resp.data.lower() or b"updated" in resp.data.lower()
|
||||
|
||||
|
||||
def test_newsletter_manage_invalid_email(client):
|
||||
resp = client.post("/api/newsletter/manage",
|
||||
data={"email": "invalid-email", "action": "subscribe"})
|
||||
assert resp.status_code == 200
|
||||
assert b"Please enter a valid email address" in resp.data
|
||||
Reference in New Issue
Block a user