Skip to content

Commit

Permalink
Validate required API credentials are there otherwise show error message
Browse files Browse the repository at this point in the history
This prevents a nil value to propagate down to the HTTP client and then
emerge in the error page as an exception.
  • Loading branch information
dmacvicar committed Feb 4, 2018
1 parent 1772a59 commit ce8a003
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class ApplicationController < ActionController::Base

before_action :validate_configuration
before_action :set_language
before_action :set_distributions
before_action :set_baseproject
Expand All @@ -32,6 +33,16 @@ class MissingParameterError < Exception; end
render :template => 'error', :formats => [:html], :layout => layout, :status => 400
end

def validate_configuration
config = Rails.configuration.x
layout = request.xhr? ? false : "application"

if config.api_username.blank? && config.opensuse_cookie.blank?
@message = _("The authentication to the OBS API has not been configured correctly.")
render :template => 'error', :formats => [:html], :layout => layout, :status => 503
end
end

def set_language
set_gettext_locale
requested_locale = FastGettext.locale
Expand Down
17 changes: 17 additions & 0 deletions test/integration/validate_configuration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require File.expand_path('../../test_helper', __FILE__)

class ValidateConfigurationTest < ActionDispatch::IntegrationTest
def test_validate_configuration
# Fake that authentication is not configured
x = Rails.configuration.x.clone
Rails.configuration.x.api_username = nil
Rails.configuration.x.opensuse_cookie = nil

visit '/'
page.assert_text 'The authentication to the OBS API has not been configured correctly.'
assert_equal 503, page.status_code

# Restore normal configuration values for other tests
Rails.configuration.x = x
end
end

0 comments on commit ce8a003

Please sign in to comment.