-
Notifications
You must be signed in to change notification settings - Fork 47
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
25167 add feature flags service enable won emails #1659
Merged
rarmitag
merged 4 commits into
bcgov:main
from
kzdev420:25167_add_feature_flags_service_enable_won_emails
Jan 10, 2025
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
This file contains 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 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,8 @@ | ||
{ | ||
"flagValues": { | ||
"string-flag": "a string value", | ||
"bool-flag": true, | ||
"integer-flag": 10, | ||
"enable-won-emails": false | ||
severinbeauvais marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '1.2.21' | ||
__version__ = '1.2.22' |
This file contains 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 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 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 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,145 @@ | ||
# Copyright © 2025 Province of British Columbia | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the 'License'); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an 'AS IS' BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Manage the Feature Flags initialization, setup and service.""" | ||
from flask import current_app | ||
from ldclient import get as ldclient_get, set_config as ldclient_set_config # noqa: I001 | ||
from ldclient.config import Config # noqa: I005 | ||
from ldclient.impl.integrations.files.file_data_source import _FileDataSource | ||
from ldclient.interfaces import UpdateProcessor | ||
|
||
from namex.models import User | ||
|
||
|
||
class FileDataSource(UpdateProcessor): | ||
"""FileDataStore has been removed, so this provides similar functionality.""" | ||
|
||
@classmethod | ||
def factory(cls, **kwargs): | ||
"""Provide a way to use local files as a source of feature flag state. | ||
|
||
.. deprecated:: 6.8.0 | ||
This module and this implementation class are deprecated and may be changed or removed in the future. | ||
Please use :func:`ldclient.integrations.Files.new_data_source()`. | ||
|
||
The keyword arguments are the same as the arguments to :func:`ldclient.integrations.Files.new_data_source()`. | ||
""" | ||
return lambda config, store, ready: _FileDataSource(store, ready, | ||
paths=kwargs.get('paths'), | ||
auto_update=kwargs.get('auto_update', False), | ||
poll_interval=kwargs.get('poll_interval', 1), | ||
force_polling=kwargs.get('force_polling', False)) | ||
|
||
|
||
class Flags(): | ||
"""Wrapper around the feature flag system. | ||
|
||
calls FAIL to FALSE | ||
|
||
If the feature flag service is unavailable | ||
AND | ||
there is no local config file | ||
Calls -> False | ||
|
||
""" | ||
|
||
def __init__(self, app=None): | ||
"""Initialize this object.""" | ||
self.sdk_key = None | ||
self.app = None | ||
|
||
if app: | ||
self.init_app(app) | ||
|
||
def init_app(self, app): | ||
"""Initialize the Feature Flag environment.""" | ||
self.app = app | ||
self.sdk_key = app.config.get('NAMEX_LD_SDK_ID') | ||
|
||
if self.sdk_key or app.env != 'production': | ||
|
||
if app.env == 'production': | ||
severinbeauvais marked this conversation as resolved.
Show resolved
Hide resolved
|
||
config = Config(sdk_key=self.sdk_key) | ||
else: | ||
factory = FileDataSource.factory(paths=['flags.json'], | ||
auto_update=True) | ||
config = Config(sdk_key=self.sdk_key, | ||
update_processor_class=factory, | ||
send_events=False) | ||
|
||
ldclient_set_config(config) | ||
client = ldclient_get() | ||
|
||
app.extensions['featureflags'] = client | ||
|
||
def teardown(self, exception): # pylint: disable=unused-argument; flask method signature | ||
"""Destroy all objects created by this extension.""" | ||
client = current_app.extensions['featureflags'] | ||
client.close() | ||
|
||
def _get_client(self): | ||
try: | ||
client = current_app.extensions['featureflags'] | ||
except KeyError: | ||
try: | ||
self.init_app(current_app) | ||
client = current_app.extensions['featureflags'] | ||
except KeyError: | ||
client = None | ||
|
||
return client | ||
|
||
@staticmethod | ||
def _get_anonymous_user(): | ||
return { | ||
'key': 'anonymous' | ||
} | ||
|
||
@staticmethod | ||
def _user_as_key(user: User): | ||
user_json = { | ||
'key': user.sub, | ||
'firstName': user.firstname, | ||
'lastName': user.lastname | ||
} | ||
return user_json | ||
|
||
def is_on(self, flag: str, user: User = None) -> bool: | ||
"""Assert that the flag is set for this user.""" | ||
client = self._get_client() | ||
|
||
if user: | ||
flag_user = self._user_as_key(user) | ||
else: | ||
flag_user = self._get_anonymous_user() | ||
|
||
try: | ||
return bool(client.variation(flag, flag_user, None)) | ||
except Exception as err: | ||
current_app.logger.error('Unable to read flags: %s' % repr(err), exc_info=True) | ||
return False | ||
|
||
def value(self, flag: str, user: User = None) -> bool: | ||
"""Retrieve the value of the (flag, user) tuple.""" | ||
client = self._get_client() | ||
|
||
if user: | ||
flag_user = self._user_as_key(user) | ||
else: | ||
flag_user = self._get_anonymous_user() | ||
|
||
try: | ||
return client.variation(flag, flag_user, None) | ||
except Exception as err: | ||
current_app.logger.error('Unable to read flags: %s' % repr(err), exc_info=True) | ||
return False |
This file contains 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,183 @@ | ||
# Copyright © 2024 Province of British Columbia | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Tests to assure the Flag Services. | ||
|
||
Test-Suite to ensure that the Flag Service is working as expected. | ||
""" | ||
import pytest | ||
from flask import Flask | ||
|
||
from namex.models import User | ||
from namex.services.flags import Flags | ||
|
||
|
||
def test_flags_init(): | ||
"""Ensure that extension can be initialized.""" | ||
app = Flask(__name__) | ||
app.env = 'development' | ||
|
||
with app.app_context(): | ||
flags = Flags(app) | ||
|
||
assert flags | ||
assert app.extensions['featureflags'] | ||
|
||
|
||
def test_flags_init_app(): | ||
"""Ensure that extension can be initialized.""" | ||
app = Flask(__name__) | ||
app.env = 'development' | ||
app.config['NAMEX_LD_SDK_ID'] = 'https://no.flag/avail' | ||
|
||
with app.app_context(): | ||
flags = Flags() | ||
flags.init_app(app) | ||
assert app.extensions['featureflags'] | ||
|
||
|
||
def test_flags_init_app_production(): | ||
"""Ensure that extension can be initialized.""" | ||
app = Flask(__name__) | ||
app.env = 'production' | ||
app.config['NAMEX_LD_SDK_ID'] = 'https://no.flag/avail' | ||
|
||
with app.app_context(): | ||
flags = Flags() | ||
flags.init_app(app) | ||
assert app.extensions['featureflags'] | ||
|
||
|
||
def test_flags_init_app_no_key_dev(): | ||
"""Assert that the extension is setup with a KEY, but in non-production mode.""" | ||
app = Flask(__name__) | ||
app.config['NAMEX_LD_SDK_ID'] = None | ||
app.env = 'development' | ||
|
||
with app.app_context(): | ||
flags = Flags() | ||
flags.init_app(app) | ||
assert app.extensions['featureflags'] | ||
|
||
|
||
def test_flags_init_app_no_key_prod(): | ||
"""Assert that prod with no key initializes, but does not setup the extension.""" | ||
app = Flask(__name__) | ||
app.config['NAMEX_LD_SDK_ID'] = None | ||
app.env = 'production' | ||
|
||
with app.app_context(): | ||
flags = Flags() | ||
flags.init_app(app) | ||
with pytest.raises(KeyError): | ||
client = app.extensions['featureflags'] | ||
assert not client | ||
|
||
|
||
def test_flags_bool_no_key_prod(): | ||
"""Assert that prod with no key initializes, but does not setup the extension.""" | ||
app = Flask(__name__) | ||
app.config['NAMEX_LD_SDK_ID'] = None | ||
app.env = 'production' | ||
|
||
with app.app_context(): | ||
flags = Flags() | ||
flags.init_app(app) | ||
on = flags.is_on('bool-flag') | ||
|
||
assert not on | ||
|
||
|
||
def test_flags_bool(): | ||
"""Assert that a boolean (True) is returned, when using the local Flag.json file.""" | ||
app = Flask(__name__) | ||
app.env = 'development' | ||
app.config['NAMEX_LD_SDK_ID'] = 'https://no.flag/avail' | ||
|
||
with app.app_context(): | ||
flags = Flags() | ||
flags.init_app(app) | ||
flag_on = flags.is_on('bool-flag') | ||
|
||
assert flag_on | ||
|
||
|
||
def test_flags_bool_missing_flag(app): | ||
"""Assert that a boolean (False) is returned when flag doesn't exist, when using the local Flag.json file.""" | ||
from namex import flags | ||
app_env = app.env | ||
try: | ||
with app.app_context(): | ||
flag_on = flags.is_on('no flag here') | ||
|
||
assert not flag_on | ||
except: # pylint: disable=bare-except; # noqa: B901, E722 | ||
# for tests we don't care | ||
assert False | ||
finally: | ||
app.env = app_env | ||
|
||
|
||
def test_flags_bool_using_current_app(): | ||
"""Assert that a boolean (True) is returned, when using the local Flag.json file.""" | ||
from namex import flags | ||
app = Flask(__name__) | ||
app.env = 'development' | ||
|
||
with app.app_context(): | ||
flag_on = flags.is_on('bool-flag') | ||
|
||
assert flag_on | ||
|
||
|
||
@pytest.mark.parametrize('test_name,flag_name,expected', [ | ||
('boolean flag', 'bool-flag', True), | ||
('string flag', 'string-flag', 'a string value'), | ||
('integer flag', 'integer-flag', 10), | ||
]) | ||
def test_flags_bool_value(test_name, flag_name, expected): | ||
"""Assert that a boolean (True) is returned, when using the local Flag.json file.""" | ||
from namex import flags | ||
app = Flask(__name__) | ||
app.env = 'development' | ||
|
||
with app.app_context(): | ||
val = flags.value(flag_name) | ||
|
||
assert val == expected | ||
|
||
|
||
def test_flag_bool_unique_user(): | ||
"""Assert that a unique user can retrieve a flag, when using the local Flag.json file.""" | ||
app = Flask(__name__) | ||
app.env = 'development' | ||
app.config['NAMEX_LD_SDK_ID'] = 'https://no.flag/avail' | ||
|
||
user = User(username='username', firstname='firstname', lastname='lastname', sub='sub', iss='iss', idp_userid='123', login_source='IDIR') | ||
|
||
app_env = app.env | ||
try: | ||
with app.app_context(): | ||
flags = Flags() | ||
flags.init_app(app) | ||
app.env = 'development' | ||
val = flags.value('bool-flag', user) | ||
flag_on = flags.is_on('bool-flag', user) | ||
|
||
assert val | ||
assert flag_on | ||
except: # pylint: disable=bare-except; # noqa: B901, E722 | ||
# for tests we don't care | ||
assert False | ||
finally: | ||
app.env = app_env |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kzdev420
I bet this key has to be imported in api/config.py.