Skip to content

Commit b4e1f13

Browse files
updating tests
1 parent e493d07 commit b4e1f13

3 files changed

Lines changed: 66 additions & 27 deletions

File tree

google-cloud-storage/lib/google/cloud/storage/bucket.rb

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -761,30 +761,50 @@ def customer_managed_encryption_enforcement_config= new_customer_managed_encrypt
761761
patch_gapi! :encryption
762762
end
763763

764-
# Updates the bucket's encryption enforcement configuration.
765-
#
766-
# This method applies a patch to the bucket's encryption settings using the
767-
# provided configuration.
768-
#
769-
# @param incoming_config [Hash, Google::Apis::StorageV1::Bucket::Encryption]
770-
# The encryption configuration to apply. If a Hash is provided, it should
771-
# contain keys corresponding to the encryption enforcement types.
772-
#
773-
# @example Updating to Google-Managed Encryption
774-
# storage = Google::Cloud::Storage.new
775-
# bucket = storage.bucket "my-bucket"
776-
#
777-
# new_config = {
778-
# google_managed_encryption_enforcement_config: { restriction_mode: "NotRestricted" }
779-
# }
780-
#
781-
# bucket.update_bucket_encryption_enforcement_config new_config
782-
#
783-
# @return [void]
784-
# @raise [Google::Cloud::Error] If the API request fails (e.g., insufficient permissions).
785-
def update_bucket_encryption_enforcement_config incoming_config
786-
patch_gapi! :encryption, bucket_encryption_config: incoming_config
787-
end
764+
# Updates the bucket's encryption enforcement configuration.
765+
#
766+
# This method applies a patch to the bucket's encryption settings using the
767+
# provided configuration.
768+
#
769+
# @param incoming_config [Hash, Google::Apis::StorageV1::Bucket::Encryption]
770+
# The encryption configuration to apply. If a Hash is provided, it should
771+
# contain keys corresponding to the encryption enforcement types.
772+
#
773+
# @example Updating to Google-Managed Encryption
774+
# storage = Google::Cloud::Storage.new
775+
# bucket = storage.bucket "my-bucket"
776+
#
777+
# new_config = {
778+
# google_managed_encryption_enforcement_config: { restriction_mode: "NotRestricted" }
779+
# }
780+
#
781+
# bucket.update_bucket_encryption_enforcement_config new_config
782+
#
783+
# @return [void]
784+
# @raise [Google::Cloud::Error] If the API request fails (e.g., insufficient permissions).
785+
def update_bucket_encryption_enforcement_config incoming_config
786+
allowed_keys = [
787+
:google_managed_encryption_enforcement_config,
788+
:customer_managed_encryption_enforcement_config,
789+
:customer_supplied_encryption_enforcement_config
790+
]
791+
# binding.pry
792+
if incoming_config.is_a? Hash
793+
input_keys = incoming_config.keys
794+
795+
raise ArgumentError, "Config cannot be empty" if input_keys.empty?
796+
797+
extra_keys = input_keys - allowed_keys
798+
unless extra_keys.empty?
799+
raise ArgumentError, "Invalid config detected: #{extra_keys.join(', ')}. " \
800+
"Only #{allowed_keys.join(', ')} are allowed."
801+
end
802+
else
803+
raise ArgumentError, "incoming_config must be a Hash"
804+
end
805+
806+
patch_gapi! :encryption, bucket_encryption_config: incoming_config
807+
end
788808

789809
##
790810
# The bucket's encryption configuration for customer-supplied encryption keys.

google-cloud-storage/samples/storage_update_bucket_encryption_enforcement_config.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ def update_bucket_encryption_enforcement_config bucket_name:
2323
storage = Google::Cloud::Storage.new
2424
bucket = storage.bucket bucket_name
2525
# Update a specific type (e.g., change GMEK to NotRestricted)
26-
google_managed_config = {
26+
new_config = {
2727
google_managed_encryption_enforcement_config: { restriction_mode: "NotRestricted" }
2828
}
29-
30-
bucket.update_bucket_encryption_enforcement_config google_managed_config
29+
30+
bucket.update_bucket_encryption_enforcement_config new_config
3131

3232
puts "Updated google_managed_config to " \
3333
"#{bucket.google_managed_encryption_enforcement_config.restriction_mode} " \

google-cloud-storage/test/google/cloud/storage/bucket_encryption_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,25 @@
177177
mock.verify
178178
end
179179

180+
it "raises error when invalid config is provided for update " do
181+
config = {
182+
wrongconfig: { restriction_mode: "NotRestricted" }
183+
}
184+
err = assert_raises(ArgumentError) do
185+
bucket.update_bucket_encryption_enforcement_config(config)
186+
end
187+
assert_match /Invalid config detected: wrongconfig/, err.message
188+
189+
end
190+
191+
it "raises error when a Hash in not provided for update " do
192+
config = "wrongconfig"
193+
err = assert_raises(ArgumentError) do
194+
bucket.update_bucket_encryption_enforcement_config(config)
195+
end
196+
assert /incoming_config must be a Hash/
197+
198+
end
180199

181200
it "deletes all encryption enforcement configs together and preserves default_kms_key" do
182201
mock = Minitest::Mock.new

0 commit comments

Comments
 (0)