API for updating attribution domains (#32730)

This commit is contained in:
Christian Schmidt 2025-01-17 09:18:55 +01:00 committed by GitHub
parent 3af6739f21
commit a3baae0b99
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
108 changed files with 141 additions and 251 deletions

View file

@ -516,38 +516,16 @@ RSpec.describe Account do
end
end
describe '#attribution_domains_as_text=' do
subject { Fabricate(:account) }
it 'sets attribution_domains accordingly' do
subject.attribution_domains_as_text = "hoge.com\nexample.com"
expect(subject.attribution_domains).to contain_exactly('hoge.com', 'example.com')
end
it 'strips leading "*."' do
subject.attribution_domains_as_text = "hoge.com\n*.example.com"
expect(subject.attribution_domains).to contain_exactly('hoge.com', 'example.com')
end
it 'strips the protocol if present' do
subject.attribution_domains_as_text = "http://hoge.com\nhttps://example.com"
expect(subject.attribution_domains).to contain_exactly('hoge.com', 'example.com')
end
it 'strips a combination of leading "*." and protocol' do
subject.attribution_domains_as_text = "http://*.hoge.com\nhttps://*.example.com"
expect(subject.attribution_domains).to contain_exactly('hoge.com', 'example.com')
end
end
describe 'Normalizations' do
describe 'username' do
it { is_expected.to normalize(:username).from(" \u3000bob \t \u00a0 \n ").to('bob') }
end
describe 'attribution_domains' do
it { is_expected.to normalize(:attribution_domains).from(['example.com', ' example.com ', ' example.net ']).to(['example.com', 'example.net']) }
it { is_expected.to normalize(:attribution_domains).from(['https://example.com', 'http://example.net', '*.example.org']).to(['example.com', 'example.net', 'example.org']) }
it { is_expected.to normalize(:attribution_domains).from(['', ' ', nil]).to([]) }
end
end
describe 'Validations' do
@ -598,6 +576,9 @@ RSpec.describe Account do
it { is_expected.to validate_absence_of(:inbox_url).on(:create) }
it { is_expected.to validate_absence_of(:shared_inbox_url).on(:create) }
it { is_expected.to validate_absence_of(:uri).on(:create) }
it { is_expected.to allow_values([], ['example.com'], (1..100).to_a).for(:attribution_domains) }
it { is_expected.to_not allow_values(['example com'], ['@'], (1..101).to_a).for(:attribution_domains) }
end
context 'when account is remote' do

View file

@ -64,6 +64,7 @@ RSpec.describe 'credentials API' do
indexable: true,
locked: false,
note: 'Hello!',
attribution_domains: ['example.com'],
source: {
privacy: 'unlisted',
sensitive: true,
@ -121,7 +122,8 @@ RSpec.describe 'credentials API' do
display_name: eq("Alice Isn't Dead"),
note: 'Hello!',
avatar: exist,
header: exist
header: exist,
attribution_domains: ['example.com']
)
end

View file

@ -15,12 +15,27 @@ RSpec.describe 'Settings verification page' do
.to have_content(verification_summary)
.and have_private_cache_control
fill_in attribution_field, with: 'host.example'
fill_in attribution_field, with: " example.com\n\n https://example.net"
expect { click_on submit_button }
.to(change { user.account.reload.attribution_domains_as_text })
.to(change { user.account.reload.attribution_domains }.to(['example.com', 'example.net']))
expect(page)
.to have_content(success_message)
expect(find_field(attribution_field).value)
.to have_content("example.com\nexample.net")
end
it 'rejects invalid attribution domains' do
visit settings_verification_path
fill_in attribution_field, with: "example.com \n invalid_com"
expect { click_on submit_button }
.to_not(change { user.account.reload.attribution_domains })
expect(page)
.to have_content(I18n.t('activerecord.errors.messages.invalid_domain_on_line', value: 'invalid_com'))
expect(find_field(attribution_field).value)
.to have_content("example.com\ninvalid_com")
end
end
@ -29,6 +44,6 @@ RSpec.describe 'Settings verification page' do
end
def attribution_field
I18n.t('simple_form.labels.account.attribution_domains_as_text')
I18n.t('simple_form.labels.account.attribution_domains')
end
end

View file

@ -1,46 +0,0 @@
# 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