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

import unittest
from test import support
import smtplib
import 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)
            server.starttls(context=self.context)
        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()