robots_txt_spec.rb 1.8 KB

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