Skip to content

Commit 5ad2cf3

Browse files
authored
[DPE-6664] Make username mandatory in set-password (#934)
* Make username mandatory * Default in get_password
1 parent bb2459a commit 5ad2cf3

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/charm.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1260,7 +1260,9 @@ def _on_set_password(self, event: ActionEvent) -> None: # noqa: C901
12601260
event.fail("The action can be run only on leader unit")
12611261
return
12621262

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

tests/unit/test_charm.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,16 @@ def test_on_set_password(harness):
393393
patch("charm.PostgresqlOperatorCharm.postgresql") as _postgresql,
394394
patch("charm.Patroni.are_all_members_ready") as _are_all_members_ready,
395395
patch("charm.PostgresqlOperatorCharm._on_leader_elected"),
396+
patch("charm.new_password", return_value="newpass"),
396397
):
397398
# Create a mock event.
398399
mock_event = MagicMock(params={})
399400

400401
# 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]
402+
_are_all_members_ready.return_value = False
403+
_postgresql.update_user_password = PropertyMock()
404+
_postgresql.update_user_password.return_value.side_effect = (
405+
PostgreSQLUpdateUserPasswordError
404406
)
405407

406408
# Test trying to set a password through a non leader unit.
@@ -424,20 +426,25 @@ def test_on_set_password(harness):
424426
_set_secret.assert_not_called()
425427

426428
# Test for an error updating when updating the user password in the database.
429+
_are_all_members_ready.return_value = True
427430
mock_event.reset_mock()
428431
harness.charm._on_set_password(mock_event)
429432
mock_event.fail.assert_called_once()
430433
_set_secret.assert_not_called()
431434

432435
# Test without providing the username option.
436+
_postgresql.update_user_password.return_value.side_effect = None
437+
mock_event.reset_mock()
433438
harness.charm._on_set_password(mock_event)
434-
assert _set_secret.call_args_list[0][0][1] == "operator-password"
439+
mock_event.fail.assert_called_once()
440+
_set_secret.assert_not_called()
435441

436442
# Also test providing the username option.
443+
mock_event.reset_mock()
437444
_set_secret.reset_mock()
438445
mock_event.params["username"] = "replication"
439446
harness.charm._on_set_password(mock_event)
440-
assert _set_secret.call_args_list[0][0][1] == "replication-password"
447+
_set_secret.assert_called_once_with("app", "replication-password", "newpass")
441448

442449
# And test providing both the username and password options.
443450
_set_secret.reset_mock()

0 commit comments

Comments
 (0)