-
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
11 changed files
with
225 additions
and
8 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 |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
CAMPAIGN_ID = "test-campaign-id" | ||
NEW_CAMPAIGN_ID = "test-new-campaign-id" | ||
API_ERROR_TEXT = "something failed" | ||
EMAIL = "[email protected]" | ||
|
||
|
||
class MockMailchimpCampaignBackend(MailchimpCampaignBackend): | ||
|
@@ -349,3 +350,18 @@ def get_campaign_request_body(self, *, recipients, subject): | |
"type": "regular", | ||
} | ||
assert backend.client.campaigns.create.mock_calls == [call(expected_body)] | ||
|
||
|
||
def test_send_test_email(backend: MockMailchimpCampaignBackend): | ||
backend.send_test_email(campaign_id=CAMPAIGN_ID, email=EMAIL) | ||
assert backend.client.campaigns.send_test_email.mock_calls == [ | ||
call(CAMPAIGN_ID, {"test_emails": [EMAIL], "send_type": "html"}) | ||
] | ||
|
||
|
||
def test_send_test_email_failure(backend: MockMailchimpCampaignBackend): | ||
backend.client.campaigns.send_test_email.side_effect = ApiClientError("", 400) | ||
with pytest.raises(CampaignBackendError) as error: | ||
backend.send_test_email(campaign_id=CAMPAIGN_ID, email=EMAIL) | ||
|
||
assert error.match("Error while sending test email") |
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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
|
||
CAMPAIGN_ID = "test-campaign-id" | ||
CAMPAIGN_URL = "http://campaign.example.com" | ||
EMAIL = "[email protected]" | ||
|
||
|
||
@pytest.fixture | ||
|
@@ -34,7 +35,7 @@ def test_save_campaign( | |
data = { | ||
"title": page.title, | ||
"slug": page.slug, | ||
"newsletter_action": "save_campaign", | ||
"newsletter-action": "save_campaign", | ||
} | ||
response = admin_client.post(url, data, follow=True) | ||
|
||
|
@@ -63,11 +64,66 @@ def test_save_campaign_failed_to_save( | |
data = { | ||
"title": page.title, | ||
"slug": page.slug, | ||
"newsletter_action": "save_campaign", | ||
"newsletter-action": "save_campaign", | ||
} | ||
response = admin_client.post(url, data, follow=True) | ||
|
||
assert "Failed to save newsletter campaign" in response.content.decode() | ||
|
||
page.refresh_from_db() | ||
assert page.newsletter_campaign == "" | ||
|
||
|
||
def test_send_test_email( | ||
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_test_email = Mock() | ||
|
||
url = reverse("wagtailadmin_pages:edit", kwargs={"page_id": page.pk}) | ||
data = { | ||
"title": page.title, | ||
"slug": page.slug, | ||
"newsletter-action": "send_test_email", | ||
"newsletter-test-email": EMAIL, | ||
} | ||
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"Test message sent to '{EMAIL}'" in html | ||
|
||
assert memory_backend.save_campaign.mock_calls == [ | ||
call(campaign_id="", recipients=None, subject=page.title, html=ANY) | ||
] | ||
assert memory_backend.send_test_email.mock_calls == [ | ||
call(campaign_id=CAMPAIGN_ID, email=EMAIL) | ||
] | ||
|
||
|
||
def test_send_test_email_invalid_email( | ||
page: ArticlePage, admin_client: Client, memory_backend: MemoryCampaignBackend | ||
): | ||
memory_backend.save_campaign = Mock(return_value=CAMPAIGN_ID) | ||
memory_backend.send_test_email = Mock() | ||
|
||
url = reverse("wagtailadmin_pages:edit", kwargs={"page_id": page.pk}) | ||
data = { | ||
"title": page.title, | ||
"slug": page.slug, | ||
"newsletter-action": "send_test_email", | ||
"newsletter-test-email": "invalid-address", | ||
} | ||
response = admin_client.post(url, data, follow=True) | ||
|
||
html = response.content.decode() | ||
assert f"Page '{page.title}' has been updated" in html | ||
assert "'email': Enter a valid email address." in html | ||
|
||
assert memory_backend.save_campaign.mock_calls == [] | ||
assert memory_backend.send_test_email.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
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,8 @@ | ||
from django import forms | ||
|
||
|
||
class SendTestEmailForm(forms.Form): | ||
email = forms.EmailField( | ||
label="Email address", | ||
help_text="Send a test email to this address", | ||
) |
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
38 changes: 38 additions & 0 deletions
38
wagtail_newsletter/static/wagtail_newsletter/js/wagtail_newsletter.js
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,38 @@ | ||
window.wagtail.app.register("wn-panel", | ||
class extends window.StimulusModule.Controller { | ||
static targets = [ | ||
"testButton", | ||
"testAddress", | ||
"testSubmit", | ||
] | ||
|
||
get testButtonProgress() { | ||
return this.application.getControllerForElementAndIdentifier( | ||
this.testButtonTarget, "w-progress" | ||
); | ||
} | ||
|
||
test(event) { | ||
this.testAddressTarget.value = event.detail.address; | ||
this.testButtonProgress.activate(); | ||
this.testSubmitTarget.click(); | ||
} | ||
} | ||
); | ||
|
||
|
||
window.wagtail.app.register("wn-test", | ||
class extends window.StimulusModule.Controller { | ||
get dialog() { | ||
return this.application.getControllerForElementAndIdentifier( | ||
this.element.closest("[data-controller=w-dialog]"), "w-dialog" | ||
); | ||
} | ||
|
||
submit() { | ||
const address = this.element.querySelector("input[name=email]").value; | ||
this.dispatch("submit", { detail: { address } }); | ||
this.dialog.hide(); | ||
} | ||
} | ||
); |
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