custom_path_spec.rb 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. # Custom subdomain is handled by rewriting at web server
  4. # Calling the original /help URL with a custom URL in header to simulate
  5. RSpec.describe 'KnowledgeBase public custom path', type: :request do
  6. let!(:knowledge_base) { create(:knowledge_base, custom_address: custom_address) }
  7. let(:path) { '/path' }
  8. let(:subdomain) { 'subdomain.example.net' }
  9. let(:locale) { knowledge_base.kb_locales.first.system_locale.locale }
  10. shared_examples 'accepting original URL' do
  11. before { fetch }
  12. it { expect(response).to have_http_status(:found) }
  13. it { expect(response).to redirect_to "/help/#{locale}" }
  14. end
  15. context 'with no custom path' do
  16. let(:custom_address) { nil }
  17. it_behaves_like 'accepting original URL'
  18. context 'when called with the subdomain' do
  19. before { fetch subdomain: subdomain, path: '/' }
  20. it { expect(response).to have_http_status(:found) }
  21. it { expect(response).to redirect_to "/help/#{locale}" }
  22. end
  23. end
  24. context 'with custom path' do
  25. let(:custom_address) { path }
  26. it_behaves_like 'accepting original URL'
  27. context 'when called with the path' do
  28. before { fetch path: path }
  29. it { expect(response).to have_http_status(:found) }
  30. it { expect(response).to redirect_to "/path/#{locale}" }
  31. end
  32. context 'when called with a custom port' do
  33. before { fetch path: path, port: 8080 }
  34. it { expect(response).to have_http_status(:found) }
  35. it { expect(response).to redirect_to ":8080/path/#{locale}" }
  36. end
  37. context 'when called with the path and subdomain' do
  38. before { fetch path: path, subdomain: subdomain }
  39. it { expect(response).to have_http_status(:found) }
  40. it { expect(response).to redirect_to "http://subdomain.example.net/path/#{locale}" }
  41. end
  42. end
  43. context 'with custom subdomain' do
  44. let(:custom_address) { subdomain }
  45. it_behaves_like 'accepting original URL'
  46. context 'when called with the subdomain' do
  47. before { fetch subdomain: subdomain, path: '/' }
  48. it { expect(response).to have_http_status(:found) }
  49. it { expect(response).to redirect_to "http://subdomain.example.net/#{locale}" }
  50. end
  51. end
  52. context 'with custom subdomain and path' do
  53. let(:custom_address) { "#{subdomain}#{path}" }
  54. it_behaves_like 'accepting original URL'
  55. context 'when called with the path and subdomain' do
  56. before { fetch path: path, subdomain: subdomain }
  57. it { expect(response).to have_http_status(:found) }
  58. it { expect(response).to redirect_to "http://subdomain.example.net/path/#{locale}" }
  59. end
  60. end
  61. def fetch(path: nil, subdomain: nil, port: nil)
  62. headers = { HTTP_X_ORIGINAL_URL: path, SERVER_NAME: subdomain, SERVER_PORT: port }.compact
  63. get '/help', headers: headers
  64. end
  65. end