|
4 | 4 | filter_escalation_policies,
|
5 | 5 | filter_integrations,
|
6 | 6 | filter_schedules,
|
| 7 | + filter_users, |
7 | 8 | migrate,
|
8 | 9 | )
|
9 | 10 |
|
@@ -35,6 +36,51 @@ def test_users_are_skipped_when_migrate_users_is_false(
|
35 | 36 | mock_oncall_client.list_users_with_notification_rules.assert_not_called()
|
36 | 37 |
|
37 | 38 |
|
| 39 | +@patch("lib.pagerduty.migrate.MIGRATE_USERS", True) |
| 40 | +@patch("lib.pagerduty.migrate.PAGERDUTY_FILTER_USERS", ["USER1", "USER3"]) |
| 41 | +@patch("lib.pagerduty.migrate.MODE", "migrate") # Skip report generation |
| 42 | +@patch("lib.pagerduty.migrate.APISession") |
| 43 | +@patch("lib.pagerduty.migrate.OnCallAPIClient") |
| 44 | +def test_only_specified_users_are_processed_when_filter_users_is_set( |
| 45 | + MockOnCallAPIClient, MockAPISession |
| 46 | +): |
| 47 | + mock_session = MockAPISession.return_value |
| 48 | + |
| 49 | + # Create test users with required fields |
| 50 | + users = [ |
| 51 | + { "id": "USER1", "name": "User 1", "oncall_user": None, "email": "[email protected]"}, |
| 52 | + { "id": "USER2", "name": "User 2", "oncall_user": None, "email": "[email protected]"}, |
| 53 | + { "id": "USER3", "name": "User 3", "oncall_user": None, "email": "[email protected]"}, |
| 54 | + { "id": "USER4", "name": "User 4", "oncall_user": None, "email": "[email protected]"} |
| 55 | + ] |
| 56 | + |
| 57 | + # Configure mock to return test users for first call, empty lists for other calls |
| 58 | + mock_session.list_all.side_effect = [ |
| 59 | + users, # users |
| 60 | + [], # schedules |
| 61 | + [], # escalation_policies |
| 62 | + [], # services |
| 63 | + [], # vendors |
| 64 | + ] |
| 65 | + mock_session.jget.return_value = {"overrides": []} |
| 66 | + |
| 67 | + # Mock the user matching function to set oncall_user |
| 68 | + with patch("lib.pagerduty.migrate.match_user") as mock_match_user: |
| 69 | + def set_oncall_user(user, _): |
| 70 | + # Just leave oncall_user as it is (None) |
| 71 | + pass |
| 72 | + |
| 73 | + mock_match_user.side_effect = set_oncall_user |
| 74 | + |
| 75 | + # Run migrate |
| 76 | + migrate() |
| 77 | + |
| 78 | + # Check that match_user was only called for USER1 and USER3 |
| 79 | + assert mock_match_user.call_count == 2 |
| 80 | + user_ids = [call_args[0][0]["id"] for call_args in mock_match_user.call_args_list] |
| 81 | + assert set(user_ids) == {"USER1", "USER3"} |
| 82 | + |
| 83 | + |
38 | 84 | class TestPagerDutyFiltering:
|
39 | 85 | def setup_method(self):
|
40 | 86 | self.mock_schedule = {
|
@@ -74,6 +120,26 @@ def setup_method(self):
|
74 | 120 | },
|
75 | 121 | }
|
76 | 122 |
|
| 123 | + self.users = [ |
| 124 | + {"id": "USER1", "name": "User 1"}, |
| 125 | + {"id": "USER2", "name": "User 2"}, |
| 126 | + {"id": "USER3", "name": "User 3"}, |
| 127 | + ] |
| 128 | + |
| 129 | + @patch("lib.pagerduty.migrate.PAGERDUTY_FILTER_USERS", ["USER1", "USER3"]) |
| 130 | + def test_filter_users(self): |
| 131 | + """Test filtering users by ID when PAGERDUTY_FILTER_USERS is set.""" |
| 132 | + filtered = filter_users(self.users) |
| 133 | + assert len(filtered) == 2 |
| 134 | + assert {u["id"] for u in filtered} == {"USER1", "USER3"} |
| 135 | + |
| 136 | + @patch("lib.pagerduty.migrate.PAGERDUTY_FILTER_USERS", []) |
| 137 | + def test_filter_users_no_filter(self): |
| 138 | + """Test that all users are kept when PAGERDUTY_FILTER_USERS is empty.""" |
| 139 | + filtered = filter_users(self.users) |
| 140 | + assert len(filtered) == 3 |
| 141 | + assert {u["id"] for u in filtered} == {"USER1", "USER2", "USER3"} |
| 142 | + |
77 | 143 | @patch("lib.pagerduty.migrate.PAGERDUTY_FILTER_TEAM", "Team 1")
|
78 | 144 | def test_filter_schedules_by_team(self):
|
79 | 145 | schedules = [
|
|
0 commit comments