def testLaunchAt(self): e1 = Event(slug="test-event1", host=Organization.objects.first()) e2 = Event( slug="test-event2", host=Organization.objects.first(), start=date.today() + timedelta(days=7), end=date.today() + timedelta(days=8), ) e3 = Event( slug="test-event3", host=Organization.objects.first(), start=date.today() + timedelta(days=-8), end=date.today() + timedelta(days=-7), ) e4 = Event( slug="test-event4", host=Organization.objects.first(), start=date.today() + timedelta(days=70), end=date.today() + timedelta(days=71), ) # case 1: no context event a1 = AskForWebsiteAction(trigger=Trigger(action="test-action", template=EmailTemplate()), ) self.assertEqual(a1.get_launch_at(), timedelta(days=-30)) # case 2: event with no start date a2 = AskForWebsiteAction( trigger=Trigger(action="test-action", template=EmailTemplate()), objects=dict(event=e1), ) self.assertEqual(a2.get_launch_at(), timedelta(days=-30)) # case 3: event with near start date a3 = AskForWebsiteAction( trigger=Trigger(action="test-action", template=EmailTemplate()), objects=dict(event=e2), ) self.assertEqual(a3.get_launch_at(), timedelta(hours=1)) # case 4: event with negative start date a4 = AskForWebsiteAction( trigger=Trigger(action="test-action", template=EmailTemplate()), objects=dict(event=e3), ) self.assertEqual(a4.get_launch_at(), timedelta(hours=1)) # case 5: event with start date in 10 weeks a5 = AskForWebsiteAction( trigger=Trigger(action="test-action", template=EmailTemplate()), objects=dict(event=e4), ) self.assertEqual(a5.get_launch_at(), timedelta(days=40, hours=1))
def testContext(self): """Make sure `get_additional_context` works correctly.""" a = AskForWebsiteAction( trigger=Trigger(action="test-action", template=EmailTemplate())) # method fails when obligatory objects are missing with self.assertRaises(KeyError): a.get_additional_context(dict()) # missing 'event' with self.assertRaises(AttributeError): # now both objects are present, but the method tries to execute # `refresh_from_db` on them a.get_additional_context(dict(event="dummy", task="dummy")) e = Event.objects.create( slug="test-event", host=Organization.objects.first(), start=date.today() + timedelta(days=7), end=date.today() + timedelta(days=8), country="GB", venue="Ministry of Magic", ) e.tags.set(Tag.objects.filter(name__in=["TTT", "SWC"])) p1 = Person.objects.create(personal="Harry", family="Potter", username="******", email="*****@*****.**") p2 = Person.objects.create( personal="Hermione", family="Granger", username="******", email="*****@*****.**", ) instructor = Role.objects.create(name="instructor") supporting = Role.objects.create(name="supporting-instructor") Task.objects.create(event=e, person=p1, role=instructor) Task.objects.create(event=e, person=p2, role=supporting) ctx = a.get_additional_context(objects=dict(event=e)) self.assertEqual( ctx, dict( workshop=e, workshop_main_type="SWC", dates=e.human_readable_date, workshop_host=Organization.objects.first(), regional_coordinator_email=["*****@*****.**"], instructors=[p1, p2], all_emails=["*****@*****.**", "*****@*****.**"], assignee="Regional Coordinator", tags=["SWC", "TTT"], ), )
def testRecipients(self): """Make sure AskForWebsiteAction correctly renders recipients. They should get overwritten by AskForWebsiteAction during email building.""" e = Event.objects.create( slug="test-event", host=Organization.objects.first(), start=date.today() + timedelta(days=7), end=date.today() + timedelta(days=8), country="GB", venue="Ministry of Magic", ) e.tags.set(Tag.objects.filter(name="LC")) p1 = Person.objects.create(personal="Harry", family="Potter", username="******", email="*****@*****.**") p2 = Person.objects.create( personal="Hermione", family="Granger", username="******", email="*****@*****.**", ) instructor = Role.objects.create(name="instructor") supporting = Role.objects.create(name="supporting-instructor") Task.objects.bulk_create([ Task(event=e, person=p1, role=instructor), Task(event=e, person=p2, role=supporting), ]) template = EmailTemplate.objects.create( slug="sample-template", subject="Welcome to {{ site.name }}", to_header="*****@*****.**", from_header="*****@*****.**", cc_header="*****@*****.**", bcc_header="*****@*****.**", reply_to_header="{{ reply_to }}", body_template="Sample text.", ) trigger = Trigger.objects.create( action="week-after-workshop-completion", template=template, ) a = AskForWebsiteAction( trigger=trigger, objects=dict(event=e), ) email = a._email() self.assertEqual(email.to, [p1.email, p2.email])
def test_event_slug(self): e = Event.objects.create( slug="test-event", host=Organization.objects.first(), start=date.today() + timedelta(days=7), end=date.today() + timedelta(days=8), country="GB", venue="Ministry of Magic", ) e.tags.set(Tag.objects.filter(name="LC")) p1 = Person.objects.create(personal="Harry", family="Potter", username="******", email="*****@*****.**") r = Role.objects.create(name="instructor") t = Task.objects.create(event=e, person=p1, role=r) a = AskForWebsiteAction( trigger=Trigger(action="test-action", template=EmailTemplate()), objects=dict(event=e, task=t), ) self.assertEqual(a.event_slug(), "test-event")
def test_all_recipients(self): e = Event.objects.create( slug="test-event", host=Organization.objects.first(), start=date.today() + timedelta(days=7), end=date.today() + timedelta(days=8), country="GB", venue="Ministry of Magic", ) e.tags.set(Tag.objects.filter(name="LC")) p1 = Person.objects.create(personal="Harry", family="Potter", username="******", email="*****@*****.**") p2 = Person.objects.create( personal="Hermione", family="Granger", username="******", email="*****@*****.**", ) instructor = Role.objects.create(name="instructor") supporting = Role.objects.create(name="supporting-instructor") Task.objects.bulk_create([ Task(event=e, person=p1, role=instructor), Task(event=e, person=p2, role=supporting), ]) a = AskForWebsiteAction( trigger=Trigger(action="test-action", template=EmailTemplate()), objects=dict(event=e), ) self.assertEqual( a.all_recipients(), "[email protected], [email protected]", )