Skip to content

Commit b3a5a4a

Browse files
author
Matthew Elwell
authored
Prevent initialisation with local evaluation without server key (#25)
* Prevent initialisation with local evaluation without server key * Fix test * Formatting
1 parent 0687c30 commit b3a5a4a

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

flagsmith/flagsmith.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ def __init__(
8383

8484
self._environment = None
8585
if enable_local_evaluation:
86+
if not environment_key.startswith("ser."):
87+
raise ValueError(
88+
"In order to use local evaluation, please generate a server key "
89+
"in the environment settings page."
90+
)
91+
8692
self.environment_data_polling_manager_thread = (
8793
EnvironmentDataPollingManager(
8894
main=self,

tests/conftest.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
import string
55

66
import pytest
7-
import responses
87
from flag_engine.environments.builders import build_environment_model
98

109
from flagsmith import Flagsmith
1110
from flagsmith.analytics import AnalyticsProcessor
12-
from flagsmith.flagsmith import DEFAULT_API_URL
1311

1412
DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
1513

tests/test_flagsmith.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from flagsmith.models import DefaultFlag
1212

1313

14-
def test_flagsmith_starts_polling_manager_on_init_if_enabled(mocker, api_key):
14+
def test_flagsmith_starts_polling_manager_on_init_if_enabled(mocker, server_api_key):
1515
# Given
1616
mock_polling_manager = mocker.MagicMock()
1717
mocker.patch(
@@ -20,7 +20,7 @@ def test_flagsmith_starts_polling_manager_on_init_if_enabled(mocker, api_key):
2020
)
2121

2222
# When
23-
Flagsmith(environment_key=api_key, enable_local_evaluation=True)
23+
Flagsmith(environment_key=server_api_key, enable_local_evaluation=True)
2424

2525
# Then
2626
mock_polling_manager.start.assert_called_once()
@@ -349,7 +349,9 @@ def test_get_identity_segments_no_traits(local_eval_flagsmith, environment_model
349349
assert segments == []
350350

351351

352-
def test_get_identity_segments_with_valid_trait(local_eval_flagsmith, environment_model):
352+
def test_get_identity_segments_with_valid_trait(
353+
local_eval_flagsmith, environment_model
354+
):
353355
# Given
354356
identifier = "identifier"
355357
traits = {"foo": "bar"} # obtained from data/environment.json
@@ -360,3 +362,8 @@ def test_get_identity_segments_with_valid_trait(local_eval_flagsmith, environmen
360362
# Then
361363
assert len(segments) == 1
362364
assert segments[0].name == "Test segment" # obtained from data/environment.json
365+
366+
367+
def test_local_evaluation_requires_server_key():
368+
with pytest.raises(ValueError):
369+
Flagsmith(environment_key="not-a-server-key", enable_local_evaluation=True)

0 commit comments

Comments
 (0)