Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions tests/system/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import mock

import requests
import pytest
import six

from google.cloud import exceptions
Expand All @@ -35,6 +36,7 @@
from google.cloud.storage._helpers import _base64_md5hash
from google.cloud.storage.bucket import LifecycleRuleDelete
from google.cloud.storage.bucket import LifecycleRuleSetStorageClass
from google.cloud import _helpers
from google.cloud import kms
from google import resumable_media
import google.auth
Expand Down Expand Up @@ -147,23 +149,43 @@ def test_get_service_account_email(self):

self.assertTrue(any(match for match in matches if match is not None))

@staticmethod
def _get_before_hmac_keys(client):
from google.cloud.storage.hmac_key import HMACKeyMetadata

before_hmac_keys = set(client.list_hmac_keys())

now = datetime.datetime.utcnow().replace(tzinfo=_helpers.UTC)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With pytz in the standard library now, which includes a utc, is there any need to still use our own handrolled?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ python3.6
Python 3.6.10 (default, Apr 20 2021, 13:17:17) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pytz
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pytz'
>>>

See googleapis/python-api-core@61490f5 (part of googleapis/python-api-core#212), which uses datetime.timezone.utc.

GCS is still stuck supporting Python 2.7, however.

yesterday = now - datetime.timedelta(days=1)

# Delete any HMAC keys older than a day.
for hmac_key in list(before_hmac_keys):
if hmac_key.time_created < yesterday:
if hmac_key.state != HMACKeyMetadata.INACTIVE_STATE:
hmac_key.state = HMACKeyMetadata.INACTIVE_STATE
hmac_key.update()
hmac_key.delete()
before_hmac_keys.remove(hmac_key)

return before_hmac_keys

def test_hmac_key_crud(self):
from google.cloud.storage.hmac_key import HMACKeyMetadata

credentials = Config.CLIENT._credentials
email = credentials.service_account_email

before_keys = set(Config.CLIENT.list_hmac_keys())
before_hmac_keys = self._get_before_hmac_keys(Config.CLIENT)

metadata, secret = Config.CLIENT.create_hmac_key(email)
self.case_hmac_keys_to_delete.append(metadata)

self.assertIsInstance(secret, six.text_type)
self.assertEqual(len(secret), 40)

after_keys = set(Config.CLIENT.list_hmac_keys())
self.assertFalse(metadata in before_keys)
self.assertTrue(metadata in after_keys)
after_hmac_keys = set(Config.CLIENT.list_hmac_keys())
self.assertFalse(metadata in before_hmac_keys)
self.assertTrue(metadata in after_hmac_keys)

another = HMACKeyMetadata(Config.CLIENT)

Expand Down Expand Up @@ -309,7 +331,6 @@ def test_bucket_update_labels(self):
self.assertEqual(bucket.labels, {})

def test_get_set_iam_policy(self):
import pytest
from google.cloud.storage.iam import STORAGE_OBJECT_VIEWER_ROLE
from google.api_core.exceptions import BadRequest, PreconditionFailed

Expand Down