2023-02-22 01:55:31 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-12 18:22:38 +02:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe WebfingerResource do
|
2017-04-13 02:17:40 +02:00
|
|
|
around do |example|
|
2017-06-04 17:13:37 +02:00
|
|
|
before_local = Rails.configuration.x.local_domain
|
|
|
|
before_web = Rails.configuration.x.web_domain
|
2017-04-13 02:17:40 +02:00
|
|
|
example.run
|
2017-06-04 17:13:37 +02:00
|
|
|
Rails.configuration.x.local_domain = before_local
|
2017-06-05 10:03:36 +02:00
|
|
|
Rails.configuration.x.web_domain = before_web
|
2017-04-13 02:17:40 +02:00
|
|
|
end
|
|
|
|
|
2017-04-12 18:22:38 +02:00
|
|
|
describe '#username' do
|
|
|
|
describe 'with a URL value' do
|
2017-06-04 17:13:37 +02:00
|
|
|
it 'raises with a route whose controller is not AccountsController' do
|
2017-04-12 18:22:38 +02:00
|
|
|
resource = 'https://example.com/users/alice/other'
|
|
|
|
|
2023-02-18 23:00:17 +01:00
|
|
|
expect do
|
2023-06-06 13:58:33 +02:00
|
|
|
described_class.new(resource).username
|
2023-02-18 23:00:17 +01:00
|
|
|
end.to raise_error(ActiveRecord::RecordNotFound)
|
2017-04-12 18:22:38 +02:00
|
|
|
end
|
|
|
|
|
2017-06-04 17:13:37 +02:00
|
|
|
it 'raises with a route whose action is not show' do
|
|
|
|
resource = 'https://example.com/users/alice'
|
|
|
|
|
|
|
|
recognized = Rails.application.routes.recognize_path(resource)
|
|
|
|
allow(recognized).to receive(:[]).with(:controller).and_return('accounts')
|
|
|
|
allow(recognized).to receive(:[]).with(:username).and_return('alice')
|
2023-07-12 10:20:10 +02:00
|
|
|
allow(recognized).to receive(:[]).with(:action).and_return('create')
|
2017-06-04 17:13:37 +02:00
|
|
|
|
2023-11-07 10:46:28 +01:00
|
|
|
allow(Rails.application.routes).to receive(:recognize_path).with(resource).and_return(recognized)
|
2017-06-04 17:13:37 +02:00
|
|
|
|
2023-02-18 23:00:17 +01:00
|
|
|
expect do
|
2023-06-06 13:58:33 +02:00
|
|
|
described_class.new(resource).username
|
2023-02-18 23:00:17 +01:00
|
|
|
end.to raise_error(ActiveRecord::RecordNotFound)
|
2023-07-12 10:20:10 +02:00
|
|
|
expect(recognized).to have_received(:[]).exactly(3).times
|
2023-11-07 10:46:28 +01:00
|
|
|
|
|
|
|
expect(Rails.application.routes).to have_received(:recognize_path)
|
|
|
|
.with(resource)
|
|
|
|
.at_least(:once)
|
2017-06-04 17:13:37 +02:00
|
|
|
end
|
|
|
|
|
2017-04-12 18:22:38 +02:00
|
|
|
it 'raises with a string that doesnt start with URL' do
|
|
|
|
resource = 'website for http://example.com/users/alice/other'
|
|
|
|
|
2023-02-18 23:00:17 +01:00
|
|
|
expect do
|
2023-06-06 13:58:33 +02:00
|
|
|
described_class.new(resource).username
|
2024-03-13 16:42:39 +01:00
|
|
|
end.to raise_error(described_class::InvalidRequest)
|
2017-04-12 18:22:38 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'finds the username in a valid https route' do
|
|
|
|
resource = 'https://example.com/users/alice'
|
|
|
|
|
2023-06-06 13:58:33 +02:00
|
|
|
result = described_class.new(resource).username
|
2017-04-12 18:22:38 +02:00
|
|
|
expect(result).to eq 'alice'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'finds the username in a mixed case http route' do
|
Fix typos (#19849)
Found via `codespell -q 3 -S ./yarn.lock,./CHANGELOG.md,./AUTHORS.md,./config/locales,./app/javascript/mastodon/locales -L ba,followings,keypair,medias,pattens,pixelx,rememberable,ro,te`
2022-11-08 17:32:03 +01:00
|
|
|
resource = 'HTTp://exAMPLe.com/users/alice'
|
2017-04-12 18:22:38 +02:00
|
|
|
|
2023-06-06 13:58:33 +02:00
|
|
|
result = described_class.new(resource).username
|
2017-04-12 18:22:38 +02:00
|
|
|
expect(result).to eq 'alice'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'finds the username in a valid http route' do
|
|
|
|
resource = 'http://example.com/users/alice'
|
|
|
|
|
2023-06-06 13:58:33 +02:00
|
|
|
result = described_class.new(resource).username
|
2017-04-12 18:22:38 +02:00
|
|
|
expect(result).to eq 'alice'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with a username and hostname value' do
|
|
|
|
it 'raises on a non-local domain' do
|
|
|
|
resource = 'user@remote-host.com'
|
|
|
|
|
2023-02-18 23:00:17 +01:00
|
|
|
expect do
|
2023-06-06 13:58:33 +02:00
|
|
|
described_class.new(resource).username
|
2023-02-18 23:00:17 +01:00
|
|
|
end.to raise_error(ActiveRecord::RecordNotFound)
|
2017-04-12 18:22:38 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'finds username for a local domain' do
|
|
|
|
Rails.configuration.x.local_domain = 'example.com'
|
|
|
|
resource = 'alice@example.com'
|
|
|
|
|
2023-06-06 13:58:33 +02:00
|
|
|
result = described_class.new(resource).username
|
2017-04-12 18:22:38 +02:00
|
|
|
expect(result).to eq 'alice'
|
|
|
|
end
|
2017-06-04 17:13:37 +02:00
|
|
|
|
|
|
|
it 'finds username for a web domain' do
|
|
|
|
Rails.configuration.x.web_domain = 'example.com'
|
|
|
|
resource = 'alice@example.com'
|
|
|
|
|
2023-06-06 13:58:33 +02:00
|
|
|
result = described_class.new(resource).username
|
2017-06-04 17:13:37 +02:00
|
|
|
expect(result).to eq 'alice'
|
|
|
|
end
|
2017-04-12 18:22:38 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with an acct value' do
|
|
|
|
it 'raises on a non-local domain' do
|
|
|
|
resource = 'acct:user@remote-host.com'
|
|
|
|
|
2023-02-18 23:00:17 +01:00
|
|
|
expect do
|
2023-06-06 13:58:33 +02:00
|
|
|
described_class.new(resource).username
|
2023-02-18 23:00:17 +01:00
|
|
|
end.to raise_error(ActiveRecord::RecordNotFound)
|
2017-04-12 18:22:38 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises on a nonsense domain' do
|
|
|
|
resource = 'acct:user@remote-host@remote-hostess.remote.local@remote'
|
|
|
|
|
2023-02-18 23:00:17 +01:00
|
|
|
expect do
|
2023-06-06 13:58:33 +02:00
|
|
|
described_class.new(resource).username
|
2023-02-18 23:00:17 +01:00
|
|
|
end.to raise_error(ActiveRecord::RecordNotFound)
|
2017-04-12 18:22:38 +02:00
|
|
|
end
|
|
|
|
|
2017-06-04 17:13:37 +02:00
|
|
|
it 'finds the username for a local account if the domain is the local one' do
|
2017-04-12 18:22:38 +02:00
|
|
|
Rails.configuration.x.local_domain = 'example.com'
|
|
|
|
resource = 'acct:alice@example.com'
|
|
|
|
|
2023-06-06 13:58:33 +02:00
|
|
|
result = described_class.new(resource).username
|
2017-04-12 18:22:38 +02:00
|
|
|
expect(result).to eq 'alice'
|
|
|
|
end
|
2017-06-04 17:13:37 +02:00
|
|
|
|
|
|
|
it 'finds the username for a local account if the domain is the Web one' do
|
|
|
|
Rails.configuration.x.web_domain = 'example.com'
|
|
|
|
resource = 'acct:alice@example.com'
|
|
|
|
|
2023-06-06 13:58:33 +02:00
|
|
|
result = described_class.new(resource).username
|
2017-06-04 17:13:37 +02:00
|
|
|
expect(result).to eq 'alice'
|
|
|
|
end
|
2017-04-12 18:22:38 +02:00
|
|
|
end
|
2020-05-14 23:28:06 +02:00
|
|
|
|
|
|
|
describe 'with a nonsense resource' do
|
|
|
|
it 'raises InvalidRequest' do
|
|
|
|
resource = 'df/:dfkj'
|
|
|
|
|
2023-02-18 23:00:17 +01:00
|
|
|
expect do
|
2023-06-06 13:58:33 +02:00
|
|
|
described_class.new(resource).username
|
2024-03-13 16:42:39 +01:00
|
|
|
end.to raise_error(described_class::InvalidRequest)
|
2020-05-14 23:28:06 +02:00
|
|
|
end
|
|
|
|
end
|
2017-04-12 18:22:38 +02:00
|
|
|
end
|
|
|
|
end
|