Skip to content

Commit

Permalink
✅ add tests for actions
Browse files Browse the repository at this point in the history
  • Loading branch information
michellewzhang committed Oct 18, 2024
1 parent 5b151be commit 20ba6c5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
18 changes: 17 additions & 1 deletion src/sentry/flags/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,30 @@ def handle_launchdarkly_event(

return [
{
"action": handle_launchdarkly_actions(result["accesses"][0]["action"]),
"action": handle_launchdarkly_actions(access["action"]),
"created_at": datetime.datetime.fromtimestamp(result["date"] / 1000.0, datetime.UTC),
"created_by": result["member"]["email"],
"created_by_type": CREATED_BY_TYPE_MAP["email"],
"flag": result["name"],
"organization_id": organization_id,
"tags": {"description": result["description"]},
}
for access in result["accesses"]
if access["action"]
in (
"createFlag",
"cloneFlag",
"deleteFlag",
"updateFallthrough",
"updateOffVariation",
"updateOn",
"updateRules",
"updateScheduledChanges",
"updateDeprecated",
"updateFlagDefaultVariations",
"updateFlagVariations",
"updateGlobalArchived",
)
]


Expand Down
52 changes: 46 additions & 6 deletions tests/sentry/flags/providers/test_launchdarkly.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def test_launchdarkly_create():
"date": 1729123465221,
"accesses": [
{"action": "createFlag", "resource": "proj/default:env/test:flag/test-flag"},
{"action": "createFlag", "resource": "proj/default:env/production:flag/test-flag"},
],
"kind": "flag",
"name": "test flag",
Expand Down Expand Up @@ -167,7 +166,7 @@ def test_launchdarkly_update():
"date": 1729123867537,
"accesses": [
{"action": "updateOn", "resource": "proj/default:env/test:flag/test-flag"},
{"action": "updateFallthrough", "resource": "proj/default:env/test:flag/test-flag"},
{"action": "randomActionReject", "resource": "proj/default:env/test:flag/test-flag"},
],
"kind": "flag",
"name": "test flag",
Expand Down Expand Up @@ -352,13 +351,14 @@ def test_launchdarkly_update():
assert flag_row["action"] == ACTION_MAP["updated"]


def test_launchdarkly_delete():
def test_launchdarkly_delete_and_update():
request_data = {
"_id": "1234",
"_accountId": "1234",
"date": 1729123867537,
"accesses": [
{"action": "deleteFlag", "resource": "proj/default:env/test:flag/test-flag"},
{"action": "updateRules", "resource": "proj/default:env/test:flag/test-flag"},
],
"kind": "flag",
"name": "test flag",
Expand All @@ -383,9 +383,49 @@ def test_launchdarkly_delete():
}

res = handle_provider_event("launchdarkly", request_data, 123)
assert len(res) == 1
flag_row = res[0]
assert flag_row["action"] == ACTION_MAP["deleted"]
assert len(res) == 2
flag_row_delete = res[0]
flag_row_update = res[1]
assert flag_row_delete["action"] == ACTION_MAP["deleted"]
assert flag_row_update["action"] == ACTION_MAP["updated"]


def test_launchdarkly_no_valid_action():
request_data = {
"_id": "1234",
"_accountId": "1234",
"date": 1729123867537,
"accesses": [
{"action": "copyFlagConfigTo", "resource": "proj/default:env/test:flag/test-flag"},
{
"action": "updateFlagRuleDescription ",
"resource": "proj/default:env/test:flag/test-flag",
},
],
"kind": "flag",
"name": "test flag",
"description": "deleted the flag",
"shortDescription": "",
"comment": "",
"member": {
"_links": {
"parent": {"href": "/api/v2/members", "type": "application/json"},
"self": {
"href": "/api/v2/members/1234",
"type": "application/json",
},
},
"_id": "1234",
"email": "michelle@example.io",
"firstName": "Michelle",
"lastName": "Doe",
},
"titleVerb": "deleted the flag",
"title": "Michelle deleted the flag [test flag](https://app.launchdarkly.com/default/test/features/test-flag) in `Test`",
}

res = handle_provider_event("launchdarkly", request_data, 123)
assert len(res) == 0


def test_bad_launchdarkly_data():
Expand Down

0 comments on commit 20ba6c5

Please sign in to comment.