Skip to content

Commit

Permalink
Add Tests around new settings and generate signed url call with new s…
Browse files Browse the repository at this point in the history
…ettings
  • Loading branch information
Yohannes-B committed Jul 22, 2024
1 parent b275944 commit 25c3e86
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions tests/test_gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,85 @@ def test_dupe_file_chunk_size(self):
self.filename, chunk_size=chunk_size
)

def test_iam_sign_blob_setting(self):
self.assertEqual(self.storage.iam_sign_blob, False)
with override_settings(
GS_IAM_SIGN_BLOB=True
):
storage = gcloud.GoogleCloudStorage()
self.assertEqual(storage.iam_sign_blob, True)

def test_sa_email_setting(self):
self.assertEqual(self.storage.sa_email, None)
with override_settings(
GS_SA_EMAIL="[email protected]"
):
storage = gcloud.GoogleCloudStorage()
self.assertEqual(storage.sa_email, "[email protected]")

def test_iam_sign_blob_no_service_account_email_raises_attribute_error(self):
with override_settings(
GS_IAM_SIGN_BLOB=True
):
storage = gcloud.GoogleCloudStorage()
storage._bucket = mock.MagicMock()
storage.credentials = mock.MagicMock()
# deleting mocked attribute to simulate no service_account_email
del storage.credentials.service_account_email
# simulating access token
storage.credentials.token = "1234"
# no sa_email or adc service_account_email found
with self.assertRaises(AttributeError, msg=(
"Sign Blob API requires service_account_email to be available "
"through ADC or setting `sa_email`"
)):
storage.url(self.filename)

def test_iam_sign_blob_with_adc_service_account_email(self):
with override_settings(
GS_IAM_SIGN_BLOB=True
):
storage = gcloud.GoogleCloudStorage()
storage._bucket = mock.MagicMock()
storage.credentials = mock.MagicMock()
# simulating adc service account email
storage.credentials.service_account_email = "[email protected]"
# simulating access token
storage.credentials.token = "1234"
blob = mock.MagicMock()
storage._bucket.blob.return_value = blob
storage.url(self.filename)
# called with adc service account email and access token
blob.generate_signed_url.assert_called_with(
expiration=timedelta(seconds=86400),
version="v4",
service_account_email=storage.credentials.service_account_email,
access_token=storage.credentials.token
)

def test_iam_sign_blob_with_sa_email_setting(self):
with override_settings(
GS_IAM_SIGN_BLOB=True,
GS_SA_EMAIL="[email protected]"
):
storage = gcloud.GoogleCloudStorage()
storage._bucket = mock.MagicMock()
storage.credentials = mock.MagicMock()
# simulating adc service account email
storage.credentials.service_account_email = "[email protected]"
# simulating access token
storage.credentials.token = "1234"
blob = mock.MagicMock()
storage._bucket.blob.return_value = blob
storage.url(self.filename)
# called with sa_email as it has final say
blob.generate_signed_url.assert_called_with(
expiration=timedelta(seconds=86400),
version="v4",
service_account_email=storage.sa_email,
access_token=storage.credentials.token
)


class GoogleCloudGzipClientTests(GCloudTestCase):
def setUp(self):
Expand Down

0 comments on commit 25c3e86

Please sign in to comment.