Skip to content

Commit 2837760

Browse files
authored
Solve bandit security in test. (#854)
1 parent 0e52fe8 commit 2837760

File tree

5 files changed

+152
-155
lines changed

5 files changed

+152
-155
lines changed

test/asyncio_test_helper.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ def my_coro_under_test():
3232
def test_it(mock_sleep):
3333
mock_sleep.side_effect = return_as_coroutine()
3434
result = run_coroutine(my_coro_under_test)
35-
assert mock_sleep.call_count == 2
36-
assert mock_sleep.call_args_list == [mock.call(1), mock.call(2)]
37-
assert result == 42
3835
"""
3936
return functools.partial(_yielded_return, return_value)
4037

test/test_client_async.py

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,24 @@ def test_tcp_tornado_client(self, mock_iostream, mock_ioloop): # pylint: disable
7373
protocol, future = AsyncModbusTCPClient( # pylint: disable=unpacking-non-sequence
7474
schedulers.IO_LOOP, framer=ModbusSocketFramer(ClientDecoder()))
7575
client = future.result()
76-
assert isinstance(client, AsyncTornadoModbusTcpClient)
77-
assert not list(client.transaction)
78-
assert isinstance(client.framer, ModbusSocketFramer)
79-
assert client.port == 502
80-
assert client._connected # pylint: disable=protected-access
81-
assert client.stream.connect.call_count == 1
82-
assert client.stream.read_until_close.call_count == 1
76+
assert isinstance(client, AsyncTornadoModbusTcpClient) #nosec
77+
assert not list(client.transaction) #nosec
78+
assert isinstance(client.framer, ModbusSocketFramer) #nosec
79+
assert client.port == 502 #nosec
80+
assert client._connected #nosec pylint: disable=protected-access
81+
assert client.stream.connect.call_count == 1 #nosec
82+
assert client.stream.read_until_close.call_count == 1 #nosec
8383

8484
def handle_failure(failure):
85-
assert isinstance(failure.exception(), ConnectionException)
85+
assert isinstance(failure.exception(), ConnectionException) #nosec
8686

8787
response = client._build_response(0x00) # pylint: disable=protected-access
8888
response.add_done_callback(handle_failure)
8989

90-
assert client._connected # pylint: disable=protected-access
90+
assert client._connected #nosec pylint: disable=protected-access
9191
client.close()
9292
protocol.stop()
93-
assert not client._connected # pylint: disable=protected-access
93+
assert not client._connected #nosec pylint: disable=protected-access
9494

9595

9696
@patch("asyncio.get_event_loop")
@@ -107,13 +107,13 @@ def test_tcp_asyncio_client(self, mock_gather, mock_loop): # pylint: disable=no-
107107
def test_tls_asyncio_client(self): # pylint: disable=no-self-use
108108
""" Test the TLS AsyncIO client. """
109109
_, client = AsyncModbusTLSClient(schedulers.ASYNC_IO) #NOSONAR pylint: disable=unpacking-non-sequence
110-
assert isinstance(client, ReconnectingAsyncioModbusTlsClient)
111-
assert isinstance(client.framer, ModbusTlsFramer)
112-
assert isinstance(client.sslctx, ssl.SSLContext)
113-
assert client.port == 802
110+
assert isinstance(client, ReconnectingAsyncioModbusTlsClient) #nosec
111+
assert isinstance(client.framer, ModbusTlsFramer) #nosec
112+
assert isinstance(client.sslctx, ssl.SSLContext) #nosec
113+
assert client.port == 802 #nosec
114114

115115
client.stop()
116-
assert client.host is None
116+
assert client.host is None #nosec
117117

118118
# -----------------------------------------------------------------------#
119119
# Test UDP client
@@ -126,22 +126,22 @@ def test_udp_tornado_client(self, mock_iostream, mock_ioloop): # pylint: disable
126126
protocol, future = AsyncModbusUDPClient( #NOSONAR pylint: disable=unpacking-non-sequence
127127
schedulers.IO_LOOP, framer=ModbusSocketFramer(ClientDecoder()))
128128
client = future.result()
129-
assert isinstance(client, AsyncTornadoModbusUdoClient)
130-
assert not list(client.transaction)
131-
assert isinstance(client.framer, ModbusSocketFramer)
132-
assert client.port == 502
133-
assert client._connected # pylint: disable=protected-access
129+
assert isinstance(client, AsyncTornadoModbusUdoClient) #nosec
130+
assert not list(client.transaction) #nosec
131+
assert isinstance(client.framer, ModbusSocketFramer) #nosec
132+
assert client.port == 502 #nosec
133+
assert client._connected #nosec pylint: disable=protected-access
134134

135135
def handle_failure(failure):
136-
assert isinstance(failure.exception(), ConnectionException)
136+
assert isinstance(failure.exception(), ConnectionException) #nosec
137137

138138
response = client._build_response(0x00) # pylint: disable=protected-access
139139
response.add_done_callback(handle_failure)
140140

141-
assert client._connected # pylint: disable=protected-access
141+
assert client._connected #nosec pylint: disable=protected-access
142142
client.close()
143143
protocol.stop()
144-
assert not client._connected # pylint: disable=protected-access
144+
assert not client._connected #nosec pylint: disable=protected-access
145145

146146
def test_udp_twisted_client(self): # pylint: disable=no-self-use
147147
""" Test the udp twisted client client initialize """
@@ -180,22 +180,22 @@ def test_serial_twisted_client(self, method, framer): # pylint: disable=no-self-
180180
port=pytest.SERIAL_PORT,
181181
proto_cls=ModbusSerClientProtocol)
182182

183-
assert isinstance(client, SerialPort)
184-
assert isinstance(client.protocol, ModbusSerClientProtocol)
185-
assert not list(client.protocol.transaction)
186-
assert isinstance(client.protocol.framer, framer)
187-
assert client.protocol._connected # pylint: disable=protected-access
183+
assert isinstance(client, SerialPort) #nosec
184+
assert isinstance(client.protocol, ModbusSerClientProtocol) #nosec
185+
assert not list(client.protocol.transaction) #nosec
186+
assert isinstance(client.protocol.framer, framer) #nosec
187+
assert client.protocol._connected #nosec pylint: disable=protected-access
188188

189189
def handle_failure(failure):
190-
assert (isinstance(failure.exception(), ConnectionException))
190+
assert (isinstance(failure.exception(), ConnectionException)) #nosec
191191

192192
response = client.protocol._buildResponse(0x00) # pylint: disable=protected-access
193193
response.addCallback(handle_failure)
194194

195-
assert client.protocol._connected # pylint: disable=protected-access
195+
assert client.protocol._connected #nosec pylint: disable=protected-access
196196
client.protocol.close()
197197
protocol.stop()
198-
assert not client.protocol._connected # pylint: disable=protected-access
198+
assert not client.protocol._connected #nosec pylint: disable=protected-access
199199

200200
@pytest.mark.parametrize("method, framer", [("rtu", ModbusRtuFramer),
201201
("socket", ModbusSocketFramer),
@@ -207,22 +207,22 @@ def test_serial_tornado_client(self, method, framer): # pylint: disable=no-self-
207207
protocol, future = AsyncModbusSerialClient( #NOSONAR pylint: disable=unpacking-non-sequence
208208
schedulers.IO_LOOP, method=method, port=pytest.SERIAL_PORT)
209209
client = future.result()
210-
assert isinstance(client, AsyncTornadoModbusSerialClient)
211-
assert not list(client.transaction)
212-
assert isinstance(client.framer, framer)
213-
assert client.port == pytest.SERIAL_PORT
214-
assert client._connected # pylint: disable=protected-access
210+
assert isinstance(client, AsyncTornadoModbusSerialClient) #nosec
211+
assert not list(client.transaction) #nosec
212+
assert isinstance(client.framer, framer) #nosec
213+
assert client.port == pytest.SERIAL_PORT #nosec
214+
assert client._connected #nosec pylint: disable=protected-access
215215

216216
def handle_failure(failure):
217-
assert isinstance(failure.exception(), ConnectionException)
217+
assert isinstance(failure.exception(), ConnectionException) #nosec
218218

219219
response = client._build_response(0x00) # pylint: disable=protected-access
220220
response.add_done_callback(handle_failure)
221221

222-
assert client._connected # pylint: disable=protected-access
222+
assert client._connected #nosec pylint: disable=protected-access
223223
client.close()
224224
protocol.stop()
225-
assert not client._connected # pylint: disable=protected-access
225+
assert not client._connected #nosec pylint: disable=protected-access
226226

227227
@patch("asyncio.get_event_loop")
228228
@patch("asyncio.gather", side_effect=mock_asyncio_gather)
@@ -239,13 +239,13 @@ def test_serial_asyncio_client(self, mock_gather, mock_event_loop, method, frame
239239
loop, client = AsyncModbusSerialClient( #NOSONAR pylint: disable=unpacking-non-sequence
240240
schedulers.ASYNC_IO, method=method, port=pytest.SERIAL_PORT, loop=loop,
241241
baudrate=19200, parity='E', stopbits=2, bytesize=7)
242-
assert isinstance(client, AsyncioModbusSerialClient)
243-
assert isinstance(client.framer, framer)
244-
assert client.port == pytest.SERIAL_PORT
245-
assert client.baudrate == 19200
246-
assert client.parity == 'E'
247-
assert client.stopbits == 2
248-
assert client.bytesize == 7
242+
assert isinstance(client, AsyncioModbusSerialClient) #nosec
243+
assert isinstance(client.framer, framer) #nosec
244+
assert client.port == pytest.SERIAL_PORT #nosec
245+
assert client.baudrate == 19200 #nosec
246+
assert client.parity == 'E' #nosec
247+
assert client.stopbits == 2 #nosec
248+
assert client.bytesize == 7 #nosec
249249
client.stop()
250250
loop.stop()
251251

0 commit comments

Comments
 (0)