|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +# contributor license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright ownership. |
| 4 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +# (the "License"); you may not use this file except in compliance with |
| 6 | +# the License. You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import unittest |
| 17 | +from unittest.mock import patch, MagicMock |
| 18 | +import logging |
| 19 | +import requests |
| 20 | +from sending import SendingClient |
| 21 | + |
| 22 | +class TestSendingClient(unittest.TestCase): |
| 23 | + def setUp(self): |
| 24 | + self.logger = logging.getLogger("TestLogger") |
| 25 | + self.logger.setLevel(logging.DEBUG) |
| 26 | + # Create a SendingClient with dummy values |
| 27 | + self.client = SendingClient( |
| 28 | + logger=self.logger, |
| 29 | + github_token="dummy-token", |
| 30 | + github_repo="apache/beam", |
| 31 | + smtp_server="smtp.example.com", |
| 32 | + smtp_port=587, |
| 33 | + email="sender@example.com", |
| 34 | + password="password" |
| 35 | + ) |
| 36 | + |
| 37 | + @patch("requests.request") |
| 38 | + def test_get_open_issues_flaky_retry(self, mock_request): |
| 39 | + # We simulate a 403 secondary rate limit error on the first request, |
| 40 | + # and a successful 200 response on the second request. |
| 41 | + mock_response_fail = MagicMock() |
| 42 | + mock_response_fail.status_code = 403 |
| 43 | + mock_response_fail.ok = False |
| 44 | + mock_response_fail.headers = {"Retry-After": "1"} |
| 45 | + mock_response_fail.raise_for_status.side_effect = requests.exceptions.HTTPError("403 Client Error") |
| 46 | + |
| 47 | + mock_response_success = MagicMock() |
| 48 | + mock_response_success.status_code = 200 |
| 49 | + mock_response_success.ok = True |
| 50 | + mock_response_success.json.return_value = { |
| 51 | + "items": [ |
| 52 | + { |
| 53 | + "number": 1234, |
| 54 | + "title": "[SECURITY] Action Required: Unmanaged Service Account Keys Detected", |
| 55 | + "body": "Test body", |
| 56 | + "state": "open", |
| 57 | + "html_url": "https://github.com/apache/beam/issues/1234", |
| 58 | + "created_at": "2026-07-02T00:00:00Z", |
| 59 | + "updated_at": "2026-07-02T00:00:00Z" |
| 60 | + } |
| 61 | + ] |
| 62 | + } |
| 63 | + |
| 64 | + mock_request.side_effect = [mock_response_fail, mock_response_success] |
| 65 | + |
| 66 | + # Call get_open_issues |
| 67 | + issues = self.client._get_open_issues("[SECURITY] Action Required: Unmanaged Service Account Keys Detected") |
| 68 | + |
| 69 | + # Verify that two requests were made (one retry) |
| 70 | + self.assertEqual(mock_request.call_count, 2) |
| 71 | + self.assertEqual(len(issues), 1) |
| 72 | + self.assertEqual(issues[0].number, 1234) |
| 73 | + |
| 74 | + @patch("requests.request") |
| 75 | + def test_get_open_issues_query_format(self, mock_request): |
| 76 | + mock_response = MagicMock() |
| 77 | + mock_response.status_code = 200 |
| 78 | + mock_response.ok = True |
| 79 | + mock_response.json.return_value = {"items": []} |
| 80 | + mock_request.return_value = mock_response |
| 81 | + |
| 82 | + title = "[SECURITY] Action Required: Unmanaged Service Account Keys Detected" |
| 83 | + self.client._get_open_issues(title) |
| 84 | + |
| 85 | + # Verify that the query parameter was passed correctly to requests |
| 86 | + mock_request.assert_called_once() |
| 87 | + args, kwargs = mock_request.call_args |
| 88 | + |
| 89 | + # Check that we passed params dict containing 'q' |
| 90 | + self.assertIn("params", kwargs) |
| 91 | + self.assertIn("q", kwargs["params"]) |
| 92 | + |
| 93 | + q = kwargs["params"]["q"] |
| 94 | + # The query should specify the repo, title (quoted), state and type |
| 95 | + self.assertIn('repo:apache/beam', q) |
| 96 | + self.assertIn('is:issue', q) |
| 97 | + self.assertIn('is:open', q) |
| 98 | + self.assertIn('in:title', q) |
| 99 | + self.assertIn(f'"{title}"', q) |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + unittest.main() |
0 commit comments