@@ -2678,3 +2678,59 @@ def test_generate_user_hash(harness):
2678
2678
assert harness .charm .generate_user_hash == sentinel .hash
2679
2679
2680
2680
_shake_128 .assert_called_once_with (b"{'relation_id_2': 'test_db'}" )
2681
+
2682
+
2683
+ def test_relations_user_databases_map (harness ):
2684
+ with (
2685
+ patch ("charm.PostgresqlOperatorCharm.postgresql" ) as _postgresql ,
2686
+ patch ("charm.Patroni.member_started" , new_callable = PropertyMock ) as _member_started ,
2687
+ patch (
2688
+ "charm.PostgresqlOperatorCharm.is_cluster_initialised" , new_callable = PropertyMock
2689
+ ) as _is_cluster_initialised ,
2690
+ ):
2691
+ # Initial empty results from the functions used in the property that's being tested.
2692
+ _postgresql .list_users_from_relation .return_value = set ()
2693
+ _postgresql .list_accessible_databases_for_user .return_value = set ()
2694
+ _postgresql .list_access_groups .return_value = {
2695
+ "identity_access" ,
2696
+ "internal_access" ,
2697
+ "relation_access" ,
2698
+ }
2699
+
2700
+ # Test when the cluster isn't initialised yet.
2701
+ _is_cluster_initialised .return_value = False
2702
+ _member_started .return_value = True
2703
+ assert harness .charm .relations_user_databases_map == {
2704
+ "operator" : "all" ,
2705
+ "replication" : "all" ,
2706
+ "rewind" : "all" ,
2707
+ }
2708
+
2709
+ # Test when the cluster is initialised but the cluster member hasn't started yet.
2710
+ _is_cluster_initialised .return_value = True
2711
+ _member_started .return_value = False
2712
+ assert harness .charm .relations_user_databases_map == {
2713
+ "operator" : "all" ,
2714
+ "replication" : "all" ,
2715
+ "rewind" : "all" ,
2716
+ }
2717
+
2718
+ # Test when there are no relation users in the database.
2719
+ _member_started .return_value = True
2720
+ assert harness .charm .relations_user_databases_map == {}
2721
+
2722
+ # Test when there are relation users in the database.
2723
+ _postgresql .list_users .return_value = ["user1" , "user2" ]
2724
+ _postgresql .list_accessible_databases_for_user .side_effect = [["db1" , "db2" ], ["db3" ]]
2725
+ assert harness .charm .relations_user_databases_map == {"user1" : "db1,db2" , "user2" : "db3" }
2726
+
2727
+ # Test when the access groups where not created yet.
2728
+ _postgresql .list_accessible_databases_for_user .side_effect = [["db1" , "db2" ], ["db3" ]]
2729
+ _postgresql .list_access_groups .return_value = set ()
2730
+ assert harness .charm .relations_user_databases_map == {
2731
+ "user1" : "db1,db2" ,
2732
+ "user2" : "db3" ,
2733
+ "operator" : "all" ,
2734
+ "replication" : "all" ,
2735
+ "rewind" : "all" ,
2736
+ }
0 commit comments