webfinger_controller_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe WellKnown::WebfingerController, type: :controller do
  4. render_views
  5. describe 'GET #show' do
  6. subject do
  7. get :show, params: { resource: resource }, format: :json
  8. end
  9. let(:alternate_domains) { [] }
  10. let(:alice) { Fabricate(:account, username: 'alice') }
  11. let(:resource) { nil }
  12. around(:each) do |example|
  13. tmp = Rails.configuration.x.alternate_domains
  14. Rails.configuration.x.alternate_domains = alternate_domains
  15. example.run
  16. Rails.configuration.x.alternate_domains = tmp
  17. end
  18. shared_examples 'a successful response' do
  19. it 'returns http success' do
  20. expect(response).to have_http_status(200)
  21. end
  22. it 'does not set a Vary header' do
  23. expect(response.headers['Vary']).to be_nil
  24. end
  25. it 'returns application/jrd+json' do
  26. expect(response.media_type).to eq 'application/jrd+json'
  27. end
  28. it 'returns links for the account' do
  29. json = body_as_json
  30. expect(json[:subject]).to eq 'acct:alice@cb6e6126.ngrok.io'
  31. expect(json[:aliases]).to include('https://cb6e6126.ngrok.io/@alice', 'https://cb6e6126.ngrok.io/users/alice')
  32. end
  33. end
  34. context 'when an account exists' do
  35. let(:resource) { alice.to_webfinger_s }
  36. before do
  37. subject
  38. end
  39. it_behaves_like 'a successful response'
  40. end
  41. context 'when an account is temporarily suspended' do
  42. let(:resource) { alice.to_webfinger_s }
  43. before do
  44. alice.suspend!
  45. subject
  46. end
  47. it_behaves_like 'a successful response'
  48. end
  49. context 'when an account is permanently suspended or deleted' do
  50. let(:resource) { alice.to_webfinger_s }
  51. before do
  52. alice.suspend!
  53. alice.deletion_request.destroy
  54. subject
  55. end
  56. it 'returns http gone' do
  57. expect(response).to have_http_status(410)
  58. end
  59. end
  60. context 'when an account is not found' do
  61. let(:resource) { 'acct:not@existing.com' }
  62. before do
  63. subject
  64. end
  65. it 'returns http not found' do
  66. expect(response).to have_http_status(404)
  67. end
  68. end
  69. context 'with an alternate domain' do
  70. let(:alternate_domains) { ['foo.org'] }
  71. before do
  72. subject
  73. end
  74. context 'when an account exists' do
  75. let(:resource) do
  76. username, = alice.to_webfinger_s.split('@')
  77. "#{username}@foo.org"
  78. end
  79. it_behaves_like 'a successful response'
  80. end
  81. context 'when the domain is wrong' do
  82. let(:resource) do
  83. username, = alice.to_webfinger_s.split('@')
  84. "#{username}@bar.org"
  85. end
  86. it 'returns http not found' do
  87. expect(response).to have_http_status(404)
  88. end
  89. end
  90. end
  91. context 'with no resource parameter' do
  92. let(:resource) { nil }
  93. before do
  94. subject
  95. end
  96. it 'returns http bad request' do
  97. expect(response).to have_http_status(400)
  98. end
  99. end
  100. context 'with a nonsense parameter' do
  101. let(:resource) { 'df/:dfkj' }
  102. before do
  103. subject
  104. end
  105. it 'returns http bad request' do
  106. expect(response).to have_http_status(400)
  107. end
  108. end
  109. end
  110. end