Example #1
0
    def test_bad_css(self):
        """This test uses an example email that causes issue #47"""
        msg = mail.MailRequest("", "", "", EXAMPLE_PREMAILER_BROKEN_CSS)
        make_email(msg, self.inbox)
        email = models.Email.objects.get()
        message_object = make_message(email)
        new_msg = mail.MailRequest("", "", "", str(message_object))

        self.assertEqual(len(msg.keys()), len(new_msg.keys()))
        self.assertEqual(len(list(msg.walk())), len(list(new_msg.walk())))
Example #2
0
    def test_signed_forwarded_digest(self):
        msg = mail.MailRequest("", "", "", EXAMPLE_SIGNED_FORWARDED_DIGEST)
        make_email(msg, self.inbox)
        self.email = models.Email.objects.get()
        email = models.Email.objects.get()
        message_object = make_message(email)
        new_msg = mail.MailRequest("", "", "", str(message_object))

        self.assertEqual(len(msg.keys()), len(new_msg.keys()))
        self.assertEqual(len(list(msg.walk())), len(list(new_msg.walk())))
Example #3
0
    def test_unicode(self):
        """This test uses an example email that contains unicode chars"""
        msg = mail.MailRequest("", "", "", EXAMPLE_MISSING_CTE)
        make_email(msg, self.inbox)
        email = models.Email.objects.get()
        message_object = make_message(email)
        new_msg = mail.MailRequest("", "", "",
                                   message_object.as_bytes().decode())

        self.assertEqual(len(msg.keys()), len(new_msg.keys()))
        self.assertEqual(len(list(msg.walk())), len(list(new_msg.walk())))
Example #4
0
    def test_to_from_works(self):
        msg = mail.MailRequest("fakepeer", "from@localhost", [u"<to1@localhost>", u"to2@localhost"], "")
        assert '<' not in msg.To, msg.To

        msg = mail.MailRequest("fakepeer", "from@localhost", [u"to1@localhost", u"to2@localhost"], "")
        assert '<' not in msg.To, msg.To

        msg = mail.MailRequest("fakepeer", "from@localhost", [u"to1@localhost", u"<to2@localhost>"], "")
        assert '<' not in msg.To, msg.To

        msg = mail.MailRequest("fakepeer", "from@localhost", [u"to1@localhost"], "")
        assert '<' not in msg.To, msg.To

        msg = mail.MailRequest("fakepeer", "from@localhost", [u"<to1@localhost>"], "")
        assert '<' not in msg.To, msg.To
Example #5
0
    def test_mail_response_mailing_list_headers(self):
        list_addr = "test.users@localhost"

        msg = mail.MailResponse(From='somedude@localhost',
                                To=list_addr,
                                Subject='subject',
                                Body="Mailing list reply.")

        print(repr(msg))

        msg["Sender"] = list_addr
        msg["Reply-To"] = list_addr
        msg["List-Id"] = list_addr
        msg["Return-Path"] = list_addr
        msg["In-Reply-To"] = 'Message-Id-1231123'
        msg["References"] = 'Message-Id-838845854'
        msg["Precedence"] = 'list'

        data = str(msg)

        req = mail.MailRequest('localhost', 'somedude@localhost', list_addr,
                               data)

        headers = [
            'Sender', 'Reply-To', 'List-Id', 'Return-Path', 'In-Reply-To',
            'References', 'Precedence'
        ]
        for header in headers:
            assert msg[header] == req[header], "%s: %r != %r" % (
                header, msg[header], req[header])

        # try a delete
        del msg['Precedence']
Example #6
0
    def test_walk(self):
        with open("tests/data/bounce.msg") as file_obj:
            bm = mail.MailRequest(None, None, None, file_obj.read())
        parts = [x for x in bm.walk()]

        assert parts
        self.assertEqual(len(parts), 6)
Example #7
0
    def test_craft_from_sample(self):
        list_name = "test.list"
        user_full_address = "tester@localhost"

        sample = mail.MailResponse(
            To=list_name + "@localhost",
            From=user_full_address,
            Subject="Test message with attachments.",
            Body="The body as one attachment.",
        )
        sample.update({"Test": "update"})

        sample.attach(filename="tests/message_tests.py",
                      content_type="text/plain",
                      disposition="attachment")

        inmsg = mail.MailRequest("fakepeer", None, None, str(sample))
        assert "Test" in sample.keys()

        for part in inmsg.to_message().walk():
            assert part.get_payload(), "inmsg busted."

        outmsg = mail.MailResponse(To=inmsg['from'],
                                   From=inmsg['to'],
                                   Subject=inmsg['subject'])

        outmsg.attach_all_parts(inmsg)

        result = outmsg.to_message()

        for part in result.walk():
            assert part.get_payload(), "outmsg parts don't have payload."
Example #8
0
def test_attach_headers():
    msg = mail.MailRequest('test_attach_headers', 'tester@localhost',
                           '*****@*****.**', 'Fake body.')

    comment.attach_headers(msg)
    for key in ['X-Post-Name', 'X-Post-User-ID', 'X-Post-Domain']:
        assert key in msg
Example #9
0
    def test_bounce_analyzer_on_bounce(self):
        with open("tests/data/bounce.msg") as file_obj:
            bm = mail.MailRequest(None, None, None, file_obj.read())
        assert bm.is_bounce()
        assert bm.bounce
        self.assertEqual(bm.bounce.score, 1.0)
        assert bm.bounce.probable()
        self.assertEqual(bm.bounce.primary_status, (5, u'Permanent Failure'))
        self.assertEqual(bm.bounce.secondary_status, (1, u'Addressing Status'))
        self.assertEqual(bm.bounce.combined_status,
                         (11, u'Bad destination mailbox address'))

        assert bm.bounce.is_hard()
        self.assertEqual(bm.bounce.is_hard(), not bm.bounce.is_soft())

        self.assertEqual(bm.bounce.remote_mta, u'gmail-smtp-in.l.google.com')
        self.assertEqual(bm.bounce.reporting_mta, u'mail.zedshaw.com')
        self.assertEqual(
            bm.bounce.final_recipient,
            u'*****@*****.**'
        )
        self.assertEqual(bm.bounce.diagnostic_codes[0], u'550-5.1.1')
        self.assertEqual(bm.bounce.action, 'failed')
        assert 'Content-Description-Parts' in bm.bounce.headers

        assert bm.bounce.error_for_humans()
Example #10
0
def test_route_reply():
    msg = mail.MailRequest('fakepeer', from_user['from'], from_user['to'],
                           str(from_user))
    reply = filter.route_reply(msg, marketroid_id, host).to_message()

    # make sure the user's address is never in a header
    for k, v in reply.items():
        assert_not_equal(reply[k], user)
Example #11
0
def test_cleanse_incoming():
    msg = mail.MailRequest('fakepeer', from_marketroid['from'],
                           from_marketroid['to'], str(from_marketroid))

    reply = filter.cleanse_incoming(msg, user_id, host).to_message()
    assert_equal(reply['from'], marketroid)
    assert_equal(reply['to'], user)
    assert_equal(reply['reply-to'], mk_anon_addr)
Example #12
0
def test_bounce_to_decorator():
    msg = mail.MailRequest(None, Data=open("tests/bounce.msg").read())

    Router.deliver(msg)
    assert Router.in_state(bounce_filtered_mod.START, msg)
    assert bounce_filtered_mod.HARD_RAN, "Hard bounce state didn't actually run: %r" % msg.route_to

    msg.bounce.primary_status = (4, u'Persistent Transient Failure')
    Router.clear_states()
    Router.deliver(msg)
    assert Router.in_state(bounce_filtered_mod.START, msg)
    assert bounce_filtered_mod.SOFT_RAN, "Soft bounce didn't actually run."

    msg = mail.MailRequest(None, Data=open("tests/signed.msg").read())
    Router.clear_states()
    Router.deliver(msg)
    assert Router.in_state(bounce_filtered_mod.END, msg), "Regular messages aren't delivering."
Example #13
0
    def test_alterative(self):
        self.msg = mail.MailRequest("", "", "", EXAMPLE_ALT)
        make_email(self.msg, self.inbox)
        self.email = models.Email.objects.get()

        response = self.client.get(self.get_url())
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.context["email"]["bodies"]), 1)
Example #14
0
    def test_mail_request(self):
        # try with a half-assed message
        msg = mail.MailRequest("localhost", "zedfrom@localhost",
                               "zedto@localhost", "Fake body.")
        self.assertEqual(msg['to'], "zedto@localhost")
        self.assertEqual(msg['from'], "zedfrom@localhost")

        msg = mail.MailRequest("localhost", "somedude@localhost",
                               ["somedude@localhost"], sample_message)
        self.assertEqual(msg.original, sample_message)

        self.assertEqual(msg['From'], "somedude@localhost")

        assert ("From" in msg)
        del msg["From"]
        assert ("From" not in msg)

        msg["From"] = "nobody@localhost"
        assert ("From" in msg)
        self.assertEqual(msg["From"], "nobody@localhost")
        msg["From"] = "somebody@localhost"
        self.assertEqual(msg["From"], "somebody@localhost")
        self.assertEqual(msg.keys(), ["To", "From"])
        self.assertEqual(msg.items(), [("To", "somedude@localhost"),
                                       ("From", "somebody@localhost")])

        # appending headers
        msg.base.append_header("To", "*****@*****.**")
        self.assertEqual(msg["To"], "somedude@localhost")
        self.assertEqual(msg.keys(), ["To", "From", "To"])
        self.assertEqual(msg.items(), [("To", "somedude@localhost"),
                                       ("From", "somebody@localhost"),
                                       ("To", "*****@*****.**")])

        # validate that upper and lower case work for headers
        assert ("FroM" in msg)
        assert ("from" in msg)
        assert ("From" in msg)
        self.assertEqual(msg['From'], msg['fRom'])
        self.assertEqual(msg['From'], msg['from'])
        self.assertEqual(msg['from'], msg['fRom'])

        # make sure repr runs
        assert repr(msg)

        assert str(msg)
Example #15
0
    def test_ConfirmationEngine_verify(self):
        confirm = self.test_ConfirmationEngine_send()
        confirm = mail.MailRequest(None, None, None, confirm)

        resp = mail.MailRequest('fakepeer', '"Somedude Smith" <somedude@localhost>', confirm['Reply-To'], 'Fake body')

        target, _, expect_secret = confirm['Reply-To'].split('-')
        expect_secret = expect_secret.split('@')[0]

        found = self.engine.verify(target, resp['from'], 'invalid_secret')
        assert not found

        pending = self.engine.verify(target, resp['from'], expect_secret)
        assert pending, "Verify failed: %r not in %r." % (expect_secret,
                                                          self.storage.confirmations)

        self.assertEqual(pending['from'], 'somedude@localhost')
        self.assertEqual(pending['to'], 'testing-subscribe@localhost')
Example #16
0
 def deliver(self, To, From, Subject, Body):
     """Overrides TestConversation.deliver to do it internally."""
     sample = mail.MailResponse(From=From,
                                To=To,
                                Subject=Subject,
                                Body=Body)
     msg = mail.MailRequest('localhost', sample['From'], sample['To'],
                            str(sample))
     routing.Router.deliver(msg)
Example #17
0
def test_queue_receiver():
    receiver = server.QueueReceiver('run/queue')
    run_queue = queue.Queue('run/queue')
    run_queue.push(str(test_mail_response_plain_text()))
    assert run_queue.count() > 0
    receiver.start(one_shot=True)
    assert_equal(run_queue.count(), 0)

    routing.Router.deliver.side_effect = raises_exception
    receiver.process_message(mail.MailRequest('localhost', 'test@localhost', 'test@localhost', 'Fake body.'))
Example #18
0
    def test_ConfirmationEngine_cancel_bad_secret(self):
        confirm = self.test_ConfirmationEngine_send()
        confirm = mail.MailRequest(None, None, None, confirm)

        target = confirm['Reply-To'].split('-')[0]
        expect_secret = "bad"

        self.engine.cancel(target, confirm['To'], expect_secret)

        self.assertIn(b"testing:somedude@localhost", self.engine.storage.confirmations.keys())
Example #19
0
def sendmail(port, host, recipients, debug=False, lmtp=False):
    """
    Used as a testing sendmail replacement for use in programs
    like mutt as an MTA.  It reads the email to send on the stdin
    and then delivers it based on the port and host settings.
    """
    relay = server.Relay(host, port=port, debug=debug, lmtp=lmtp)
    data = sys.stdin.read()
    msg = mail.MailRequest(None, recipients, None, data)
    relay.deliver(msg)
Example #20
0
    def test_bad_css(self):
        """This test uses an example email that causes issue #47"""
        self.msg = mail.MailRequest("", "", "", EXAMPLE_PREMAILER_BROKEN_CSS)
        make_email(self.msg, self.inbox)
        self.email = models.Email.objects.get()

        response = self.client.get(self.get_url())
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.context["email"]["bodies"]), 1)
        self.assertNotIn("Part of this message could not be parsed - it may not display correctly",
                         response.content.decode("utf-8"))
Example #21
0
    def test_ConfirmationEngine_cancel(self):
        confirm = self.test_ConfirmationEngine_send()
        confirm = mail.MailRequest(None, None, None, confirm)

        target, _, expect_secret = confirm['Reply-To'].split('-')
        expect_secret = expect_secret.split('@')[0]

        self.engine.cancel(target, confirm['To'], expect_secret)

        found = self.engine.verify(target, confirm['To'], expect_secret)
        assert not found
Example #22
0
def test_copy_parts():
    bm = mail.MailRequest(None, None, None, open("tests/bounce.msg").read())

    resp = mail.MailResponse(To=bm['to'], From=bm['from'], Subject=bm['subject'])

    resp.attach_all_parts(bm)

    resp = resp.to_message()
    bm = bm.to_message()

    assert_equal(len([x for x in bm.walk()]), len([x for x in resp.walk()]))
Example #23
0
    def test_premime(self):
        self.msg = mail.MailRequest("", "", "", EXAMPLE_PREMIME_EMAIL)
        make_email(self.msg, self.inbox)
        self.email = models.Email.objects.get()

        response = self.client.get(self.get_url())
        self.assertEqual(response.status_code, 200)

        self.assertEqual(self.email.parts.all().count(), 1)
        self.assertEqual(len(response.context["email"]["bodies"]), 1)
        self.assertEqual(response.context["email"]["bodies"][0], "<pre>Hi,\n\nHow are you?\n\nThanks,\nTest\n</pre>")
Example #24
0
def test_mail_request():
    # try with a half-assed message
    msg = mail.MailRequest("localhost", "zedfrom@localhost", "zedto@localhost",
                           "Fake body.")
    assert msg['to'] == "zedto@localhost", "To is %r" % msg['to']
    assert msg['from'] == "zedfrom@localhost", "From is %r" % msg['from']

    msg = mail.MailRequest("localhost", "somedude@localhost",
                           ["somedude@localhost"], sample_message)
    assert msg.original == sample_message

    assert_equal(msg['From'], "somedude@localhost")

    assert ("From" in msg)
    del msg["From"]
    assert ("From" not in msg)

    msg["From"] = "nobody@localhost"
    assert ("From" in msg)
    assert_equal(msg["From"], "nobody@localhost")
    msg["From"] = "somebody@localhost"
    assert_equal(msg["From"], "somebody@localhost")
    assert_equal(msg.keys(), ["To", "From"])

    # appending headers
    msg.Email.append_header("To", "*****@*****.**")
    assert_equal(msg["To"], "somedude@localhost")
    assert_equal(msg.keys(), ["To", "From", "To"])

    # validate that upper and lower case work for headers
    assert ("FroM" in msg)
    assert ("from" in msg)
    assert ("From" in msg)
    assert_equal(msg['From'], msg['fRom'])
    assert_equal(msg['From'], msg['from'])
    assert_equal(msg['from'], msg['fRom'])

    # make sure repr runs
    print repr(msg)

    return msg
Example #25
0
def test_mail_request_attachments():
    sample = test_mail_response_attachments()
    data = str(sample)

    msg = mail.MailRequest("localhost", None, None, data)

    msg_parts = msg.all_parts()
    sample_parts = sample.all_parts()

    readme = open("./README.rst").read()

    assert msg_parts[0].body == sample_parts[0].body
    assert readme == msg_parts[1].body
    assert msg.body() == sample_parts[0].body

    # test that we get at least one message for messages without attachments
    sample = test_mail_response_plain_text()
    msg = mail.MailRequest("localhost", None, None, str(sample))
    msg_parts = msg.all_parts()
    assert_equal(len(msg_parts), 0)
    assert msg.body()
Example #26
0
def sendmail_command(port=8825, host='127.0.0.1', debug=0, TRAILING=None):
    """
    Used as a testing sendmail replacement for use in programs
    like mutt as an MTA.  It reads the email to send on the stdin
    and then delivers it based on the port and host settings.

    salmon sendmail -port 8825 -host 127.0.0.1 -debug 0 -- [recipients]
    """
    relay = server.Relay(host, port=port, debug=debug)
    data = sys.stdin.read()
    msg = mail.MailRequest(None, TRAILING, None, data)
    relay.deliver(msg)
Example #27
0
    def test_signed_forwarded_digest(self):
        self.msg = mail.MailRequest("", "", "", EXAMPLE_SIGNED_FORWARDED_DIGEST)
        make_email(self.msg, self.inbox)
        self.email = models.Email.objects.get()

        response = self.client.get(self.get_url())
        self.assertEqual(response.status_code, 200)

        leaf_part_count = len([i for i in self.email.parts.all() if i.is_leaf_node()])
        self.assertEqual(leaf_part_count, 12)
        self.assertEqual(len(response.context["email"]["bodies"]), 1)
        self.assertEqual(response.context["email"]["bodies"][0], "<pre>Hello\n</pre>")
Example #28
0
    def test_copy_parts(self):
        with open("tests/data/bounce.msg") as file_obj:
            bm = mail.MailRequest(None, None, None, file_obj.read())

        resp = mail.MailResponse(To=bm['to'], From=bm['from'], Subject=bm['subject'])

        resp.attach_all_parts(bm)

        resp = resp.to_message()
        bm = bm.to_message()

        self.assertEqual(len([x for x in bm.walk()]), len([x for x in resp.walk()]))
Example #29
0
    def test_digest(self):
        self.msg = mail.MailRequest("", "", "", EXAMPLE_DIGEST)
        make_email(self.msg, self.inbox)
        self.email = models.Email.objects.get()

        response = self.client.get(self.get_url())
        self.assertEqual(response.status_code, 200)

        # this email should display all leaves
        leaf_part_count = len([i for i in self.email.parts.all() if i.is_leaf_node()
                               and i.content_type != "application/pgp-signature"])
        self.assertEqual(len(response.context["email"]["bodies"]), leaf_part_count)
Example #30
0
    def test_queue_receiver(self, router_mock):
        receiver = server.QueueReceiver('run/queue')
        run_queue = queue.Queue('run/queue')
        run_queue.push(str(generate_mail(factory=mail.MailResponse)))
        assert run_queue.count() > 0
        receiver.start(one_shot=True)
        self.assertEqual(run_queue.count(), 0)
        self.assertEqual(run_queue.count(), 0)
        self.assertEqual(router_mock.deliver.call_count, 1)

        router_mock.deliver.side_effect = RuntimeError("Raised on purpose")
        receiver.process_message(mail.MailRequest('localhost', 'test@localhost', 'test@localhost', 'Fake body.'))