Skip to content

Commit 1c136fe

Browse files
added a test for refresh_token()
1 parent c7c979b commit 1c136fe

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

test/test_chirpstack_client.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,41 @@ def test_refresh_token_get_device(self, mock_sleep, mock_insecure_channel, mock_
17211721
# assertations
17221722
self.assertEqual(result, client.get_device(mock_dev_eui))
17231723

1724+
@patch("chirpstack_api_wrapper.api.DeviceServiceStub")
1725+
@patch('chirpstack_api_wrapper.grpc.insecure_channel')
1726+
@patch("chirpstack_api_wrapper.time.sleep", return_value=None) #dont time.sleep() for test case
1727+
def test_refresh_token_get_device_no_login_on_init(self, mock_sleep, mock_insecure_channel, mock_device_service_stub):
1728+
"""
1729+
Test refresh token's happy path when used by ChirpstackClient.get_device() and the client did not try to log in on init
1730+
- refresh_token() should call get_device() after token is refreshed
1731+
"""
1732+
# Mock the gRPC channel and login response
1733+
mock_channel = Mock()
1734+
mock_insecure_channel.return_value = mock_channel
1735+
1736+
# Mock the DeviceServiceStub
1737+
mock_device_service_stub_instance = mock_device_service_stub.return_value
1738+
mock_device_service_stub_instance.Get.return_value = Mock(device_info="mock_device_info")
1739+
1740+
# Create a ChirpstackClient instance
1741+
client = ChirpstackClient(CHIRPSTACK_ACT_EMAIL, CHIRPSTACK_ACT_PASSWORD, CHIRPSTACK_API_INTERFACE,False)
1742+
1743+
# Mock the dev_eui
1744+
mock_dev_eui = "mock_dev_eui"
1745+
1746+
# Mock the login method to return a dummy token
1747+
with patch.object(client, "login", return_value="dummy_token"):
1748+
# Create a dummy gRPC error
1749+
dummy_error = Mock()
1750+
dummy_error.code.return_value = grpc.StatusCode.UNAUTHENTICATED
1751+
dummy_error.details.return_value = "InvalidToken"
1752+
1753+
# Call the refresh_token method
1754+
result = client.refresh_token(dummy_error, client.get_device, mock_dev_eui)
1755+
1756+
# assertations
1757+
self.assertEqual(result, client.get_device(mock_dev_eui))
1758+
17241759
@patch("chirpstack_api_wrapper.api.DeviceServiceStub")
17251760
@patch('chirpstack_api_wrapper.grpc.insecure_channel')
17261761
@patch("chirpstack_api_wrapper.time.sleep", return_value=None) #dont time.sleep() for test case

0 commit comments

Comments
 (0)