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.

137 lines
4.3 KiB

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