Skip to content

Commit

Permalink
Merge pull request #248 from canonical/IAM-1240
Browse files Browse the repository at this point in the history
Handle client_created event being emitted multiple times
  • Loading branch information
nsklikas authored Jan 13, 2025
2 parents b01cf86 + e5b9bcf commit 1efb8aa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ def _on_oauth_client_created(self, event: ClientCreatedEvent) -> None:
event.defer()
return

if self.peer_data[f"oauth_{event.relation_id}"]:
logger.info("Got client_created event, but client already exists. Ignoring event")
return

target_oauth_client = OAuthClient(
**event.snapshot(),
**{"metadata": {"integration-id": str(event.relation_id)}},
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,36 @@ def test_when_succeeds(
assert harness.charm.peer_data[f"oauth_{oauth_integration}"] == {"client_id": "client_id"}
mocked_provider.assert_called_once_with(oauth_integration, "client_id", "client_secret")

def test_client_created_emitted_twice(
self,
harness: Harness,
mocked_workload_service: MagicMock,
mocked_oauth_client_config: dict,
peer_integration: int,
oauth_integration: int,
) -> None:
harness.set_leader(True)
mocked_workload_service.is_running = True

with (
patch(
"charm.CommandLine.create_oauth_client",
return_value=OAuthClient(client_id="client_id", client_secret="client_secret"),
),
patch(
"charm.OAuthProvider.set_client_credentials_in_relation_data"
) as mocked_provider,
):
harness.charm.oauth_provider.on.client_created.emit(
relation_id=oauth_integration, **mocked_oauth_client_config
)
harness.charm.oauth_provider.on.client_created.emit(
relation_id=oauth_integration, **mocked_oauth_client_config
)

assert harness.charm.peer_data[f"oauth_{oauth_integration}"] == {"client_id": "client_id"}
mocked_provider.assert_called_once()


class TestOAuthClientChangedEvent:
def test_when_hydra_service_not_ready(
Expand Down

0 comments on commit 1efb8aa

Please # to comment.