Rspec Carrierwave/fog/aws

974 views Asked by At

I'm trying to start testing my application (admittedly did not follow good TDD because I'm writing tests after the fact). When I go to run it I'm getting the below error, even though I'm not testing anything anything with the uploader right now.

/home/action/.gem/ruby/2.1.1/gems/fog-core-1.30.0/lib/fog/core/service.rb:244:in `validate_options': Missing required arguments: aws_access_key_id, aws_secret_access_key (ArgumentError) 

I have my aws keys defined properly in an application.yml file using the Figaro gem so I'm not sure why I'm getting this error.

It's all working perfectly well when I upload an image myself from the browser. The only problem comes when I go to run my test suite using rspec I get this error.

carrierwave.rb:

CarrierWave.configure do |config|
  config.fog_credentials = {
    provider:               'AWS',                        # required
    aws_access_key_id:      ENV['aws_access_key_id'],     # required
    aws_secret_access_key:  ENV['aws_secret_access_key']  # required
  }
  config.fog_directory  = 'tracklocal'                     # required
  config.fog_public     = false          # optional, defaults to true
  config.fog_attributes = {'Cache-Control'=>"max-age=#{365.day.to_i}"} # optional, defaults to {}
end

All I'm testing right now is this:

company_spec.rb:

require 'spec_helper'

describe Company do

    it { should validate_presence_of(:company_name)}

end

but I can't even run that test. I'm a noob to testing so any help would be appreciated here.

Full Trace:

/home/action/.gem/ruby/2.1.1/gems/fog-core-1.30.0/lib/fog/core/service.rb:244:in `validate_options': Missing required arguments: aws_access_key_id, aws_secret_access_key (ArgumentError)                                                                                   
    from /home/action/.gem/ruby/2.1.1/gems/fog-core-1.30.0/lib/fog/core/service.rb:268:in `handle_settings'                                                                                                                                                             
    from /home/action/.gem/ruby/2.1.1/gems/fog-core-1.30.0/lib/fog/core/service.rb:98:in `new'                                                                                                                                                                          
    from /home/action/.gem/ruby/2.1.1/gems/fog-core-1.30.0/lib/fog/storage.rb:25:in `new'                                                                                                                                                                               
    from /home/action/.gem/ruby/2.1.1/gems/carrierwave-0.10.0/lib/carrierwave/uploader/configuration.rb:83:in `eager_load_fog'                                                                                                                                          
    from /home/action/.gem/ruby/2.1.1/gems/carrierwave-0.10.0/lib/carrierwave/uploader/configuration.rb:96:in `fog_credentials='                                                                                                                                        
    from /home/action/workspace/servicepay/config/initializers/carrierwave.rb:2:in `block in <top (required)>'                                                                                                                                                          
    from /home/action/.gem/ruby/2.1.1/gems/carrierwave-0.10.0/lib/carrierwave/uploader/configuration.rb:118:in `configure'                                                                                                                                              
    from /home/action/.gem/ruby/2.1.1/gems/carrierwave-0.10.0/lib/carrierwave.rb:14:in `configure'                                                                                                                                                                      
    from /home/action/workspace/servicepay/config/initializers/carrierwave.rb:1:in `<top (required)>'                                                                                                                                                                   
    from /home/action/.gem/ruby/2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load'                                                                                                                                                         
    from /home/action/.gem/ruby/2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `block in load'                                                                                                                                                
    from /home/action/.gem/ruby/2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:232:in `load_dependency'                                                                                                                                              
    from /home/action/.gem/ruby/2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load'                                                                                                                                                         
    from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/engine.rb:648:in `block in load_config_initializer'                                                                                                                                                 
    from /home/action/.gem/ruby/2.1.1/gems/activesupport-4.1.0/lib/active_support/notifications.rb:161:in `instrument'                                                                                                                                                  
    from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/engine.rb:647:in `load_config_initializer'                                                                                                                                                          
    from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/engine.rb:612:in `block (2 levels) in <class:Engine>'                                                                                                                                               
    from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/engine.rb:611:in `each'                                                                                                                                                                             
    from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/engine.rb:611:in `block in <class:Engine>'                                                                                                                                                          
    from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/initializable.rb:30:in `instance_exec'                                                                                                                                                              
    from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/initializable.rb:30:in `run'                                                                                                                                                                        
    from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/initializable.rb:55:in `block in run_initializers'                                                                                                                                                  
    from /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'                                                                                                                                                                
    from /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'                                                                                                                              
    from /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/tsort.rb:418:in `block (2 levels) in each_strongly_connected_component_from'                                                                                                                         
    from /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'                                                                                                                                             
    from /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/tsort.rb:417:in `block in each_strongly_connected_component_from'                                                                                                                                    
    from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/initializable.rb:44:in `each'                                                                                                                                                                       
    from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/initializable.rb:44:in `tsort_each_child'                                                                                                                                                           
    from /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/tsort.rb:411:in `call'                                                                                                                                                                               
    from /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/tsort.rb:411:in `each_strongly_connected_component_from'                                                                                                                                             
    from /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'                                                                                                                                         
    from /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/tsort.rb:345:in `each'                                                                                                                                                                               
    from /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/tsort.rb:345:in `call'                                                                                                                                                                               
    from /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'                                                                                                                                                  
    from /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'                                                                                                                                                                         
    from /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'                                                                                                                                                                         
    from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/initializable.rb:54:in `run_initializers'                                                                                                                                                           
    from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/application.rb:288:in `initialize!'                                                                                                                                                                 
    from /home/action/workspace/servicepay/config/environment.rb:5:in `<top (required)>'                                                                                                                                                                                
    from /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'                                                                                                                                                  
    from /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'                                                                                                                                                  
    from /home/action/workspace/servicepay/spec/spec_helper.rb:3:in `<top (required)>'                                                                                                                                                                                  
    from /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'                         

Updated trace after adding test: scope in application.yml

/home/action/.gem/ruby/2.1.1/gems/fog-core-1.30.0/lib/fog/core/service.rb:244:in `validate_options': Missing required arguments: aws_access_key_id, aws_secret_access_key (ArgumentError)                                                                                   
    from /home/action/.gem/ruby/2.1.1/gems/fog-core-1.30.0/lib/fog/core/service.rb:268:in `handle_settings'                                                                                                                                                             
    from /home/action/.gem/ruby/2.1.1/gems/fog-core-1.30.0/lib/fog/core/service.rb:98:in `new'                                                                                                                                                                          
    from /home/action/.gem/ruby/2.1.1/gems/fog-core-1.30.0/lib/fog/storage.rb:25:in `new'                                                                                                                                                                               
    from /home/action/workspace/servicepay/spec/support/fog_helper.rb:3:in `<top (required)>'                                                                                                                                                                           
    from /home/action/.gem/ruby/2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:247:in `require'                                                                                                                                                      
    from /home/action/.gem/ruby/2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:247:in `block in require'                                                                                                                                             
    from /home/action/.gem/ruby/2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:232:in `load_dependency'                                                                                                                                              
    from /home/action/.gem/ruby/2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:247:in `require'                                                                                                                                                      
    from /home/action/workspace/servicepay/spec/spec_helper.rb:11:in `block in <top (required)>'                                                                                                                                                                        
    from /home/action/workspace/servicepay/spec/spec_helper.rb:11:in `each'                                                                                                                                                                                             
    from /home/action/workspace/servicepay/spec/spec_helper.rb:11:in `<top (required)>'                                                                                                                                                                                 
    from /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'                                                                                                                                                  
    from /home/action/.parts/packages/ruby2.1/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'                                                                                                                                                  
    from /home/action/.gem/ruby/2.1.1/gems/rspec-core-3.3.1/lib/rspec/core/configuration.rb:1280:in `block in requires='                                                                                                                                                
    from /home/action/.gem/ruby/2.1.1/gems/rspec-core-3.3.1/lib/rspec/core/configuration.rb:1280:in `each'                                                                                                                                                              
    from /home/action/.gem/ruby/2.1.1/gems/rspec-core-3.3.1/lib/rspec/core/configuration.rb:1280:in `requires='                                                                                                                                                         
    from /home/action/.gem/ruby/2.1.1/gems/rspec-core-3.3.1/lib/rspec/core/configuration_options.rb:109:in `block in process_options_into'                                                                                                                              
    from /home/action/.gem/ruby/2.1.1/gems/rspec-core-3.3.1/lib/rspec/core/configuration_options.rb:108:in `each'                                                                                                                                                       
    from /home/action/.gem/ruby/2.1.1/gems/rspec-core-3.3.1/lib/rspec/core/configuration_options.rb:108:in `process_options_into'                                                                                                                                       
    from /home/action/.gem/ruby/2.1.1/gems/rspec-core-3.3.1/lib/rspec/core/configuration_options.rb:21:in `configure'                                                                                                                                                   
    from /home/action/.gem/ruby/2.1.1/gems/rspec-core-3.3.1/lib/rspec/core/runner.rb:101:in `setup'                                                                                                                                                                     
    from /home/action/.gem/ruby/2.1.1/gems/rspec-core-3.3.1/lib/rspec/core/runner.rb:88:in `run'                                                                                                                                                                        
    from /home/action/.gem/ruby/2.1.1/gems/rspec-core-3.3.1/lib/rspec/core/runner.rb:73:in `run'                                                                                                                                                                        
    from /home/action/.gem/ruby/2.1.1/gems/rspec-core-3.3.1/lib/rspec/core/runner.rb:41:in `invoke'                                                                                                                                                                     
    from /home/action/.gem/ruby/2.1.1/gems/rspec-core-3.3.1/exe/rspec:4:in `<top (required)>'                                                                                                                                                                           
    from /home/action/.gem/ruby/2.1.1/bin/rspec:23:in `load'                                                                                                                                                                                                            
    from /home/action/.gem/ruby/2.1.1/bin/rspec:23:in `<main>'                             
3

There are 3 answers

1
Yury Lebedev On

You have added your AWS keys to the application.yml, but the carrierwave does not knows anything about them.

You need to create an config/initializers/carrierwave_aws.rb initializer:

CarrierWave.configure do |config|
  config.storage    = :aws
  config.aws_bucket = ENV.fetch('S3_BUCKET_NAME')
  config.aws_acl    = :'public-read'
  config.asset_host = 'http://example.com'
  config.aws_authenticated_url_expiration = 60 * 60 * 24 * 365

  config.aws_credentials = {
    access_key_id:     ENV.fetch('aws_access_key_id'),
    secret_access_key: ENV.fetch('aws_secret_access_key'),
    region:            ENV.fetch('aws_region')
  }
end
5
Ugo Mare On

If you use figaro, are you sure to not using a scope ?

development:
   aws_access_key_id: '#################'
   aws_secret_access_key '##############'

Inaccessible by test environment.

0
Steve Q On

I had to reset everything up from scratch. I'm not quite sure what happened, but I got it working.