source: test/test_session.py

Last change on this file was 1935:38acf28f3905, checked in by Stefan Schwarzer <sschwarzer@…>, 6 months ago
Format docstrings and comments Reformat docstrings and comments to 80 characters. Traditionally, the margin for the code was at 79 columns, so I chose a bit less for the comments. With the switch to Black for formatting the right margin is now at nearly 90 columns, so it makes sense to also allow a greater right margin for docstrings and comments. Lines still don't get too long because the formatted text often starts only in column 5 or 9.
File size: 4.1 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 have happened.
15    """
16
17    def __init__(self):
18        self.calls = []
19
20    def add_call(self, *args):
21        self.calls.append(args)
22
23    def connect(self, host, port):
24        self.add_call("connect", host, port)
25
26    def set_debuglevel(self, value):
27        self.add_call("set_debuglevel", value)
28
29    def login(self, user, password):
30        self.add_call("login", user, password)
31
32    def set_pasv(self, flag):
33        self.add_call("set_pasv", flag)
34
35
36class EncryptedMockSession(MockSession):
37    def auth_tls(self):
38        self.add_call("auth_tls")
39
40    def prot_p(self):
41        self.add_call("prot_p")
42
43
44class TestSessionFactory:
45    """
46    Test if session factories created by `ftputil.session.session_factory`
47    trigger the expected calls.
48    """
49
50    def test_defaults(self):
51        """
52        Test defaults (apart from base class).
53        """
54        factory = ftputil.session.session_factory(base_class=MockSession)
55        session = factory("host", "user", "password")
56        assert session.calls == [("connect", "host", 21), ("login", "user", "password")]
57
58    def test_different_port(self):
59        """
60        Test setting the command channel port with `port`.
61        """
62        factory = ftputil.session.session_factory(base_class=MockSession, port=2121)
63        session = factory("host", "user", "password")
64        assert session.calls == [
65            ("connect", "host", 2121),
66            ("login", "user", "password"),
67        ]
68
69    def test_use_passive_mode(self):
70        """
71        Test explicitly setting passive/active mode with `use_passive_mode`.
72        """
73        # Passive mode
74        factory = ftputil.session.session_factory(
75            base_class=MockSession, use_passive_mode=True
76        )
77        session = factory("host", "user", "password")
78        assert session.calls == [
79            ("connect", "host", 21),
80            ("login", "user", "password"),
81            ("set_pasv", True),
82        ]
83        # Active mode
84        factory = ftputil.session.session_factory(
85            base_class=MockSession, use_passive_mode=False
86        )
87        session = factory("host", "user", "password")
88        assert session.calls == [
89            ("connect", "host", 21),
90            ("login", "user", "password"),
91            ("set_pasv", False),
92        ]
93
94    def test_encrypt_data_channel(self):
95        """
96        Test request to call `prot_p` with `encrypt_data_channel`.
97        """
98        # With encrypted data channel (default for encrypted session).
99        factory = ftputil.session.session_factory(base_class=EncryptedMockSession)
100        session = factory("host", "user", "password")
101        assert session.calls == [
102            ("connect", "host", 21),
103            ("login", "user", "password"),
104            ("prot_p",),
105        ]
106        #
107        factory = ftputil.session.session_factory(
108            base_class=EncryptedMockSession, encrypt_data_channel=True
109        )
110        session = factory("host", "user", "password")
111        assert session.calls == [
112            ("connect", "host", 21),
113            ("login", "user", "password"),
114            ("prot_p",),
115        ]
116        # Without encrypted data channel.
117        factory = ftputil.session.session_factory(
118            base_class=EncryptedMockSession, encrypt_data_channel=False
119        )
120        session = factory("host", "user", "password")
121        assert session.calls == [("connect", "host", 21), ("login", "user", "password")]
122
123    def test_debug_level(self):
124        """
125        Test setting the debug level on the session.
126        """
127        factory = ftputil.session.session_factory(base_class=MockSession, debug_level=1)
128        session = factory("host", "user", "password")
129        assert session.calls == [
130            ("connect", "host", 21),
131            ("set_debuglevel", 1),
132            ("login", "user", "password"),
133        ]
Note: See TracBrowser for help on using the repository browser.