webfinger_resource_spec.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe WebfingerResource do
  4. around do |example|
  5. before_local = Rails.configuration.x.local_domain
  6. before_web = Rails.configuration.x.web_domain
  7. example.run
  8. Rails.configuration.x.local_domain = before_local
  9. Rails.configuration.x.web_domain = before_web
  10. end
  11. describe '#username' do
  12. describe 'with a URL value' do
  13. it 'raises with a route whose controller is not AccountsController' do
  14. resource = 'https://example.com/users/alice/other'
  15. expect do
  16. described_class.new(resource).username
  17. end.to raise_error(ActiveRecord::RecordNotFound)
  18. end
  19. it 'raises with a route whose action is not show' do
  20. resource = 'https://example.com/users/alice'
  21. recognized = Rails.application.routes.recognize_path(resource)
  22. allow(recognized).to receive(:[]).with(:controller).and_return('accounts')
  23. allow(recognized).to receive(:[]).with(:username).and_return('alice')
  24. allow(recognized).to receive(:[]).with(:action).and_return('create')
  25. expect(Rails.application.routes).to receive(:recognize_path).with(resource).and_return(recognized).at_least(:once)
  26. expect do
  27. described_class.new(resource).username
  28. end.to raise_error(ActiveRecord::RecordNotFound)
  29. expect(recognized).to have_received(:[]).exactly(3).times
  30. end
  31. it 'raises with a string that doesnt start with URL' do
  32. resource = 'website for http://example.com/users/alice/other'
  33. expect do
  34. described_class.new(resource).username
  35. end.to raise_error(WebfingerResource::InvalidRequest)
  36. end
  37. it 'finds the username in a valid https route' do
  38. resource = 'https://example.com/users/alice'
  39. result = described_class.new(resource).username
  40. expect(result).to eq 'alice'
  41. end
  42. it 'finds the username in a mixed case http route' do
  43. resource = 'HTTp://exAMPLe.com/users/alice'
  44. result = described_class.new(resource).username
  45. expect(result).to eq 'alice'
  46. end
  47. it 'finds the username in a valid http route' do
  48. resource = 'http://example.com/users/alice'
  49. result = described_class.new(resource).username
  50. expect(result).to eq 'alice'
  51. end
  52. end
  53. describe 'with a username and hostname value' do
  54. it 'raises on a non-local domain' do
  55. resource = 'user@remote-host.com'
  56. expect do
  57. described_class.new(resource).username
  58. end.to raise_error(ActiveRecord::RecordNotFound)
  59. end
  60. it 'finds username for a local domain' do
  61. Rails.configuration.x.local_domain = 'example.com'
  62. resource = 'alice@example.com'
  63. result = described_class.new(resource).username
  64. expect(result).to eq 'alice'
  65. end
  66. it 'finds username for a web domain' do
  67. Rails.configuration.x.web_domain = 'example.com'
  68. resource = 'alice@example.com'
  69. result = described_class.new(resource).username
  70. expect(result).to eq 'alice'
  71. end
  72. end
  73. describe 'with an acct value' do
  74. it 'raises on a non-local domain' do
  75. resource = 'acct:user@remote-host.com'
  76. expect do
  77. described_class.new(resource).username
  78. end.to raise_error(ActiveRecord::RecordNotFound)
  79. end
  80. it 'raises on a nonsense domain' do
  81. resource = 'acct:user@remote-host@remote-hostess.remote.local@remote'
  82. expect do
  83. described_class.new(resource).username
  84. end.to raise_error(ActiveRecord::RecordNotFound)
  85. end
  86. it 'finds the username for a local account if the domain is the local one' do
  87. Rails.configuration.x.local_domain = 'example.com'
  88. resource = 'acct:alice@example.com'
  89. result = described_class.new(resource).username
  90. expect(result).to eq 'alice'
  91. end
  92. it 'finds the username for a local account if the domain is the Web one' do
  93. Rails.configuration.x.web_domain = 'example.com'
  94. resource = 'acct:alice@example.com'
  95. result = described_class.new(resource).username
  96. expect(result).to eq 'alice'
  97. end
  98. end
  99. describe 'with a nonsense resource' do
  100. it 'raises InvalidRequest' do
  101. resource = 'df/:dfkj'
  102. expect do
  103. described_class.new(resource).username
  104. end.to raise_error(WebfingerResource::InvalidRequest)
  105. end
  106. end
  107. end
  108. end