test_smtpnet.py 1.85 KB
Newer Older
1 2 3
import unittest
from test import support
import smtplib
4 5

ssl = support.import_module("ssl")
6

7
support.requires("network")
8

9 10 11 12 13 14 15 16 17 18

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)
19 20 21 22 23 24 25
            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
26 27
            server.ehlo()
            server.quit()
28 29


30 31 32
class SmtpSSLTest(unittest.TestCase):
    testServer = 'smtp.gmail.com'
    remotePort = 465
33
    context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
34 35

    def test_connect(self):
36
        support.get_attribute(smtplib, 'SMTP_SSL')
37 38
        with support.transient_internet(self.testServer):
            server = smtplib.SMTP_SSL(self.testServer, self.remotePort)
39 40
            server.ehlo()
            server.quit()
41

42 43 44 45
    def test_connect_default_port(self):
        support.get_attribute(smtplib, 'SMTP_SSL')
        with support.transient_internet(self.testServer):
            server = smtplib.SMTP_SSL(self.testServer)
46 47
            server.ehlo()
            server.quit()
48

49 50 51 52
    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)
53 54
            server.ehlo()
            server.quit()
55 56


57
def test_main():
58
    support.run_unittest(SmtpTest, SmtpSSLTest)
59 60 61

if __name__ == "__main__":
    test_main()