error_spec.rb 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Error handling', type: :request do
  4. shared_examples 'JSON response format' do
  5. let(:as) { :json }
  6. it { expect(response).to have_http_status(http_status) }
  7. it { expect(json_response).to be_a_kind_of(Hash) }
  8. it do
  9. # There is a special case where we mask technical errors and return
  10. # a random error code that can be easily found in the logs by an
  11. # administrator. However, this makes it hard to check for the exact error
  12. # message. Therefore we only check for the substring in this particular case
  13. if message == 'Please contact your administrator' || message == 'Mysql2::Error' || message == 'PG::ForeignKeyViolation'
  14. expect(json_response['error']).to include(message)
  15. else
  16. expect(json_response['error']).to eq(message)
  17. end
  18. end
  19. end
  20. shared_examples 'HTML response format' do
  21. let(:as) { :html }
  22. it { expect(response).to have_http_status(http_status) }
  23. it { expect(response.content_type).to start_with('text/html') }
  24. it { expect(response.body).to include('<html') }
  25. it { expect(response.body).to include("<title>#{title}</title>") }
  26. it { expect(response.body).to include("<h1>#{headline}</h1>") }
  27. it { expect(response.body).to include(message) }
  28. end
  29. context 'URL route does not exist' do
  30. before do
  31. get url, as: as
  32. end
  33. let(:url) { '/not_existing_url' }
  34. let(:message) { "No route matches [GET] #{url}" }
  35. let(:http_status) { :not_found }
  36. context 'requesting JSON' do
  37. include_examples 'JSON response format'
  38. end
  39. context 'requesting HTML' do
  40. let(:title) { '404: Not Found' }
  41. let(:headline) { '404: Requested resource was not found' }
  42. include_examples 'HTML response format'
  43. context 'when request ends with URL' do
  44. let(:url) { "//////#{message}" }
  45. let(:message) { 'this__website__is__closed__visit__our__new__site:_someother.com' }
  46. include_examples 'HTML response format'
  47. end
  48. end
  49. end
  50. context 'request is not authenticated' do
  51. before do
  52. authenticated_as(create(:agent), password: 'wrongpw')
  53. get '/api/v1/organizations', as: as
  54. end
  55. let(:message) { 'Invalid BasicAuth credentials' }
  56. let(:http_status) { :unauthorized }
  57. context 'requesting JSON' do
  58. include_examples 'JSON response format'
  59. end
  60. context 'requesting HTML' do
  61. let(:title) { '401: Unauthorized' }
  62. let(:headline) { '401: Unauthorized' }
  63. include_examples 'HTML response format'
  64. end
  65. end
  66. context 'request is forbidden' do
  67. before do
  68. get '/api/v1/organizations', as: as
  69. end
  70. let(:message) { 'Authentication required' }
  71. let(:http_status) { :forbidden }
  72. context 'requesting JSON' do
  73. include_examples 'JSON response format'
  74. end
  75. context 'requesting HTML' do
  76. let(:title) { '403: Forbidden' }
  77. let(:headline) { '403: Forbidden' }
  78. include_examples 'HTML response format'
  79. end
  80. end
  81. context 'exception is raised' do
  82. before do
  83. authenticated_as(create(user))
  84. get '/tests/raised_exception', params: { exception: exception.name, message: message }, as: as
  85. end
  86. shared_examples 'exception check' do |message, exception, http_status, title, headline|
  87. context "#{exception} is raised" do
  88. let(:exception) { exception }
  89. let(:http_status) { http_status }
  90. let(:message) { message }
  91. context 'requesting JSON' do
  92. include_examples 'JSON response format'
  93. end
  94. context 'requesting HTML' do
  95. let(:title) { title }
  96. let(:headline) { headline }
  97. include_examples 'HTML response format'
  98. end
  99. end
  100. end
  101. shared_examples 'handles exception' do |exception, http_status, title, headline, message = 'some error message'|
  102. include_examples 'exception check', message, exception, http_status, title, headline
  103. end
  104. shared_examples 'masks exception' do |exception, http_status, title, headline|
  105. include_examples 'exception check', 'Please contact your administrator', exception, http_status, title, headline
  106. end
  107. context 'with agent user' do
  108. let(:user) { :agent }
  109. include_examples 'handles exception', Exceptions::NotAuthorized, :unauthorized, '401: Unauthorized', '401: Unauthorized'
  110. include_examples 'handles exception', Exceptions::Forbidden, :forbidden, '403: Forbidden', '403: Forbidden'
  111. include_examples 'handles exception', Pundit::NotAuthorizedError, :forbidden, '403: Forbidden', '403: Forbidden', 'Not authorized'
  112. include_examples 'handles exception', ActiveRecord::RecordNotFound, :not_found, '404: Not Found', '404: Requested resource was not found'
  113. include_examples 'handles exception', Exceptions::UnprocessableEntity, :unprocessable_entity, '422: Unprocessable Entity', '422: The change you wanted was rejected.'
  114. include_examples 'masks exception', ArgumentError, :unprocessable_entity, '422: Unprocessable Entity', '422: The change you wanted was rejected.'
  115. include_examples 'masks exception', StandardError, :internal_server_error, '500: Something went wrong', "500: We're sorry, but something went wrong."
  116. end
  117. context 'with admin user' do
  118. let(:user) { :admin }
  119. include_examples 'handles exception', Exceptions::NotAuthorized, :unauthorized, '401: Unauthorized', '401: Unauthorized'
  120. include_examples 'handles exception', Exceptions::Forbidden, :forbidden, '403: Forbidden', '403: Forbidden'
  121. include_examples 'handles exception', Pundit::NotAuthorizedError, :forbidden, '403: Forbidden', '403: Forbidden', 'Not authorized'
  122. include_examples 'handles exception', ActiveRecord::RecordNotFound, :not_found, '404: Not Found', '404: Requested resource was not found'
  123. include_examples 'handles exception', Exceptions::UnprocessableEntity, :unprocessable_entity, '422: Unprocessable Entity', '422: The change you wanted was rejected.'
  124. include_examples 'handles exception', ArgumentError, :unprocessable_entity, '422: Unprocessable Entity', '422: The change you wanted was rejected.'
  125. include_examples 'handles exception', StandardError, :internal_server_error, '500: Something went wrong', "500: We're sorry, but something went wrong."
  126. end
  127. end
  128. end