1
0

webfinger_resource_spec.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.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. allow(Rails.application.routes).to receive(:recognize_path).with(resource).and_return(recognized)
  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. expect(Rails.application.routes).to have_received(:recognize_path)
  31. .with(resource)
  32. .at_least(:once)
  33. end
  34. it 'raises with a string that doesnt start with URL' do
  35. resource = 'website for http://example.com/users/alice/other'
  36. expect do
  37. described_class.new(resource).username
  38. end.to raise_error(described_class::InvalidRequest)
  39. end
  40. it 'finds the username in a valid https route' do
  41. resource = 'https://example.com/users/alice'
  42. result = described_class.new(resource).username
  43. expect(result).to eq 'alice'
  44. end
  45. it 'finds the username in a mixed case http route' do
  46. resource = 'HTTp://exAMPLe.com/users/alice'
  47. result = described_class.new(resource).username
  48. expect(result).to eq 'alice'
  49. end
  50. it 'finds the username in a valid http route' do
  51. resource = 'http://example.com/users/alice'
  52. result = described_class.new(resource).username
  53. expect(result).to eq 'alice'
  54. end
  55. end
  56. describe 'with a username and hostname value' do
  57. it 'raises on a non-local domain' do
  58. resource = 'user@remote-host.com'
  59. expect do
  60. described_class.new(resource).username
  61. end.to raise_error(ActiveRecord::RecordNotFound)
  62. end
  63. it 'finds username for a local domain' do
  64. Rails.configuration.x.local_domain = 'example.com'
  65. resource = 'alice@example.com'
  66. result = described_class.new(resource).username
  67. expect(result).to eq 'alice'
  68. end
  69. it 'finds username for a web domain' do
  70. Rails.configuration.x.web_domain = 'example.com'
  71. resource = 'alice@example.com'
  72. result = described_class.new(resource).username
  73. expect(result).to eq 'alice'
  74. end
  75. end
  76. describe 'with an acct value' do
  77. it 'raises on a non-local domain' do
  78. resource = 'acct:user@remote-host.com'
  79. expect do
  80. described_class.new(resource).username
  81. end.to raise_error(ActiveRecord::RecordNotFound)
  82. end
  83. it 'raises on a nonsense domain' do
  84. resource = 'acct:user@remote-host@remote-hostess.remote.local@remote'
  85. expect do
  86. described_class.new(resource).username
  87. end.to raise_error(ActiveRecord::RecordNotFound)
  88. end
  89. it 'finds the username for a local account if the domain is the local one' do
  90. Rails.configuration.x.local_domain = 'example.com'
  91. resource = 'acct:alice@example.com'
  92. result = described_class.new(resource).username
  93. expect(result).to eq 'alice'
  94. end
  95. it 'finds the username for a local account if the domain is the Web one' do
  96. Rails.configuration.x.web_domain = 'example.com'
  97. resource = 'acct:alice@example.com'
  98. result = described_class.new(resource).username
  99. expect(result).to eq 'alice'
  100. end
  101. end
  102. describe 'with a nonsense resource' do
  103. it 'raises InvalidRequest' do
  104. resource = 'df/:dfkj'
  105. expect do
  106. described_class.new(resource).username
  107. end.to raise_error(described_class::InvalidRequest)
  108. end
  109. end
  110. end
  111. end