source: test/test_session.py @ 1720:a10aefe0f71f

Last change on this file since 1720:a10aefe0f71f was 1720:a10aefe0f71f, checked in by Stefan Schwarzer <sschwarzer@…>, 2 years ago
Remove support for `M2Crypto` M2Crypto was used to get FTP_TLS support in Python 2. Since we now target Python 3.5+, users can and should use `ftplib.FTP_TLS` to get FTP_TLS support.
File size: 4.3 KB
Line 
1# Copyright (C) 2014-2018, Stefan Schwarzer <sschwarzer@sschwarzer.net>
2# and ftputil contributors (see `doc/contributors.txt`)
3# See the file LICENSE for licensing terms.
4
5"""
6Unit tests for session factory helpers.
7"""
8
9import ftputil.session
10
11
12class MockSession:
13    """
14    Mock session base class to determine if all expected calls
15    have happened.
16    """
17
18    def __init__(self):
19        self.calls = []
20
21    def add_call(self, *args):
22        self.calls.append(args)
23
24    def connect(self, host, port):
25        self.add_call("connect", host, port)
26
27    def set_debuglevel(self, value):
28        self.add_call("set_debuglevel", value)
29
30    def login(self, user, password):
31        self.add_call("login", user, password)
32
33    def set_pasv(self, flag):
34        self.add_call("set_pasv", flag)
35
36
37class EncryptedMockSession(MockSession):
38
39    def auth_tls(self):
40        self.add_call("auth_tls")
41
42    def prot_p(self):
43        self.add_call("prot_p")
44
45
46class TestSessionFactory:
47    """
48    Test if session factories created by
49    `ftputil.session.session_factory` trigger the expected calls.
50    """
51
52    def test_defaults(self):
53        """Test defaults (apart from base class)."""
54        factory = \
55          ftputil.session.session_factory(base_class=MockSession)
56        session = factory("host", "user", "password")
57        assert (session.calls ==
58                [("connect", "host", 21), ("login", "user", "password")])
59
60    def test_different_port(self):
61        """Test setting the command channel port with `port`."""
62        factory = \
63          ftputil.session.session_factory(base_class=MockSession, port=2121)
64        session = factory("host", "user", "password")
65        assert (session.calls ==
66                [("connect", "host", 2121), ("login", "user", "password")])
67
68    def test_use_passive_mode(self):
69        """
70        Test explicitly setting passive/active mode with
71        `use_passive_mode`.
72        """
73        # Passive mode
74        factory = ftputil.session.session_factory(base_class=MockSession,
75                                                  use_passive_mode=True)
76        session = factory("host", "user", "password")
77        assert session.calls == [("connect", "host", 21),
78                                 ("login", "user", "password"),
79                                 ("set_pasv", True)]
80        # Active mode
81        factory = ftputil.session.session_factory(base_class=MockSession,
82                                                  use_passive_mode=False)
83        session = factory("host", "user", "password")
84        assert session.calls == [("connect", "host", 21),
85                                 ("login", "user", "password"),
86                                 ("set_pasv", False)]
87
88    def test_encrypt_data_channel(self):
89        """Test request to call `prot_p` with `encrypt_data_channel`."""
90        # With encrypted data channel (default for encrypted session).
91        factory = ftputil.session.session_factory(
92                    base_class=EncryptedMockSession)
93        session = factory("host", "user", "password")
94        assert session.calls == [("connect", "host", 21),
95                                 ("login", "user", "password"),
96                                 ("prot_p",)]
97        #
98        factory = ftputil.session.session_factory(
99                    base_class=EncryptedMockSession, encrypt_data_channel=True)
100        session = factory("host", "user", "password")
101        assert session.calls == [("connect", "host", 21),
102                                 ("login", "user", "password"),
103                                 ("prot_p",)]
104        # Without encrypted data channel.
105        factory = ftputil.session.session_factory(
106                    base_class=EncryptedMockSession, encrypt_data_channel=False)
107        session = factory("host", "user", "password")
108        assert session.calls == [("connect", "host", 21),
109                                 ("login", "user", "password")]
110
111    def test_debug_level(self):
112        """Test setting the debug level on the session."""
113        factory = ftputil.session.session_factory(base_class=MockSession,
114                                                  debug_level=1)
115        session = factory("host", "user", "password")
116        assert session.calls == [("connect", "host", 21),
117                                 ("set_debuglevel", 1),
118                                 ("login", "user", "password")]
Note: See TracBrowser for help on using the repository browser.