summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_smtpnet.py
blob: 86224ef2e45beb669e3503f2300595bb515c1656 (plain)
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
#!/usr/bin/env python3

import unittest
from test import support
import smtplib

ssl = support.import_module("ssl")

support.requires("network")


class SmtpTest(unittest.TestCase):
    testServer = 'smtp.gmail.com'
    remotePort = 25
    context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)

    def test_connect_starttls(self):
        support.get_attribute(smtplib, 'SMTP_SSL')
        with support.transient_internet(self.testServer):
            server = smtplib.SMTP(self.testServer, self.remotePort)
            try:
                server.starttls(context=self.context)
            except smtplib.SMTPException as e:
                if e.args[0] == 'STARTTLS extension not supported by server.':
                    unittest.skip(e.args[0])
                else:
                    raise
            server.ehlo()
            server.quit()


class SmtpSSLTest(unittest.TestCase):
    testServer = 'smtp.gmail.com'
    remotePort = 465
    context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)

    def test_connect(self):
        support.get_attribute(smtplib, 'SMTP_SSL')
        with support.transient_internet(self.testServer):
            server = smtplib.SMTP_SSL(self.testServer, self.remotePort)
            server.ehlo()
            server.quit()

    def test_connect_default_port(self):
        support.get_attribute(smtplib, 'SMTP_SSL')
        with support.transient_internet(self.testServer):
            server = smtplib.SMTP_SSL(self.testServer)
            server.ehlo()
            server.quit()

    def test_connect_using_sslcontext(self):
        support.get_attribute(smtplib, 'SMTP_SSL')
        with support.transient_internet(self.testServer):
            server = smtplib.SMTP_SSL(self.testServer, self.remotePort, context=self.context)
            server.ehlo()
            server.quit()


def test_main():
    support.run_unittest(SmtpTest, SmtpSSLTest)

if __name__ == "__main__":
    test_main()