闭社主体 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.

102 lines
6.0 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. expect(response).to have_http_status(:success)
  44. expect(response.content_type).to eq 'application/jrd+json'
  45. expect(response.body).to eq "{\"subject\":\"acct:alice@cb6e6126.ngrok.io\",\"aliases\":[\"https://cb6e6126.ngrok.io/@alice\",\"https://cb6e6126.ngrok.io/users/alice\"],\"links\":[{\"rel\":\"http://webfinger.net/rel/profile-page\",\"type\":\"text/html\",\"href\":\"https://cb6e6126.ngrok.io/@alice\"},{\"rel\":\"http://schemas.google.com/g/2010#updates-from\",\"type\":\"application/atom+xml\",\"href\":\"https://cb6e6126.ngrok.io/users/alice.atom\"},{\"rel\":\"self\",\"type\":\"application/activity+json\",\"href\":\"https://cb6e6126.ngrok.io/@alice\"},{\"rel\":\"salmon\",\"href\":\"#{api_salmon_url(alice.id)}\"},{\"rel\":\"magic-public-key\",\"href\":\"data:application/magic-public-key,RSA.x4D6DyZa3zGa1XLhd_VG1bLGvK-Dmyz93WJfWNezIKeSuJkmA0f2NmoOfLUoumq9szN2Xt0GLDX06tDajdYPPXgLtDG0o1qqTrIJ7UTyYhbo94Wotl9iJvEwa5IjP1Mn00YJ_KvFrzKCm15PC7up6r-NtHsqoYS8X1KAqcbnptU=.AQAB\"},{\"rel\":\"http://ostatus.org/schema/1.0/subscribe\",\"template\":\"https://cb6e6126.ngrok.io/authorize_follow?acct={uri}\"}]}"
  46. end
  47. it 'returns JSON when account can be found' do
  48. get :show, params: { resource: alice.to_webfinger_s }, format: :xml
  49. expect(response).to have_http_status(:success)
  50. expect(response.content_type).to eq 'application/xrd+xml'
  51. expect(response.body).to eq <<"XML"
  52. <?xml version="1.0"?>
  53. <XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
  54. <Subject>acct:alice@cb6e6126.ngrok.io</Subject>
  55. <Alias>https://cb6e6126.ngrok.io/@alice</Alias>
  56. <Alias>https://cb6e6126.ngrok.io/users/alice</Alias>
  57. <Link rel="http://webfinger.net/rel/profile-page" type="text/html" href="https://cb6e6126.ngrok.io/@alice"/>
  58. <Link rel="http://schemas.google.com/g/2010#updates-from" type="application/atom+xml" href="https://cb6e6126.ngrok.io/users/alice.atom"/>
  59. <Link rel="salmon" href="#{api_salmon_url(alice.id)}"/>
  60. <Link rel="magic-public-key" href="data:application/magic-public-key,RSA.x4D6DyZa3zGa1XLhd_VG1bLGvK-Dmyz93WJfWNezIKeSuJkmA0f2NmoOfLUoumq9szN2Xt0GLDX06tDajdYPPXgLtDG0o1qqTrIJ7UTyYhbo94Wotl9iJvEwa5IjP1Mn00YJ_KvFrzKCm15PC7up6r-NtHsqoYS8X1KAqcbnptU=.AQAB"/>
  61. <Link rel="http://ostatus.org/schema/1.0/subscribe" template="https://cb6e6126.ngrok.io/authorize_follow?acct={uri}"/>
  62. </XRD>
  63. XML
  64. end
  65. it 'returns http not found when account cannot be found' do
  66. get :show, params: { resource: 'acct:not@existing.com' }, format: :json
  67. expect(response).to have_http_status(:not_found)
  68. end
  69. it 'returns JSON when account can be found with alternate domains' do
  70. Rails.configuration.x.alternate_domains = ["foo.org"]
  71. username, domain = alice.to_webfinger_s.split("@")
  72. get :show, params: { resource: "#{username}@foo.org" }, format: :json
  73. expect(response).to have_http_status(:success)
  74. expect(response.content_type).to eq 'application/jrd+json'
  75. expect(response.body).to eq "{\"subject\":\"acct:alice@cb6e6126.ngrok.io\",\"aliases\":[\"https://cb6e6126.ngrok.io/@alice\",\"https://cb6e6126.ngrok.io/users/alice\"],\"links\":[{\"rel\":\"http://webfinger.net/rel/profile-page\",\"type\":\"text/html\",\"href\":\"https://cb6e6126.ngrok.io/@alice\"},{\"rel\":\"http://schemas.google.com/g/2010#updates-from\",\"type\":\"application/atom+xml\",\"href\":\"https://cb6e6126.ngrok.io/users/alice.atom\"},{\"rel\":\"self\",\"type\":\"application/activity+json\",\"href\":\"https://cb6e6126.ngrok.io/@alice\"},{\"rel\":\"salmon\",\"href\":\"#{api_salmon_url(alice.id)}\"},{\"rel\":\"magic-public-key\",\"href\":\"data:application/magic-public-key,RSA.x4D6DyZa3zGa1XLhd_VG1bLGvK-Dmyz93WJfWNezIKeSuJkmA0f2NmoOfLUoumq9szN2Xt0GLDX06tDajdYPPXgLtDG0o1qqTrIJ7UTyYhbo94Wotl9iJvEwa5IjP1Mn00YJ_KvFrzKCm15PC7up6r-NtHsqoYS8X1KAqcbnptU=.AQAB\"},{\"rel\":\"http://ostatus.org/schema/1.0/subscribe\",\"template\":\"https://cb6e6126.ngrok.io/authorize_follow?acct={uri}\"}]}"
  76. end
  77. it 'returns http not found when account can not be found with alternate domains' do
  78. Rails.configuration.x.alternate_domains = ["foo.org"]
  79. username, domain = alice.to_webfinger_s.split("@")
  80. get :show, params: { resource: "#{username}@bar.org" }, format: :json
  81. expect(response).to have_http_status(:not_found)
  82. end
  83. end
  84. end