2020-06-12 06:15:25 +00:00
|
|
|
from time import sleep
|
|
|
|
|
2020-06-03 06:43:29 +00:00
|
|
|
from django.core import mail
|
2020-06-15 05:30:08 +00:00
|
|
|
from django.test import TestCase, TransactionTestCase
|
2024-01-08 05:40:33 +00:00
|
|
|
from wagtail.rich_text import RichText
|
|
|
|
from wagtail.test.utils import WagtailTestUtils
|
2024-01-09 22:17:43 +00:00
|
|
|
from wagtail.test.utils.form_data import (nested_form_data, rich_text,
|
|
|
|
streamfield)
|
2020-05-29 01:56:25 +00:00
|
|
|
|
2020-08-18 01:30:15 +00:00
|
|
|
from birdsong.models import CampaignStatus
|
2020-08-18 04:03:32 +00:00
|
|
|
from tests.app.models import ExtendedContact, SaleCampaign
|
2020-06-03 06:43:29 +00:00
|
|
|
|
2020-05-29 01:56:25 +00:00
|
|
|
|
|
|
|
class TestCampaignAdmin(WagtailTestUtils, TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.campaign = SaleCampaign.objects.create(
|
2024-01-09 05:27:11 +00:00
|
|
|
name="Test campaign",
|
|
|
|
subject="The subject",
|
|
|
|
body=[("rich_text", RichText("<p>The body</p>"))],
|
2020-05-29 01:56:25 +00:00
|
|
|
)
|
|
|
|
self.login()
|
|
|
|
|
|
|
|
def post_data(self, overrides={}):
|
|
|
|
post_data = {
|
2024-01-09 05:27:11 +00:00
|
|
|
"name": "Created campaign",
|
|
|
|
"subject": "New subject",
|
|
|
|
"body": streamfield([("rich_text", rich_text("<p>Just some content</p>"))]),
|
2020-05-29 01:56:25 +00:00
|
|
|
}
|
|
|
|
post_data.update(overrides)
|
|
|
|
return nested_form_data(post_data)
|
|
|
|
|
|
|
|
def test_create(self):
|
|
|
|
response = self.client.post(
|
2024-01-09 05:27:11 +00:00
|
|
|
"/admin/app/salecampaign/create/", self.post_data(), follow=True
|
2020-05-29 01:56:25 +00:00
|
|
|
)
|
2024-03-21 20:16:23 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2020-06-15 05:47:52 +00:00
|
|
|
|
2020-05-29 01:56:25 +00:00
|
|
|
def test_edit(self):
|
|
|
|
response = self.client.post(
|
2024-01-09 05:27:11 +00:00
|
|
|
f"/admin/app/salecampaign/edit/{self.campaign.id}/",
|
|
|
|
self.post_data(overrides={"name": "A Different Name"}),
|
|
|
|
follow=True,
|
2020-05-29 01:56:25 +00:00
|
|
|
)
|
2024-03-21 20:16:23 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2024-01-09 05:27:11 +00:00
|
|
|
self.assertContains(response, "A Different Name")
|
2020-05-29 01:56:25 +00:00
|
|
|
|
|
|
|
def test_preview(self):
|
|
|
|
response = self.client.get(
|
2024-01-09 05:27:11 +00:00
|
|
|
f"/admin/app/salecampaign/preview/{self.campaign.id}/",
|
2020-05-29 01:56:25 +00:00
|
|
|
)
|
2024-03-21 20:16:23 +00:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2024-01-09 05:27:11 +00:00
|
|
|
self.assertContains(response, "<p>The body</p>")
|
2020-05-29 02:34:53 +00:00
|
|
|
|
|
|
|
def test_live_preview(self):
|
2020-06-03 06:16:14 +00:00
|
|
|
# TODO (post with ajax headers?)
|
|
|
|
pass
|
2020-05-29 01:56:25 +00:00
|
|
|
|
|
|
|
|
2020-06-15 05:04:48 +00:00
|
|
|
class TestSending(WagtailTestUtils, TransactionTestCase):
|
2020-05-29 02:34:53 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.campaign = SaleCampaign.objects.create(
|
2024-01-09 05:27:11 +00:00
|
|
|
name="Test campaign",
|
|
|
|
subject="The subject",
|
|
|
|
body=[("rich_text", RichText("<p>The body</p>"))],
|
2020-05-29 02:34:53 +00:00
|
|
|
)
|
2020-06-03 06:16:14 +00:00
|
|
|
for person in [
|
2024-01-09 05:27:11 +00:00
|
|
|
("Terry", "Testington", "North", "terry@tests.com"),
|
|
|
|
("Wag", "Tail", "South", "wag@tail.com"),
|
2020-06-03 06:16:14 +00:00
|
|
|
]:
|
|
|
|
ExtendedContact.objects.create(
|
|
|
|
first_name=person[0],
|
|
|
|
last_name=person[1],
|
|
|
|
location=person[2],
|
|
|
|
email=person[3],
|
|
|
|
)
|
2020-05-29 02:34:53 +00:00
|
|
|
self.login()
|
|
|
|
|
|
|
|
def test_send_test(self):
|
2020-06-03 06:16:14 +00:00
|
|
|
self.client.post(
|
2024-01-09 05:27:11 +00:00
|
|
|
f"/admin/app/salecampaign/send_test/{self.campaign.id}/",
|
2020-06-03 06:16:14 +00:00
|
|
|
{
|
2024-01-09 05:27:11 +00:00
|
|
|
"email": "have@email.com",
|
|
|
|
"first_name": "Find",
|
|
|
|
"last_name": "Me",
|
|
|
|
"location": "Moon",
|
|
|
|
},
|
2020-06-03 06:16:14 +00:00
|
|
|
)
|
2024-05-10 06:07:32 +00:00
|
|
|
sleep(3) # Allow time to send
|
2020-06-03 06:16:14 +00:00
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
2024-05-10 06:07:32 +00:00
|
|
|
self.assertTrue("Hi Find Me" in str(mail.outbox[0].message()))
|
|
|
|
self.campaign.refresh_from_db()
|
2020-08-18 01:30:15 +00:00
|
|
|
self.assertNotEqual(self.campaign.status, CampaignStatus.SENT)
|
2020-05-29 02:34:53 +00:00
|
|
|
|
|
|
|
def test_send(self):
|
2024-01-09 05:27:11 +00:00
|
|
|
self.client.get(f"/admin/app/salecampaign/send_campaign/{self.campaign.id}/")
|
2020-06-03 06:16:14 +00:00
|
|
|
|
2024-05-10 06:07:32 +00:00
|
|
|
sleep(3) # Allow time to send
|
2024-03-21 20:16:23 +00:00
|
|
|
self.assertEqual(len(mail.outbox), 2)
|
|
|
|
self.assertEqual(self.campaign.receipts.all().count(), 2)
|
2020-08-18 01:30:15 +00:00
|
|
|
# Get fresh from db (altered in a thread)
|
2024-05-10 06:07:32 +00:00
|
|
|
self.campaign.refresh_from_db()
|
|
|
|
self.assertEqual(self.campaign.status, CampaignStatus.SENT)
|