Skip to content

Commit b6420bf

Browse files
committed
Make username mandatory
1 parent 94c25c3 commit b6420bf

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/charm.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,9 @@ def _on_get_password(self, event: ActionEvent) -> None:
12401240
12411241
If no user is provided, the password of the operator user is returned.
12421242
"""
1243-
username = event.params.get("username", USER)
1243+
if not (username := event.params.get("username")):
1244+
event.fail("The action requires a username")
1245+
return
12441246
if username not in PASSWORD_USERS and self.is_ldap_enabled:
12451247
event.fail("The action can be run only for system users when LDAP is enabled")
12461248
return
@@ -1260,7 +1262,9 @@ def _on_set_password(self, event: ActionEvent) -> None: # noqa: C901
12601262
event.fail("The action can be run only on leader unit")
12611263
return
12621264

1263-
username = event.params.get("username", USER)
1265+
if not (username := event.params.get("username")):
1266+
event.fail("The action requires a username")
1267+
return
12641268
if username not in SYSTEM_USERS and self.is_ldap_enabled:
12651269
event.fail("The action can be run only for system users when LDAP is enabled")
12661270
return

tests/unit/test_charm.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ def test_on_get_password(harness):
376376
mock_event.reset_mock()
377377
del mock_event.params["username"]
378378
harness.charm._on_get_password(mock_event)
379-
mock_event.set_results.assert_called_once_with({"password": "test-password"})
379+
mock_event.fail.assert_called_once()
380+
mock_event.set_results.assert_not_called()
380381

381382
# Also test providing the username option.
382383
mock_event.reset_mock()
@@ -393,14 +394,16 @@ def test_on_set_password(harness):
393394
patch("charm.PostgresqlOperatorCharm.postgresql") as _postgresql,
394395
patch("charm.Patroni.are_all_members_ready") as _are_all_members_ready,
395396
patch("charm.PostgresqlOperatorCharm._on_leader_elected"),
397+
patch("charm.new_password", return_value="newpass"),
396398
):
397399
# Create a mock event.
398400
mock_event = MagicMock(params={})
399401

400402
# Set some values for the other mocks.
401-
_are_all_members_ready.side_effect = [False, True, True, True, True]
402-
_postgresql.update_user_password = PropertyMock(
403-
side_effect=[PostgreSQLUpdateUserPasswordError, None, None, None]
403+
_are_all_members_ready.return_value = False
404+
_postgresql.update_user_password = PropertyMock()
405+
_postgresql.update_user_password.return_value.side_effect = (
406+
PostgreSQLUpdateUserPasswordError
404407
)
405408

406409
# Test trying to set a password through a non leader unit.
@@ -424,20 +427,25 @@ def test_on_set_password(harness):
424427
_set_secret.assert_not_called()
425428

426429
# Test for an error updating when updating the user password in the database.
430+
_are_all_members_ready.return_value = True
427431
mock_event.reset_mock()
428432
harness.charm._on_set_password(mock_event)
429433
mock_event.fail.assert_called_once()
430434
_set_secret.assert_not_called()
431435

432436
# Test without providing the username option.
437+
_postgresql.update_user_password.return_value.side_effect = None
438+
mock_event.reset_mock()
433439
harness.charm._on_set_password(mock_event)
434-
assert _set_secret.call_args_list[0][0][1] == "operator-password"
440+
mock_event.fail.assert_called_once()
441+
_set_secret.assert_not_called()
435442

436443
# Also test providing the username option.
444+
mock_event.reset_mock()
437445
_set_secret.reset_mock()
438446
mock_event.params["username"] = "replication"
439447
harness.charm._on_set_password(mock_event)
440-
assert _set_secret.call_args_list[0][0][1] == "replication-password"
448+
_set_secret.assert_called_once_with("app", "replication-password", "newpass")
441449

442450
# And test providing both the username and password options.
443451
_set_secret.reset_mock()
@@ -1209,7 +1217,7 @@ def test_on_get_password_secrets(harness):
12091217
mock_event.reset_mock()
12101218
del mock_event.params["username"]
12111219
harness.charm._on_get_password(mock_event)
1212-
mock_event.set_results.assert_called_once_with({"password": "test-password"})
1220+
mock_event.fail.assert_called_once_with("The action requires a username")
12131221

12141222
# Also test providing the username option.
12151223
mock_event.reset_mock()

0 commit comments

Comments
 (0)