From bb10a669e1129c662ba01f223bd5a0ee7f2a0344 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 14 Jul 2014 18:00:14 +0200 Subject: fix controller refactor and features Also save debug log on failing features --- .../controller_extension/authentication.rb | 23 --------------- app/controllers/controller_extension/errors.rb | 34 ++++++++++++++++++++++ app/controllers/controller_extension/json_file.rb | 1 + .../controller_extension/token_authentication.rb | 2 ++ app/controllers/v1/configs_controller.rb | 2 +- config/initializers/add_controller_methods.rb | 1 + features/config.feature | 5 +--- features/support/hooks.rb | 14 ++++++++- 8 files changed, 53 insertions(+), 29 deletions(-) create mode 100644 app/controllers/controller_extension/errors.rb diff --git a/app/controllers/controller_extension/authentication.rb b/app/controllers/controller_extension/authentication.rb index 687bc6e..e2b24f0 100644 --- a/app/controllers/controller_extension/authentication.rb +++ b/app/controllers/controller_extension/authentication.rb @@ -26,29 +26,6 @@ module ControllerExtension::Authentication redirect_to home_url if logged_in? end - def access_denied - respond_to_error :not_authorized, :forbidden, home_url - end - - def login_required - # Warden will intercept the 401 response and call - # SessionController#unauthenticated instead. - respond_to_error :not_authorized_login, :unauthorized, login_url - end - - def respond_to_error(message, status=nil, redirect=nil) - message = t(message) if message.is_a?(Symbol) - respond_to do |format| - format.html do - redirect_to redirect, alert: message - end - format.json do - status ||= :unprocessable_entity - render json: {error: message}, status: status - end - end - end - def admin? current_user.is_admin? end diff --git a/app/controllers/controller_extension/errors.rb b/app/controllers/controller_extension/errors.rb new file mode 100644 index 0000000..8f8edde --- /dev/null +++ b/app/controllers/controller_extension/errors.rb @@ -0,0 +1,34 @@ +module ControllerExtension::Errors + extend ActiveSupport::Concern + + protected + + def access_denied + respond_to_error :not_authorized, :forbidden, home_url + end + + def login_required + # Warden will intercept the 401 response and call + # SessionController#unauthenticated instead. + respond_to_error :not_authorized_login, :unauthorized, login_url + end + + def not_found + respond_to_error :not_found, :not_found, home_url + end + + + def respond_to_error(message, status=nil, redirect=nil) + error = message + message = t(message) if message.is_a?(Symbol) + respond_to do |format| + format.html do + redirect_to redirect, alert: message + end + format.json do + status ||= :unprocessable_entity + render json: {error: error, message: message}, status: status + end + end + end +end diff --git a/app/controllers/controller_extension/json_file.rb b/app/controllers/controller_extension/json_file.rb index 0cb4b6d..6be919a 100644 --- a/app/controllers/controller_extension/json_file.rb +++ b/app/controllers/controller_extension/json_file.rb @@ -1,5 +1,6 @@ module ControllerExtension::JsonFile extend ActiveSupport::Concern + include ControllerExtension::Errors protected diff --git a/app/controllers/controller_extension/token_authentication.rb b/app/controllers/controller_extension/token_authentication.rb index 1cb6ffa..4ad1977 100644 --- a/app/controllers/controller_extension/token_authentication.rb +++ b/app/controllers/controller_extension/token_authentication.rb @@ -1,6 +1,8 @@ module ControllerExtension::TokenAuthentication extend ActiveSupport::Concern + protected + def token @token ||= authenticate_with_http_token do |token, options| Token.find_by_token(token) diff --git a/app/controllers/v1/configs_controller.rb b/app/controllers/v1/configs_controller.rb index 0b2a64a..accdf5a 100644 --- a/app/controllers/v1/configs_controller.rb +++ b/app/controllers/v1/configs_controller.rb @@ -22,7 +22,7 @@ class V1::ConfigsController < ApiController protected def service_paths - Hash[SERVICES.map{|k,v| [k,"/1/configs/#{str}"] } ] + Hash[SERVICES.map{|k,v| [k,"/1/configs/#{v}"] } ] end def sanitize_filename diff --git a/config/initializers/add_controller_methods.rb b/config/initializers/add_controller_methods.rb index 03e8393..f107544 100644 --- a/config/initializers/add_controller_methods.rb +++ b/config/initializers/add_controller_methods.rb @@ -2,4 +2,5 @@ ActiveSupport.on_load(:application_controller) do include ControllerExtension::Authentication include ControllerExtension::TokenAuthentication include ControllerExtension::Flash + include ControllerExtension::Errors end diff --git a/features/config.feature b/features/config.feature index 6e6c429..066d4c4 100644 --- a/features/config.feature +++ b/features/config.feature @@ -29,10 +29,7 @@ Feature: Download Provider Configuration Scenario: Missing provider config When I send a GET request to "/provider.json" Then the response status should be "404" - And the response should be: - """ - {"error": "not found"} - """ + And the response should have ".error" with the text "not_found" Scenario: Fetch list of available configs When I send a GET request to "/1/configs.json" diff --git a/features/support/hooks.rb b/features/support/hooks.rb index 360f231..19928d8 100644 --- a/features/support/hooks.rb +++ b/features/support/hooks.rb @@ -1,6 +1,18 @@ -After('@tempfile') do +After '@tempfile' do if @tempfile @tempfile.close @tempfile.unlink end end + +After do |scenario| + if scenario.failed? + logfile_path = Rails.root + 'tmp' + logfile_path += "#{scenario.title.gsub(/\s/, '_')}.log" + File.open(logfile_path, 'w') do |test_log| + test_log.puts scenario.title + test_log.puts "=========================" + test_log.puts `tail log/test.log -n 200` + end + end +end -- cgit v1.2.3