Skip to content
26 changes: 15 additions & 11 deletions google-cloud-storage/acceptance/storage/bucket_encryption_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@

let :bucket do
b = safe_gcs_execute { storage.bucket(bucket_name) || storage.create_bucket(bucket_name, location: bucket_location) }
b.default_kms_key = kms_key
b.customer_managed_encryption_enforcement_config = customer_managed_config
safe_gcs_execute { b.default_kms_key = kms_key }
safe_gcs_execute { b.customer_managed_encryption_enforcement_config = customer_managed_config }
b
end

Expand Down Expand Up @@ -71,15 +71,15 @@

it "can update its default kms key to another key" do
_(bucket.default_kms_key).must_equal kms_key
bucket.default_kms_key = kms_key_2
safe_gcs_execute { bucket.default_kms_key = kms_key_2 }
_(bucket.default_kms_key).must_equal kms_key_2
bucket.reload!
_(bucket.default_kms_key).must_equal kms_key_2
end

it "can remove its default kms key by setting encryption to nil" do
_(bucket.default_kms_key).must_equal kms_key
bucket.default_kms_key = nil
safe_gcs_execute { bucket.default_kms_key = nil }
_(bucket.default_kms_key).must_be :nil?
bucket.reload!
_(bucket.default_kms_key).must_be :nil?
Expand All @@ -101,10 +101,12 @@
it "updates encryption enforcement configs" do
_(bucket.customer_supplied_encryption_enforcement_config).must_be :nil?

bucket.customer_supplied_encryption_enforcement_config = customer_supplied_config
safe_gcs_execute { bucket.customer_supplied_encryption_enforcement_config = customer_supplied_config }
_(bucket.customer_supplied_encryption_enforcement_config.restriction_mode).must_equal "FullyRestricted"
bucket.update do |b|
b.google_managed_encryption_enforcement_config = google_managed_config
safe_gcs_execute do
bucket.update do |b|
b.google_managed_encryption_enforcement_config = google_managed_config
end
end
_(bucket.google_managed_encryption_enforcement_config.restriction_mode).must_equal "FullyRestricted"

Expand All @@ -114,10 +116,12 @@
end

it "deletes all encryption enforcement configs" do
bucket.update do |b|
b.customer_managed_encryption_enforcement_config = nil
b.customer_supplied_encryption_enforcement_config = nil
b.google_managed_encryption_enforcement_config = nil
safe_gcs_execute do
bucket.update do |b|
b.customer_managed_encryption_enforcement_config = nil
b.customer_supplied_encryption_enforcement_config = nil
b.google_managed_encryption_enforcement_config = nil
end
end
# Removed all encryption enforcement configs without removing default_kms_key
_(bucket.customer_managed_encryption_enforcement_config).must_be :nil?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@

it "generates a signed post object v4 with acl and cache-control file headers" do
fields = {
"acl" => "public-read",
"acl" => "project-private",
"cache-control" => "public,max-age=86400"
}
post_object = bucket.generate_signed_post_policy_v4 "test-object", expires: 10, fields: fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,45 @@
let(:file_name) { "CloudLogo1" }
let(:file_name2) { "CloudLogo2" }

before(:all) do
before do
bucket.files.all { |f| f.delete rescue nil }
bucket.create_file local_file, file_name
bucket.create_file local_file, file_name2
custom_hash1 = context_custom_hash custom_context_key: custom_context_key1, custom_context_value: custom_context_value1
custom_hash2 = context_custom_hash custom_context_key: custom_context_key2, custom_context_value: custom_context_value2
set_object_contexts bucket_name: bucket.name, file_name: file_name, custom_context_key: custom_context_key1, custom_context_value: custom_context_value1
set_object_contexts bucket_name: bucket.name, file_name: file_name2, custom_context_key: custom_context_key2, custom_context_value: custom_context_value2
end

after do
Comment thread
nidhiii-27 marked this conversation as resolved.
Resolved
bucket.files.all { |f| f.delete rescue nil }
end

it "lists objects with a specific context key and value" do
list = bucket.files filter: "contexts.\"#{custom_context_key1}\"=\"#{custom_context_value1}\""
_(list).wont_be :empty?
list.each do |file|
_(file.name).must_equal file_name
end
end

it "lists objects with a specific context key" do
list = bucket.files filter: "contexts.\"#{custom_context_key1}\":*"
_(list).wont_be :empty?
list.each do |file|
_(file.name).must_equal file_name
end
end

it "lists objects that do not have a specific context key" do
list = bucket.files filter: "-contexts.\"#{custom_context_key1}\":*"
_(list).wont_be :empty?
list.each do |file|
_(file.name).wont_equal file_name
end
end

it "lists objects that do not have a specific context key and value" do
list = bucket.files filter: "-contexts.\"#{custom_context_key2}\"=\"#{custom_context_value2}\""
_(list).wont_be :empty?
list.each do |file|
_(file.name).must_equal file_name
_(file.name).wont_equal file_name2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@
assert bucket.uniform_bucket_level_access?
_(bucket.uniform_bucket_level_access_locked_at).must_be_kind_of DateTime

err = expect do
expect do
file.acl.add_reader user_val
end.must_raise Google::Cloud::PermissionDeniedError
_(err.message).must_match /does not have storage.objects.get access to/
end.must_raise Google::Cloud::InvalidArgumentError
end

it "sets uniform_bucket_level_access true and is unable to get the file" do
skip PAP_SKIP_MESSAGE
refute bucket.uniform_bucket_level_access?
file = bucket.create_file local_file, "ReaderTest.png"

Expand Down Expand Up @@ -137,10 +137,9 @@
assert bucket.policy_only?
_(bucket.policy_only_locked_at).must_be_kind_of DateTime

err = expect do
expect do
file.acl.add_reader user_val
end.must_raise Google::Cloud::PermissionDeniedError
_(err.message).must_match /does not have storage.objects.get access to/
end.must_raise Google::Cloud::InvalidArgumentError
end

it "sets DEPRECATED policy_only true and is unable to get the file" do
Expand Down
44 changes: 26 additions & 18 deletions google-cloud-storage/samples/acceptance/acls_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
bucket.files.each(&:delete)
sleep 1
# always reset the bucket permissions
bucket.default_acl.private!
retry_resource_exhaustion { bucket.default_acl.private! }
sleep 1
bucket.acl.private!
retry_resource_exhaustion { bucket.acl.private! }
end

it "print_bucket_acl" do
bucket.acl.add_owner email
retry_resource_exhaustion { bucket.acl.add_owner email }

out, _err = capture_io do
StoragePrintBucketACL.new.print_bucket_acl bucket_name: bucket.name
Expand All @@ -60,7 +60,7 @@
end

it "print_bucket_acl_for_user" do
bucket.acl.add_owner email
retry_resource_exhaustion { bucket.acl.add_owner email }

expected_output = <<~OUTPUT
Permissions for #{email}:
Expand All @@ -74,35 +74,43 @@
end

it "add_bucket_owner" do
assert_output "Added OWNER permission for #{email} to #{bucket.name}\n" do
StorageAddBucketOwner.new.add_bucket_owner bucket_name: bucket.name,
email: email
retry_resource_exhaustion do
assert_output "Added OWNER permission for #{email} to #{bucket.name}\n" do
StorageAddBucketOwner.new.add_bucket_owner bucket_name: bucket.name,
email: email
end
end
assert_includes bucket.acl.owners, email
end

it "remove_bucket_acl" do
bucket.acl.add_owner email
assert_output "Removed ACL permissions for #{email} from #{bucket.name}\n" do
StorageRemoveBucketACL.new.remove_bucket_acl bucket_name: bucket.name,
email: email
retry_resource_exhaustion { bucket.acl.add_owner email }
retry_resource_exhaustion do
assert_output "Removed ACL permissions for #{email} from #{bucket.name}\n" do
StorageRemoveBucketACL.new.remove_bucket_acl bucket_name: bucket.name,
email: email
end
end
refute_includes bucket.acl.owners, email
end

it "add_bucket_default_owner" do
assert_output "Added default OWNER permission for #{email} to #{bucket.name}\n" do
StorageAddBucketDefaultOwner.new.add_bucket_default_owner bucket_name: bucket.name,
email: email
retry_resource_exhaustion do
assert_output "Added default OWNER permission for #{email} to #{bucket.name}\n" do
StorageAddBucketDefaultOwner.new.add_bucket_default_owner bucket_name: bucket.name,
email: email
end
end
assert_includes bucket.default_acl.owners, email
end

it "remove_bucket_default_acl" do
bucket.default_acl.add_owner email
assert_output "Removed default ACL permissions for #{email} from #{bucket.name}\n" do
StorageRemoveBucketDefaultACL.new.remove_bucket_default_acl bucket_name: bucket.name,
email: email
retry_resource_exhaustion { bucket.default_acl.add_owner email }
retry_resource_exhaustion do
assert_output "Removed default ACL permissions for #{email} from #{bucket.name}\n" do
StorageRemoveBucketDefaultACL.new.remove_bucket_default_acl bucket_name: bucket.name,
email: email
end
end
refute_includes bucket.default_acl.owners, email
end
Expand Down
8 changes: 8 additions & 0 deletions google-cloud-storage/samples/acceptance/buckets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,12 @@
end

describe "requester_pays" do
let(:bucket) { create_bucket_helper random_bucket_name }
after { delete_bucket_helper bucket.name }

it "enable_requester_pays, disable_requester_pays, get_requester_pays_status" do
# enable_requester_pays
bucket.user_project = true
bucket.requester_pays = false

assert_output "Requester pays has been enabled for #{bucket.name}\n" do
Expand All @@ -333,6 +337,10 @@
get_requester_pays_status bucket_name: bucket.name
end
refute bucket.requester_pays?
ensure
bucket.user_project = true
bucket.requester_pays = false
bucket.user_project = nil
end
end

Expand Down
17 changes: 11 additions & 6 deletions google-cloud-storage/samples/acceptance/files_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
end

after do
bucket.requester_pays = false
bucket.files.each(&:delete)
end

Expand Down Expand Up @@ -225,20 +224,24 @@ def mock_cipher.random_key
end

it "download_file_requester_pays" do
bucket.requester_pays = true
bucket.create_file local_file, remote_file_name
rp_bucket = create_bucket_helper random_bucket_name
rp_bucket.user_project = true
rp_bucket.requester_pays = true
rp_bucket.create_file local_file, remote_file_name

Tempfile.open [downloaded_file] do |tmpfile|
tmpfile.binmode

assert_output "Downloaded #{remote_file_name} using billing project #{storage_client.project}\n" do
download_file_requester_pays bucket_name: bucket.name,
download_file_requester_pays bucket_name: rp_bucket.name,
file_name: remote_file_name,
local_file_path: tmpfile
end

assert File.file? tmpfile
end
ensure
delete_bucket_helper rp_bucket.name if rp_bucket
end

it "download_encrypted_file" do
Expand Down Expand Up @@ -339,10 +342,12 @@ def mock_cipher.random_key
set_object_contexts bucket_name: bucket.name, file_name: remote_file_name, custom_context_key: custom_context_key2, custom_context_value: custom_context_value2
end
it "fetches all object contexts" do

assert_output "Custom Contexts for #{remote_file_name} are:\nKey: #{custom_context_key1}, Value: #{custom_context_value1}\nKey: #{custom_context_key2}, Value: #{custom_context_value2}\n" do
out, _err = capture_io do
get_object_contexts bucket_name: bucket.name, file_name: remote_file_name
end
assert_includes out, "Custom Contexts for #{remote_file_name} are:\n"
assert_includes out, "Key: #{custom_context_key1}, Value: #{custom_context_value1}\n"
assert_includes out, "Key: #{custom_context_key2}, Value: #{custom_context_value2}\n"
end
end

Expand Down
4 changes: 2 additions & 2 deletions google-cloud-storage/samples/acceptance/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def create_bucket_helper bucket_name
def delete_bucket_helper bucket_name
storage_client = Google::Cloud::Storage.new
retry_resource_exhaustion do
bucket = storage_client.bucket bucket_name
bucket = storage_client.bucket bucket_name, user_project: true
return unless bucket

bucket.files.each(&:delete)
Expand Down Expand Up @@ -126,7 +126,7 @@ def random_topic_name

def clean_up_fixture_bucket
storage_client = Google::Cloud::Storage.new
if (b = storage_client.bucket $fixture_bucket_name)
if (b = storage_client.bucket $fixture_bucket_name, user_project: true)
puts "Deleting fixture bucket #{$fixture_bucket_name} for #{storage_client.project_id}"
b.files(versions: true).all do |file|
file.delete generation: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def disable_requester_pays bucket_name:
require "google/cloud/storage"

storage = Google::Cloud::Storage.new
bucket = storage.bucket bucket_name
bucket = storage.bucket bucket_name, skip_lookup: true, user_project: true

bucket.requester_pays = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_requester_pays_status bucket_name:
require "google/cloud/storage"

storage = Google::Cloud::Storage.new
bucket = storage.bucket bucket_name
bucket = storage.bucket bucket_name, user_project: true

if bucket.requester_pays
puts "Requester pays status is enabled for #{bucket_name}"
Expand Down
2 changes: 1 addition & 1 deletion google-cloud-storage/samples/storage_list_buckets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def list_buckets

storage = Google::Cloud::Storage.new

storage.buckets.each do |bucket|
storage.buckets.all do |bucket|
puts bucket.name
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def file.crc32c
file.gapi.crc32c = Digest::CRC32c.base64digest gzipped_data

Tempfile.open "google-cloud" do |tmpfile|
tmpfile.binmode
# write to the file since the mocked call won't
tmpfile.write gzipped_data
tmpfile.rewind
Expand All @@ -267,6 +268,7 @@ def file.crc32c
file.gapi.crc32c = Digest::CRC32c.base64digest gzipped_data

Tempfile.open "google-cloud" do |tmpfile|
tmpfile.binmode
# write to the file since the mocked call won't
tmpfile.write gzipped_data
tmpfile.rewind
Expand Down Expand Up @@ -1614,7 +1616,7 @@ def file_user_project.sleep *args
end

def gzip_data data
gz = StringIO.new("")
gz = StringIO.new "".b
z = Zlib::GzipWriter.new(gz)
z.write data
z.close # write the gzip footer
Expand Down
Loading