Skip to content

Commit 6607451

Browse files
added get_app test
1 parent 8ed518b commit 6607451

File tree

1 file changed

+124
-1
lines changed

1 file changed

+124
-1
lines changed

test/test_chirpstack_client.py

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,129 @@ def test_list_all_tenants_unauthenticated_grpc_error(self, mock_sleep, mock_inse
340340
# Assert the result
341341
self.assertEqual(tenants, ["Tenant1", "Tenant1"])
342342

343+
class TestGetApp(unittest.TestCase):
344+
345+
@patch('chirpstack_api_wrapper.api.ApplicationServiceStub')
346+
@patch('chirpstack_api_wrapper.grpc.insecure_channel')
347+
def test_get_app_happy_path(self, mock_insecure_channel, mock_app_stub):
348+
"""
349+
Test get_app() method's happy path
350+
"""
351+
# Mock the gRPC channel and login response
352+
mock_channel = Mock()
353+
mock_insecure_channel.return_value = mock_channel
354+
355+
# Mock the ApplicationServiceStub
356+
mock_app_stub_instance = mock_app_stub.return_value
357+
mock_app_stub_instance.Get.return_value = Mock(app_info="mock_app_info")
358+
359+
# Create a ChirpstackClient instance
360+
client = ChirpstackClient(CHIRPSTACK_ACT_EMAIL, CHIRPSTACK_ACT_PASSWORD, CHIRPSTACK_API_INTERFACE)
361+
362+
# Mock the app ID
363+
mock_app_id = "mock_app_id"
364+
365+
# Call get_app
366+
app_info = client.get_app(mock_app_id)
367+
368+
# Assert the result
369+
self.assertEqual(app_info.app_info, "mock_app_info")
370+
371+
@patch('chirpstack_api_wrapper.api.ApplicationServiceStub')
372+
@patch('chirpstack_api_wrapper.grpc.insecure_channel')
373+
@patch("chirpstack_api_wrapper.time.sleep", return_value=None) #dont time.sleep() for test case
374+
def test_get_app_unauthenticated_grpc_error(self, mock_sleep, mock_insecure_channel, mock_app_service_stub):
375+
"""
376+
Test get_app() when grpc error is raised for UNAUTHENTICATED and token needs to be refreshed
377+
"""
378+
# Mock the gRPC channel and login response
379+
mock_channel = Mock()
380+
mock_insecure_channel.return_value = mock_channel
381+
382+
# Mock the get_device method to raise grpc.RpcError()
383+
mock_rpc_error = grpc.RpcError()
384+
mock_rpc_error.code = lambda: grpc.StatusCode.UNAUTHENTICATED
385+
mock_rpc_error.details = lambda: 'ExpiredSignature'
386+
387+
# Mock the ApplicationServiceStub
388+
mock_app_service_stub_instance = mock_app_service_stub.return_value
389+
mock_app_service_stub_instance.Get.side_effect = mock_rpc_error
390+
391+
# Create a ChirpstackClient instance
392+
client = ChirpstackClient(CHIRPSTACK_ACT_EMAIL, CHIRPSTACK_ACT_PASSWORD, CHIRPSTACK_API_INTERFACE)
393+
394+
# Mock the app ID
395+
mock_app_id = "mock_app_id"
396+
397+
# Mock the login method to return a dummy token
398+
with patch.object(client, "login", return_value="dummy_token"):
399+
400+
#mock refresh token successfully logging in and retrying the method in testing
401+
with patch.object(client, "refresh_token", return_value="mock_app_info"):
402+
# Call the method in testing
403+
app_info = client.get_app(mock_app_id)
404+
405+
# assertations
406+
self.assertEqual(app_info, "mock_app_info")
407+
408+
@patch('chirpstack_api_wrapper.api.ApplicationServiceStub')
409+
@patch('chirpstack_api_wrapper.grpc.insecure_channel')
410+
def test_get_app_not_found_grpc_error(self, mock_insecure_channel, mock_app_service_stub):
411+
"""
412+
Test get_app() when grpc error is raised for NOT_FOUND
413+
"""
414+
# Mock the gRPC channel and login response
415+
mock_channel = Mock()
416+
mock_insecure_channel.return_value = mock_channel
417+
418+
# Mock the get_device method to raise grpc.RpcError()
419+
mock_rpc_error = grpc.RpcError()
420+
mock_rpc_error.code = lambda: grpc.StatusCode.NOT_FOUND
421+
mock_rpc_error.details = lambda: 'Object does not exist'
422+
423+
# Mock the ApplicationServiceStub
424+
mock_app_service_stub_instance = mock_app_service_stub.return_value
425+
mock_app_service_stub_instance.Get.side_effect = mock_rpc_error
426+
427+
# Create a ChirpstackClient instance
428+
client = ChirpstackClient(CHIRPSTACK_ACT_EMAIL, CHIRPSTACK_ACT_PASSWORD, CHIRPSTACK_API_INTERFACE)
429+
430+
# Mock the app ID
431+
mock_app_id = "mock_app_id"
432+
433+
result = client.get_app(mock_app_id)
434+
435+
self.assertEqual(result, {})
436+
437+
@patch('chirpstack_api_wrapper.api.ApplicationServiceStub')
438+
@patch('chirpstack_api_wrapper.grpc.insecure_channel')
439+
def test_get_app_other_grpc_error(self, mock_insecure_channel, mock_app_service_stub):
440+
"""
441+
Test get_app() when grpc error is not NOT_FOUND or UNAUTHENTICATED
442+
"""
443+
# Mock the gRPC channel and login response
444+
mock_channel = Mock()
445+
mock_insecure_channel.return_value = mock_channel
446+
447+
# Mock the get_device method to raise grpc.RpcError()
448+
mock_rpc_error = grpc.RpcError()
449+
mock_rpc_error.code = lambda: grpc.StatusCode.INTERNAL
450+
mock_rpc_error.details = lambda: 'other'
451+
452+
# Mock the ApplicationServiceStub
453+
mock_app_service_stub_instance = mock_app_service_stub.return_value
454+
mock_app_service_stub_instance.Get.side_effect = mock_rpc_error
455+
456+
# Create a ChirpstackClient instance
457+
client = ChirpstackClient(CHIRPSTACK_ACT_EMAIL, CHIRPSTACK_ACT_PASSWORD, CHIRPSTACK_API_INTERFACE)
458+
459+
# Mock the app ID
460+
mock_app_id = "mock_app_id"
461+
462+
result = client.get_app(mock_app_id)
463+
464+
self.assertEqual(result, {})
465+
343466
class TestGetDevice(unittest.TestCase):
344467

345468
@patch('chirpstack_api_wrapper.api.DeviceServiceStub')
@@ -560,7 +683,7 @@ def test_get_device_profile_not_found_grpc_error(self, mock_insecure_channel, mo
560683

561684
@patch('chirpstack_api_wrapper.api.DeviceProfileServiceStub')
562685
@patch('chirpstack_api_wrapper.grpc.insecure_channel')
563-
def test_get_device_other_grpc_error(self, mock_insecure_channel, mock_device_profile_service_stub):
686+
def test_get_device_profile_other_grpc_error(self, mock_insecure_channel, mock_device_profile_service_stub):
564687
"""
565688
Test get_device_profile() when grpc error is not NOT_FOUND or UNAUTHENTICATED
566689
"""

0 commit comments

Comments
 (0)