Merge remote-tracking branch 'parent/main' into upstream-20240117

This commit is contained in:
KMY 2025-01-17 16:29:11 +09:00
commit 5d79bd078c
150 changed files with 2982 additions and 1485 deletions

View file

@ -3,6 +3,7 @@
require 'rails_helper'
RSpec.describe Account do
include_examples 'Account::Search'
include_examples 'Reviewable'
context 'with an account record' do
@ -48,14 +49,16 @@ RSpec.describe Account do
end
describe '#local?' do
it 'returns true when domain is null' do
account = Fabricate(:account, domain: nil)
expect(account).to be_local
context 'when the domain is null' do
subject { Fabricate.build :account, domain: nil }
it { is_expected.to be_local }
end
it 'returns false when domain is present' do
account = Fabricate(:account, domain: 'foreign.tld')
expect(account).to_not be_local
context 'when the domain is present' do
subject { Fabricate.build :account, domain: 'host.example' }
it { is_expected.to_not be_local }
end
end
@ -66,12 +69,6 @@ RSpec.describe Account do
it { is_expected.to_not be_remote }
end
context 'when the domain is blank' do
subject { Fabricate.build :account, domain: '' }
it { is_expected.to_not be_remote }
end
context 'when the domain is present' do
subject { Fabricate.build :account, domain: 'host.example' }
@ -79,6 +76,20 @@ RSpec.describe Account do
end
end
describe '#actor_type_application?' do
context 'when the actor is not of type application' do
subject { Fabricate.build :account, actor_type: 'Person' }
it { is_expected.to_not be_actor_type_application }
end
context 'when the actor is of type application' do
subject { Fabricate.build :account, actor_type: 'Application' }
it { is_expected.to be_actor_type_application }
end
end
describe 'Local domain user methods' do
subject { Fabricate(:account, domain: nil, username: 'alice') }
@ -540,298 +551,6 @@ RSpec.describe Account do
end
end
describe '.search_for' do
before do
_missing = Fabricate(
:account,
display_name: 'Missing',
username: 'missing',
domain: 'missing.com'
)
end
it 'does not return suspended users' do
Fabricate(
:account,
display_name: 'Display Name',
username: 'username',
domain: 'example.com',
suspended: true
)
results = described_class.search_for('username')
expect(results).to eq []
end
it 'does not return unapproved users' do
match = Fabricate(
:account,
display_name: 'Display Name',
username: 'username'
)
match.user.update(approved: false)
results = described_class.search_for('username')
expect(results).to eq []
end
it 'does not return unconfirmed users' do
match = Fabricate(
:account,
display_name: 'Display Name',
username: 'username'
)
match.user.update(confirmed_at: nil)
results = described_class.search_for('username')
expect(results).to eq []
end
it 'accepts ?, \, : and space as delimiter' do
match = Fabricate(
:account,
display_name: 'A & l & i & c & e',
username: 'username',
domain: 'example.com'
)
results = described_class.search_for('A?l\i:c e')
expect(results).to eq [match]
end
it 'finds accounts with matching display_name' do
match = Fabricate(
:account,
display_name: 'Display Name',
username: 'username',
domain: 'example.com'
)
results = described_class.search_for('display')
expect(results).to eq [match]
end
it 'finds accounts with matching username' do
match = Fabricate(
:account,
display_name: 'Display Name',
username: 'username',
domain: 'example.com'
)
results = described_class.search_for('username')
expect(results).to eq [match]
end
it 'finds accounts with matching domain' do
match = Fabricate(
:account,
display_name: 'Display Name',
username: 'username',
domain: 'example.com'
)
results = described_class.search_for('example')
expect(results).to eq [match]
end
it 'limits via constant by default' do
stub_const('Account::Search::DEFAULT_LIMIT', 1)
2.times.each { Fabricate(:account, display_name: 'Display Name') }
results = described_class.search_for('display')
expect(results.size).to eq 1
end
it 'accepts arbitrary limits' do
2.times.each { Fabricate(:account, display_name: 'Display Name') }
results = described_class.search_for('display', limit: 1)
expect(results.size).to eq 1
end
it 'ranks multiple matches higher' do
matches = [
{ username: 'username', display_name: 'username' },
{ display_name: 'Display Name', username: 'username', domain: 'example.com' },
].map(&method(:Fabricate).curry(2).call(:account))
results = described_class.search_for('username')
expect(results).to eq matches
end
end
describe '.advanced_search_for' do
let(:account) { Fabricate(:account) }
context 'when limiting search to followed accounts' do
it 'accepts ?, \, : and space as delimiter' do
match = Fabricate(
:account,
display_name: 'A & l & i & c & e',
username: 'username',
domain: 'example.com'
)
account.follow!(match)
results = described_class.advanced_search_for('A?l\i:c e', account, limit: 10, following: true)
expect(results).to eq [match]
end
it 'does not return non-followed accounts' do
Fabricate(
:account,
display_name: 'A & l & i & c & e',
username: 'username',
domain: 'example.com'
)
results = described_class.advanced_search_for('A?l\i:c e', account, limit: 10, following: true)
expect(results).to eq []
end
it 'does not return suspended users' do
Fabricate(
:account,
display_name: 'Display Name',
username: 'username',
domain: 'example.com',
suspended: true
)
results = described_class.advanced_search_for('username', account, limit: 10, following: true)
expect(results).to eq []
end
it 'does not return unapproved users' do
match = Fabricate(
:account,
display_name: 'Display Name',
username: 'username'
)
match.user.update(approved: false)
results = described_class.advanced_search_for('username', account, limit: 10, following: true)
expect(results).to eq []
end
it 'does not return unconfirmed users' do
match = Fabricate(
:account,
display_name: 'Display Name',
username: 'username'
)
match.user.update(confirmed_at: nil)
results = described_class.advanced_search_for('username', account, limit: 10, following: true)
expect(results).to eq []
end
end
context 'when limiting search to follower accounts' do
it 'accepts ?, \, : and space as delimiter' do
match = Fabricate(
:account,
display_name: 'A & l & i & c & e',
username: 'username',
domain: 'example.com'
)
match.follow!(account)
results = described_class.advanced_search_for('A?l\i:c e', account, limit: 10, follower: true)
expect(results).to eq [match]
end
it 'does not return non-follower accounts' do
Fabricate(
:account,
display_name: 'A & l & i & c & e',
username: 'username',
domain: 'example.com'
)
results = described_class.advanced_search_for('A?l\i:c e', account, limit: 10, follower: true)
expect(results).to eq []
end
end
it 'does not return suspended users' do
Fabricate(
:account,
display_name: 'Display Name',
username: 'username',
domain: 'example.com',
suspended: true
)
results = described_class.advanced_search_for('username', account)
expect(results).to eq []
end
it 'does not return unapproved users' do
match = Fabricate(
:account,
display_name: 'Display Name',
username: 'username'
)
match.user.update(approved: false)
results = described_class.advanced_search_for('username', account)
expect(results).to eq []
end
it 'does not return unconfirmed users' do
match = Fabricate(
:account,
display_name: 'Display Name',
username: 'username'
)
match.user.update(confirmed_at: nil)
results = described_class.advanced_search_for('username', account)
expect(results).to eq []
end
it 'accepts ?, \, : and space as delimiter' do
match = Fabricate(
:account,
display_name: 'A & l & i & c & e',
username: 'username',
domain: 'example.com'
)
results = described_class.advanced_search_for('A?l\i:c e', account)
expect(results).to eq [match]
end
it 'limits result count by default value' do
stub_const('Account::Search::DEFAULT_LIMIT', 1)
2.times { Fabricate(:account, display_name: 'Display Name') }
results = described_class.advanced_search_for('display', account)
expect(results.size).to eq 1
end
it 'accepts arbitrary limits' do
2.times { Fabricate(:account, display_name: 'Display Name') }
results = described_class.advanced_search_for('display', account, limit: 1)
expect(results.size).to eq 1
end
it 'ranks followed accounts higher' do
match = Fabricate(:account, username: 'Matching')
followed_match = Fabricate(:account, username: 'Matcher')
Fabricate(:follow, account: account, target_account: followed_match)
results = described_class.advanced_search_for('match', account)
expect(results).to eq [followed_match, match]
expect(results.first.rank).to be > results.last.rank
end
end
describe '#statuses_count' do
subject { Fabricate(:account) }
@ -1030,6 +749,8 @@ RSpec.describe Account do
describe 'Validations' do
it { is_expected.to validate_presence_of(:username) }
it { is_expected.to_not allow_value('').for(:domain) }
context 'when account is local' do
subject { Fabricate.build :account, domain: nil }
@ -1068,6 +789,11 @@ RSpec.describe Account do
it { is_expected.to allow_value(fields_empty_name_value).for(:fields) }
it { is_expected.to_not allow_values(fields_over_limit, fields_empty_name).for(:fields) }
it { is_expected.to validate_absence_of(:followers_url).on(:create) }
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) }
end
context 'when account is remote' do

View file

@ -8,4 +8,18 @@ RSpec.describe AccountWarning do
it { is_expected.to normalize(:text).from(nil).to('') }
end
end
describe '#appeal_eligible?' do
context 'when created too long ago' do
subject { Fabricate.build :account_warning, created_at: (described_class::APPEAL_WINDOW * 2).ago }
it { is_expected.to_not be_appeal_eligible }
end
context 'when created recently' do
subject { Fabricate.build :account_warning, created_at: (described_class::APPEAL_WINDOW - 2.days).ago }
it { is_expected.to be_appeal_eligible }
end
end
end

View file

@ -9,7 +9,7 @@ RSpec.describe Appeal do
it { is_expected.to validate_length_of(:text).is_at_most(described_class::TEXT_LENGTH_LIMIT) }
context 'with a strike created too long ago' do
let(:strike) { Fabricate.build :account_warning, created_at: (described_class::MAX_STRIKE_AGE * 2).ago }
let(:strike) { Fabricate.build :account_warning, created_at: (AccountWarning::APPEAL_WINDOW * 2).ago }
it { is_expected.to_not allow_values(strike).for(:strike).against(:base).on(:create) }
end

View file

@ -5,6 +5,29 @@ require 'rails_helper'
RSpec.describe Invite do
include_examples 'Expireable'
describe 'Associations' do
it { is_expected.to belong_to(:user).inverse_of(:invites) }
it { is_expected.to have_many(:users).inverse_of(:invite) }
end
describe 'Validations' do
it { is_expected.to validate_length_of(:comment).is_at_most(described_class::COMMENT_SIZE_LIMIT) }
end
describe 'Scopes' do
describe '.available' do
let!(:no_expires) { Fabricate :invite, expires_at: nil }
let!(:past_expires) { Fabricate :invite, expires_at: 2.days.ago }
let!(:future_expires) { Fabricate :invite, expires_at: 2.days.from_now }
it 'returns future and non-epiring records' do
expect(described_class.available)
.to include(no_expires, future_expires)
.and not_include(past_expires)
end
end
end
describe '#valid_for_use?' do
it 'returns true when there are no limitations' do
invite = Fabricate(:invite, max_uses: nil, expires_at: nil)
@ -37,4 +60,26 @@ RSpec.describe Invite do
expect(invite.valid_for_use?).to be false
end
end
describe 'Callbacks' do
describe 'Setting the invite code' do
context 'when creating a new record' do
subject { Fabricate.build :invite }
it 'sets a code value' do
expect { subject.save }
.to change(subject, :code).from(be_blank).to(be_present)
end
end
context 'when updating a record' do
subject { Fabricate :invite }
it 'does not change the code value' do
expect { subject.update(max_uses: 123_456) }
.to not_change(subject, :code)
end
end
end
end
end

View file

@ -12,6 +12,8 @@ RSpec.describe IpBlock do
it { is_expected.to validate_presence_of(:severity) }
it { is_expected.to validate_uniqueness_of(:ip) }
it { is_expected.to allow_values(:sign_up_requires_approval, :sign_up_block, :no_access).for(:severity) }
end
describe '#to_log_human_identifier' do

View file

@ -9,6 +9,8 @@ RSpec.describe Status do
let(:bob) { Fabricate(:account, username: 'bob') }
let(:other) { Fabricate(:status, account: bob, text: 'Skulls for the skull god! The enemy\'s gates are sideways!') }
include_examples 'Status::Visibility'
describe '#local?' do
it 'returns true when no remote URI is set' do
expect(subject.local?).to be true
@ -84,178 +86,6 @@ RSpec.describe Status do
end
end
describe '#hidden?' do
context 'when private_visibility?' do
it 'returns true' do
subject.visibility = :private
expect(subject.hidden?).to be true
end
end
context 'when direct_visibility?' do
it 'returns true' do
subject.visibility = :direct
expect(subject.hidden?).to be true
end
end
context 'when public_visibility?' do
it 'returns false' do
subject.visibility = :public
expect(subject.hidden?).to be false
end
end
context 'when unlisted_visibility?' do
it 'returns false' do
subject.visibility = :unlisted
expect(subject.hidden?).to be false
end
end
end
describe '#compute_searchability' do
subject { Fabricate(:status, account: account, searchability: status_searchability) }
let(:account_searchability) { :public }
let(:status_searchability) { :public }
let(:account_domain) { 'example.com' }
let(:silenced_at) { nil }
let(:account) { Fabricate(:account, domain: account_domain, searchability: account_searchability, silenced_at: silenced_at) }
context 'when public-public' do
it 'returns public' do
expect(subject.compute_searchability).to eq 'public'
end
end
context 'when public-public but silenced' do
let(:silenced_at) { Time.now.utc }
it 'returns private' do
expect(subject.compute_searchability).to eq 'private'
end
end
context 'when public-public_unlisted but silenced' do
let(:silenced_at) { Time.now.utc }
let(:status_searchability) { :public_unlisted }
it 'returns private' do
expect(subject.compute_searchability).to eq 'private'
end
end
context 'when public-public_unlisted' do
let(:status_searchability) { :public_unlisted }
it 'returns public' do
expect(subject.compute_searchability).to eq 'public'
end
it 'returns public_unlisted for local' do
expect(subject.compute_searchability_local).to eq 'public_unlisted'
end
end
context 'when public-private' do
let(:status_searchability) { :private }
it 'returns private' do
expect(subject.compute_searchability).to eq 'private'
end
end
context 'when public-direct' do
let(:status_searchability) { :direct }
it 'returns direct' do
expect(subject.compute_searchability).to eq 'direct'
end
end
context 'when private-public' do
let(:account_searchability) { :private }
it 'returns private' do
expect(subject.compute_searchability).to eq 'private'
end
end
context 'when direct-public' do
let(:account_searchability) { :direct }
it 'returns direct' do
expect(subject.compute_searchability).to eq 'direct'
end
end
context 'when limited-public' do
let(:account_searchability) { :limited }
it 'returns limited' do
expect(subject.compute_searchability).to eq 'limited'
end
end
context 'when private-limited' do
let(:account_searchability) { :private }
let(:status_searchability) { :limited }
it 'returns limited' do
expect(subject.compute_searchability).to eq 'limited'
end
end
context 'when private-public of local account' do
let(:account_searchability) { :private }
let(:account_domain) { nil }
let(:status_searchability) { :public }
it 'returns public' do
expect(subject.compute_searchability).to eq 'public'
end
end
context 'when direct-public of local account' do
let(:account_searchability) { :direct }
let(:account_domain) { nil }
let(:status_searchability) { :public }
it 'returns public' do
expect(subject.compute_searchability).to eq 'public'
end
end
context 'when limited-public of local account' do
let(:account_searchability) { :limited }
let(:account_domain) { nil }
let(:status_searchability) { :public }
it 'returns public' do
expect(subject.compute_searchability).to eq 'public'
end
end
context 'when public-public_unlisted of local account' do
let(:account_searchability) { :public }
let(:account_domain) { nil }
let(:status_searchability) { :public_unlisted }
it 'returns public' do
expect(subject.compute_searchability).to eq 'public'
end
it 'returns public_unlisted for local' do
expect(subject.compute_searchability_local).to eq 'public_unlisted'
end
it 'returns private for activitypub' do
expect(subject.compute_searchability_activitypub).to eq 'private'
end
end
end
describe '#quote' do
let(:target_status) { Fabricate(:status) }
let(:quote) { true }

View file

@ -0,0 +1,44 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Trends::Links do
describe 'Trends::Links::Query' do
subject { described_class.new.query }
describe '#records' do
context 'with scored cards' do
let!(:higher_score) { Fabricate :preview_card_trend, score: 10, language: 'en' }
let!(:lower_score) { Fabricate :preview_card_trend, score: 1, language: 'es' }
it 'returns higher score first' do
expect(subject.records)
.to eq([higher_score.preview_card, lower_score.preview_card])
end
context 'with preferred locale' do
before { subject.in_locale!('es') }
it 'returns in language order' do
expect(subject.records)
.to eq([lower_score.preview_card, higher_score.preview_card])
end
end
context 'when account has chosen languages' do
let!(:lang_match_higher_score) { Fabricate :preview_card_trend, score: 10, language: 'is' }
let!(:lang_match_lower_score) { Fabricate :preview_card_trend, score: 1, language: 'da' }
let(:user) { Fabricate :user, chosen_languages: %w(da is) }
let(:account) { Fabricate :account, user: user }
before { subject.filtered_for!(account) }
it 'returns results' do
expect(subject.records)
.to eq([lang_match_higher_score.preview_card, lang_match_lower_score.preview_card, higher_score.preview_card, lower_score.preview_card])
end
end
end
end
end
end

View file

@ -45,6 +45,45 @@ RSpec.describe Trends::Statuses do
end
end
describe 'Trends::Statuses::Query methods' do
subject { described_class.new.query }
describe '#records' do
context 'with scored cards' do
let!(:higher_score) { Fabricate :status_trend, score: 10, language: 'en' }
let!(:lower_score) { Fabricate :status_trend, score: 1, language: 'es' }
it 'returns higher score first' do
expect(subject.records)
.to eq([higher_score.status, lower_score.status])
end
context 'with preferred locale' do
before { subject.in_locale!('es') }
it 'returns in language order' do
expect(subject.records)
.to eq([lower_score.status, higher_score.status])
end
end
context 'when account has chosen languages' do
let!(:lang_match_higher_score) { Fabricate :status_trend, score: 10, language: 'is' }
let!(:lang_match_lower_score) { Fabricate :status_trend, score: 1, language: 'da' }
let(:user) { Fabricate :user, chosen_languages: %w(da is) }
let(:account) { Fabricate :account, user: user }
before { subject.filtered_for!(account) }
it 'returns results' do
expect(subject.records)
.to eq([lang_match_higher_score.status, lang_match_lower_score.status, higher_score.status, lower_score.status])
end
end
end
end
end
describe '#add' do
let(:status) { Fabricate(:status) }

View file

@ -56,6 +56,45 @@ RSpec.describe Trends::Tags do
end
end
describe 'Trends::Tags::Query' do
subject { described_class.new.query }
describe '#records' do
context 'with scored cards' do
let!(:higher_score) { Fabricate :tag_trend, score: 10, language: 'en' }
let!(:lower_score) { Fabricate :tag_trend, score: 1, language: 'es' }
it 'returns higher score first' do
expect(subject.records)
.to eq([higher_score.tag, lower_score.tag])
end
context 'with preferred locale' do
before { subject.in_locale!('es') }
it 'returns in language order' do
expect(subject.records)
.to eq([lower_score.tag, higher_score.tag])
end
end
context 'when account has chosen languages' do
let!(:lang_match_higher_score) { Fabricate :tag_trend, score: 10, language: 'is' }
let!(:lang_match_lower_score) { Fabricate :tag_trend, score: 1, language: 'da' }
let(:user) { Fabricate :user, chosen_languages: %w(da is) }
let(:account) { Fabricate :account, user: user }
before { subject.filtered_for!(account) }
it 'returns results' do
expect(subject.records)
.to eq([lang_match_higher_score.tag, lang_match_lower_score.tag, higher_score.tag, lower_score.tag])
end
end
end
end
end
describe '#refresh' do
let!(:today) { at_time }
let!(:yesterday) { today - 1.day }