robots_txt_spec.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'RobotsTxt', type: :request do
  4. shared_examples 'returns default robot instructions' do
  5. it 'returns default robot instructions' do
  6. expect(response.body).to match(%r{^Allow: /help/$}).and match(%r{^Disallow: /$})
  7. end
  8. end
  9. context 'when no Knowledge Base exists' do
  10. before do
  11. get '/robots.txt'
  12. end
  13. it 'returns success' do
  14. expect(response).to have_http_status(:ok)
  15. end
  16. it 'returns text' do
  17. expect(response.content_type).to start_with('text/plain')
  18. end
  19. include_examples 'returns default robot instructions'
  20. end
  21. context 'when Knowledge Base exists' do
  22. let(:custom_address) { nil }
  23. let(:server_name) { Setting.get('fqdn') }
  24. before do
  25. create(:knowledge_base, custom_address: custom_address)
  26. get '/robots.txt', headers: { SERVER_NAME: server_name }
  27. end
  28. include_examples 'returns default robot instructions'
  29. context 'when custom path is configured' do
  30. let(:custom_address) { '/knowledge_base' }
  31. it 'returns rules with custom path' do
  32. expect(response.body).to match(%r{^Allow: /knowledge_base$}).and match(%r{^Disallow: /$})
  33. end
  34. end
  35. context 'when custom domain is configured' do
  36. let(:custom_address) { 'kb.com/knowledge_base' }
  37. context 'when requesting main domain' do
  38. include_examples 'returns default robot instructions'
  39. end
  40. context 'when requesting KB domain' do
  41. let(:server_name) { 'kb.com' }
  42. it 'returns domain rules' do
  43. expect(response.body).to match(%r{^Allow: /$}).and satisfy { |val| !val.match?(%r{^Disallow}) }
  44. end
  45. end
  46. end
  47. end
  48. end