Add ability to manage which websites can credit you in link previews (#31819)

This commit is contained in:
Eugen Rochko 2024-09-10 14:00:40 +02:00 committed by GitHub
parent 3929e3c6d2
commit e0c27a5047
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
92 changed files with 381 additions and 160 deletions

View file

@ -95,6 +95,7 @@ RSpec.describe Settings::MigrationsController do
before do
moved_to = Fabricate(:account, also_known_as: [ActivityPub::TagManager.instance.uri_for(user.account)])
p moved_to.acct
user.account.migrations.create!(acct: moved_to.acct)
end

View file

@ -747,6 +747,22 @@ RSpec.describe Account do
end
end
describe '#can_be_attributed_from?' do
subject { Fabricate(:account, attribution_domains: %w(example.com)) }
it 'returns true for a matching domain' do
expect(subject.can_be_attributed_from?('example.com')).to be true
end
it 'returns true for a subdomain of a domain' do
expect(subject.can_be_attributed_from?('foo.example.com')).to be true
end
it 'returns false for a non-matching domain' do
expect(subject.can_be_attributed_from?('hoge.com')).to be false
end
end
describe 'Normalizations' do
describe 'username' do
it { is_expected.to normalize(:username).from(" \u3000bob \t \u00a0 \n ").to('bob') }

View file

@ -63,6 +63,26 @@ RSpec.describe ActivityPub::ProcessAccountService do
end
end
context 'with attribution domains' do
let(:payload) do
{
id: 'https://foo.test',
type: 'Actor',
inbox: 'https://foo.test/inbox',
attributionDomains: [
'example.com',
],
}.with_indifferent_access
end
it 'parses attribution domains' do
account = subject.call('alice', 'example.com', payload)
expect(account.attribution_domains)
.to match_array(%w(example.com))
end
end
context 'when account is not suspended' do
subject { described_class.new.call(account.username, account.domain, payload) }

View file

@ -274,7 +274,7 @@ RSpec.describe BulkImportService do
let(:rows) do
[
{ 'domain' => 'blocked.com' },
{ 'domain' => 'to_block.com' },
{ 'domain' => 'to-block.com' },
]
end
@ -286,7 +286,7 @@ RSpec.describe BulkImportService do
it 'blocks all the new domains' do
subject.call(import)
expect(account.domain_blocks.pluck(:domain)).to contain_exactly('alreadyblocked.com', 'blocked.com', 'to_block.com')
expect(account.domain_blocks.pluck(:domain)).to contain_exactly('alreadyblocked.com', 'blocked.com', 'to-block.com')
end
it 'marks the import as finished' do
@ -302,7 +302,7 @@ RSpec.describe BulkImportService do
let(:rows) do
[
{ 'domain' => 'blocked.com' },
{ 'domain' => 'to_block.com' },
{ 'domain' => 'to-block.com' },
]
end
@ -314,7 +314,7 @@ RSpec.describe BulkImportService do
it 'blocks all the new domains' do
subject.call(import)
expect(account.domain_blocks.pluck(:domain)).to contain_exactly('blocked.com', 'to_block.com')
expect(account.domain_blocks.pluck(:domain)).to contain_exactly('blocked.com', 'to-block.com')
end
it 'marks the import as finished' do

View file

@ -0,0 +1,125 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe DomainValidator do
let(:record) { record_class.new }
context 'with no options' do
let(:record_class) do
Class.new do
include ActiveModel::Validations
attr_accessor :domain
validates :domain, domain: true
end
end
describe '#validate_each' do
context 'with a nil value' do
it 'does not add errors' do
record.domain = nil
expect(record).to be_valid
expect(record.errors).to be_empty
end
end
context 'with a valid domain' do
it 'does not add errors' do
record.domain = 'example.com'
expect(record).to be_valid
expect(record.errors).to be_empty
end
end
context 'with a domain that is too long' do
it 'adds an error' do
record.domain = "#{'a' * 300}.com"
expect(record).to_not be_valid
expect(record.errors.where(:domain)).to_not be_empty
end
end
context 'with a domain with an empty segment' do
it 'adds an error' do
record.domain = '.example.com'
expect(record).to_not be_valid
expect(record.errors.where(:domain)).to_not be_empty
end
end
context 'with a domain with an invalid character' do
it 'adds an error' do
record.domain = '*.example.com'
expect(record).to_not be_valid
expect(record.errors.where(:domain)).to_not be_empty
end
end
context 'with a domain that would fail parsing' do
it 'adds an error' do
record.domain = '/'
expect(record).to_not be_valid
expect(record.errors.where(:domain)).to_not be_empty
end
end
end
end
context 'with acct option' do
let(:record_class) do
Class.new do
include ActiveModel::Validations
attr_accessor :acct
validates :acct, domain: { acct: true }
end
end
describe '#validate_each' do
context 'with a nil value' do
it 'does not add errors' do
record.acct = nil
expect(record).to be_valid
expect(record.errors).to be_empty
end
end
context 'with no domain' do
it 'does not add errors' do
record.acct = 'hoge_123'
expect(record).to be_valid
expect(record.errors).to be_empty
end
end
context 'with a valid domain' do
it 'does not add errors' do
record.acct = 'hoge_123@example.com'
expect(record).to be_valid
expect(record.errors).to be_empty
end
end
context 'with an invalid domain' do
it 'adds an error' do
record.acct = 'hoge_123@.example.com'
expect(record).to_not be_valid
expect(record.errors.where(:acct)).to_not be_empty
end
end
end
end
end

View file

@ -0,0 +1,46 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe LinesValidator do
let(:record_class) do
Class.new do
include ActiveModel::Validations
attr_accessor :text
validates :text, lines: { maximum: 5 }
end
end
let(:record) { record_class.new }
describe '#validate_each' do
context 'with a nil value' do
it 'does not add errors' do
record.text = nil
expect(record).to be_valid
expect(record.errors).to be_empty
end
end
context 'with lines below the limit' do
it 'does not add errors' do
record.text = "hoge\n" * 5
expect(record).to be_valid
expect(record.errors).to be_empty
end
end
context 'with more lines than limit' do
it 'adds an error' do
record.text = "hoge\n" * 6
expect(record).to_not be_valid
expect(record.errors.where(:text)).to_not be_empty
end
end
end
end