Skip to content
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

fix(github-4555): use api_key name for changed_by #4561

Merged
merged 3 commits into from
Sep 4, 2024
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
2 changes: 1 addition & 1 deletion api/edge_api/identities/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def save(self, **kwargs):
"environment_api_key": identity.environment_api_key,
"identity_id": identity.id,
"identity_identifier": identity.identifier,
"changed_by_user_id": request.user.id,
"changed_by_user_id": request.user.pk,
"new_enabled_state": self.instance.enabled,
"new_value": new_value,
"previous_enabled_state": getattr(previous_state, "enabled", None),
Expand Down
11 changes: 8 additions & 3 deletions api/edge_api/identities/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from task_processor.decorators import register_task_handler
from task_processor.models import TaskPriority

from api_keys.models import MasterAPIKey
from audit.models import AuditLog
from audit.related_object_type import RelatedObjectType
from edge_api.identities.types import IdentityChangeset
Expand All @@ -24,7 +25,7 @@ def call_environment_webhook_for_feature_state_change(
environment_api_key: str,
identity_id: typing.Union[id, str],
identity_identifier: str,
changed_by_user_id: int,
changed_by_user_id: int | str,
timestamp: str,
new_enabled_state: bool = None,
new_value: typing.Union[bool, int, str] = None,
Expand All @@ -40,10 +41,14 @@ def call_environment_webhook_for_feature_state_change(
return

feature = Feature.objects.get(id=feature_id)
changed_by = FFAdminUser.objects.get(id=changed_by_user_id)
match changed_by_user_id:
case str():
changed_by = MasterAPIKey.objects.get(id=changed_by_user_id).name
case _:
changed_by = FFAdminUser.objects.get(id=changed_by_user_id).email

data = {
"changed_by": changed_by.email,
"changed_by": changed_by,
"timestamp": timestamp,
"new_state": None,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import typing
import uuid
from unittest.mock import MagicMock

import pytest
from core.constants import BOOLEAN, INTEGER, STRING
Expand Down Expand Up @@ -351,15 +352,15 @@ def test_edge_identities_create_featurestate_returns_400_if_feature_state_alread


def test_edge_identities_create_featurestate(
dynamodb_wrapper_v2,
admin_client,
environment,
environment_api_key,
identity_document_without_fs,
edge_identity_dynamo_wrapper_mock,
feature,
feature_name,
webhook_mock,
dynamodb_wrapper_v2: DynamoEnvironmentV2Wrapper,
admin_client_new: APIClient,
environment: int,
environment_api_key: str,
identity_document_without_fs: dict,
edge_identity_dynamo_wrapper_mock: MagicMock,
feature: int,
feature_name: str,
webhook_mock: MagicMock,
):
# Given
edge_identity_dynamo_wrapper_mock.get_item_from_uuid_or_404.return_value = (
Expand All @@ -381,7 +382,7 @@ def test_edge_identities_create_featurestate(
}

# When
response = admin_client.post(
response = admin_client_new.post(
url, data=json.dumps(data), content_type="application/json"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.utils import timezone
from pytest_mock import MockerFixture

from api_keys.models import MasterAPIKey
from audit.models import AuditLog
from audit.related_object_type import RelatedObjectType
from edge_api.identities.tasks import (
Expand All @@ -16,7 +17,9 @@
IdentityOverridesV2Changeset,
IdentityOverrideV2,
)
from environments.identities.models import Identity
from environments.models import Environment, Webhook
from features.models import Feature
from webhooks.webhooks import WebhookEventType


Expand Down Expand Up @@ -128,6 +131,49 @@ def test_call_environment_webhook_for_feature_state_change_with_previous_state_o
assert data["timestamp"] == now_isoformat


def test_call_environment_webhook_for_feature_state_change_with_master_api_key_id(
mocker: MockerFixture,
environment: Environment,
feature: Feature,
identity: Identity,
master_api_key_object: MasterAPIKey,
) -> None:
# Given
mock_call_environment_webhooks = mocker.patch(
"edge_api.identities.tasks.call_environment_webhooks"
)
Webhook.objects.create(environment=environment, url="https://foo.com/webhook")

mock_feature_state_data = mocker.MagicMock()
mocker.patch.object(
Webhook,
"generate_webhook_feature_state_data",
return_value=mock_feature_state_data,
)

now_isoformat = timezone.now().isoformat()
previous_enabled_state = True
previous_value = "foo"

# When
call_environment_webhook_for_feature_state_change(
feature_id=feature.id,
environment_api_key=environment.api_key,
identity_id=identity.id,
identity_identifier=identity.identifier,
changed_by_user_id=master_api_key_object.id,
timestamp=now_isoformat,
previous_enabled_state=previous_enabled_state,
previous_value=previous_value,
)

# Then
call_args = mock_call_environment_webhooks.call_args

data = call_args[0][1]
assert data["changed_by"] == master_api_key_object.name


@pytest.mark.parametrize(
"previous_enabled_state, previous_value, new_enabled_state, new_value",
(
Expand Down
Loading