|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch, MagicMock |
| 3 | +from dev_tools.prepared_env import PreparedEnv |
| 4 | +from dev_tools.github_repository import GithubRepository |
| 5 | + |
| 6 | + |
| 7 | +class TestPreparedEnvSecurity(unittest.TestCase): |
| 8 | + @patch('requests.post') |
| 9 | + def test_report_status_to_github_token_in_header(self, mock_post): |
| 10 | + # Setup |
| 11 | + mock_response = MagicMock() |
| 12 | + mock_response.status_code = 201 |
| 13 | + mock_post.return_value = mock_response |
| 14 | + |
| 15 | + repo = GithubRepository('my-org', 'my-repo', 'my-token') |
| 16 | + env = PreparedEnv(repo, 'my-commit', 'compare-commit', None, None) |
| 17 | + |
| 18 | + # Execute |
| 19 | + env.report_status_to_github('success', 'desc', 'ctx') |
| 20 | + |
| 21 | + # Verify |
| 22 | + args, kwargs = mock_post.call_args |
| 23 | + url = args[0] |
| 24 | + headers = kwargs.get('headers', {}) |
| 25 | + |
| 26 | + # Security check: Token should NOT be in the URL |
| 27 | + self.assertNotIn('access_token=my-token', url, "Token should not be passed in the URL") |
| 28 | + |
| 29 | + # Security check: Token should be in the Authorization header |
| 30 | + self.assertEqual( |
| 31 | + headers.get('Authorization'), |
| 32 | + 'Bearer my-token', |
| 33 | + "Token should be passed in the Authorization header", |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +if __name__ == '__main__': |
| 38 | + unittest.main() |
0 commit comments