diff --git a/grafana_api/user.py b/grafana_api/user.py index 1b97c07..48576c8 100644 --- a/grafana_api/user.py +++ b/grafana_api/user.py @@ -275,7 +275,7 @@ def get_user_teams(self, id: int) -> list: f"{APIEndpoints.USERS.value}/{id}/teams", ) - if api_call == list() or api_call[0].get("id") is None: + if api_call != list() and api_call[0].get("id") is None: logging.error(f"Check the error: {api_call}.") raise Exception else: @@ -459,7 +459,7 @@ def get_user_teams(self) -> list: f"{APIEndpoints.USER.value}/teams", ) - if api_call == list() or api_call[0].get("id") is None: + if api_call != list() and api_call[0].get("id") is None: logging.error(f"Check the error: {api_call}.") raise Exception else: diff --git a/tests/unittests/test_user.py b/tests/unittests/test_user.py index f591410..b008796 100644 --- a/tests/unittests/test_user.py +++ b/tests/unittests/test_user.py @@ -274,6 +274,17 @@ def test_get_user_teams_no_teams(self, call_the_api_mock): call_the_api_mock.return_value = list() + self.assertEqual(list(), user.get_user_teams(1)) + + @patch("grafana_api.api.Api.call_the_api") + def test_get_user_teams_invalid_teams(self, call_the_api_mock): + model: APIModel = APIModel( + host=MagicMock(), username=MagicMock(), password=MagicMock() + ) + user: User = User(grafana_api_model=model) + + call_the_api_mock.return_value = list([{"id": None}]) + with self.assertRaises(Exception): user.get_user_teams(1) @@ -430,6 +441,15 @@ def test_get_user_teams_no_teams(self, call_the_api_mock): call_the_api_mock.return_value = list() + self.assertEqual(list(), current_user.get_user_teams()) + + @patch("grafana_api.api.Api.call_the_api") + def test_get_user_teams_invalid_teams(self, call_the_api_mock): + model: APIModel = APIModel(host=MagicMock(), token=MagicMock()) + current_user: CurrentUser = CurrentUser(grafana_api_model=model) + + call_the_api_mock.return_value = list([{"id": None}]) + with self.assertRaises(Exception): current_user.get_user_teams()