|
| 1 | +# Copyright 2021 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +"""Tests for the "delete_gcp_association" module.""" |
| 16 | + |
| 17 | +import unittest |
| 18 | +from unittest import mock |
| 19 | + |
| 20 | +from google.auth.transport import requests |
| 21 | + |
| 22 | +from . import delete_gcp_association |
| 23 | + |
| 24 | + |
| 25 | +class DeleteGCPAssociationTest(unittest.TestCase): |
| 26 | + |
| 27 | + def test_initialize_command_line_args(self): |
| 28 | + actual = delete_gcp_association.initialize_command_line_args( |
| 29 | + ["--credentials_file=./foo.json", "--organization_id=123"]) |
| 30 | + self.assertIsNotNone(actual) |
| 31 | + |
| 32 | + def test_initialize_command_line_args_organization_id_too_big(self): |
| 33 | + invalid_organization_id = 2**64 |
| 34 | + actual = delete_gcp_association.initialize_command_line_args( |
| 35 | + [f"--organization_id={invalid_organization_id}"]) |
| 36 | + self.assertIsNone(actual) |
| 37 | + |
| 38 | + def test_initialize_command_line_args_negative_organization_id(self): |
| 39 | + actual = delete_gcp_association.initialize_command_line_args( |
| 40 | + ["--organization_id=-1"]) |
| 41 | + self.assertIsNone(actual) |
| 42 | + |
| 43 | + @mock.patch.object(requests, "AuthorizedSession", autospec=True) |
| 44 | + @mock.patch.object(requests.requests, "Response", autospec=True) |
| 45 | + def test_http_error(self, mock_response, mock_session): |
| 46 | + mock_session.request.return_value = mock_response |
| 47 | + type(mock_response).status_code = mock.PropertyMock(return_value=400) |
| 48 | + mock_response.raise_for_status.side_effect = ( |
| 49 | + requests.requests.exceptions.HTTPError()) |
| 50 | + |
| 51 | + with self.assertRaises(requests.requests.exceptions.HTTPError): |
| 52 | + delete_gcp_association.delete_gcp_association(mock_session, 123) |
| 53 | + |
| 54 | + @mock.patch.object(requests, "AuthorizedSession", autospec=True) |
| 55 | + @mock.patch.object(requests.requests, "Response", autospec=True) |
| 56 | + def test_happy_path(self, mock_response, mock_session): |
| 57 | + mock_session.request.return_value = mock_response |
| 58 | + type(mock_response).status_code = mock.PropertyMock(return_value=200) |
| 59 | + |
| 60 | + delete_gcp_association.delete_gcp_association(mock_session, 123) |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + unittest.main() |
0 commit comments