source: test/test_session.py @ 1660:93ea351f922b

Last change on this file since 1660:93ea351f922b was 1660:93ea351f922b, checked in by Stefan Schwarzer <sschwarzer@…>, 4 years ago
Remove `unittest.main` calls Since we now use pytest as test runner and not a shell loop in the makefile, there's no longer a need for explicit `unittest.main` calls. ticket: 98
File size: 5.4 KB
Line 
1# Copyright (C) 2016, 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
9from __future__ import unicode_literals
10
11import unittest
12
13import ftputil.session
14
15
16class MockSession(object):
17    """
18    Mock session base class to determine if all expected calls
19    have happened.
20    """
21
22    def __init__(self):
23        self.calls = []
24
25    def add_call(self, *args):
26        self.calls.append(args)
27
28    def connect(self, host, port):
29        self.add_call("connect", host, port)
30
31    def _fix_socket(self):
32        self.add_call("_fix_socket")
33
34    def set_debuglevel(self, value):
35        self.add_call("set_debuglevel", value)
36
37    def login(self, user, password):
38        self.add_call("login", user, password)
39
40    def set_pasv(self, flag):
41        self.add_call("set_pasv", flag)
42
43
44class EncryptedMockSession(MockSession):
45
46    def auth_tls(self):
47        self.add_call("auth_tls")
48
49    def prot_p(self):
50        self.add_call("prot_p")
51
52
53class TestSessionFactory(unittest.TestCase):
54    """
55    Test if session factories created by
56    `ftputil.session.session_factory` trigger the expected calls.
57    """
58
59    def test_defaults(self):
60        """Test defaults (apart from base class)."""
61        factory = \
62          ftputil.session.session_factory(base_class=MockSession)
63        session = factory("host", "user", "password")
64        assert (session.calls ==
65                [("connect", "host", 21), ("login", "user", "password")])
66
67    def test_different_port(self):
68        """Test setting the command channel port with `port`."""
69        factory = \
70          ftputil.session.session_factory(base_class=MockSession, port=2121)
71        session = factory("host", "user", "password")
72        assert (session.calls ==
73                [("connect", "host", 2121), ("login", "user", "password")])
74
75    def test_use_passive_mode(self):
76        """
77        Test explicitly setting passive/active mode with
78        `use_passive_mode`.
79        """
80        # Passive mode
81        factory = ftputil.session.session_factory(base_class=MockSession,
82                                                  use_passive_mode=True)
83        session = factory("host", "user", "password")
84        assert session.calls == [("connect", "host", 21),
85                                 ("login", "user", "password"),
86                                 ("set_pasv", True)]
87        # Active mode
88        factory = ftputil.session.session_factory(base_class=MockSession,
89                                                  use_passive_mode=False)
90        session = factory("host", "user", "password")
91        assert session.calls == [("connect", "host", 21),
92                                 ("login", "user", "password"),
93                                 ("set_pasv", False)]
94
95    def test_encrypt_data_channel(self):
96        """Test request to call `prot_p` with `encrypt_data_channel`."""
97        # With encrypted data channel (default for encrypted session).
98        factory = ftputil.session.session_factory(
99                    base_class=EncryptedMockSession)
100        session = factory("host", "user", "password")
101        assert session.calls == [("connect", "host", 21),
102                                 ("login", "user", "password"),
103                                 ("prot_p",)]
104        #
105        factory = ftputil.session.session_factory(
106                    base_class=EncryptedMockSession, encrypt_data_channel=True)
107        session = factory("host", "user", "password")
108        assert session.calls == [("connect", "host", 21),
109                                 ("login", "user", "password"),
110                                 ("prot_p",)]
111        # Without encrypted data channel.
112        factory = ftputil.session.session_factory(
113                    base_class=EncryptedMockSession, encrypt_data_channel=False)
114        session = factory("host", "user", "password")
115        assert session.calls == [("connect", "host", 21),
116                                 ("login", "user", "password")]
117
118    def test_debug_level(self):
119        """Test setting the debug level on the session."""
120        factory = ftputil.session.session_factory(base_class=MockSession,
121                                                  debug_level=1)
122        session = factory("host", "user", "password")
123        assert session.calls == [("connect", "host", 21),
124                                 ("set_debuglevel", 1),
125                                 ("login", "user", "password")]
126
127    def test_m2crypto_session(self):
128        """Test call sequence for M2Crypto session."""
129        factory = \
130          ftputil.session.session_factory(base_class=EncryptedMockSession)
131        # Return `True` to fake that this is a session deriving from
132        # `M2Crypto.ftpslib.FTP_TLS`.
133        factory._use_m2crypto_ftpslib = lambda self: True
134        # Override `_fix_socket` here, not in `MockSession`. Since
135        # the created session class _inherits_ from `MockSession`,
136        # it would override the `_fix_socket` there.
137        factory._fix_socket = lambda self: self.add_call("_fix_socket")
138        session = factory("host", "user", "password")
139        assert session.calls == [("connect", "host", 21),
140                                 ("auth_tls",),
141                                 ("_fix_socket",),
142                                 ("login", "user", "password"),
143                                 ("prot_p",)]
Note: See TracBrowser for help on using the repository browser.