forked from googleapis/python-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_hmac_key_metadata.py
More file actions
87 lines (60 loc) · 2.58 KB
/
test_hmac_key_metadata.py
File metadata and controls
87 lines (60 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import pytest
from google.cloud import _helpers as _cloud_helpers
from . import _helpers
def ensure_hmac_key_deleted(hmac_key):
from google.cloud.storage.hmac_key import HMACKeyMetadata
if hmac_key.state != HMACKeyMetadata.INACTIVE_STATE:
hmac_key.state = HMACKeyMetadata.INACTIVE_STATE
hmac_key.update()
_helpers.retry_429_harder(hmac_key.delete)()
@pytest.fixture
def scrubbed_hmac_keys(storage_client):
before_hmac_keys = set(storage_client.list_hmac_keys())
now = datetime.datetime.utcnow().replace(tzinfo=_cloud_helpers.UTC)
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:
ensure_hmac_key_deleted(hmac_key)
before_hmac_keys.remove(hmac_key)
hmac_keys_to_delete = []
yield before_hmac_keys, hmac_keys_to_delete
# Delete any HMAC keys we created
for hmac_key in hmac_keys_to_delete:
ensure_hmac_key_deleted(hmac_key)
def test_hmac_key_crud(storage_client, scrubbed_hmac_keys, service_account):
from google.cloud.storage.hmac_key import HMACKeyMetadata
before_hmac_keys, hmac_keys_to_delete = scrubbed_hmac_keys
email = service_account.service_account_email
metadata, secret = storage_client.create_hmac_key(email)
hmac_keys_to_delete.append(metadata)
assert isinstance(secret, str)
assert len(secret) == 40
after_hmac_keys = set(storage_client.list_hmac_keys())
assert metadata not in before_hmac_keys
assert metadata in after_hmac_keys
another = HMACKeyMetadata(storage_client)
another._properties["accessId"] = "nonesuch"
assert not another.exists()
another._properties["accessId"] = metadata.access_id
assert another.exists()
another.reload()
assert another._properties == metadata._properties
metadata.state = HMACKeyMetadata.INACTIVE_STATE
metadata.update()
metadata.delete()
hmac_keys_to_delete.remove(metadata)