custom_path_spec.rb 2.8 KB

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