Skip to content

Commit 384f37c

Browse files
committed
TT-98 feat: Add test to feature toggle manager file
1 parent 71e7ed4 commit 384f37c

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
lines changed

commons/feature_toggles/feature_toggle_manager.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def __init__(self, key: str, label: str = None):
1414
self.label = label
1515
self.configuration = {}
1616

17-
def _get_configuration(self, key: str, label: str):
17+
def get_configuration(self, key: str, label: str):
1818
connection_str = self.AZURE_CONNECTION_STRING
1919
client = AzureAppConfigurationClient.from_connection_string(
2020
connection_str
@@ -25,27 +25,27 @@ def _get_configuration(self, key: str, label: str):
2525

2626
return configuration
2727

28-
def _get_data_configuration(self):
29-
self.configuration = self._get_configuration(self.key, self.label)
28+
def get_data_configuration(self):
29+
self.configuration = self.get_configuration(self.key, self.label)
3030
result = json.loads(self.configuration.value)
3131

3232
return result
3333

34-
def _is_toggle_enabled(self):
35-
data = self._get_data_configuration()
34+
def is_toggle_enabled(self):
35+
data = self.get_data_configuration()
3636
result = data["enabled"]
3737

3838
return result
3939

4040
def is_toggle_enabled_for_user(self):
41-
data = self._get_data_configuration()
41+
data = self.get_data_configuration()
4242
client_filters = data["conditions"]["client_filters"]
4343
first_client = client_filters[0]
4444
list_users = first_client["parameters"]["Audience"]["Users"]
4545
current_user = current_user_email()
4646

4747
return (
4848
True
49-
if current_user in list_users and self._is_toggle_enabled()
49+
if current_user in list_users and self.is_toggle_enabled()
5050
else False
5151
)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from commons.feature_toggles.feature_toggle_manager import FeatureToggleManager
2+
from unittest.mock import Mock, patch
3+
from pytest import mark
4+
5+
6+
def mock_payload(enabled, user):
7+
return {
8+
"id": "test-feature-toggle",
9+
"description": "Feature Toggle test Backend",
10+
"enabled": enabled,
11+
"conditions": {
12+
"client_filters": [
13+
{
14+
"name": "Microsoft.Targeting",
15+
"parameters": {
16+
"Audience": {
17+
"Users": [user],
18+
"Groups": [],
19+
"DefaultRolloutPercentage": 50,
20+
}
21+
},
22+
}
23+
]
24+
},
25+
}
26+
27+
28+
@patch(
29+
'azure.appconfiguration.AzureAppConfigurationClient.from_connection_string',
30+
new_callable=Mock,
31+
)
32+
@patch(
33+
'commons.feature_toggles.feature_toggle_manager.FeatureToggleManager.get_data_configuration',
34+
new_callable=Mock,
35+
)
36+
@patch('commons.feature_toggles.feature_toggle_manager.current_user_email')
37+
@mark.parametrize(
38+
'user_email_enabled,currrent_user_email,is_toggle_enabled,expected_result',
39+
[
40+
41+
('[email protected]', '[email protected]', False, False),
42+
43+
('[email protected]', '[email protected]', False, False),
44+
],
45+
)
46+
def test_if_is_toggle_enabled_for_user(
47+
current_user_email_mock,
48+
get_data_configuration_mock,
49+
from_connection_string_mock,
50+
user_email_enabled,
51+
currrent_user_email,
52+
is_toggle_enabled,
53+
expected_result,
54+
):
55+
current_user_email_mock.return_value = currrent_user_email
56+
feature_toggle_manager = FeatureToggleManager("test-feature-toggle")
57+
feature_toggle_manager.get_data_configuration.return_value = mock_payload(
58+
is_toggle_enabled, user_email_enabled
59+
)
60+
61+
assert (
62+
feature_toggle_manager.is_toggle_enabled_for_user() == expected_result
63+
)

0 commit comments

Comments
 (0)