Skip to content

Commit

Permalink
Added validation for global active storage service configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ghousemohamed committed May 31, 2022
1 parent 66b9bb9 commit 3c848ca
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
10 changes: 9 additions & 1 deletion activestorage/CHANGELOG.md
@@ -1,4 +1,12 @@
* Fixes proxy downloads of files over 5mb
* Raise an exception if `config.active_storage.service` is not set.

If Active Storage is configured and `config.active_storage.service` is not
set in the respective environment's configuration file, then an exception
is raised with a meaningful message when attempting to use Active Storage.

*Ghouse Mohamed*

* Fixes proxy downloads of files over 5mb

Previously, trying to view and/or download files larger than 5mb stored in
services like S3 via proxy mode could return corrupted files at around
Expand Down
8 changes: 8 additions & 0 deletions activestorage/lib/active_storage/attached/model.rb
Expand Up @@ -215,6 +215,14 @@ def validate_service_configuration(association_name, service)
ActiveStorage::Blob.services.fetch(service) do
raise ArgumentError, "Cannot configure service :#{service} for #{name}##{association_name}"
end
else
validate_global_service_configuration
end
end

def validate_global_service_configuration
if connected? && ActiveStorage::Blob.table_exists? && Rails.configuration.active_storage.service.nil?
raise RuntimeError, "Missing Active Storage service name. Specify Active Storage service name for config.active_storage.service in config/environments/#{Rails.env}.rb"
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions activestorage/test/dummy/config/environments/test.rb
Expand Up @@ -33,6 +33,9 @@
# Disable request forgery protection in test environment.
config.action_controller.allow_forgery_protection = false

# Store uploaded files on the local file system in a temporary directory
config.active_storage.service = :test

# Raises error for missing translations
# config.i18n.raise_on_missing_translations = true

Expand Down
12 changes: 12 additions & 0 deletions activestorage/test/models/attached/many_test.rb
Expand Up @@ -761,6 +761,18 @@ def highlights
assert_equal highlights.third.filename.to_s, @user.highlights.third.filename.to_s
end

test "raises error when global service configuration is missing" do
Rails.configuration.active_storage.stub(:service, nil) do
error = assert_raises RuntimeError do
User.class_eval do
has_one_attached :featured_photos
end
end

assert_match(/Missing Active Storage service name. Specify Active Storage service name for config.active_storage.service in config\/environments\/test.rb/, error.message)
end
end

test "raises error when misconfigured service is passed" do
error = assert_raises ArgumentError do
User.class_eval do
Expand Down
12 changes: 12 additions & 0 deletions activestorage/test/models/attached/one_test.rb
Expand Up @@ -667,6 +667,18 @@ def avatar
end
end

test "raises error when global service configuration is missing" do
Rails.configuration.active_storage.stub(:service, nil) do
error = assert_raises RuntimeError do
User.class_eval do
has_one_attached :featured_photos
end
end

assert_match(/Missing Active Storage service name. Specify Active Storage service name for config.active_storage.service in config\/environments\/test.rb/, error.message)
end
end

test "raises error when misconfigured service is passed" do
error = assert_raises ArgumentError do
User.class_eval do
Expand Down

0 comments on commit 3c848ca

Please sign in to comment.