@@ -393,14 +393,16 @@ def test_on_set_password(harness):
393
393
patch ("charm.PostgresqlOperatorCharm.postgresql" ) as _postgresql ,
394
394
patch ("charm.Patroni.are_all_members_ready" ) as _are_all_members_ready ,
395
395
patch ("charm.PostgresqlOperatorCharm._on_leader_elected" ),
396
+ patch ("charm.new_password" , return_value = "newpass" ),
396
397
):
397
398
# Create a mock event.
398
399
mock_event = MagicMock (params = {})
399
400
400
401
# 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
404
406
)
405
407
406
408
# Test trying to set a password through a non leader unit.
@@ -424,20 +426,25 @@ def test_on_set_password(harness):
424
426
_set_secret .assert_not_called ()
425
427
426
428
# Test for an error updating when updating the user password in the database.
429
+ _are_all_members_ready .return_value = True
427
430
mock_event .reset_mock ()
428
431
harness .charm ._on_set_password (mock_event )
429
432
mock_event .fail .assert_called_once ()
430
433
_set_secret .assert_not_called ()
431
434
432
435
# Test without providing the username option.
436
+ _postgresql .update_user_password .return_value .side_effect = None
437
+ mock_event .reset_mock ()
433
438
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 ()
435
441
436
442
# Also test providing the username option.
443
+ mock_event .reset_mock ()
437
444
_set_secret .reset_mock ()
438
445
mock_event .params ["username" ] = "replication"
439
446
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" )
441
448
442
449
# And test providing both the username and password options.
443
450
_set_secret .reset_mock ()
0 commit comments