mik-laj 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_r319293921
 
 

 ##########
 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:
   Yes. This is a credentials object because the `from_service_account_info` 
method is the Credentials class factory method. It's allways return Credentials 
object. This PR does not change the behavior of it.
   
   All tests pass this locally, but let's wait for Travis.
   <img width="643" alt="Screenshot 2019-08-30 at 00 15 30" 
src="https://user-images.githubusercontent.com/12058428/63980307-70b33a80-cabb-11e9-8c28-e33d5e56aec1.png";>
   

----------------------------------------------------------------
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