|
20 | 20 |
|
21 | 21 | from google.auth import credentials as google_auth_credentials |
22 | 22 | import pytest |
23 | | - |
| 23 | +import requests |
24 | 24 |
|
25 | 25 | import firebase_admin |
26 | | -from firebase_admin import _utils |
| 26 | +from firebase_admin import _utils, _http_client, exceptions |
27 | 27 | from firebase_admin import dataconnect |
28 | 28 | from tests import testutils |
29 | 29 |
|
@@ -724,3 +724,59 @@ def test_get_headers(self): |
724 | 724 | assert isinstance(headers, dict) |
725 | 725 | assert headers.get("X-Firebase-Client") == f"fire-admin-python/{firebase_admin.__version__}" |
726 | 726 | assert headers.get("x-goog-api-client") == _utils.get_metrics_header() |
| 727 | + |
| 728 | + |
| 729 | +class TestDataConnectApiClientMakeGqlRequest: |
| 730 | + |
| 731 | + def setup_method(self): |
| 732 | + self.cred = testutils.MockCredential() |
| 733 | + self.app = firebase_admin.initialize_app( |
| 734 | + self.cred, options={'projectId': 'test-project'} |
| 735 | + ) |
| 736 | + self.api_client = dataconnect._DataConnectApiClient(BASE_CONFIG, self.app) |
| 737 | + |
| 738 | + def teardown_method(self, method): |
| 739 | + del method |
| 740 | + testutils.cleanup_apps() |
| 741 | + |
| 742 | + @mock.patch.object(_http_client.JsonHttpClient, "body_and_response") |
| 743 | + def test_make_gql_request_success(self, mock_body_and_response): |
| 744 | + mock_response = mock.Mock(spec=requests.Response) |
| 745 | + mock_body_and_response.return_value = ({"data": "val"}, mock_response) |
| 746 | + url = "https://example.com/endpoint" |
| 747 | + headers = {"key": "val"} |
| 748 | + payload = {"query": "foo"} |
| 749 | + |
| 750 | + res = self.api_client._make_gql_request(url, headers, payload) |
| 751 | + assert res == {"data": "val"} |
| 752 | + mock_body_and_response.assert_called_once_with( |
| 753 | + "post", url=url, headers=headers, json=payload |
| 754 | + ) |
| 755 | + |
| 756 | + def test_make_gql_request_missing_url(self): |
| 757 | + headers = {"key": "val"} |
| 758 | + payload = {"query": "foo"} |
| 759 | + with pytest.raises(ValueError, match="url, headers, and payload must all be specified."): |
| 760 | + self.api_client._make_gql_request(None, headers, payload) |
| 761 | + |
| 762 | + def test_make_gql_request_missing_headers(self): |
| 763 | + url = "https://example.com/endpoint" |
| 764 | + payload = {"query": "foo"} |
| 765 | + with pytest.raises(ValueError, match="url, headers, and payload must all be specified."): |
| 766 | + self.api_client._make_gql_request(url, None, payload) |
| 767 | + |
| 768 | + def test_make_gql_request_missing_payload(self): |
| 769 | + url = "https://example.com/endpoint" |
| 770 | + headers = {"key": "val"} |
| 771 | + with pytest.raises(ValueError, match="url, headers, and payload must all be specified."): |
| 772 | + self.api_client._make_gql_request(url, headers, None) |
| 773 | + |
| 774 | + @mock.patch.object(_http_client.JsonHttpClient, "body_and_response") |
| 775 | + def test_make_gql_request_error(self, mock_body_and_response): |
| 776 | + mock_body_and_response.side_effect = requests.exceptions.RequestException() |
| 777 | + url = "https://example.com/endpoint" |
| 778 | + headers = {"key": "val"} |
| 779 | + payload = {"query": "foo"} |
| 780 | + |
| 781 | + with pytest.raises(exceptions.FirebaseError): |
| 782 | + self.api_client._make_gql_request(url, headers, payload) |
0 commit comments