source: test/test_session.py @ 1564:c5b353a1c23d

Last change on this file since 1564:c5b353a1c23d was 1564:c5b353a1c23d, checked in by Stefan Schwarzer <sschwarzer@…>, 6 years ago
List contributors in `doc/contributors.txt`. So far, individual files had copyright notices for contributors. However, this makes it difficult to properly adapt files in case of refactoring: If a piece of code is moved to another file, I would need to find out if this code was contributed by someone else and change the copyright notice in the target file accordingly. With the new approach, every file refers to the file `doc/contributors.txt`, which contains the names of contributors.
File size: 5.7 KB
Line 
1# Copyright (C) 2014, 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        self.assertEqual(session.calls, [("connect", "host", 21),
65                                         ("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        self.assertEqual(session.calls, [("connect", "host", 2121),
73                                         ("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        self.assertEqual(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        self.assertEqual(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        self.assertEqual(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        self.assertEqual(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        self.assertEqual(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        self.assertEqual(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        self.assertEqual(session.calls, [("connect", "host", 21),
140                                         ("auth_tls",),
141                                         ("_fix_socket",),
142                                         ("login", "user", "password"),
143                                         ("prot_p",)])
144
145
146if __name__ == "__main__":
147    unittest.main()
Note: See TracBrowser for help on using the repository browser.