error_spec.rb 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # Copyright (C) 2012-2024 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(:expected_type) { 'application/json' }
  6. it { expect(response).to have_http_status(http_status) }
  7. it { expect(json_response).to be_a(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(:expected_type) { 'text/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 match("<h1[^>]*>#{headline}</h1>") }
  27. it { expect(response.body).to include(CGI.escapeHTML(message)) }
  28. end
  29. context 'URL route does not exist' do
  30. before do
  31. get url, headers: { 'Accept' => expected_type }
  32. end
  33. let(:url) { '/not_existing_url' }
  34. let(:message) { "This page doesn't exist." }
  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) { '//////this__website__is__closed__visit__our__new__site:_someother.com' }
  45. include_examples 'HTML response format'
  46. end
  47. end
  48. end
  49. context 'request is not authenticated' do
  50. before do
  51. stub_const('Auth::BRUTE_FORCE_SLEEP', 0)
  52. authenticated_as(create(:agent), password: 'wrongpw')
  53. get '/api/v1/organizations', headers: { 'Accept' => expected_type }
  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', headers: { 'Accept' => expected_type }
  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. let(:origin) { 'tests' }
  83. before do
  84. authenticated_as(create(user))
  85. get '/tests/raised_exception', params: { origin: origin, exception: exception.name, message: message }, headers: { 'Accept' => expected_type }
  86. end
  87. shared_examples 'exception check' do |message, exception, http_status, title, headline|
  88. context "#{exception} is raised" do
  89. let(:exception) { exception }
  90. let(:http_status) { http_status }
  91. let(:message) { message }
  92. context 'requesting JSON' do
  93. include_examples 'JSON response format'
  94. end
  95. context 'requesting HTML' do
  96. let(:title) { title }
  97. let(:headline) { headline }
  98. include_examples 'HTML response format'
  99. end
  100. end
  101. end
  102. shared_examples 'handles exception' do |exception, http_status, title, headline, message = 'some error message'|
  103. include_examples 'exception check', message, exception, http_status, title, headline
  104. end
  105. shared_examples 'masks exception' do |exception, http_status, title, headline|
  106. include_examples 'exception check', 'Please contact your administrator', exception, http_status, title, headline
  107. end
  108. context 'with agent user' do
  109. let(:user) { :agent }
  110. include_examples 'handles exception', Exceptions::NotAuthorized, :unauthorized, '401: Unauthorized', '401: Unauthorized'
  111. include_examples 'handles exception', Exceptions::Forbidden, :forbidden, '403: Forbidden', '403: Forbidden'
  112. include_examples 'handles exception', Pundit::NotAuthorizedError, :forbidden, '403: Forbidden', '403: Forbidden', 'Not authorized'
  113. include_examples 'handles exception', ActiveRecord::RecordNotFound, :not_found, '404: Not Found', '404: Requested resource was not found'
  114. include_examples 'handles exception', Exceptions::UnprocessableEntity, :unprocessable_entity, '422: Unprocessable Entity', '422: The change you wanted was rejected.'
  115. include_examples 'masks exception', ArgumentError, :unprocessable_entity, '422: Unprocessable Entity', '422: The change you wanted was rejected.'
  116. include_examples 'masks exception', StandardError, :internal_server_error, '500: An unknown error occurred', '500: An unknown error occurred.'
  117. end
  118. context 'with admin user' do
  119. let(:user) { :admin }
  120. include_examples 'handles exception', Exceptions::NotAuthorized, :unauthorized, '401: Unauthorized', '401: Unauthorized'
  121. include_examples 'handles exception', Exceptions::Forbidden, :forbidden, '403: Forbidden', '403: Forbidden'
  122. include_examples 'handles exception', Pundit::NotAuthorizedError, :forbidden, '403: Forbidden', '403: Forbidden', 'Not authorized'
  123. include_examples 'handles exception', ActiveRecord::RecordNotFound, :not_found, '404: Not Found', '404: Requested resource was not found'
  124. include_examples 'handles exception', Exceptions::UnprocessableEntity, :unprocessable_entity, '422: Unprocessable Entity', '422: The change you wanted was rejected.'
  125. include_examples 'handles exception', ArgumentError, :unprocessable_entity, '422: Unprocessable Entity', '422: The change you wanted was rejected.'
  126. include_examples 'handles exception', StandardError, :internal_server_error, '500: An unknown error occurred', '500: An unknown error occurred.'
  127. end
  128. context 'with mobile controller' do
  129. let(:origin) { 'mobile' }
  130. context 'with agent user' do
  131. let(:user) { :agent }
  132. include_examples 'handles exception', Exceptions::NotAuthorized, :unauthorized, '401: Unauthorized', '401'
  133. include_examples 'handles exception', Exceptions::Forbidden, :forbidden, '403: Forbidden', '403'
  134. include_examples 'handles exception', Pundit::NotAuthorizedError, :forbidden, '403: Forbidden', '403', 'Not authorized'
  135. include_examples 'handles exception', ActiveRecord::RecordNotFound, :not_found, '404: Not Found', '404'
  136. include_examples 'handles exception', Exceptions::UnprocessableEntity, :unprocessable_entity, '422: Unprocessable Entity', '422'
  137. include_examples 'masks exception', ArgumentError, :unprocessable_entity, '422: Unprocessable Entity', '422'
  138. include_examples 'masks exception', StandardError, :internal_server_error, '500: An unknown error occurred', '500'
  139. end
  140. context 'with admin user' do
  141. let(:user) { :admin }
  142. include_examples 'handles exception', Exceptions::NotAuthorized, :unauthorized, '401: Unauthorized', '401'
  143. include_examples 'handles exception', Exceptions::Forbidden, :forbidden, '403: Forbidden', '403'
  144. include_examples 'handles exception', Pundit::NotAuthorizedError, :forbidden, '403: Forbidden', '403', 'Not authorized'
  145. include_examples 'handles exception', ActiveRecord::RecordNotFound, :not_found, '404: Not Found', '404'
  146. include_examples 'handles exception', Exceptions::UnprocessableEntity, :unprocessable_entity, '422: Unprocessable Entity', '422'
  147. include_examples 'handles exception', ArgumentError, :unprocessable_entity, '422: Unprocessable Entity', '422'
  148. include_examples 'handles exception', StandardError, :internal_server_error, '500: An unknown error occurred', '500'
  149. end
  150. end
  151. end
  152. end