Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions api/features/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ def validate_name(self, name: str): # type: ignore[no-untyped-def]
project = self.context["project"]
feature_name_regex = project.feature_name_regex

if project.only_allow_lower_case_feature_names and name != name.lower():
raise serializers.ValidationError("Feature name must be lower case.")

if not project.is_feature_name_valid(name):
raise serializers.ValidationError(
f"Feature name must match regex: {feature_name_regex}"
Expand Down
6 changes: 5 additions & 1 deletion api/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,17 @@ def live_segment_count(self) -> int:

def is_feature_name_valid(self, feature_name: str) -> bool:
"""
Validate the feature name based on the feature_name_regex attribute.
Validate the feature name based on the only_allow_lower_case_feature_names and
feature_name_regex attributes.

Since we always want to evaluate the regex against the whole string, we're wrapping the
attribute value in ^(...)$. Note that ^(...)$ and ^^(...)$$ are equivalent (in case the
attribute already has the boundaries defined)
"""
return (
not self.only_allow_lower_case_feature_names
or feature_name == feature_name.lower()
) and (
not self.feature_name_regex
or re.match(f"^{self.feature_name_regex}$", feature_name) is not None
)
Expand Down
70 changes: 59 additions & 11 deletions api/tests/unit/features/test_unit_features_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ def test_create_feature__dynamo_enabled__triggers_single_dynamo_write(
project.save()

url = reverse("api-v1:projects:project-features-list", args=[project.id])
data = {"name": "Test feature flag", "type": STANDARD, "project": project.id}
data = {"name": "test_feature_flag", "type": STANDARD, "project": project.id}

mock_dynamo_environment_wrapper.is_enabled = True
mock_dynamo_environment_wrapper.reset_mock()
Expand Down Expand Up @@ -1903,14 +1903,62 @@ def test_create_feature__name_does_not_match_regex__returns_400(
)


def test_create_feature__lower_case_only_and_mixed_case_name__returns_400(
admin_client_new: APIClient, project: Project
) -> None:
# Given
# only_allow_lower_case_feature_names defaults to True
url = reverse("api-v1:projects:project-features-list", args=[project.id])
data = {"name": "MixedCaseFeature", "type": STANDARD, "project": project.id}

# When
response = admin_client_new.post(url, data=data)

# Then
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json()["name"][0] == "Feature name must be lower case."


def test_create_feature__lower_case_only_and_lower_case_name__returns_201(
admin_client_new: APIClient, project: Project
) -> None:
# Given
# only_allow_lower_case_feature_names defaults to True
url = reverse("api-v1:projects:project-features-list", args=[project.id])
data = {"name": "lower_case_feature", "type": STANDARD, "project": project.id}

# When
response = admin_client_new.post(url, data=data)

# Then
assert response.status_code == status.HTTP_201_CREATED


def test_create_feature__lower_case_only_disabled_and_mixed_case_name__returns_201(
admin_client_new: APIClient, project: Project
) -> None:
# Given
project.only_allow_lower_case_feature_names = False
project.save()

url = reverse("api-v1:projects:project-features-list", args=[project.id])
data = {"name": "MixedCaseFeature", "type": STANDARD, "project": project.id}

# When
response = admin_client_new.post(url, data=data)

# Then
assert response.status_code == status.HTTP_201_CREATED


def test_create_feature__valid_data__creates_audit_log(
admin_client_new: APIClient,
project: Project,
environment: Environment,
) -> None:
# Given
url = reverse("api-v1:projects:project-features-list", args=[project.id])
data = {"name": "Test feature flag", "type": STANDARD, "project": project.id}
data = {"name": "test_feature_flag", "type": STANDARD, "project": project.id}

# When
response = admin_client_new.post(url, data=data)
Expand Down Expand Up @@ -1997,7 +2045,7 @@ def test_create_feature__with_tags__creates_tagged_feature(
) -> None:
# Given - set up data
default_value = "Test"
feature_name = "Test feature"
feature_name = "test_feature"
data = {
"name": feature_name,
"project": project.id,
Expand Down Expand Up @@ -3381,7 +3429,7 @@ def test_create_feature__missing_required_metadata__returns_400(
url = reverse("api-v1:projects:project-features-list", args=[project.id])
description = "This is the description"
data = {
"name": "Test feature",
"name": "test_feature",
"description": description,
}

Expand All @@ -3406,7 +3454,7 @@ def test_create_feature__with_optional_metadata__returns_201(
description = "This is the description"
field_value = 10
data = {
"name": "Test feature",
"name": "test_feature",
"description": description,
"metadata": [
{
Expand Down Expand Up @@ -3442,7 +3490,7 @@ def test_create_feature__with_required_metadata__returns_201(
description = "This is the description"
field_value = 10
data = {
"name": "Test feature",
"name": "test_feature",
"description": description,
"metadata": [
{
Expand Down Expand Up @@ -3478,7 +3526,7 @@ def test_create_feature__required_metadata_org_content_type__returns_201(
description = "This is the description"
field_value = 10
data = {
"name": "Test feature",
"name": "test_feature",
"description": description,
"metadata": [
{
Expand Down Expand Up @@ -4916,7 +4964,7 @@ def test_create_feature__duplicate_metadata_id__keeps_metadata_isolated(

# Create first feature with metadata
first_feature_data = {
"name": "First Feature",
"name": "first_feature",
"description": "First feature description",
"metadata": [
{
Expand All @@ -4941,7 +4989,7 @@ def test_create_feature__duplicate_metadata_id__keeps_metadata_isolated(

# Given - Create second feature
second_feature_data = {
"name": "Second Feature",
"name": "second_feature",
"description": "Second feature description",
"metadata": [
{
Expand Down Expand Up @@ -5013,7 +5061,7 @@ def test_create_feature__required_metadata_on_other_project__returns_201(
model_field=model_field,
)
url = reverse("api-v1:projects:project-features-list", args=[project.id])
data = {"name": "Test feature cross project", "description": "desc"}
data = {"name": "test_feature_cross_project", "description": "desc"}

# When
response = admin_client.post(
Expand Down Expand Up @@ -5082,7 +5130,7 @@ def test_create_feature__type_provided__validates_and_sets_type(
) -> None:
# Given
url = reverse("api-v1:projects:project-features-list", args=[project.id])
data = {"name": f"test_feature_{feature_type}", "type": feature_type}
data = {"name": f"test_feature_{feature_type.lower()}", "type": feature_type}

# When
response = admin_client_new.post(
Expand Down
25 changes: 25 additions & 0 deletions api/tests/unit/projects/test_unit_projects_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,31 @@ def test_is_feature_name_valid__regex_and_name__returns_expected( # type: ignor
assert result == expected_result


@pytest.mark.parametrize(
"only_allow_lower_case_feature_names, feature_name, expected_result",
(
(True, "lowercasefeature", True),
(True, "MixedCaseFeature", False),
(True, "UPPERCASEFEATURE", False),
(False, "MixedCaseFeature", True),
),
)
def test_is_feature_name_valid__lower_case_setting_and_name__returns_expected( # type: ignore[no-untyped-def]
only_allow_lower_case_feature_names, feature_name, expected_result
):
# Given
project = Project(
name="test",
only_allow_lower_case_feature_names=only_allow_lower_case_feature_names,
)

# When
result = project.is_feature_name_valid(feature_name)

# Then
assert result == expected_result


def test_save_project__name_updated__clears_environment_caches( # type: ignore[no-untyped-def]
environment, project, mocker
):
Expand Down
Loading