-
-
Notifications
You must be signed in to change notification settings - Fork 864
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Tests around new settings and generate signed url call with new s…
…ettings
- Loading branch information
1 parent
b275944
commit 25c3e86
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
|