source: test/test_session.py @ 1494:30df847fea39

Last change on this file since 1494:30df847fea39 was 1494:30df847fea39, checked in by Stefan Schwarzer <sschwarzer@…>, 7 years ago
Added unit tests for `session_factory` helper. Most tests are done with a mock class, but there's also a test in `test_real_ftp.py` that will use `M2Crypto.ftpslib.FTP_TLS` to check if the workaround in the `Session` class in `ftputil.session.session_factory` works. Note that since M2Crypto isn't available for Python 3, the real FTP test is skipped for Python 3. (This requires the `unittest2` module or here the `unittest` in Python 2.7 and 3.3.)
File size: 4.7 KB
Line 
1# Copyright (C) 2014, Stefan Schwarzer
2
3"""
4Unit tests for session factory helpers.
5"""
6
7from __future__ import unicode_literals
8
9import unittest
10
11import ftputil.session
12
13
14class MockSession(object):
15    """
16    Mock session base class to determine if all expected calls
17    have happened.
18    """
19
20    def __init__(self):
21        self.calls = []
22   
23    def add_call(self, *args):
24        self.calls.append(args)
25
26    def connect(self, host, port):
27        self.add_call("connect", host, port)
28
29    def auth_tls(self):
30        self.add_call("auth_tls")
31
32    def _fix_socket(self):
33        self.add_call("_fix_socket")
34
35    def set_debuglevel(self, value):
36        self.add_call("set_debuglevel", value)
37
38    def login(self, user, password):
39        self.add_call("login", user, password)
40
41    def set_pasv(self, flag):
42        self.add_call("set_pasv", flag)
43
44    def prot_p(self):
45        self.add_call("prot_p")
46
47
48class TestSessionFactory(unittest.TestCase):
49    """
50    Test if session factories created by
51    `ftputil.session.session_factory` trigger the expected calls.
52    """
53
54    def test_defaults(self):
55        """Test defaults (apart from base class)."""
56        factory = \
57          ftputil.session.session_factory(base_class=MockSession)
58        session = factory("host", "user", "password")
59        self.assertEqual(session.calls, [("connect", "host", 21),
60                                         ("login", "user", "password")])
61
62    def test_different_port(self):
63        """Test setting the command channel port with `port`."""
64        factory = \
65          ftputil.session.session_factory(base_class=MockSession, port=2121)
66        session = factory("host", "user", "password")
67        self.assertEqual(session.calls, [("connect", "host", 2121),
68                                         ("login", "user", "password")])
69
70    def test_use_passive_mode(self):
71        """
72        Test explicitly setting passive/active mode with
73        `use_passive_mode`.
74        """
75        # Passive mode
76        factory = ftputil.session.session_factory(base_class=MockSession,
77                                                  use_passive_mode=True)
78        session = factory("host", "user", "password")
79        self.assertEqual(session.calls, [("connect", "host", 21),
80                                         ("login", "user", "password"),
81                                         ("set_pasv", True)])
82        # Active mode
83        factory = ftputil.session.session_factory(base_class=MockSession,
84                                                  use_passive_mode=False)
85        session = factory("host", "user", "password")
86        self.assertEqual(session.calls, [("connect", "host", 21),
87                                         ("login", "user", "password"),
88                                         ("set_pasv", False)])
89
90    def test_encrypt_data_channel(self):
91        """Test request to call `prot_p` with `encrypt_data_channel`."""
92        factory = ftputil.session.session_factory(base_class=MockSession,
93                                                  encrypt_data_channel=True)
94        session = factory("host", "user", "password")
95        self.assertEqual(session.calls, [("connect", "host", 21),
96                                         ("login", "user", "password"),
97                                         ("prot_p",)])
98
99    def test_debug_level(self):
100        """Test setting the debug level on the session."""
101        factory = ftputil.session.session_factory(base_class=MockSession,
102                                                  debug_level=1)
103        session = factory("host", "user", "password")
104        self.assertEqual(session.calls, [("connect", "host", 21),
105                                         ("set_debuglevel", 1),
106                                         ("login", "user", "password")])
107
108    def test_m2crypto_session(self):
109        """Test call sequence for M2Crypto session."""
110        factory = ftputil.session.session_factory(base_class=MockSession)
111        # Return `True` to fake that this is a session deriving from
112        # `M2Crypto.ftpslib.FTP_TLS`.
113        factory._use_m2crypto_ftpslib = lambda self: True
114        # Override `_fix_socket` here, not in `MockSession`. Since
115        # the created session class _inherits_ from `MockSession`,
116        # it would override the `_fix_socket` there.
117        factory._fix_socket = lambda self: self.add_call("_fix_socket")
118        session = factory("host", "user", "password")
119        self.assertEqual(session.calls, [("connect", "host", 21),
120                                         ("auth_tls",),
121                                         ("_fix_socket",),
122                                         ("login", "user", "password")])
123
124
125if __name__ == "__main__":
126    unittest.main()
Note: See TracBrowser for help on using the repository browser.