Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
TT-98 feat: Add test to feature toggle manager file
  • Loading branch information
PaulRC-ioet committed Jan 8, 2021
commit 384f37c80b6c9f5f92ccda50dce51250813f99ac
14 changes: 7 additions & 7 deletions commons/feature_toggles/feature_toggle_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, key: str, label: str = None):
self.label = label
self.configuration = {}

def _get_configuration(self, key: str, label: str):
def get_configuration(self, key: str, label: str):
connection_str = self.AZURE_CONNECTION_STRING
client = AzureAppConfigurationClient.from_connection_string(
connection_str
Expand All @@ -25,27 +25,27 @@ def _get_configuration(self, key: str, label: str):

return configuration

def _get_data_configuration(self):
self.configuration = self._get_configuration(self.key, self.label)
def get_data_configuration(self):
self.configuration = self.get_configuration(self.key, self.label)
result = json.loads(self.configuration.value)

return result

def _is_toggle_enabled(self):
data = self._get_data_configuration()
def is_toggle_enabled(self):
data = self.get_data_configuration()
result = data["enabled"]

return result

def is_toggle_enabled_for_user(self):
data = self._get_data_configuration()
data = self.get_data_configuration()
client_filters = data["conditions"]["client_filters"]
first_client = client_filters[0]
list_users = first_client["parameters"]["Audience"]["Users"]
current_user = current_user_email()

return (
True
if current_user in list_users and self._is_toggle_enabled()
if current_user in list_users and self.is_toggle_enabled()
else False
)
63 changes: 63 additions & 0 deletions tests/commons/feature_toggles/feature_toggles_manager_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from commons.feature_toggles.feature_toggle_manager import FeatureToggleManager
from unittest.mock import Mock, patch
from pytest import mark


def mock_payload(enabled, user):
return {
"id": "test-feature-toggle",
"description": "Feature Toggle test Backend",
"enabled": enabled,
"conditions": {
"client_filters": [
{
"name": "Microsoft.Targeting",
"parameters": {
"Audience": {
"Users": [user],
"Groups": [],
"DefaultRolloutPercentage": 50,
}
},
}
]
},
}


@patch(
'azure.appconfiguration.AzureAppConfigurationClient.from_connection_string',
new_callable=Mock,
)
@patch(
'commons.feature_toggles.feature_toggle_manager.FeatureToggleManager.get_data_configuration',
new_callable=Mock,
)
@patch('commons.feature_toggles.feature_toggle_manager.current_user_email')
@mark.parametrize(
'user_email_enabled,currrent_user_email,is_toggle_enabled,expected_result',
[
('[email protected]', '[email protected]', True, True),
('[email protected]', '[email protected]', False, False),
('[email protected]', '[email protected]', True, False),
('[email protected]', '[email protected]', False, False),
],
)
def test_if_is_toggle_enabled_for_user(
current_user_email_mock,
get_data_configuration_mock,
from_connection_string_mock,
user_email_enabled,
currrent_user_email,
is_toggle_enabled,
expected_result,
):
current_user_email_mock.return_value = currrent_user_email
feature_toggle_manager = FeatureToggleManager("test-feature-toggle")
feature_toggle_manager.get_data_configuration.return_value = mock_payload(
is_toggle_enabled, user_email_enabled
)

assert (
feature_toggle_manager.is_toggle_enabled_for_user() == expected_result
)