kaxil commented on a change in pull request #5907: [AIRFLOW-5303] Use 
project_id from GCP credentials
URL: https://github.com/apache/airflow/pull/5907#discussion_r319292238
 
 

 ##########
 File path: tests/contrib/hooks/test_gcp_api_base_hook.py
 ##########
 @@ -227,6 +229,88 @@ class TestGoogleCloudBaseHook(unittest.TestCase):
     def setUp(self):
         self.instance = hook.GoogleCloudBaseHook()
 
+    @mock.patch(MODULE_NAME + '.google.auth.default', 
return_value=("CREDENTIALS", "PROJECT_ID"))
+    def test_get_credentials_and_project_id_with_default_auth(self, 
mock_auth_default):
+        self.instance.extras = {}
+        result = self.instance._get_credentials_and_project_id()
+        mock_auth_default.assert_called_once_with(scopes=self.instance.scopes)
+        self.assertEqual(('CREDENTIALS', 'PROJECT_ID'), result)
+
+    @mock.patch(  # type: ignore
+        MODULE_NAME + 
'.google.oauth2.service_account.Credentials.from_service_account_file',
+        **{'return_value.project_id': "PROJECT_ID"}
+    )
+    def test_get_credentials_and_project_id_with_service_account_file(self, 
mock_from_service_account_file):
+        self.instance.extras = {
+            'extra__google_cloud_platform__key_path': "KEY_PATH.json"
+        }
+        result = self.instance._get_credentials_and_project_id()
+        
mock_from_service_account_file.assert_called_once_with('KEY_PATH.json', 
scopes=self.instance.scopes)
+        self.assertEqual((mock_from_service_account_file.return_value, 
'PROJECT_ID'), result)
+
+    @mock.patch(MODULE_NAME + 
'.google.oauth2.service_account.Credentials.from_service_account_file')
+    def 
test_get_credentials_and_project_id_with_service_account_file_and_p12_key(
+        self,
+        mock_from_service_account_file
+    ):
+        self.instance.extras = {
+            'extra__google_cloud_platform__key_path': "KEY_PATH.p12"
+        }
+        with self.assertRaises(AirflowException):
+            self.instance._get_credentials_and_project_id()
+
+    @mock.patch(MODULE_NAME + 
'.google.oauth2.service_account.Credentials.from_service_account_file')
+    def 
test_get_credentials_and_project_id_with_service_account_file_and_unknown_key(
+        self,
+        mock_from_service_account_file
+    ):
+        self.instance.extras = {
+            'extra__google_cloud_platform__key_path': "KEY_PATH.unknown"
+        }
+        with self.assertRaises(AirflowException):
+            self.instance._get_credentials_and_project_id()
+
+    @mock.patch(  # type: ignore
+        MODULE_NAME + 
'.google.oauth2.service_account.Credentials.from_service_account_info',
+        **{'return_value.project_id': "PROJECT_ID"}
+    )
+    def test_get_credentials_and_project_id_with_service_account_info(self, 
mock_from_service_account_file):
+        service_account = {
+            'private_key': "PRIVATE_KEY"
+        }
+        self.instance.extras = {
+            'extra__google_cloud_platform__keyfile_dict': 
json.dumps(service_account)
+        }
+        result = self.instance._get_credentials_and_project_id()
+        
mock_from_service_account_file.assert_called_once_with(service_account, 
scopes=self.instance.scopes)
+        self.assertEqual((mock_from_service_account_file.return_value, 
'PROJECT_ID'), result)
 
 Review comment:
   (mock_from_service_account_file.return_value, 'PROJECT_ID') -> The first 
item is credential block?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to