Пример #1
0
    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 = RecruitHelpersAction(trigger=Trigger(action="test-action",
                                                  template=EmailTemplate()), )
        self.assertEqual(a1.get_launch_at(), timedelta(days=-21))

        # case 2: event with no start date
        a2 = RecruitHelpersAction(
            trigger=Trigger(action="test-action", template=EmailTemplate()),
            objects=dict(event=e1),
        )
        self.assertEqual(a2.get_launch_at(), timedelta(days=-21))

        # case 3: event with near start date
        a3 = RecruitHelpersAction(
            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 = RecruitHelpersAction(
            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 = RecruitHelpersAction(
            trigger=Trigger(action="test-action", template=EmailTemplate()),
            objects=dict(event=e4),
        )
        self.assertEqual(a5.get_launch_at(), timedelta(days=49, hours=1))
Пример #2
0
    def test_reading_file_from_disk(self):
        """Non-default database engine backend shouldn't allow to read from
        disk."""
        # we're using a different backend than what's used for normal templates
        # because we don't want to allow users to render what they shouldn't
        template = "{% include 'base.html' %}"
        data = dict()
        with self.assertRaises(TemplateDoesNotExist):
            # with this test, we're confirming that `db_backend`, a default for
            # EmailTemplate, is unable to reach `base.html` file
            EmailTemplate.render_template(template, data)

        # this must work, because we're using Django-main template engine
        EmailTemplate.render_template(template, data, default_engine="django")
Пример #3
0
    def testContext(self):
        """Make sure `get_additional_context` works correctly."""
        a = SelfOrganisedRequestAction(
            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' and 'request'
        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", request="dummy"))

        # totally fake Event and SelfOrganisedSubmission
        e = Event.objects.create(
            slug="test-event",
            host=Organization.objects.first(),
            administrator=Organization.objects.get(domain="self-organized"),
            start=date.today() + timedelta(days=7),
            end=date.today() + timedelta(days=8),
            country="GB",
        )
        e.tags.set(
            Tag.objects.filter(name__in=["LC", "Circuits", "automated-email"]))
        r = SelfOrganisedSubmission.objects.create(
            state="p",
            personal="Harry",
            family="Potter",
            email="*****@*****.**",
            institution_other_name="Hogwarts",
            workshop_url="",
            workshop_format="",
            workshop_format_other="",
            workshop_types_other_explain="",
            language=Language.objects.get(name="English"),
            event=e,
            additional_contact=TAG_SEPARATOR.join(
                ["*****@*****.**", "*****@*****.**"]),
        )
        r.workshop_types.set(Curriculum.objects.filter(carpentry="LC"))

        ctx = a.get_additional_context(objects=dict(event=e, request=r))
        self.maxDiff = None
        self.assertEqual(
            ctx,
            dict(
                workshop=e,
                request=r,
                workshop_main_type="LC",
                dates=e.human_readable_date,
                host=Organization.objects.first(),
                regional_coordinator_email=["*****@*****.**"],
                short_notice=True,
                all_emails=[
                    "*****@*****.**", "*****@*****.**", "*****@*****.**"
                ],
                assignee="Regional Coordinator",
                tags=["automated-email", "Circuits", "LC"],
            ),
        )
    def test_all_recipients(self):
        # totally fake Event and SelfOrganisedSubmission
        e = Event.objects.create(
            slug="test-event",
            host=Organization.objects.first(),
            administrator=Organization.objects.get(domain="self-organized"),
            start=date.today() + timedelta(days=7),
            end=date.today() + timedelta(days=8),
            country="GB",
        )
        e.tags.set(Tag.objects.filter(name__in=["LC", "Circuits", "automated-email"]))
        r = SelfOrganisedSubmission.objects.create(
            state="p",
            personal="Harry",
            family="Potter",
            email="*****@*****.**",
            institution_other_name="Hogwarts",
            workshop_url="",
            workshop_format="",
            workshop_format_other="",
            workshop_types_other_explain="",
            language=Language.objects.get(name="English"),
            event=e,
            additional_contact=TAG_SEPARATOR.join(["*****@*****.**", "*****@*****.**"]),
        )
        r.workshop_types.set(Curriculum.objects.filter(carpentry="LC"))

        a = SelfOrganisedRequestAction(
            trigger=Trigger(action="test-action", template=EmailTemplate()),
            objects=dict(event=e, request=r),
        )

        self.assertEqual(
            a.all_recipients(), "[email protected], [email protected], [email protected]"
        )
Пример #5
0
    def test_all_recipients(self):
        # totally fake Event
        e = Event.objects.create(
            slug="test-event",
            host=Organization.objects.first(),
            administrator=Organization.objects.get(domain="carpentries.org"),
            start=date.today() + timedelta(days=7),
            end=date.today() + timedelta(days=8),
            contact=TAG_SEPARATOR.join(["*****@*****.**", "*****@*****.**"]),
            country="GB",
        )
        e.tags.set(Tag.objects.filter(name__in=["LC", "automated-email"]))
        # tasks
        Task.objects.create(person=self.person1, role=self.host, event=e)
        Task.objects.create(
            person=self.person2,
            role=self.instructor,
            event=e,
        )
        Task.objects.create(
            person=self.person3,
            role=self.instructor,
            event=e,
        )

        a = InstructorsHostIntroductionAction(
            trigger=Trigger(action="test-action", template=EmailTemplate()),
            objects=dict(event=e),
        )

        self.assertEqual(
            a.all_recipients(),
            "[email protected], [email protected], [email protected], [email protected], [email protected]",
        )
Пример #6
0
 def testLaunchAt(self):
     # the trigger and email template below are totally fake
     # and shouldn't pass validation
     a = NewInstructorAction(
         trigger=Trigger(action="test-action", template=EmailTemplate())
     )
     self.assertEqual(a.get_launch_at(), timedelta(hours=1))
Пример #7
0
    def testLaunchTimestamp(self):
        # the trigger and email template below are totally fake
        # and shouldn't pass validation
        a = BaseAction(trigger=Trigger(action="test-action", template=EmailTemplate()))
        a.launch_at = timedelta(days=10)

        self.assertEqual(a.get_launch_at(), timedelta(days=10))
    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",
            address="Underground",
            latitude=20.0,
            longitude=20.0,
            url="https://test-event.example.com",
        )
        e.tags.set(Tag.objects.filter(name="SWC"))
        p = Person.objects.create(personal="Harry",
                                  family="Potter",
                                  email="*****@*****.**")
        r = Role.objects.create(name="supporting-instructor")
        t = Task.objects.create(event=e, person=p, role=r)

        a = NewSupportingInstructorAction(
            trigger=Trigger(action="test-action", template=EmailTemplate()),
            objects=dict(event=e, task=t),
        )

        self.assertEqual(a.all_recipients(), "*****@*****.**")
Пример #9
0
    def test_all_recipients(self):
        wr = WorkshopRequest(email="*****@*****.**")
        e = Event.objects.create(
            slug="test-event1",
            host=Organization.objects.first(),
            start=datetime(2020, 10, 30),
            end=datetime(2020, 11, 1),
            country="GB",
            venue="Ministry of Magic",
        )
        e.tags.set(Tag.objects.filter(name="LC"))
        Task.objects.bulk_create([
            Task(event=e, person=self.person1, role=self.instructor_role),
            Task(event=e, person=self.person2, role=self.instructor_role),
        ])

        a = GenericAction(
            trigger=Trigger(action="test-action", template=EmailTemplate()),
            objects=dict(request=wr, event=e),
        )

        self.assertEqual(
            a.all_recipients(),
            "*****@*****.**",
        )
Пример #10
0
    def testAdditionalContext(self):
        # the trigger and email template below are totally fake
        # and shouldn't pass validation
        a = BaseAction(trigger=Trigger(action="test-action", template=EmailTemplate()))
        a.additional_context = dict(obj1="test1", obj2="test2", obj3=123)

        self.assertEqual(
            a.get_additional_context(), {"obj1": "test1", "obj2": "test2", "obj3": 123}
        )
Пример #11
0
    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),
        )

        # case 1: no context event
        a1 = PostWorkshopAction(
            trigger=Trigger(action="test-action", template=EmailTemplate()),
        )
        self.assertEqual(a1.get_launch_at(), timedelta(days=7))

        # case 2: event with no end date
        a2 = PostWorkshopAction(
            trigger=Trigger(action="test-action", template=EmailTemplate()),
            objects=dict(event=e1),
        )
        self.assertEqual(a2.get_launch_at(), timedelta(days=7))

        # case 3: event with end date
        a3 = PostWorkshopAction(
            trigger=Trigger(action="test-action", template=EmailTemplate()),
            objects=dict(event=e2),
        )
        self.assertEqual(a3.get_launch_at(), timedelta(days=8 + 7))

        # case 4: event with negative end date
        a4 = PostWorkshopAction(
            trigger=Trigger(action="test-action", template=EmailTemplate()),
            objects=dict(event=e3),
        )
        self.assertEqual(a4.get_launch_at(), timedelta(days=7))
Пример #12
0
    def test_rendering_invalid_template(self):
        """Invalid filter or template tag should raise TemplateSyntaxError,
        whereas invalid variable should be changed to
        `XXX-unset-variable-XXX`."""
        template1 = "Hello, {{ name | nonexistentfilter }}!"
        data1 = dict(name="Harry")
        with self.assertRaises(TemplateSyntaxError):
            EmailTemplate.render_template(template1, data1)

        template2 = "Hello, {% invalidtag name %}!"
        data2 = dict(name="Harry")
        with self.assertRaises(TemplateSyntaxError):
            EmailTemplate.render_template(template2, data2)

        template3 = "Hello, {{ invalidname }}!"
        data3 = dict(name="Harry")
        self.assertEqual(
            EmailTemplate.render_template(template3, data3),
            "Hello, XXX-unset-variable-XXX!",
        )
Пример #13
0
    def test_rendering_template(self):
        template = ("Hello, {{ name }} {% if last_name %}{{ last_name }}"
                    "{% else %}Smith{% endif %}")

        data1 = {}
        # let's keep the hardcoded setting for missing variable in the template
        # system - just in case someone changes it unexpectedly
        expected_output1 = "Hello, XXX-unset-variable-XXX Smith"
        self.assertEqual(EmailTemplate.render_template(template, data1),
                         expected_output1)

        data2 = {"name": "Harry"}
        expected_output2 = "Hello, Harry Smith"
        self.assertEqual(EmailTemplate.render_template(template, data2),
                         expected_output2)

        data3 = {"name": "Harry", "last_name": "Potter"}
        expected_output3 = "Hello, Harry Potter"
        self.assertEqual(EmailTemplate.render_template(template, data3),
                         expected_output3)
Пример #14
0
    def testContext(self):
        """Make sure `get_additional_context` works correctly."""

        a = NewInstructorAction(
            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(KeyError):
            a.get_additional_context(dict(event="dummy"))  # missing 'task'
        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",
            address="Underground",
            latitude=20.0,
            longitude=20.0,
            url="https://test-event.example.com",
        )
        e.tags.set(Tag.objects.filter(name="SWC"))
        p = Person.objects.create(
            personal="Harry", family="Potter", email="*****@*****.**"
        )
        r = Role.objects.create(name="instructor")
        t = Task.objects.create(event=e, person=p, role=r)

        ctx = a.get_additional_context(objects=dict(event=e, task=t))
        self.assertEqual(
            ctx,
            dict(
                workshop=e,
                workshop_main_type="SWC",
                dates=e.human_readable_date,
                host=Organization.objects.first(),
                regional_coordinator_email=["*****@*****.**"],
                person=p,
                instructor=p,
                role=r,
                task=t,
                assignee="Regional Coordinator",
                tags=["SWC"],
            ),
        )
Пример #15
0
    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"],
            ),
        )
Пример #16
0
 def test_event_slug(self):
     wr = WorkshopRequest(email="*****@*****.**")
     event = Event.objects.create(
         slug="test-event1",
         host=Organization.objects.first(),
         start=datetime(2020, 10, 30),
         end=datetime(2020, 11, 1),
     )
     a = GenericAction(
         trigger=Trigger(action="test-action", template=EmailTemplate()),
         objects=dict(request=wr, event=event),
     )
     self.assertEqual(a.event_slug(), "test-event1")
Пример #17
0
    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="*****@*****.**",
        )
        p3 = Person.objects.create(
            personal="Ron", family="Weasley", username="******", email="*****@*****.**"
        )
        p4 = Person.objects.create(
            personal="Draco",
            family="Malfoy",
            username="******",
            email="*****@*****.**",
        )
        host = Role.objects.create(name="host")
        instructor = Role.objects.create(name="instructor")
        supporting_instructor = Role.objects.create(name="supporting-instructor")
        Task.objects.bulk_create(
            [
                Task(event=e, person=p1, role=instructor),
                Task(event=e, person=p2, role=instructor),
                Task(event=e, person=p3, role=host),
                Task(event=e, person=p1, role=host),
                Task(event=e, person=p4, role=supporting_instructor),
            ]
        )

        a = PostWorkshopAction(
            trigger=Trigger(action="test-action", template=EmailTemplate()),
            objects=dict(event=e),
        )

        self.assertEqual(
            a.all_recipients(),
            "[email protected], [email protected], [email protected], [email protected]",
        )
Пример #18
0
    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"))

        a = InstructorsHostIntroductionAction(
            trigger=Trigger(action="test-action", template=EmailTemplate()),
            objects=dict(event=e),
        )

        self.assertEqual(a.event_slug(), "test-event")
Пример #19
0
    def testContext(self):
        """Make sure `get_additional_context` works correctly."""
        a = RecruitHelpersAction(
            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"))

        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"]))
        Task.objects.create(event=e, person=self.person1, role=self.host_role)
        Task.objects.create(event=e,
                            person=self.person2,
                            role=self.instructor_role)

        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=["*****@*****.**"],
                hosts=[self.person1],
                instructors=[self.person2],
                all_emails=["*****@*****.**", "*****@*****.**"],
                assignee="Regional Coordinator",
                tags=["SWC", "TTT"],
            ),
        )
Пример #20
0
    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"))
        t = Task.objects.create(event=e,
                                person=self.person1,
                                role=self.instructor_role)

        a = RecruitHelpersAction(
            trigger=Trigger(action="test-action", template=EmailTemplate()),
            objects=dict(event=e, task=t),
        )

        self.assertEqual(a.event_slug(), "test-event")
Пример #21
0
    def testContext(self):
        a = GenericAction(
            trigger=Trigger(action="test-action", template=EmailTemplate()))
        with self.assertRaises(KeyError):
            a.get_additional_context(dict())  # missing 'request' object
        with self.assertRaises(AttributeError):
            # impossible to access `assigned_to` on a string
            a.get_additional_context(dict(request="dummy"))
        with self.assertRaises(AttributeError):
            # impossible to access `refresh_from_db` on a string
            a.get_additional_context(dict(request="dummy", event="dummy"))

        wr = WorkshopRequest()
        ctx = a.get_additional_context(dict(request=wr))
        self.assertEqual(ctx, {
            "request": wr,
            "assignee": "Regional Coordinator"
        })

        event = Event.objects.create(
            slug="test-event1",
            host=Organization.objects.first(),
            start=datetime(2020, 10, 30),
            end=datetime(2020, 11, 1),
        )
        event.tags.set([Tag.objects.get(name="SWC")])
        ctx = a.get_additional_context(dict(request=wr, event=event))
        self.assertEqual(
            ctx,
            {
                "request": wr,
                "workshop": event,
                "workshop_main_type": "SWC",
                "dates": "Oct 30 - Nov 01, 2020",
                "workshop_host": Organization.objects.first(),
                "regional_coordinator_email": ["*****@*****.**"],
                "all_emails": [],
                "assignee": "Regional Coordinator",
                "tags": ["SWC"],
            },
        )
Пример #22
0
    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 = PostWorkshopAction(
            trigger=Trigger(action="test-action", template=EmailTemplate()),
            objects=dict(event=e, task=t),
        )

        self.assertEqual(a.event_slug(), "test-event")
Пример #23
0
    def test_drop_empty_contacts(self):
        e = Event.objects.create(
            slug="test-event",
            host=Organization.objects.first(),
            administrator=Organization.objects.get(domain="self-organized"),
            start=date.today() + timedelta(days=7),
            end=date.today() + timedelta(days=8),
            country="GB",
            contact="*****@*****.**",  # this won't be picked up
        )
        e.tags.set(
            Tag.objects.filter(name__in=["LC", "Circuits", "automated-email"]))
        r = SelfOrganisedSubmission.objects.create(
            state="p",
            personal="Harry",
            family="Potter",
            email="",
            institution_other_name="Hogwarts",
            workshop_url="",
            workshop_format="",
            workshop_format_other="",
            workshop_types_other_explain="",
            language=Language.objects.get(name="English"),
            event=e,
            additional_contact=TAG_SEPARATOR,
        )
        r.workshop_types.set(Curriculum.objects.filter(carpentry="LC"))

        a = SelfOrganisedRequestAction(
            trigger=Trigger(action="test-action", template=EmailTemplate()),
            objects=dict(event=e, request=r),
        )

        self.assertEqual(a.all_recipients(), "")
        self.assertEqual(
            a.get_additional_context(dict(event=e, request=r))["all_emails"],
            [])
Пример #24
0
    def test_drop_empty_contacts(self):
        """Make sure `get_additional_context` works correctly when contacts are empty
        for the event."""
        e = Event.objects.create(
            slug="test-event",
            host=Organization.objects.first(),
            administrator=Organization.objects.get(domain="carpentries.org"),
            start=date.today() + timedelta(days=7),
            end=date.today() + timedelta(days=8),
            contact="",
            country="GB",
        )
        e.tags.set(Tag.objects.filter(name__in=["LC", "automated-email"]))

        a = InstructorsHostIntroductionAction(
            trigger=Trigger(action="test-action", template=EmailTemplate()),
            objects=dict(event=e),
        )

        self.person1.email = ""
        self.person1.save()
        Task.objects.create(person=self.person1, role=self.host, event=e)
        Task.objects.create(
            person=self.person2,
            role=self.instructor,
            event=e,
        )
        Task.objects.create(
            person=self.person3,
            role=self.instructor,
            event=e,
        )

        ctx = a.get_additional_context(objects=dict(event=e))
        expected = ["*****@*****.**", "*****@*****.**"]
        self.assertEqual(ctx["all_emails"], expected)
        self.assertEqual(a.all_recipients(), "[email protected], [email protected]")
Пример #25
0
    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"))
        Task.objects.bulk_create([
            Task(event=e, person=self.person1, role=self.instructor_role),
            Task(event=e, person=self.person2, role=self.instructor_role),
        ])

        a = RecruitHelpersAction(
            trigger=Trigger(action="test-action", template=EmailTemplate()),
            objects=dict(event=e),
        )

        self.assertEqual(
            a.all_recipients(),
            "[email protected], [email protected]",
        )
Пример #26
0
    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]",
        )
Пример #27
0
    def test_syntax_check(self):
        """All fields should have Django syntax check validation enabled."""
        # case 1: wrong tag
        tpl1 = EmailTemplate(
            slug="test-template-1",
            subject="Wrong tag {% tag %}",
            to_header="Wrong tag {% tag %}",
            from_header="Wrong tag {% tag %}",
            cc_header="Wrong tag {% tag %}",
            bcc_header="Wrong tag {% tag %}",
            reply_to_header="Wrong tag {% tag %}",
            body_template="Wrong tag {% tag %}",
        )
        with self.assertRaises(ValidationError) as e:
            tpl1.clean()

        self.assertIn("subject", e.exception.error_dict)
        self.assertIn("to_header", e.exception.error_dict)
        self.assertIn("from_header", e.exception.error_dict)
        self.assertIn("cc_header", e.exception.error_dict)
        self.assertIn("bcc_header", e.exception.error_dict)
        self.assertIn("reply_to_header", e.exception.error_dict)
        self.assertIn("body_template", e.exception.error_dict)
        self.assertIn("Invalid", e.exception.message_dict["subject"][0])

        # case 2: missing opening or closing tags
        tpl2 = EmailTemplate(
            slug="test-template-2",
            subject="Missing {% url ",
            to_header="Missing {{ tag",
            from_header="Missing %}",
            cc_header="Missing }}",
            bcc_header="Missing {% tag }",
            reply_to_header="Missing {{ tag }",
            body_template="Missing { tag }}",
        )
        with self.assertRaises(ValidationError) as e:
            tpl2.clean()

        self.assertIn("subject", e.exception.error_dict)
        self.assertIn("to_header", e.exception.error_dict)
        self.assertIn("from_header", e.exception.error_dict)
        self.assertIn("cc_header", e.exception.error_dict)
        self.assertIn("bcc_header", e.exception.error_dict)
        self.assertIn("reply_to_header", e.exception.error_dict)
        self.assertIn("body_template", e.exception.error_dict)
        self.assertIn("Missing", e.exception.message_dict["subject"][0])

        # case 3: empty values should pass
        tpl3 = EmailTemplate(
            slug="test-template-3",
            subject="",
            to_header="",
            from_header="",
            cc_header="",
            bcc_header="",
            reply_to_header="",
            body_template="",
        )
        tpl3.clean()
Пример #28
0
    def testContext(self):
        """Make sure `get_additional_context` works correctly."""
        a = InstructorsHostIntroductionAction(
            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 event is present, but the method tries to execute `refresh_from_db`
            # on it
            a.get_additional_context(dict(event="dummy"))

        # totally fake Event
        e = Event.objects.create(
            slug="test-event",
            host=Organization.objects.first(),
            administrator=Organization.objects.get(domain="carpentries.org"),
            start=date.today() + timedelta(days=7),
            end=date.today() + timedelta(days=8),
            contact=TAG_SEPARATOR.join(["*****@*****.**", "*****@*****.**"]),
            country="GB",
        )
        e.tags.set(Tag.objects.filter(name__in=["LC", "automated-email"]))
        # tasks
        host = Task.objects.create(person=self.person1,
                                   role=self.host,
                                   event=e)
        instructor1 = Task.objects.create(
            person=self.person2,
            role=self.instructor,
            event=e,
        )
        instructor2 = Task.objects.create(
            person=self.person3,
            role=self.instructor,
            event=e,
        )
        supporting_instructor1 = Task.objects.create(
            person=self.person4,
            role=self.supporting_instructor,
            event=e,
        )
        supporting_instructor2 = Task.objects.create(
            person=self.person5,
            role=self.supporting_instructor,
            event=e,
        )

        ctx = a.get_additional_context(objects=dict(event=e))
        self.maxDiff = None
        expected = dict(
            workshop=e,
            workshop_main_type="LC",
            dates=e.human_readable_date,
            workshop_host=Organization.objects.first(),
            regional_coordinator_email=["*****@*****.**"],
            host=host.person,
            instructors=[instructor1.person, instructor2.person],
            instructor1=instructor1.person,
            instructor2=instructor2.person,
            supporting_instructors=[
                supporting_instructor1.person,
                supporting_instructor2.person,
            ],
            supporting_instructor1=supporting_instructor1.person,
            supporting_instructor2=supporting_instructor2.person,
            hosts=[host.person],
            all_emails=[
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**",
            ],
            assignee="Regional Coordinator",
            tags=["automated-email", "LC"],
        )
        self.assertEqual(ctx, expected)
 def testLaunchAt(self):
     # the trigger and email template below are totally fake
     # and shouldn't pass validation
     trigger = Trigger(action="test-action", template=EmailTemplate())
     a = SelfOrganisedRequestAction(trigger=trigger)
     self.assertEqual(a.get_launch_at(), timedelta(hours=1))
Пример #30
0
 def testLaunchAt(self):
     a1 = GenericAction(trigger=Trigger(action="test-action",
                                        template=EmailTemplate()), )
     self.assertEqual(a1.get_launch_at(), timedelta(hours=1))