示例#1
0
    def test_from_sitewide_integration(self):
        """Test the ability to load an Emailer from a sitewide integration."""
        integration = self._integration()
        emailer = Emailer.from_sitewide_integration(self._db)

        # The Emailer's configuration is based on the sitewide integration.
        eq_("smtp_username", emailer.smtp_username)
        eq_("smtp_password", emailer.smtp_password)
        eq_("smtp_host", emailer.smtp_host)
        eq_("me@registry", emailer.from_address)

        # Default EmailTemplates have been created for all known email types.
        for email_type in Emailer.EMAIL_TYPES:
            template = emailer.templates[email_type]
            eq_(Emailer.SUBJECTS[email_type], template.subject_template)
            eq_(Emailer.BODIES[email_type], template.body_template)

        # Configure custom subject lines and body templates for the
        # known email types, and build another Emailer.
        for email_type in Emailer.EMAIL_TYPES:
            integration.setting(email_type +
                                "_subject").value = ("subject %s" % email_type)
            integration.setting(email_type + "_body").value = ("body %s" %
                                                               email_type)
        emailer = Emailer.from_sitewide_integration(self._db)
        for email_type in Emailer.EMAIL_TYPES:
            template = emailer.templates[email_type]
            eq_("subject %s" % email_type, template.subject_template)
            eq_("body %s" % email_type, template.body_template)
示例#2
0
    def test__send_email(self):
        """Verify that send_email calls certain methods on smtplib.SMTP."""
        integration = self._integration()
        emailer = Emailer.from_sitewide_integration(self._db)
        mock = MockSMTP()
        emailer._send_email("you@library", "email body", mock)

        # Five smtplib.SMTP methods were called.
        connect, starttls, login, sendmail, quit = mock.calls
        eq_(('connect', (emailer.smtp_host, emailer.smtp_port), {}), connect)

        eq_(('starttls', (), {}), starttls)

        eq_(('login', (emailer.smtp_username, emailer.smtp_password), {}),
            login)

        eq_(('sendmail',
             (emailer.from_address, "you@library", "email body"), {}),
            sendmail)

        eq_(("quit", (), {}), quit)
示例#3
0
    def test_templates(self):
        """Test the emails generated by the default templates."""
        integration = self._integration()
        emailer = Emailer.from_sitewide_integration(self._db)

        # Start with arguments common to both email templates.
        args = dict(
            rel_desc="support address",
            library="My Public Library",
            library_web_url="https://library/",
        )

        # Generate the address-designation template.
        designation_template = emailer.templates[Emailer.ADDRESS_DESIGNATED]
        body = designation_template.body("me@registry", "you@library", **args)

        # Verify that the headers were set correctly.
        for phrase in [
                "From: me@registry",
                "To: you@library",
                "This address designated as the support address for My Public".
                replace(" ", "_"),  # Part of the encoding process
        ]:
            assert phrase in body

        # Verify that the body was set correctly.
        expect = """This email address, you@library, has been registered with the Library Simplified library registry as the support address for the library My Public Library (https://library/).

If this is obviously wrong (for instance, you don't work at a public library), please accept our apologies and contact the Library Simplified support address at me@registry -- something has gone wrong.

If you do work at a public library, but you're not sure what this means, please speak to a technical point of contact at your library, or contact the Library Simplified support address at me@registry."""
        text_part = MIMEText(expect, 'plain', 'utf-8')
        assert text_part.get_payload() in body

        # The confirmation template has a couple extra fields that need
        # filling in.
        confirmation_template = emailer.templates[
            Emailer.ADDRESS_NEEDS_CONFIRMATION]
        args['confirmation_link'] = "http://registry/confirm"
        body2 = confirmation_template.body("me@registry", "you@library",
                                           **args)

        # The address confirmation template is the address designation
        # template with a couple extra paragraphs and a different
        # subject line.
        extra = """If you do know what this means, you should also know that you
're not quite done. We need to confirm that you actually meant to use this email address for this purpose. If everything looks right, please visit this link:

http://registry/confirm

The link will expire in about a day. If the link expires, just re-register your library with the library registry, and a fresh confirmation email like this will be sent out."""

        # Verify the subject line
        assert "Confirm_the_" in body2

        # Verify that the extra content is there. (TODO: I wasn't able
        # to check the whole thing because expect2 parses into a
        # slightly different Message object than is generated by
        # Emailer.)
        expect2 = expect + "\n\n" + extra
        for phrase in ["\nhttp://registry/confirm\n", "The link will expire"]:
            assert phrase in body2