|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import datetime |
| 16 | +from unittest import mock |
| 17 | + |
| 18 | +from google.cloud.storage import _grpc_conversions |
| 19 | +from google.cloud import _storage_v2 |
| 20 | + |
| 21 | + |
| 22 | +def test_blob_to_proto_simple_fields(): |
| 23 | + blob = mock.Mock( |
| 24 | + spec=[ |
| 25 | + "name", |
| 26 | + "bucket", |
| 27 | + "content_type", |
| 28 | + "metadata", |
| 29 | + "kms_key_name", |
| 30 | + "cache_control", |
| 31 | + "content_disposition", |
| 32 | + "content_encoding", |
| 33 | + "content_language", |
| 34 | + "temporary_hold", |
| 35 | + "event_based_hold", |
| 36 | + "custom_time", |
| 37 | + "acl", |
| 38 | + "retention", |
| 39 | + ] |
| 40 | + ) |
| 41 | + blob.name = "blob-name" |
| 42 | + blob.bucket.name = "bucket-name" |
| 43 | + blob.content_type = "text/plain" |
| 44 | + blob.metadata = {"key": "value"} |
| 45 | + blob.kms_key_name = "kms-key" |
| 46 | + blob.cache_control = "no-cache" |
| 47 | + blob.content_disposition = "attachment" |
| 48 | + blob.content_encoding = "gzip" |
| 49 | + blob.content_language = "en" |
| 50 | + blob.temporary_hold = True |
| 51 | + blob.event_based_hold = False |
| 52 | + blob.custom_time = None |
| 53 | + blob.acl = None |
| 54 | + blob.retention = None |
| 55 | + |
| 56 | + proto = _grpc_conversions.blob_to_proto(blob) |
| 57 | + |
| 58 | + assert proto.name == "blob-name" |
| 59 | + assert proto.bucket == "projects/_/buckets/bucket-name" |
| 60 | + assert proto.content_type == "text/plain" |
| 61 | + assert proto.metadata == {"key": "value"} |
| 62 | + assert proto.kms_key == "kms-key" |
| 63 | + assert proto.cache_control == "no-cache" |
| 64 | + assert proto.content_disposition == "attachment" |
| 65 | + assert proto.content_encoding == "gzip" |
| 66 | + assert proto.content_language == "en" |
| 67 | + assert proto.temporary_hold is True |
| 68 | + assert proto.event_based_hold is False |
| 69 | + |
| 70 | + |
| 71 | +def test_blob_to_proto_custom_time(): |
| 72 | + blob = mock.Mock(spec=["name", "bucket", "custom_time", "acl", "retention"]) |
| 73 | + blob.name = "blob-name" |
| 74 | + blob.bucket.name = "bucket-name" |
| 75 | + blob.custom_time = datetime.datetime( |
| 76 | + 2025, 1, 1, 12, 0, 0, tzinfo=datetime.timezone.utc |
| 77 | + ) |
| 78 | + blob.acl = None |
| 79 | + blob.retention = None |
| 80 | + # ensure other fields don't cause issues if missing |
| 81 | + for attr in _grpc_conversions._BLOB_ATTR_TO_PROTO_FIELD: |
| 82 | + setattr(blob, attr, None) |
| 83 | + |
| 84 | + proto = _grpc_conversions.blob_to_proto(blob) |
| 85 | + |
| 86 | + assert int(proto.custom_time.timestamp()) == int(blob.custom_time.timestamp()) |
| 87 | + |
| 88 | + |
| 89 | +def test_blob_to_proto_acl(): |
| 90 | + blob = mock.Mock(spec=["name", "bucket", "acl", "custom_time", "retention"]) |
| 91 | + blob.name = "blob-name" |
| 92 | + blob.bucket.name = "bucket-name" |
| 93 | + |
| 94 | + acl_mock = mock.MagicMock() |
| 95 | + acl_mock.loaded = True |
| 96 | + acl_mock.__iter__.return_value = iter( |
| 97 | + [ |
| 98 | + {"role": "READER", "entity": "allUsers"}, |
| 99 | + {"role": "OWNER", "entity": "user-123"}, |
| 100 | + ] |
| 101 | + ) |
| 102 | + blob.acl = acl_mock |
| 103 | + |
| 104 | + blob.custom_time = None |
| 105 | + blob.retention = None |
| 106 | + for attr in _grpc_conversions._BLOB_ATTR_TO_PROTO_FIELD: |
| 107 | + setattr(blob, attr, None) |
| 108 | + |
| 109 | + proto = _grpc_conversions.blob_to_proto(blob) |
| 110 | + |
| 111 | + assert len(proto.acl) == 2 |
| 112 | + assert proto.acl[0].role == "READER" |
| 113 | + assert proto.acl[0].entity == "allUsers" |
| 114 | + assert proto.acl[1].role == "OWNER" |
| 115 | + assert proto.acl[1].entity == "user-123" |
| 116 | + |
| 117 | + |
| 118 | +def test_blob_to_proto_retention(): |
| 119 | + blob = mock.Mock(spec=["name", "bucket", "retention", "custom_time", "acl"]) |
| 120 | + blob.name = "blob-name" |
| 121 | + blob.bucket.name = "bucket-name" |
| 122 | + |
| 123 | + retain_until_time = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc) |
| 124 | + blob.retention = {"mode": "Locked", "retain_until_time": retain_until_time} |
| 125 | + |
| 126 | + blob.custom_time = None |
| 127 | + blob.acl = None |
| 128 | + for attr in _grpc_conversions._BLOB_ATTR_TO_PROTO_FIELD: |
| 129 | + setattr(blob, attr, None) |
| 130 | + |
| 131 | + proto = _grpc_conversions.blob_to_proto(blob) |
| 132 | + |
| 133 | + assert proto.retention.mode == _storage_v2.Object.Retention.Mode.LOCKED |
| 134 | + assert int(proto.retention.retain_until_time.timestamp()) == int( |
| 135 | + retain_until_time.timestamp() |
| 136 | + ) |
0 commit comments