|
1044 | 1044 | expect { uploaded_file.retention = retention }.must_raise Google::Cloud::PermissionDeniedError |
1045 | 1045 | end |
1046 | 1046 | end |
| 1047 | + |
| 1048 | + describe "object contexts" do |
| 1049 | + let(:custom_context_key1) { "my-custom-key" } |
| 1050 | + let(:custom_context_value1) { "my-custom-value" } |
| 1051 | + let(:custom_context_key2) { "my-custom-key-2" } |
| 1052 | + let(:custom_context_value2) { "my-custom-value-2" } |
| 1053 | + let(:local_file) { "acceptance/data/CloudPlatform_128px_Retina.png" } |
| 1054 | + let(:file_name) { "CloudLogo1" } |
| 1055 | + |
| 1056 | + before do |
| 1057 | + bucket.create_file local_file, file_name |
| 1058 | + end |
| 1059 | + |
| 1060 | + it "sets and retrieves custom context key and value" do |
| 1061 | + file = bucket.file file_name |
| 1062 | + file.contexts = Google::Apis::StorageV1::Object::Contexts.new( |
| 1063 | + custom: context_custom_hash(custom_context_key: custom_context_key1 ,custom_context_value: custom_context_value1) |
| 1064 | + ) |
| 1065 | + file.reload! |
| 1066 | + _(file.contexts.custom[custom_context_key1].value).must_equal custom_context_value1 |
| 1067 | + end |
| 1068 | + |
| 1069 | + it "rejects special characters in custom context key and value" do |
| 1070 | + invalid_key = 'my"-invalid-key' |
| 1071 | + custom_value = 'my-custom-value' |
| 1072 | + |
| 1073 | + file = bucket.file file_name |
| 1074 | + |
| 1075 | + err = _ { |
| 1076 | + file.contexts = Google::Apis::StorageV1::Object::Contexts.new( |
| 1077 | + custom: context_custom_hash(custom_context_key: invalid_key, custom_context_value: custom_value) |
| 1078 | + ) |
| 1079 | + }.must_raise Google::Cloud::InvalidArgumentError |
| 1080 | + |
| 1081 | + _(err.message).must_match(/Object context key cannot contain/) |
| 1082 | + |
| 1083 | + invalid_key = 'my-custom-key' |
| 1084 | + custom_value = 'my-invalid/value' |
| 1085 | + |
| 1086 | + err = _ { |
| 1087 | + file.contexts = Google::Apis::StorageV1::Object::Contexts.new( |
| 1088 | + custom: context_custom_hash(custom_context_key: invalid_key, custom_context_value: custom_value) |
| 1089 | + ) |
| 1090 | + }.must_raise Google::Cloud::InvalidArgumentError |
| 1091 | + |
| 1092 | + _(err.message).must_match(/Object context value cannot contain/) |
| 1093 | + end |
| 1094 | + |
| 1095 | + it "rejects unicode characters in keys and values" do |
| 1096 | + invalid_key = '🚀-launcher' |
| 1097 | + custom_value = 'my-custom-value' |
| 1098 | + file = bucket.file file_name |
| 1099 | + err = _ { |
| 1100 | + file.contexts = Google::Apis::StorageV1::Object::Contexts.new( |
| 1101 | + custom: context_custom_hash(custom_context_key: invalid_key, custom_context_value: custom_value) |
| 1102 | + ) |
| 1103 | + }.must_raise Google::Cloud::InvalidArgumentError |
| 1104 | + _(err.message).must_match(/Object context key must start with an alphanumeric character./) |
| 1105 | + |
| 1106 | + invalid_key = "my-custom-key" |
| 1107 | + custom_value = '✨-sparkle' |
| 1108 | + |
| 1109 | + err = _ { |
| 1110 | + file.contexts = Google::Apis::StorageV1::Object::Contexts.new( |
| 1111 | + custom: context_custom_hash(custom_context_key: invalid_key, custom_context_value: custom_value) |
| 1112 | + ) |
| 1113 | + }.must_raise Google::Cloud::InvalidArgumentError |
| 1114 | + |
| 1115 | + _(err.message).must_match(/Object context value must start with an alphanumeric character./) |
| 1116 | + end |
| 1117 | + |
| 1118 | + it "modifies existing custom context key and value" do |
| 1119 | + file = bucket.file file_name |
| 1120 | + file.contexts = Google::Apis::StorageV1::Object::Contexts.new( |
| 1121 | + custom: context_custom_hash(custom_context_key: custom_context_key1 ,custom_context_value: custom_context_value1) |
| 1122 | + ) |
| 1123 | + file.reload! |
| 1124 | + _(file.contexts.custom[custom_context_key1].value).must_equal custom_context_value1 |
| 1125 | + |
| 1126 | + file.contexts = Google::Apis::StorageV1::Object::Contexts.new( |
| 1127 | + custom: context_custom_hash(custom_context_key: custom_context_key1 ,custom_context_value: custom_context_value2) |
| 1128 | + ) |
| 1129 | + file.reload! |
| 1130 | + _(file.contexts.custom[custom_context_key1].value).must_equal custom_context_value2 |
| 1131 | + end |
| 1132 | + |
| 1133 | + it "overwrites existing context key and value" do |
| 1134 | + file = bucket.file file_name |
| 1135 | + file.contexts = Google::Apis::StorageV1::Object::Contexts.new( |
| 1136 | + custom: context_custom_hash(custom_context_key: custom_context_key1 ,custom_context_value: custom_context_value1) |
| 1137 | + ) |
| 1138 | + file.reload! |
| 1139 | + _(file.contexts.custom[custom_context_key1].value).must_equal custom_context_value1 |
| 1140 | + |
| 1141 | + file.contexts = Google::Apis::StorageV1::Object::Contexts.new( |
| 1142 | + custom: context_custom_hash(custom_context_key: custom_context_key2 ,custom_context_value: custom_context_value2) |
| 1143 | + ) |
| 1144 | + file.reload! |
| 1145 | + _(file.contexts.custom[custom_context_key2].value).must_equal custom_context_value2 |
| 1146 | + end |
| 1147 | + |
| 1148 | + it "sets and retrieves multiple custom context keys and values" do |
| 1149 | + file = bucket.file file_name |
| 1150 | + custom_hash1 = context_custom_hash custom_context_key: custom_context_key1, custom_context_value: custom_context_value1 |
| 1151 | + custom_hash2 = context_custom_hash custom_context_key: custom_context_key2, custom_context_value: custom_context_value2 |
| 1152 | + |
| 1153 | + file.contexts = Google::Apis::StorageV1::Object::Contexts.new( |
| 1154 | + custom: { |
| 1155 | + custom_context_key1 => custom_hash1[custom_context_key1], |
| 1156 | + custom_context_key2 => custom_hash2[custom_context_key2] |
| 1157 | + } |
| 1158 | + ) |
| 1159 | + file.reload! |
| 1160 | + _(file.contexts.custom[custom_context_key1].value).must_equal custom_context_value1 |
| 1161 | + _(file.contexts.custom[custom_context_key2].value).must_equal custom_context_value2 |
| 1162 | + end |
| 1163 | + |
| 1164 | + it "removes individual context" do |
| 1165 | + file = bucket.file file_name |
| 1166 | + custom_hash1 = context_custom_hash custom_context_key: custom_context_key1, custom_context_value: custom_context_value1 |
| 1167 | + custom_hash2 = context_custom_hash custom_context_key: custom_context_key2, custom_context_value: custom_context_value2 |
| 1168 | + file.contexts = Google::Apis::StorageV1::Object::Contexts.new( |
| 1169 | + custom: { |
| 1170 | + custom_context_key1 => custom_hash1[custom_context_key1], |
| 1171 | + custom_context_key2 => custom_hash2[custom_context_key2] |
| 1172 | + } |
| 1173 | + ) |
| 1174 | + file.reload! |
| 1175 | + _(file.contexts.custom[custom_context_key1].value).must_equal custom_context_value1 |
| 1176 | + _(file.contexts.custom[custom_context_key2].value).must_equal custom_context_value2 |
| 1177 | + |
| 1178 | + file.contexts = Google::Apis::StorageV1::Object::Contexts.new( |
| 1179 | + custom: { |
| 1180 | + custom_context_key1 => nil |
| 1181 | + } |
| 1182 | + ) |
| 1183 | + file.reload! |
| 1184 | + _(file.contexts.custom[custom_context_key1]).must_be_nil |
| 1185 | + _(file.contexts.custom[custom_context_key2].value).must_equal custom_context_value2 |
| 1186 | + end |
| 1187 | + |
| 1188 | + it "clears all contexts" do |
| 1189 | + file = bucket.file file_name |
| 1190 | + custom_hash1 = context_custom_hash custom_context_key: custom_context_key1, custom_context_value: custom_context_value1 |
| 1191 | + file.contexts = Google::Apis::StorageV1::Object::Contexts.new( |
| 1192 | + custom: { |
| 1193 | + custom_context_key1=> custom_hash1[custom_context_key1] |
| 1194 | + } |
| 1195 | + ) |
| 1196 | + file.reload! |
| 1197 | + _(file.contexts.custom[custom_context_key1].value).must_equal custom_context_value1 |
| 1198 | + |
| 1199 | + file.contexts = nil |
| 1200 | + file.reload! |
| 1201 | + _(file.contexts).must_be_nil |
| 1202 | + end |
| 1203 | + end |
1047 | 1204 | end |
0 commit comments