Spaces:
Running
Running
| """Unit tests for publish-help request schema and email delivery.""" | |
| from unittest.mock import MagicMock, patch | |
| import pytest | |
| from pydantic import ValidationError | |
| from app.schemas.admin import PublishHelpRequest | |
| class TestPublishHelpRequestSchema: | |
| """R-029: Publish-help endpoint uses validated Pydantic schema.""" | |
| def test_valid_minimal_request(self): | |
| req = PublishHelpRequest( | |
| book_title="My Book", | |
| platform_ids=["amazon", "kobo"], | |
| ) | |
| assert req.book_title == "My Book" | |
| assert req.platform_ids == ["amazon", "kobo"] | |
| assert req.message == "" | |
| def test_rejects_empty_platform_list(self): | |
| with pytest.raises(ValidationError): | |
| PublishHelpRequest(book_title="My Book", platform_ids=[]) | |
| def test_rejects_unknown_platform(self): | |
| with pytest.raises(ValidationError): | |
| PublishHelpRequest(book_title="My Book", platform_ids=["unknown_store"]) | |
| def test_rejects_invalid_book_id_uuid(self): | |
| with pytest.raises(ValidationError): | |
| PublishHelpRequest( | |
| book_title="My Book", | |
| book_id="not-a-uuid", | |
| platform_ids=["amazon"], | |
| ) | |
| def test_accepts_valid_book_id_uuid(self): | |
| req = PublishHelpRequest( | |
| book_title="My Book", | |
| book_id="550e8400-e29b-41d4-a716-446655440000", | |
| platform_ids=["amazon"], | |
| message="Need KDP help", | |
| source_url="https://amazon.com/dp/123", | |
| ) | |
| assert req.book_id == "550e8400-e29b-41d4-a716-446655440000" | |
| def test_rejects_oversized_message(self): | |
| with pytest.raises(ValidationError): | |
| PublishHelpRequest( | |
| book_title="My Book", | |
| platform_ids=["amazon"], | |
| message="x" * 2001, | |
| ) | |
| class TestPublishHelpEmail: | |
| """Email service sends publish-help to SUPPORT_EMAIL.""" | |
| def test_send_publish_help_request(self, mock_send): | |
| from app.services.email_service import EmailService | |
| with patch("app.services.email_service.cfg") as mock_cfg: | |
| mock_cfg.SUPPORT_EMAIL = "support@example.com" | |
| mock_cfg.EMAIL_FROM_ADDRESS = "noreply@example.com" | |
| mock_cfg.EMAIL_FROM_NAME = "AuthorBot" | |
| svc = EmailService() | |
| svc.send_publish_help_request( | |
| author_name="Jane Author", | |
| author_email="jane@example.com", | |
| book_title="Test Book", | |
| platform_names=["Amazon", "Kobo"], | |
| message="Please help", | |
| submitted_at="2026-07-04T12:00:00+00:00", | |
| book_id="550e8400-e29b-41d4-a716-446655440000", | |
| isbn="9781234567890", | |
| book_author="Jane Author", | |
| source_url="https://amazon.com/dp/123", | |
| ) | |
| mock_send.assert_called_once() | |
| assert mock_send.call_args[0][0] == "support@example.com" | |
| assert "Test Book" in mock_send.call_args[0][1] | |
| def test_send_publish_help_confirmation(self, mock_send): | |
| from app.services.email_service import EmailService | |
| svc = EmailService() | |
| svc.send_publish_help_confirmation( | |
| to="jane@example.com", | |
| name="Jane", | |
| book_title="Test Book", | |
| platform_names=["Amazon"], | |
| ) | |
| mock_send.assert_called_once() | |
| assert mock_send.call_args[0][0] == "jane@example.com" | |