-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
1,003 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
docs/_static/screenshot.png filter=lfs diff=lfs merge=lfs -text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import pytest | ||
|
||
from wagtail.models import Site | ||
|
||
from wagtail_newsletter.test.models import ArticlePage | ||
|
||
|
||
@pytest.fixture | ||
def page(): | ||
page = ArticlePage(title="Test Article") | ||
Site.objects.get().root_page.add_child(instance=page) | ||
return page |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
|
||
from django.test import Client | ||
from django.urls import reverse | ||
|
||
from tests.conftest import MemoryCampaignBackend | ||
from wagtail_newsletter.test.models import ArticlePage | ||
|
||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
CAMPAIGN_ID = "test-campaign-id" | ||
CAMPAIGN_URL = "http://campaign.example.com" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"action,label", | ||
[ | ||
("save_campaign", "Newsletter: Save campaign"), | ||
("send_test_email", "Newsletter: Send test email"), | ||
("send_campaign", "Newsletter: Send campaign"), | ||
("schedule_campaign", "Newsletter: Schedule campaign"), | ||
("unschedule_campaign", "Newsletter: Unschedule campaign"), | ||
], | ||
) | ||
def test_action_restricted( | ||
page: ArticlePage, | ||
admin_client: Client, | ||
memory_backend: MemoryCampaignBackend, | ||
monkeypatch: pytest.MonkeyPatch, | ||
action: str, | ||
label: str, | ||
): | ||
memory_backend.save_campaign = Mock(return_value=CAMPAIGN_ID) | ||
memory_backend.get_campaign = Mock(return_value=Mock(url=CAMPAIGN_URL)) | ||
memory_backend.send_test_email = Mock() | ||
memory_backend.send_campaign = Mock() | ||
memory_backend.schedule_campaign = Mock() | ||
memory_backend.unschedule_campaign = Mock() | ||
|
||
monkeypatch.setattr( | ||
ArticlePage, "has_newsletter_permission", Mock(return_value=False) | ||
) | ||
|
||
if action == "unschedule_campaign": | ||
response = admin_client.post( | ||
reverse("wagtail_newsletter:unschedule", kwargs={"page_id": page.pk}), | ||
follow=True, | ||
) | ||
|
||
else: | ||
url = reverse("wagtailadmin_pages:edit", kwargs={"page_id": page.pk}) | ||
data = { | ||
"title": page.title, | ||
"slug": page.slug, | ||
"newsletter-action": action, | ||
} | ||
response = admin_client.post(url, data, follow=True) | ||
|
||
html = response.content.decode() | ||
|
||
if action != "unschedule_campaign": | ||
assert f"Page '{page.title}' has been updated" in html | ||
|
||
assert f"You do not have permission to perform the action {label}" in html | ||
|
||
assert memory_backend.save_campaign.mock_calls == [] | ||
assert memory_backend.send_test_email.mock_calls == [] | ||
assert memory_backend.send_campaign.mock_calls == [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from unittest.mock import ANY, Mock, call | ||
|
||
import pytest | ||
|
||
from django.test import Client | ||
from django.urls import reverse | ||
|
||
from tests.conftest import MemoryCampaignBackend | ||
from wagtail_newsletter.campaign_backends import CampaignBackendError | ||
from wagtail_newsletter.test.models import ArticlePage | ||
|
||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
CAMPAIGN_ID = "test-campaign-id" | ||
CAMPAIGN_URL = "http://campaign.example.com" | ||
EMAIL = "[email protected]" | ||
|
||
|
||
def test_save_campaign( | ||
page: ArticlePage, admin_client: Client, memory_backend: MemoryCampaignBackend | ||
): | ||
memory_backend.save_campaign = Mock(return_value=CAMPAIGN_ID) | ||
memory_backend.get_campaign = Mock(return_value=Mock(url=CAMPAIGN_URL)) | ||
|
||
url = reverse("wagtailadmin_pages:edit", kwargs={"page_id": page.pk}) | ||
data = { | ||
"title": page.title, | ||
"slug": page.slug, | ||
"newsletter-action": "save_campaign", | ||
} | ||
response = admin_client.post(url, data, follow=True) | ||
|
||
html = response.content.decode() | ||
assert f"Page '{page.title}' has been updated" in html | ||
assert ( | ||
f"Newsletter campaign '{page.title}' has been saved to Testing" | ||
in html | ||
) | ||
assert f'href="{CAMPAIGN_URL}"' in html | ||
|
||
assert memory_backend.save_campaign.mock_calls == [ | ||
call(campaign_id="", recipients=None, subject=page.title, html=ANY) | ||
] | ||
|
||
page.refresh_from_db() | ||
assert page.newsletter_campaign == CAMPAIGN_ID | ||
|
||
|
||
def test_save_campaign_failed_to_save( | ||
page: ArticlePage, admin_client: Client, memory_backend: MemoryCampaignBackend | ||
): | ||
memory_backend.save_campaign = Mock(side_effect=CampaignBackendError("Mock error")) | ||
|
||
url = reverse("wagtailadmin_pages:edit", kwargs={"page_id": page.pk}) | ||
data = { | ||
"title": page.title, | ||
"slug": page.slug, | ||
"newsletter-action": "save_campaign", | ||
} | ||
response = admin_client.post(url, data, follow=True) | ||
|
||
assert "Mock error" in response.content.decode() | ||
|
||
page.refresh_from_db() | ||
assert page.newsletter_campaign == "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
from datetime import date, datetime, time, timedelta, timezone | ||
from unittest.mock import ANY, Mock, call | ||
|
||
import pytest | ||
|
||
from django.test import Client | ||
from django.urls import reverse | ||
from django.utils.formats import localize | ||
|
||
from tests.conftest import MemoryCampaignBackend | ||
from wagtail_newsletter.campaign_backends import CampaignBackendError | ||
from wagtail_newsletter.test.models import ArticlePage | ||
|
||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
CAMPAIGN_ID = "test-campaign-id" | ||
CAMPAIGN_URL = "http://campaign.example.com" | ||
EMAIL = "[email protected]" | ||
|
||
|
||
def get_schedule_time(delta: timedelta): | ||
return datetime.combine(date.today() + delta, time(12)) | ||
|
||
|
||
def test_schedule_campaign( | ||
page: ArticlePage, admin_client: Client, memory_backend: MemoryCampaignBackend | ||
): | ||
memory_backend.save_campaign = Mock(return_value=CAMPAIGN_ID) | ||
memory_backend.get_campaign = Mock(return_value=Mock(url=CAMPAIGN_URL)) | ||
memory_backend.schedule_campaign = Mock() | ||
|
||
url = reverse("wagtailadmin_pages:edit", kwargs={"page_id": page.pk}) | ||
schedule_time = get_schedule_time(timedelta(days=1)) | ||
data = { | ||
"title": page.title, | ||
"slug": page.slug, | ||
"newsletter-action": "schedule_campaign", | ||
"newsletter-schedule-schedule_time": schedule_time.isoformat(), | ||
} | ||
response = admin_client.post(url, data, follow=True) | ||
|
||
html = response.content.decode() | ||
assert f"Page '{page.title}' has been updated" in html | ||
assert ( | ||
f"Newsletter campaign '{page.title}' has been saved to Testing" | ||
in html | ||
) | ||
assert f"Campaign scheduled to send at {localize(schedule_time)}" in html | ||
|
||
assert memory_backend.save_campaign.mock_calls == [ | ||
call(campaign_id="", recipients=None, subject=page.title, html=ANY) | ||
] | ||
assert memory_backend.schedule_campaign.mock_calls == [ | ||
call( | ||
campaign_id=CAMPAIGN_ID, | ||
schedule_time=schedule_time.replace(tzinfo=timezone.utc), | ||
) | ||
] | ||
|
||
|
||
def test_schedule_campaign_failed_to_schedule( | ||
page: ArticlePage, admin_client: Client, memory_backend: MemoryCampaignBackend | ||
): | ||
memory_backend.save_campaign = Mock(return_value=CAMPAIGN_ID) | ||
memory_backend.get_campaign = Mock(return_value=Mock(url=CAMPAIGN_URL)) | ||
memory_backend.schedule_campaign = Mock( | ||
side_effect=CampaignBackendError("Mock error") | ||
) | ||
|
||
url = reverse("wagtailadmin_pages:edit", kwargs={"page_id": page.pk}) | ||
schedule_time = get_schedule_time(timedelta(days=1)) | ||
data = { | ||
"title": page.title, | ||
"slug": page.slug, | ||
"newsletter-action": "schedule_campaign", | ||
"newsletter-schedule-schedule_time": schedule_time.isoformat(), | ||
} | ||
response = admin_client.post(url, data, follow=True) | ||
|
||
assert "Mock error" in response.content.decode() | ||
|
||
|
||
def test_schedule_in_the_past( | ||
page: ArticlePage, admin_client: Client, memory_backend: MemoryCampaignBackend | ||
): | ||
memory_backend.save_campaign = Mock(return_value=CAMPAIGN_ID) | ||
memory_backend.get_campaign = Mock(return_value=Mock(url=CAMPAIGN_URL)) | ||
|
||
url = reverse("wagtailadmin_pages:edit", kwargs={"page_id": page.pk}) | ||
schedule_time = get_schedule_time(timedelta(days=-1)) | ||
data = { | ||
"title": page.title, | ||
"slug": page.slug, | ||
"newsletter-action": "schedule_campaign", | ||
"newsletter-schedule-schedule_time": schedule_time.isoformat(), | ||
} | ||
response = admin_client.post(url, data, follow=True) | ||
|
||
assert "Schedule time: Date must be in the future." in response.content.decode() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from unittest.mock import ANY, Mock, call | ||
|
||
import pytest | ||
|
||
from django.test import Client | ||
from django.urls import reverse | ||
|
||
from tests.conftest import MemoryCampaignBackend | ||
from wagtail_newsletter.campaign_backends import CampaignBackendError | ||
from wagtail_newsletter.test.models import ArticlePage | ||
|
||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
CAMPAIGN_ID = "test-campaign-id" | ||
CAMPAIGN_URL = "http://campaign.example.com" | ||
EMAIL = "[email protected]" | ||
|
||
|
||
def test_send_campaign( | ||
page: ArticlePage, admin_client: Client, memory_backend: MemoryCampaignBackend | ||
): | ||
memory_backend.save_campaign = Mock(return_value=CAMPAIGN_ID) | ||
memory_backend.get_campaign = Mock(return_value=Mock(url=CAMPAIGN_URL)) | ||
memory_backend.send_campaign = Mock() | ||
|
||
url = reverse("wagtailadmin_pages:edit", kwargs={"page_id": page.pk}) | ||
data = { | ||
"title": page.title, | ||
"slug": page.slug, | ||
"newsletter-action": "send_campaign", | ||
} | ||
response = admin_client.post(url, data, follow=True) | ||
|
||
html = response.content.decode() | ||
assert f"Page '{page.title}' has been updated" in html | ||
assert ( | ||
f"Newsletter campaign '{page.title}' has been saved to Testing" | ||
in html | ||
) | ||
assert "Newsletter campaign is now sending" in html | ||
|
||
assert memory_backend.save_campaign.mock_calls == [ | ||
call(campaign_id="", recipients=None, subject=page.title, html=ANY) | ||
] | ||
assert memory_backend.send_campaign.mock_calls == [call(CAMPAIGN_ID)] | ||
|
||
|
||
def test_send_campaign_failed_to_send( | ||
page: ArticlePage, admin_client: Client, memory_backend: MemoryCampaignBackend | ||
): | ||
memory_backend.save_campaign = Mock(return_value=CAMPAIGN_ID) | ||
memory_backend.get_campaign = Mock(return_value=Mock(url=CAMPAIGN_URL)) | ||
memory_backend.send_campaign = Mock(side_effect=CampaignBackendError("Mock error")) | ||
|
||
url = reverse("wagtailadmin_pages:edit", kwargs={"page_id": page.pk}) | ||
data = { | ||
"title": page.title, | ||
"slug": page.slug, | ||
"newsletter-action": "send_campaign", | ||
} | ||
response = admin_client.post(url, data, follow=True) | ||
|
||
assert "Mock error" in response.content.decode() |
Oops, something went wrong.