Skip to content

Commit a2b108d

Browse files
committed
feat: Add unit tests
1 parent c7b1df5 commit a2b108d

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

grafana_api/datasource.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ class DatasourceLabelBasedAccessControl:
10741074
def __init__(self, grafana_api_model: APIModel):
10751075
self.grafana_api_model = grafana_api_model
10761076

1077-
def get_lbac_rules_for_datasoure(self, uid: str) -> list:
1077+
def get_lbac_rules_for_datasource(self, uid: str) -> list:
10781078
"""The method includes a functionality to get all datasource label based access control rules for team specified by the datasource uid
10791079
10801080
Args:
@@ -1106,7 +1106,7 @@ def get_lbac_rules_for_datasoure(self, uid: str) -> list:
11061106
logging.error("There is no uid defined.")
11071107
raise ValueError
11081108

1109-
def update_lbac_rules_for_datasoure(self, uid: str) -> dict:
1109+
def update_lbac_rules_for_datasource(self, uid: str) -> dict:
11101110
"""The method includes a functionality to enable the datasource cache specified by the datasource uid
11111111
11121112
Args:

tests/unittests/test_datasource.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
DatasourcePermissions,
1313
DatasourceLegacyPermissions,
1414
DatasourceQueryResourceCaching,
15+
DatasourceLabelBasedAccessControl,
1516
)
1617

1718

@@ -1022,3 +1023,63 @@ def test_update_datasource_cache_no_valid_result(self, call_the_api_mock):
10221023

10231024
with self.assertRaises(Exception):
10241025
datasource.update_datasource_cache("test", datasource_cache)
1026+
1027+
class DatasourceLabelBasedAccessControlTestCase(TestCase):
1028+
@patch("grafana_api.api.Api.call_the_api")
1029+
def test_get_lbac_rules_for_datasource(self, call_the_api_mock):
1030+
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
1031+
datasource: DatasourceLabelBasedAccessControl = DatasourceLabelBasedAccessControl(grafana_api_model=model)
1032+
1033+
call_the_api_mock.return_value = list([{"id": 1}])
1034+
1035+
self.assertEqual([{"id": 1}], datasource.get_lbac_rules_for_datasource("test"))
1036+
1037+
@patch("grafana_api.api.Api.call_the_api")
1038+
def test_get_lbac_rules_for_datasource_no_uid(self, call_the_api_mock):
1039+
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
1040+
datasource: DatasourceLabelBasedAccessControl = DatasourceLabelBasedAccessControl(grafana_api_model=model)
1041+
1042+
call_the_api_mock.return_value = None
1043+
1044+
with self.assertRaises(ValueError):
1045+
datasource.get_lbac_rules_for_datasource("")
1046+
1047+
@patch("grafana_api.api.Api.call_the_api")
1048+
def test_get_lbac_rules_for_datasource_no_valid_rules(self, call_the_api_mock):
1049+
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
1050+
datasource: DatasourceLabelBasedAccessControl = DatasourceLabelBasedAccessControl(grafana_api_model=model)
1051+
1052+
call_the_api_mock.return_value = None
1053+
1054+
with self.assertRaises(Exception):
1055+
datasource.get_lbac_rules_for_datasource("test")
1056+
1057+
@patch("grafana_api.api.Api.call_the_api")
1058+
def test_update_lbac_rules_for_datasource(self, call_the_api_mock):
1059+
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
1060+
datasource: DatasourceLabelBasedAccessControl = DatasourceLabelBasedAccessControl(grafana_api_model=model)
1061+
1062+
call_the_api_mock.return_value = dict({"dataSourceID": 1})
1063+
1064+
self.assertEqual({"dataSourceID": 1}, datasource.update_lbac_rules_for_datasource("test"))
1065+
1066+
@patch("grafana_api.api.Api.call_the_api")
1067+
def test_update_lbac_rules_for_datasource_no_uid(self, call_the_api_mock):
1068+
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
1069+
datasource: DatasourceLabelBasedAccessControl = DatasourceLabelBasedAccessControl(grafana_api_model=model)
1070+
1071+
call_the_api_mock.return_value = None
1072+
1073+
with self.assertRaises(ValueError):
1074+
datasource.update_lbac_rules_for_datasource("")
1075+
1076+
@patch("grafana_api.api.Api.call_the_api")
1077+
def test_update_lbac_rules_for_datasource_no_update_possible(self, call_the_api_mock):
1078+
model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
1079+
datasource: DatasourceLabelBasedAccessControl = DatasourceLabelBasedAccessControl(grafana_api_model=model)
1080+
1081+
call_the_api_mock.return_value = dict()
1082+
1083+
with self.assertRaises(Exception):
1084+
datasource.update_lbac_rules_for_datasource("test")
1085+

0 commit comments

Comments
 (0)