4
4
5
5
"""tests that verify the connect behavior w.r.t. port number and TLS"""
6
6
7
+ import pytest
7
8
import socket
8
9
import ssl
9
- from unittest import TestCase , main
10
10
from unittest .mock import Mock , call , patch
11
11
12
12
import adafruit_minimqtt .adafruit_minimqtt as MQTT
13
13
14
14
15
- class PortSslSetup ( TestCase ) :
15
+ class TestPortSslSetup :
16
16
"""This class contains tests that verify how host/port and TLS is set for connect().
17
17
These tests assume that there is no MQTT broker running on the hosts/ports they connect to.
18
18
"""
@@ -35,7 +35,7 @@ def test_default_port(self) -> None:
35
35
ssl_mock = Mock ()
36
36
ssl_context .wrap_socket = ssl_mock
37
37
38
- with self . assertRaises (MQTT .MMQTTException ):
38
+ with pytest . raises (MQTT .MMQTTException ):
39
39
mqtt_client .connect ()
40
40
41
41
ssl_mock .assert_not_called ()
@@ -58,51 +58,51 @@ def test_connect_override(self):
58
58
connect_retries = 1 ,
59
59
)
60
60
61
- with self . assertRaises (MQTT .MMQTTException ):
61
+ with pytest . raises (MQTT .MMQTTException ):
62
62
expected_host = "127.0.0.2"
63
63
expected_port = 1884
64
- self . assertNotEqual ( expected_port , port , " port override should differ" )
65
- self . assertNotEqual ( expected_host , host , " host override should differ" )
64
+ assert expected_port != port # port override should differ
65
+ assert expected_host != host # host override should differ
66
66
mqtt_client .connect (host = expected_host , port = expected_port )
67
67
68
68
connect_mock .assert_called ()
69
69
# Assuming the repeated calls will have the same arguments.
70
70
connect_mock .assert_has_calls ([call ((expected_host , expected_port ))])
71
71
72
- def test_tls_port (self ) -> None :
72
+ @pytest .mark .parametrize ("port" , (None , 8883 ))
73
+ def test_tls_port (self , port ) -> None :
73
74
"""verify that when is_ssl=True is set, the default port is 8883
74
75
and the socket is TLS wrapped. Also test that the TLS port can be overridden."""
75
76
host = "127.0.0.1"
76
77
77
- for port in [None , 8884 ]:
78
- if port is None :
79
- expected_port = 8883
80
- else :
81
- expected_port = port
82
- with self .subTest ():
83
- ssl_mock = Mock ()
84
- mqtt_client = MQTT .MQTT (
85
- broker = host ,
86
- port = port ,
87
- socket_pool = socket ,
88
- is_ssl = True ,
89
- ssl_context = ssl_mock ,
90
- connect_retries = 1 ,
91
- )
92
-
93
- socket_mock = Mock ()
94
- connect_mock = Mock (side_effect = OSError )
95
- socket_mock .connect = connect_mock
96
- ssl_mock .wrap_socket = Mock (return_value = socket_mock )
97
-
98
- with self .assertRaises (MQTT .MMQTTException ):
99
- mqtt_client .connect ()
100
-
101
- ssl_mock .wrap_socket .assert_called ()
102
-
103
- connect_mock .assert_called ()
104
- # Assuming the repeated calls will have the same arguments.
105
- connect_mock .assert_has_calls ([call ((host , expected_port ))])
78
+ if port is None :
79
+ expected_port = 8883
80
+ else :
81
+ expected_port = port
82
+
83
+ ssl_mock = Mock ()
84
+ mqtt_client = MQTT .MQTT (
85
+ broker = host ,
86
+ port = port ,
87
+ socket_pool = socket ,
88
+ is_ssl = True ,
89
+ ssl_context = ssl_mock ,
90
+ connect_retries = 1 ,
91
+ )
92
+
93
+ socket_mock = Mock ()
94
+ connect_mock = Mock (side_effect = OSError )
95
+ socket_mock .connect = connect_mock
96
+ ssl_mock .wrap_socket = Mock (return_value = socket_mock )
97
+
98
+ with pytest .raises (MQTT .MMQTTException ):
99
+ mqtt_client .connect ()
100
+
101
+ ssl_mock .wrap_socket .assert_called ()
102
+
103
+ connect_mock .assert_called ()
104
+ # Assuming the repeated calls will have the same arguments.
105
+ connect_mock .assert_has_calls ([call ((host , expected_port ))])
106
106
107
107
def test_tls_without_ssl_context (self ) -> None :
108
108
"""verify that when is_ssl=True is set, the code will check that ssl_context is not None"""
@@ -116,10 +116,6 @@ def test_tls_without_ssl_context(self) -> None:
116
116
connect_retries = 1 ,
117
117
)
118
118
119
- with self . assertRaises (AttributeError ) as context :
119
+ with pytest . raises (AttributeError ) as context :
120
120
mqtt_client .connect ()
121
- self .assertTrue ("ssl_context must be set" in str (context ))
122
-
123
-
124
- if __name__ == "__main__" :
125
- main ()
121
+ assert "ssl_context must be set" in str (context )
0 commit comments