File: test_mail.py

package info (click to toggle)
python-scrapy 2.13.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,664 kB
  • sloc: python: 52,028; xml: 199; makefile: 25; sh: 7
file content (158 lines) | stat: -rw-r--r-- 5,095 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from email.charset import Charset
from io import BytesIO

from twisted.internet import defer
from twisted.internet._sslverify import ClientTLSOptions

from scrapy.mail import MailSender


class TestMailSender:
    def test_send(self):
        mailsender = MailSender(debug=True)
        mailsender.send(
            to=["test@scrapy.org"],
            subject="subject",
            body="body",
            _callback=self._catch_mail_sent,
        )

        assert self.catched_msg

        assert self.catched_msg["to"] == ["test@scrapy.org"]
        assert self.catched_msg["subject"] == "subject"
        assert self.catched_msg["body"] == "body"

        msg = self.catched_msg["msg"]
        assert msg["to"] == "test@scrapy.org"
        assert msg["subject"] == "subject"
        assert msg.get_payload() == "body"
        assert msg.get("Content-Type") == "text/plain"

    def test_send_single_values_to_and_cc(self):
        mailsender = MailSender(debug=True)
        mailsender.send(
            to="test@scrapy.org",
            subject="subject",
            body="body",
            cc="test@scrapy.org",
            _callback=self._catch_mail_sent,
        )

    def test_send_html(self):
        mailsender = MailSender(debug=True)
        mailsender.send(
            to=["test@scrapy.org"],
            subject="subject",
            body="<p>body</p>",
            mimetype="text/html",
            _callback=self._catch_mail_sent,
        )

        msg = self.catched_msg["msg"]
        assert msg.get_payload() == "<p>body</p>"
        assert msg.get("Content-Type") == "text/html"

    def test_send_attach(self):
        attach = BytesIO()
        attach.write(b"content")
        attach.seek(0)
        attachs = [("attachment", "text/plain", attach)]

        mailsender = MailSender(debug=True)
        mailsender.send(
            to=["test@scrapy.org"],
            subject="subject",
            body="body",
            attachs=attachs,
            _callback=self._catch_mail_sent,
        )

        assert self.catched_msg
        assert self.catched_msg["to"] == ["test@scrapy.org"]
        assert self.catched_msg["subject"] == "subject"
        assert self.catched_msg["body"] == "body"

        msg = self.catched_msg["msg"]
        assert msg["to"] == "test@scrapy.org"
        assert msg["subject"] == "subject"

        payload = msg.get_payload()
        assert isinstance(payload, list)
        assert len(payload) == 2

        text, attach = payload
        assert text.get_payload(decode=True) == b"body"
        assert text.get_charset() == Charset("us-ascii")
        assert attach.get_payload(decode=True) == b"content"

    def _catch_mail_sent(self, **kwargs):
        self.catched_msg = {**kwargs}

    def test_send_utf8(self):
        subject = "sübjèçt"
        body = "bödÿ-àéïöñß"
        mailsender = MailSender(debug=True)
        mailsender.send(
            to=["test@scrapy.org"],
            subject=subject,
            body=body,
            charset="utf-8",
            _callback=self._catch_mail_sent,
        )

        assert self.catched_msg
        assert self.catched_msg["subject"] == subject
        assert self.catched_msg["body"] == body

        msg = self.catched_msg["msg"]
        assert msg["subject"] == subject
        assert msg.get_payload(decode=True).decode("utf-8") == body
        assert msg.get_charset() == Charset("utf-8")
        assert msg.get("Content-Type") == 'text/plain; charset="utf-8"'

    def test_send_attach_utf8(self):
        subject = "sübjèçt"
        body = "bödÿ-àéïöñß"
        attach = BytesIO()
        attach.write(body.encode("utf-8"))
        attach.seek(0)
        attachs = [("attachment", "text/plain", attach)]

        mailsender = MailSender(debug=True)
        mailsender.send(
            to=["test@scrapy.org"],
            subject=subject,
            body=body,
            attachs=attachs,
            charset="utf-8",
            _callback=self._catch_mail_sent,
        )

        assert self.catched_msg
        assert self.catched_msg["subject"] == subject
        assert self.catched_msg["body"] == body

        msg = self.catched_msg["msg"]
        assert msg["subject"] == subject
        assert msg.get_charset() == Charset("utf-8")
        assert msg.get("Content-Type") == 'multipart/mixed; charset="utf-8"'

        payload = msg.get_payload()
        assert isinstance(payload, list)
        assert len(payload) == 2

        text, attach = payload
        assert text.get_payload(decode=True).decode("utf-8") == body
        assert text.get_charset() == Charset("utf-8")
        assert attach.get_payload(decode=True).decode("utf-8") == body

    def test_create_sender_factory_with_host(self):
        mailsender = MailSender(debug=False, smtphost="smtp.testhost.com")

        factory = mailsender._create_sender_factory(
            to_addrs=["test@scrapy.org"], msg="test", d=defer.Deferred()
        )

        context = factory.buildProtocol("test@scrapy.org").context
        assert isinstance(context, ClientTLSOptions)