webfinger_controller_spec.rb 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe WellKnown::WebfingerController do
  4. include RoutingHelper
  5. render_views
  6. describe 'GET #show' do
  7. subject(:perform_show!) do
  8. get :show, params: { resource: resource }, format: :json
  9. end
  10. let(:alternate_domains) { [] }
  11. let(:alice) { Fabricate(:account, username: 'alice') }
  12. let(:resource) { nil }
  13. around(:each) do |example|
  14. tmp = Rails.configuration.x.alternate_domains
  15. Rails.configuration.x.alternate_domains = alternate_domains
  16. example.run
  17. Rails.configuration.x.alternate_domains = tmp
  18. end
  19. shared_examples 'a successful response' do
  20. it 'returns http success' do
  21. expect(response).to have_http_status(200)
  22. end
  23. it 'does not set a Vary header' do
  24. expect(response.headers['Vary']).to be_nil
  25. end
  26. it 'returns application/jrd+json' do
  27. expect(response.media_type).to eq 'application/jrd+json'
  28. end
  29. it 'returns links for the account' do
  30. json = body_as_json
  31. expect(json[:subject]).to eq 'acct:alice@cb6e6126.ngrok.io'
  32. expect(json[:aliases]).to include('https://cb6e6126.ngrok.io/@alice', 'https://cb6e6126.ngrok.io/users/alice')
  33. end
  34. end
  35. context 'when an account exists' do
  36. let(:resource) { alice.to_webfinger_s }
  37. before do
  38. perform_show!
  39. end
  40. it_behaves_like 'a successful response'
  41. end
  42. context 'when an account is temporarily suspended' do
  43. let(:resource) { alice.to_webfinger_s }
  44. before do
  45. alice.suspend!
  46. perform_show!
  47. end
  48. it_behaves_like 'a successful response'
  49. end
  50. context 'when an account is permanently suspended or deleted' do
  51. let(:resource) { alice.to_webfinger_s }
  52. before do
  53. alice.suspend!
  54. alice.deletion_request.destroy
  55. perform_show!
  56. end
  57. it 'returns http gone' do
  58. expect(response).to have_http_status(410)
  59. end
  60. end
  61. context 'when an account is not found' do
  62. let(:resource) { 'acct:not@existing.com' }
  63. before do
  64. perform_show!
  65. end
  66. it 'returns http not found' do
  67. expect(response).to have_http_status(404)
  68. end
  69. end
  70. context 'with an alternate domain' do
  71. let(:alternate_domains) { ['foo.org'] }
  72. before do
  73. perform_show!
  74. end
  75. context 'when an account exists' do
  76. let(:resource) do
  77. username, = alice.to_webfinger_s.split('@')
  78. "#{username}@foo.org"
  79. end
  80. it_behaves_like 'a successful response'
  81. end
  82. context 'when the domain is wrong' do
  83. let(:resource) do
  84. username, = alice.to_webfinger_s.split('@')
  85. "#{username}@bar.org"
  86. end
  87. it 'returns http not found' do
  88. expect(response).to have_http_status(404)
  89. end
  90. end
  91. end
  92. context 'when the old name scheme is used to query the instance actor' do
  93. let(:resource) do
  94. "#{Rails.configuration.x.local_domain}@#{Rails.configuration.x.local_domain}"
  95. end
  96. before do
  97. perform_show!
  98. end
  99. it 'returns http success' do
  100. expect(response).to have_http_status(200)
  101. end
  102. it 'does not set a Vary header' do
  103. expect(response.headers['Vary']).to be_nil
  104. end
  105. it 'returns application/jrd+json' do
  106. expect(response.media_type).to eq 'application/jrd+json'
  107. end
  108. it 'returns links for the internal account' do
  109. json = body_as_json
  110. expect(json[:subject]).to eq 'acct:mastodon.internal@cb6e6126.ngrok.io'
  111. expect(json[:aliases]).to eq ['https://cb6e6126.ngrok.io/actor']
  112. end
  113. end
  114. context 'with no resource parameter' do
  115. let(:resource) { nil }
  116. before do
  117. perform_show!
  118. end
  119. it 'returns http bad request' do
  120. expect(response).to have_http_status(400)
  121. end
  122. end
  123. context 'with a nonsense parameter' do
  124. let(:resource) { 'df/:dfkj' }
  125. before do
  126. perform_show!
  127. end
  128. it 'returns http bad request' do
  129. expect(response).to have_http_status(400)
  130. end
  131. end
  132. context 'when an account has an avatar' do
  133. let(:alice) { Fabricate(:account, username: 'alice', avatar: attachment_fixture('attachment.jpg')) }
  134. let(:resource) { alice.to_webfinger_s }
  135. it 'returns avatar in response' do
  136. perform_show!
  137. avatar_link = get_avatar_link(body_as_json)
  138. expect(avatar_link).to_not be_nil
  139. expect(avatar_link[:type]).to eq alice.avatar.content_type
  140. expect(avatar_link[:href]).to eq full_asset_url(alice.avatar)
  141. end
  142. context 'with limited federation mode' do
  143. before do
  144. allow(Rails.configuration.x).to receive(:limited_federation_mode).and_return(true)
  145. end
  146. it 'does not return avatar in response' do
  147. perform_show!
  148. avatar_link = get_avatar_link(body_as_json)
  149. expect(avatar_link).to be_nil
  150. end
  151. end
  152. context 'when enabling DISALLOW_UNAUTHENTICATED_API_ACCESS' do
  153. around do |example|
  154. ClimateControl.modify DISALLOW_UNAUTHENTICATED_API_ACCESS: 'true' do
  155. example.run
  156. end
  157. end
  158. it 'does not return avatar in response' do
  159. perform_show!
  160. avatar_link = get_avatar_link(body_as_json)
  161. expect(avatar_link).to be_nil
  162. end
  163. end
  164. end
  165. context 'when an account does not have an avatar' do
  166. let(:alice) { Fabricate(:account, username: 'alice', avatar: nil) }
  167. let(:resource) { alice.to_webfinger_s }
  168. before do
  169. perform_show!
  170. end
  171. it 'does not return avatar in response' do
  172. avatar_link = get_avatar_link(body_as_json)
  173. expect(avatar_link).to be_nil
  174. end
  175. end
  176. end
  177. private
  178. def get_avatar_link(json)
  179. json[:links].find { |link| link[:rel] == 'http://webfinger.net/rel/avatar' }
  180. end
  181. end