Skip to content

Commit

Permalink
Fix Active Storage test configurations for CI
Browse files Browse the repository at this point in the history
This change is an up-port of #50787 which applied a similar fix to 7-1-stable.

Since these tests weren't running before, we didn't notice when the DirectUploads controller tests were broken. My theory is that it has something to do with changing `response.parsed_body` to return a HWIA (in #49003).

This change is different from the 7-1-stable PR in that it removes the need to stringify or symbolize any keys, since we are comparing the metadata with string keys. This is a follow up to #43705.

Co-authored-by: Sean Doyle <seanpdoyle@users.noreply.github.com>
Co-authored-by: Jean byroot Boussier <jean.boussier+github@shopify.com>
  • Loading branch information
3 people committed Feb 6, 2024
1 parent 554e5c2 commit 4873347
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
8 changes: 5 additions & 3 deletions activestorage/Rakefile
Expand Up @@ -13,11 +13,13 @@ Rake::TestTask.new do |t|
end

if ENV["encrypted_0fb9444d0374_key"] && ENV["encrypted_0fb9444d0374_iv"]
file "test/service/configurations.yml" do
system "openssl aes-256-cbc -K $encrypted_0fb9444d0374_key -iv $encrypted_0fb9444d0374_iv -in test/service/configurations.yml.enc -out test/service/configurations.yml -d"
config_file = "test/service/configurations.yml"
file config_file do
puts "Generating #{config_file} for Active Storage tests..."
system "openssl aes-256-cbc -K $encrypted_0fb9444d0374_key -iv $encrypted_0fb9444d0374_iv -in #{config_file}.enc -out #{config_file} -d"
end

task test: "test/service/configurations.yml"
task test: config_file
end

task :package
Expand Down
62 changes: 31 additions & 31 deletions activestorage/test/controllers/direct_uploads_controller_test.rb
Expand Up @@ -17,13 +17,13 @@ class ActiveStorage::S3DirectUploadsControllerTest < ActionDispatch::Integration
test "creating new direct upload" do
checksum = OpenSSL::Digest::MD5.base64digest("Hello")
metadata = {
"foo": "bar",
"my_key_1": "my_value_1",
"my_key_2": "my_value_2",
"platform": "my_platform",
"library_ID": "12345",
"custom": {
"my_key_3": "my_value_3"
"foo" => "bar",
"my_key_1" => "my_value_1",
"my_key_2" => "my_value_2",
"platform" => "my_platform",
"library_ID" => "12345",
"custom" => {
"my_key_3" => "my_value_3"
}
}

Expand All @@ -35,7 +35,7 @@ class ActiveStorage::S3DirectUploadsControllerTest < ActionDispatch::Integration
assert_equal "hello.txt", details["filename"]
assert_equal 6, details["byte_size"]
assert_equal checksum, details["checksum"]
assert_equal metadata, details["metadata"].deep_transform_keys(&:to_sym)
assert_equal metadata, details["metadata"]
assert_equal "text/plain", details["content_type"]
assert_match SERVICE_CONFIGURATIONS[:s3][:bucket], details["direct_upload"]["url"]
assert_match(/s3(-[-a-z0-9]+)?\.(\S+)?amazonaws\.com/, details["direct_upload"]["url"])
Expand Down Expand Up @@ -63,25 +63,25 @@ class ActiveStorage::GCSDirectUploadsControllerTest < ActionDispatch::Integratio
test "creating new direct upload" do
checksum = OpenSSL::Digest::MD5.base64digest("Hello")
metadata = {
"foo": "bar",
"my_key_1": "my_value_1",
"my_key_2": "my_value_2",
"platform": "my_platform",
"library_ID": "12345",
"custom": {
"my_key_3": "my_value_3"
"foo" => "bar",
"my_key_1" => "my_value_1",
"my_key_2" => "my_value_2",
"platform" => "my_platform",
"library_ID" => "12345",
"custom" => {
"my_key_3" => "my_value_3"
}
}

post rails_direct_uploads_url, params: { blob: {
filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain", metadata: metadata } }

@response.parsed_body.tap do |details|
response.parsed_body.tap do |details|
assert_equal ActiveStorage::Blob.find(details["id"]), ActiveStorage::Blob.find_signed!(details["signed_id"])
assert_equal "hello.txt", details["filename"]
assert_equal 6, details["byte_size"]
assert_equal checksum, details["checksum"]
assert_equal metadata, details["metadata"].deep_transform_keys(&:to_sym)
assert_equal metadata, details["metadata"]
assert_equal "text/plain", details["content_type"]
assert_match %r{storage\.googleapis\.com/#{@config[:bucket]}}, details["direct_upload"]["url"]
assert_equal({ "Content-MD5" => checksum, "Content-Disposition" => "inline; filename=\"hello.txt\"; filename*=UTF-8''hello.txt", "x-goog-meta-my_key_3" => "my_value_3" }, details["direct_upload"]["headers"])
Expand All @@ -108,22 +108,22 @@ class ActiveStorage::AzureStorageDirectUploadsControllerTest < ActionDispatch::I
test "creating new direct upload" do
checksum = OpenSSL::Digest::MD5.base64digest("Hello")
metadata = {
"foo": "bar",
"my_key_1": "my_value_1",
"my_key_2": "my_value_2",
"platform": "my_platform",
"library_ID": "12345"
"foo" => "bar",
"my_key_1" => "my_value_1",
"my_key_2" => "my_value_2",
"platform" => "my_platform",
"library_ID" => "12345"
}

post rails_direct_uploads_url, params: { blob: {
filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain", metadata: metadata } }

@response.parsed_body.tap do |details|
response.parsed_body.tap do |details|
assert_equal ActiveStorage::Blob.find(details["id"]), ActiveStorage::Blob.find_signed!(details["signed_id"])
assert_equal "hello.txt", details["filename"]
assert_equal 6, details["byte_size"]
assert_equal checksum, details["checksum"]
assert_equal metadata, details["metadata"].deep_transform_keys(&:to_sym)
assert_equal metadata, details["metadata"]
assert_equal "text/plain", details["content_type"]
assert_match %r{#{@config[:storage_account_name]}\.blob\.core\.windows\.net/#{@config[:container]}}, details["direct_upload"]["url"]
assert_equal({ "Content-Type" => "text/plain", "Content-MD5" => checksum, "x-ms-blob-content-disposition" => "inline; filename=\"hello.txt\"; filename*=UTF-8''hello.txt", "x-ms-blob-type" => "BlockBlob" }, details["direct_upload"]["headers"])
Expand All @@ -148,7 +148,7 @@ class ActiveStorage::DiskDirectUploadsControllerTest < ActionDispatch::Integrati
post rails_direct_uploads_url, params: { blob: {
filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain", metadata: metadata } }

@response.parsed_body.tap do |details|
response.parsed_body.tap do |details|
assert_equal ActiveStorage::Blob.find(details["id"]), ActiveStorage::Blob.find_signed!(details["signed_id"])
assert_equal "hello.txt", details["filename"]
assert_equal 6, details["byte_size"]
Expand All @@ -163,19 +163,19 @@ class ActiveStorage::DiskDirectUploadsControllerTest < ActionDispatch::Integrati
test "creating new direct upload does not include root in json" do
checksum = OpenSSL::Digest::MD5.base64digest("Hello")
metadata = {
"foo": "bar",
"my_key_1": "my_value_1",
"my_key_2": "my_value_2",
"platform": "my_platform",
"library_ID": "12345"
"foo" => "bar",
"my_key_1" => "my_value_1",
"my_key_2" => "my_value_2",
"platform" => "my_platform",
"library_ID" => "12345"
}

set_include_root_in_json(true) do
post rails_direct_uploads_url, params: { blob: {
filename: "hello.txt", byte_size: 6, checksum: checksum, content_type: "text/plain", metadata: metadata } }
end

@response.parsed_body.tap do |details|
response.parsed_body.tap do |details|
assert_nil details["blob"]
assert_not_nil details["id"]
end
Expand Down
5 changes: 3 additions & 2 deletions activestorage/test/dummy/config/environments/test.rb
Expand Up @@ -37,9 +37,10 @@
config.active_storage.service = :local

SERVICE_CONFIGURATIONS = begin
ActiveSupport::ConfigurationFile.parse(File.expand_path("service/configurations.yml", __dir__)).deep_symbolize_keys
config_file = Rails.root.join("../service/configurations.yml")
ActiveSupport::ConfigurationFile.parse(config_file, symbolize_names: true)
rescue Errno::ENOENT
puts "Missing service configuration file in test/service/configurations.yml"
puts "Missing service configuration file in #{config_file}"
{}
end
# Azure service tests are currently failing on the main branch.
Expand Down

0 comments on commit 4873347

Please sign in to comment.