|
27 | 27 | import mock
|
28 | 28 |
|
29 | 29 | import requests
|
| 30 | +import pytest |
30 | 31 | import six
|
31 | 32 |
|
32 | 33 | from google.cloud import exceptions
|
|
35 | 36 | from google.cloud.storage._helpers import _base64_md5hash
|
36 | 37 | from google.cloud.storage.bucket import LifecycleRuleDelete
|
37 | 38 | from google.cloud.storage.bucket import LifecycleRuleSetStorageClass
|
| 39 | +from google.cloud import _helpers |
38 | 40 | from google.cloud import kms
|
39 | 41 | from google import resumable_media
|
40 | 42 | import google.auth
|
@@ -147,23 +149,43 @@ def test_get_service_account_email(self):
|
147 | 149 |
|
148 | 150 | self.assertTrue(any(match for match in matches if match is not None))
|
149 | 151 |
|
| 152 | + @staticmethod |
| 153 | + def _get_before_hmac_keys(client): |
| 154 | + from google.cloud.storage.hmac_key import HMACKeyMetadata |
| 155 | + |
| 156 | + before_hmac_keys = set(client.list_hmac_keys()) |
| 157 | + |
| 158 | + now = datetime.datetime.utcnow().replace(tzinfo=_helpers.UTC) |
| 159 | + yesterday = now - datetime.timedelta(days=1) |
| 160 | + |
| 161 | + # Delete any HMAC keys older than a day. |
| 162 | + for hmac_key in list(before_hmac_keys): |
| 163 | + if hmac_key.time_created < yesterday: |
| 164 | + if hmac_key.state != HMACKeyMetadata.INACTIVE_STATE: |
| 165 | + hmac_key.state = HMACKeyMetadata.INACTIVE_STATE |
| 166 | + hmac_key.update() |
| 167 | + hmac_key.delete() |
| 168 | + before_hmac_keys.remove(hmac_key) |
| 169 | + |
| 170 | + return before_hmac_keys |
| 171 | + |
150 | 172 | def test_hmac_key_crud(self):
|
151 | 173 | from google.cloud.storage.hmac_key import HMACKeyMetadata
|
152 | 174 |
|
153 | 175 | credentials = Config.CLIENT._credentials
|
154 | 176 | email = credentials.service_account_email
|
155 | 177 |
|
156 |
| - before_keys = set(Config.CLIENT.list_hmac_keys()) |
| 178 | + before_hmac_keys = self._get_before_hmac_keys(Config.CLIENT) |
157 | 179 |
|
158 | 180 | metadata, secret = Config.CLIENT.create_hmac_key(email)
|
159 | 181 | self.case_hmac_keys_to_delete.append(metadata)
|
160 | 182 |
|
161 | 183 | self.assertIsInstance(secret, six.text_type)
|
162 | 184 | self.assertEqual(len(secret), 40)
|
163 | 185 |
|
164 |
| - after_keys = set(Config.CLIENT.list_hmac_keys()) |
165 |
| - self.assertFalse(metadata in before_keys) |
166 |
| - self.assertTrue(metadata in after_keys) |
| 186 | + after_hmac_keys = set(Config.CLIENT.list_hmac_keys()) |
| 187 | + self.assertFalse(metadata in before_hmac_keys) |
| 188 | + self.assertTrue(metadata in after_hmac_keys) |
167 | 189 |
|
168 | 190 | another = HMACKeyMetadata(Config.CLIENT)
|
169 | 191 |
|
@@ -309,7 +331,6 @@ def test_bucket_update_labels(self):
|
309 | 331 | self.assertEqual(bucket.labels, {})
|
310 | 332 |
|
311 | 333 | def test_get_set_iam_policy(self):
|
312 |
| - import pytest |
313 | 334 | from google.cloud.storage.iam import STORAGE_OBJECT_VIEWER_ROLE
|
314 | 335 | from google.api_core.exceptions import BadRequest, PreconditionFailed
|
315 | 336 |
|
|
0 commit comments