闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.7 KiB

  1. require 'rails_helper'
  2. describe WellKnown::WebfingerController, type: :controller do
  3. render_views
  4. describe 'GET #show' do
  5. let(:alice) do
  6. Fabricate(:account, username: 'alice')
  7. end
  8. before do
  9. alice.private_key = <<-PEM
  10. -----BEGIN RSA PRIVATE KEY-----
  11. MIICXQIBAAKBgQDHgPoPJlrfMZrVcuF39UbVssa8r4ObLP3dYl9Y17Mgp5K4mSYD
  12. R/Y2ag58tSi6ar2zM3Ze3QYsNfTq0NqN1g89eAu0MbSjWqpOsgntRPJiFuj3hai2
  13. X2Im8TBrkiM/UyfTRgn8q8WvMoKbXk8Lu6nqv420eyqhhLxfUoCpxuem1QIDAQAB
  14. AoGBAIKsOh2eM7spVI8mdgQKheEG/iEsnPkQ2R8ehfE9JzjmSbXbqghQJDaz9NU+
  15. G3Uu4R31QT0VbCudE9SSA/UPFl82GeQG4QLjrSE+PSjSkuslgSXelJHfAJ+ycGax
  16. ajtPyiQD0e4c2loagHNHPjqK9OhHx9mFnZWmoagjlZ+mQGEpAkEA8GtqfS65IaRQ
  17. uVhMzpp25rF1RWOwaaa+vBPkd7pGdJEQGFWkaR/a9UkU+2C4ZxGBkJDP9FApKVQI
  18. RANEwN3/hwJBANRuw5+es6BgBv4PD387IJvuruW2oUtYP+Lb2Z5k77J13hZTr0db
  19. Oo9j1UbbR0/4g+vAcsDl4JD9c/9LrGYEpcMCQBon9Yvs+2M3lziy7JhFoc3zXIjS
  20. Ea1M4M9hcqe78lJYPeIH3z04o/+vlcLLgQRlmSz7NESmO/QtGkEcAezhuh0CQHji
  21. pzO4LeO/gXslut3eGcpiYuiZquOjToecMBRwv+5AIKd367Che4uJdh6iPcyGURvh
  22. IewfZFFdyZqnx20ui90CQQC1W2rK5Y30wAunOtSLVA30TLK/tKrTppMC3corjKlB
  23. FTX8IvYBNTbpEttc1VCf/0ccnNpfb0CrFNSPWxRj7t7D
  24. -----END RSA PRIVATE KEY-----
  25. PEM
  26. alice.public_key = <<-PEM
  27. -----BEGIN PUBLIC KEY-----
  28. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHgPoPJlrfMZrVcuF39UbVssa8
  29. r4ObLP3dYl9Y17Mgp5K4mSYDR/Y2ag58tSi6ar2zM3Ze3QYsNfTq0NqN1g89eAu0
  30. MbSjWqpOsgntRPJiFuj3hai2X2Im8TBrkiM/UyfTRgn8q8WvMoKbXk8Lu6nqv420
  31. eyqhhLxfUoCpxuem1QIDAQAB
  32. -----END PUBLIC KEY-----
  33. PEM
  34. alice.save!
  35. end
  36. around(:each) do |example|
  37. before = Rails.configuration.x.alternate_domains
  38. example.run
  39. Rails.configuration.x.alternate_domains = before
  40. end
  41. it 'returns JSON when account can be found' do
  42. get :show, params: { resource: alice.to_webfinger_s }, format: :json
  43. json = body_as_json
  44. expect(response).to have_http_status(200)
  45. expect(response.content_type).to eq 'application/jrd+json'
  46. expect(json[:subject]).to eq 'acct:alice@cb6e6126.ngrok.io'
  47. expect(json[:aliases]).to include('https://cb6e6126.ngrok.io/@alice', 'https://cb6e6126.ngrok.io/users/alice')
  48. end
  49. it 'returns JSON when account can be found' do
  50. get :show, params: { resource: alice.to_webfinger_s }, format: :xml
  51. xml = Nokogiri::XML(response.body)
  52. expect(response).to have_http_status(200)
  53. expect(response.content_type).to eq 'application/xrd+xml'
  54. expect(xml.at_xpath('//xmlns:Subject').content).to eq 'acct:alice@cb6e6126.ngrok.io'
  55. expect(xml.xpath('//xmlns:Alias').map(&:content)).to include('https://cb6e6126.ngrok.io/@alice', 'https://cb6e6126.ngrok.io/users/alice')
  56. end
  57. it 'returns http not found when account cannot be found' do
  58. get :show, params: { resource: 'acct:not@existing.com' }, format: :json
  59. expect(response).to have_http_status(:not_found)
  60. end
  61. it 'returns JSON when account can be found with alternate domains' do
  62. Rails.configuration.x.alternate_domains = ['foo.org']
  63. username, = alice.to_webfinger_s.split('@')
  64. get :show, params: { resource: "#{username}@foo.org" }, format: :json
  65. json = body_as_json
  66. expect(response).to have_http_status(200)
  67. expect(response.content_type).to eq 'application/jrd+json'
  68. expect(json[:subject]).to eq 'acct:alice@cb6e6126.ngrok.io'
  69. expect(json[:aliases]).to include('https://cb6e6126.ngrok.io/@alice', 'https://cb6e6126.ngrok.io/users/alice')
  70. end
  71. it 'returns http not found when account can not be found with alternate domains' do
  72. Rails.configuration.x.alternate_domains = ['foo.org']
  73. username, = alice.to_webfinger_s.split('@')
  74. get :show, params: { resource: "#{username}@bar.org" }, format: :json
  75. expect(response).to have_http_status(:not_found)
  76. end
  77. end
  78. end