webfinger_controller_spec.rb 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 http not found when account cannot be found' do
  50. get :show, params: { resource: 'acct:not@existing.com' }, format: :json
  51. expect(response).to have_http_status(:not_found)
  52. end
  53. it 'returns JSON when account can be found with alternate domains' do
  54. Rails.configuration.x.alternate_domains = ['foo.org']
  55. username, = alice.to_webfinger_s.split('@')
  56. get :show, params: { resource: "#{username}@foo.org" }, format: :json
  57. json = body_as_json
  58. expect(response).to have_http_status(200)
  59. expect(response.content_type).to eq 'application/jrd+json'
  60. expect(json[:subject]).to eq 'acct:alice@cb6e6126.ngrok.io'
  61. expect(json[:aliases]).to include('https://cb6e6126.ngrok.io/@alice', 'https://cb6e6126.ngrok.io/users/alice')
  62. end
  63. it 'returns http not found when account can not be found with alternate domains' do
  64. Rails.configuration.x.alternate_domains = ['foo.org']
  65. username, = alice.to_webfinger_s.split('@')
  66. get :show, params: { resource: "#{username}@bar.org" }, format: :json
  67. expect(response).to have_http_status(:not_found)
  68. end
  69. end
  70. end