-
Notifications
You must be signed in to change notification settings - Fork 0
Tt 98 toogles backend #250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
89f6c51
TT-98 feat: add feature flags
PaulRC-ioet 71e7ed4
TT-98 fix: refactor feature toggle manager
PaulRC-ioet 384f37c
TT-98 feat: Add test to feature toggle manager file
PaulRC-ioet 6cde781
TT-98 fix: refactor code in feature toggles file
PaulRC-ioet f3e7109
TT-98 fix: upgrate version of azure core
PaulRC-ioet 036c8db
TT-98 fix: refactor comments made by Roberto
PaulRC-ioet 14b377d
TT-98 fix: remove import in time_entries_namespaces
PaulRC-ioet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import os | ||
| import json | ||
| from time_tracker_api.security import current_user_email | ||
| from azure.appconfiguration import AzureAppConfigurationClient | ||
|
|
||
|
|
||
| class MSConfig: | ||
| def check_variables_are_defined(): | ||
| auth_variables = ['AZURE_APP_CONFIGURATION_CONNECTION_STRING'] | ||
| for var in auth_variables: | ||
| if var not in os.environ: | ||
| raise EnvironmentError( | ||
| "{} is not defined in the environment".format(var) | ||
| ) | ||
|
|
||
| check_variables_are_defined() | ||
| AZURE_APP_CONFIGURATION_CONNECTION_STRING = os.environ.get( | ||
PaulRC-ioet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 'AZURE_APP_CONFIGURATION_CONNECTION_STRING' | ||
| ) | ||
|
|
||
|
|
||
| class FeatureToggleManager: | ||
| def __init__(self, key: str, label: str = None, config=MSConfig): | ||
| self.key = key | ||
| self.label = label | ||
| self.config = config | ||
| self.client = self.get_azure_app_configuration_client() | ||
| self.configuration = {} | ||
Angeluz-07 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def get_azure_app_configuration_client(self): | ||
| connection_str = self.config.AZURE_APP_CONFIGURATION_CONNECTION_STRING | ||
| client = AzureAppConfigurationClient.from_connection_string( | ||
| connection_str | ||
| ) | ||
|
|
||
| return client | ||
|
|
||
| def get_configuration(self, key: str, label: str): | ||
| configuration = self.client.get_configuration_setting( | ||
| key=f".appconfig.featureflag/{self.key}", label=self.label | ||
Angeluz-07 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| return configuration | ||
|
|
||
| def get_data_configuration(self): | ||
| self.configuration = self.get_configuration(self.key, self.label) | ||
Angeluz-07 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| result = json.loads(self.configuration.value) | ||
|
|
||
| return result | ||
|
|
||
| def is_toggle_enabled(self): | ||
| data = self.get_data_configuration() | ||
| result = data["enabled"] | ||
|
|
||
| return result | ||
|
|
||
| def get_list_users(self): | ||
| data = self.get_data_configuration() | ||
| client_filters = data["conditions"]["client_filters"] | ||
| first_client = client_filters[0] | ||
| list_users = first_client["parameters"]["Audience"]["Users"] | ||
|
|
||
| return list_users | ||
|
|
||
| def is_toggle_enabled_for_user(self): | ||
| list_users = self.get_list_users() | ||
| current_user = current_user_email() | ||
|
|
||
| return current_user in list_users and self.is_toggle_enabled() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
tests/commons/feature_toggles/feature_toggles_manager_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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): | ||
Angeluz-07 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.