Browse Source

Refactoring: Error request specs are not in RSpec format.

Thorsten Eckel 5 years ago
parent
commit
4b6839cd82
4 changed files with 103 additions and 139 deletions
  1. 5 22
      app/controllers/tests_controller.rb
  2. 1 5
      config/routes/test.rb
  3. 0 112
      spec/requests/basic_spec.rb
  4. 97 0
      spec/requests/error_spec.rb

+ 5 - 22
app/controllers/tests_controller.rb

@@ -9,29 +9,12 @@ class TestsController < ApplicationController
     render json: result
   end
 
-  # GET /test/unprocessable_entity
-  def error_unprocessable_entity
-    raise Exceptions::UnprocessableEntity, 'some error message'
-  end
-
-  # GET /test/not_authorized
-  def error_not_authorized
-    raise Exceptions::NotAuthorized, 'some error message'
-  end
-
-  # GET /test/ar_not_found
-  def error_ar_not_found
-    raise ActiveRecord::RecordNotFound, 'some error message'
-  end
-
-  # GET /test/standard_error
-  def error_standard_error
-    raise StandardError, 'some error message'
-  end
+  # GET /test/raised_exception
+  def error_raised_exception
+    exception = params.fetch(:exception, 'StandardError')
+    message   = params.fetch(:message, 'no message provided')
 
-  # GET /test/argument_error
-  def error_argument_error
-    raise ArgumentError, 'some error message'
+    raise exception.safe_constantize, message
   end
 
 end

+ 1 - 5
config/routes/test.rb

@@ -25,10 +25,6 @@ Zammad::Application.routes.draw do
   match '/tests_taskbar',                     to: 'tests#taskbar',                    via: :get
   match '/tests_text_module',                 to: 'tests#text_module',                via: :get
   match '/tests/wait/:sec',                   to: 'tests#wait',                       via: :get
-  match '/tests/unprocessable_entity',        to: 'tests#error_unprocessable_entity', via: :get
-  match '/tests/not_authorized',              to: 'tests#error_not_authorized',       via: :get
-  match '/tests/ar_not_found',                to: 'tests#error_ar_not_found',         via: :get
-  match '/tests/standard_error',              to: 'tests#error_standard_error',       via: :get
-  match '/tests/argument_error',              to: 'tests#error_argument_error',       via: :get
+  match '/tests/raised_exception',            to: 'tests#error_raised_exception',     via: :get
 
 end

+ 0 - 112
spec/requests/basic_spec.rb

@@ -1,112 +0,0 @@
-require 'rails_helper'
-
-RSpec.describe 'Basics', type: :request do
-
-  describe 'request handling' do
-
-    it 'does json requests' do
-
-      # 404
-      get '/not_existing_url', as: :json
-      expect(response).to have_http_status(:not_found)
-      expect(json_response).to be_a_kind_of(Hash)
-      expect(json_response['error']).to eq('No route matches [GET] /not_existing_url')
-
-      # 401
-      get '/api/v1/organizations', as: :json
-      expect(response).to have_http_status(:unauthorized)
-      expect(json_response).to be_a_kind_of(Hash)
-      expect(json_response['error']).to eq('authentication failed')
-
-      # 422
-      get '/tests/unprocessable_entity', as: :json
-      expect(response).to have_http_status(:unprocessable_entity)
-      expect(json_response).to be_a_kind_of(Hash)
-      expect(json_response['error']).to eq('some error message')
-
-      # 401
-      get '/tests/not_authorized', as: :json
-      expect(response).to have_http_status(:unauthorized)
-      expect(json_response).to be_a_kind_of(Hash)
-      expect(json_response['error']).to eq('some error message')
-
-      # 401
-      get '/tests/ar_not_found', as: :json
-      expect(response).to have_http_status(:not_found)
-      expect(json_response).to be_a_kind_of(Hash)
-      expect(json_response['error']).to eq('some error message')
-
-      # 500
-      get '/tests/standard_error', as: :json
-      expect(response).to have_http_status(:internal_server_error)
-      expect(json_response).to be_a_kind_of(Hash)
-      expect(json_response['error']).to eq('some error message')
-
-      # 422
-      get '/tests/argument_error', as: :json
-      expect(response).to have_http_status(:unprocessable_entity)
-      expect(json_response).to be_a_kind_of(Hash)
-      expect(json_response['error']).to eq('some error message')
-    end
-
-    it 'does html requests' do
-
-      # 404
-      get '/not_existing_url'
-      expect(response).to have_http_status(:not_found)
-      expect(response.body).to match(/<html/)
-      expect(response.body).to match(%r{<title>404: Not Found</title>})
-      expect(response.body).to match(%r{<h1>404: Requested resource was not found</h1>})
-      expect(response.body).to match(%r{No route matches \[GET\] /not_existing_url})
-
-      # 401
-      get '/api/v1/organizations'
-      expect(response).to have_http_status(:unauthorized)
-      expect(response.body).to match(/<html/)
-      expect(response.body).to match(%r{<title>401: Unauthorized</title>})
-      expect(response.body).to match(%r{<h1>401: Unauthorized</h1>})
-      expect(response.body).to match(/authentication failed/)
-
-      # 422
-      get '/tests/unprocessable_entity'
-      expect(response).to have_http_status(:unprocessable_entity)
-      expect(response.body).to match(/<html/)
-      expect(response.body).to match(%r{<title>422: Unprocessable Entity</title>})
-      expect(response.body).to match(%r{<h1>422: The change you wanted was rejected.</h1>})
-      expect(response.body).to match(/some error message/)
-
-      # 401
-      get '/tests/not_authorized'
-      expect(response).to have_http_status(:unauthorized)
-      expect(response.body).to match(/<html/)
-      expect(response.body).to match(%r{<title>401: Unauthorized</title>})
-      expect(response.body).to match(%r{<h1>401: Unauthorized</h1>})
-      expect(response.body).to match(/some error message/)
-
-      # 401
-      get '/tests/ar_not_found'
-      expect(response).to have_http_status(:not_found)
-      expect(response.body).to match(/<html/)
-      expect(response.body).to match(%r{<title>404: Not Found</title>})
-      expect(response.body).to match(%r{<h1>404: Requested resource was not found</h1>})
-      expect(response.body).to match(/some error message/)
-
-      # 500
-      get '/tests/standard_error'
-      expect(response).to have_http_status(:internal_server_error)
-      expect(response.body).to match(/<html/)
-      expect(response.body).to match(%r{<title>500: Something went wrong</title>})
-      expect(response.body).to match(%r{<h1>500: We're sorry, but something went wrong.</h1>})
-      expect(response.body).to match(/some error message/)
-
-      # 422
-      get '/tests/argument_error'
-      expect(response).to have_http_status(:unprocessable_entity)
-      expect(response.body).to match(/<html/)
-      expect(response.body).to match(%r{<title>422: Unprocessable Entity</title>})
-      expect(response.body).to match(%r{<h1>422: The change you wanted was rejected.</h1>})
-      expect(response.body).to match(/some error message/)
-    end
-  end
-
-end

+ 97 - 0
spec/requests/error_spec.rb

@@ -0,0 +1,97 @@
+require 'rails_helper'
+
+RSpec.describe 'Error handling', type: :request do
+
+  shared_examples 'JSON response format' do
+    let(:as) { :json }
+
+    it { expect(response).to have_http_status(http_status) }
+    it { expect(json_response).to be_a_kind_of(Hash) }
+    it { expect(json_response['error']).to eq(message) }
+  end
+
+  shared_examples 'HTML response format' do
+    let(:as) { :html }
+
+    it { expect(response).to have_http_status(http_status) }
+    it { expect(response.body).to include('<html') }
+    it { expect(response.body).to include("<title>#{title}</title>") }
+    it { expect(response.body).to include("<h1>#{headline}</h1>") }
+    it { expect(response.body).to include(message) }
+  end
+
+  context 'URL route does not exist' do
+
+    before do
+      get '/not_existing_url', as: as
+    end
+
+    let(:message) { 'No route matches [GET] /not_existing_url' }
+    let(:http_status) { :not_found }
+
+    context 'requesting JSON' do
+      include_examples 'JSON response format'
+    end
+
+    context 'requesting HTML' do
+      let(:title) { '404: Not Found' }
+      let(:headline) { '404: Requested resource was not found' }
+
+      include_examples 'HTML response format'
+    end
+  end
+
+  context 'request is not authenticated' do
+
+    before do
+      get '/api/v1/organizations', as: as
+    end
+
+    let(:message) { 'authentication failed' }
+    let(:http_status) { :unauthorized }
+
+    context 'requesting JSON' do
+      include_examples 'JSON response format'
+    end
+
+    context 'requesting HTML' do
+      let(:title) { '401: Unauthorized' }
+      let(:headline) { '401: Unauthorized' }
+
+      include_examples 'HTML response format'
+    end
+  end
+
+  context 'exception is raised' do
+
+    before do
+      get '/tests/raised_exception', params: { exception: exception.name, message: message }, as: as
+    end
+
+    shared_examples 'handles exception' do |exception, http_status, title, headline|
+
+      context "#{exception} is raised" do
+        let(:exception) { exception }
+        let(:http_status) { http_status }
+        let(:message) { 'some error message' }
+
+        context 'requesting JSON' do
+          include_examples 'JSON response format'
+        end
+
+        context 'requesting HTML' do
+          let(:title) { title }
+          let(:headline) { headline }
+
+          include_examples 'HTML response format'
+        end
+      end
+    end
+
+    include_examples 'handles exception', ActiveRecord::RecordNotFound, :not_found, '404: Not Found', '404: Requested resource was not found'
+    include_examples 'handles exception', Exceptions::NotAuthorized, :unauthorized, '401: Unauthorized', '401: Unauthorized'
+    include_examples 'handles exception', Exceptions::UnprocessableEntity, :unprocessable_entity, '422: Unprocessable Entity', '422: The change you wanted was rejected.'
+    include_examples 'handles exception', ArgumentError, :unprocessable_entity, '422: Unprocessable Entity', '422: The change you wanted was rejected.'
+    include_examples 'handles exception', StandardError, :internal_server_error, '500: Something went wrong', "500: We're sorry, but something went wrong."
+  end
+end