2023-02-22 09:55:31 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-21 06:53:20 +09:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-05-04 12:49:53 +09:00
|
|
|
RSpec.describe Account do
|
2024-09-04 00:37:45 +09:00
|
|
|
include_examples 'Reviewable'
|
|
|
|
|
2023-06-06 22:51:42 +09:00
|
|
|
context 'with an account record' do
|
2017-06-23 01:34:27 +09:00
|
|
|
subject { Fabricate(:account) }
|
2016-02-26 23:28:08 +09:00
|
|
|
|
2023-02-20 13:24:14 +09:00
|
|
|
let(:bob) { Fabricate(:account, username: 'bob') }
|
|
|
|
|
2016-02-26 23:28:08 +09:00
|
|
|
describe '#follow!' do
|
|
|
|
it 'creates a follow' do
|
|
|
|
follow = subject.follow!(bob)
|
|
|
|
|
|
|
|
expect(follow).to be_instance_of Follow
|
|
|
|
expect(follow.account).to eq subject
|
|
|
|
expect(follow.target_account).to eq bob
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#unfollow!' do
|
|
|
|
before do
|
|
|
|
subject.follow!(bob)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'destroys a follow' do
|
|
|
|
unfollow = subject.unfollow!(bob)
|
2016-02-25 08:17:01 +09:00
|
|
|
|
2016-02-26 23:28:08 +09:00
|
|
|
expect(unfollow).to be_instance_of Follow
|
|
|
|
expect(unfollow.account).to eq subject
|
|
|
|
expect(unfollow.target_account).to eq bob
|
|
|
|
expect(unfollow.destroyed?).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#following?' do
|
|
|
|
it 'returns true when the target is followed' do
|
|
|
|
subject.follow!(bob)
|
|
|
|
expect(subject.following?(bob)).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if the target is not followed' do
|
|
|
|
expect(subject.following?(bob)).to be false
|
|
|
|
end
|
|
|
|
end
|
2016-02-25 08:17:01 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#local?' do
|
2016-02-26 23:28:08 +09:00
|
|
|
it 'returns true when the account is local' do
|
2017-06-23 01:34:27 +09:00
|
|
|
account = Fabricate(:account, domain: nil)
|
|
|
|
expect(account.local?).to be true
|
2016-02-26 23:28:08 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when the account is on a different domain' do
|
2017-06-23 01:34:27 +09:00
|
|
|
account = Fabricate(:account, domain: 'foreign.tld')
|
|
|
|
expect(account.local?).to be false
|
2016-02-26 23:28:08 +09:00
|
|
|
end
|
2017-04-11 05:58:06 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Local domain user methods' do
|
2023-02-20 13:24:14 +09:00
|
|
|
subject { Fabricate(:account, domain: nil, username: 'alice') }
|
|
|
|
|
2017-04-11 05:58:06 +09:00
|
|
|
around do |example|
|
|
|
|
before = Rails.configuration.x.local_domain
|
|
|
|
example.run
|
|
|
|
Rails.configuration.x.local_domain = before
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#to_webfinger_s' do
|
|
|
|
it 'returns a webfinger string for the account' do
|
|
|
|
Rails.configuration.x.local_domain = 'example.com'
|
|
|
|
|
|
|
|
expect(subject.to_webfinger_s).to eq 'acct:alice@example.com'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#local_username_and_domain' do
|
|
|
|
it 'returns the username and local domain for the account' do
|
|
|
|
Rails.configuration.x.local_domain = 'example.com'
|
|
|
|
|
|
|
|
expect(subject.local_username_and_domain).to eq 'alice@example.com'
|
|
|
|
end
|
|
|
|
end
|
2016-02-25 08:17:01 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#acct' do
|
2016-02-26 23:28:08 +09:00
|
|
|
it 'returns username for local users' do
|
2017-06-23 01:34:27 +09:00
|
|
|
account = Fabricate(:account, domain: nil, username: 'alice')
|
|
|
|
expect(account.acct).to eql 'alice'
|
2016-02-26 23:28:08 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns username@domain for foreign users' do
|
2017-06-23 01:34:27 +09:00
|
|
|
account = Fabricate(:account, domain: 'foreign.tld', username: 'alice')
|
|
|
|
expect(account.acct).to eql 'alice@foreign.tld'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#save_with_optional_media!' do
|
2017-11-06 13:54:12 +09:00
|
|
|
before do
|
2018-04-25 21:12:28 +09:00
|
|
|
stub_request(:get, 'https://remote.test/valid_avatar').to_return(request_fixture('avatar.txt'))
|
|
|
|
stub_request(:get, 'https://remote.test/invalid_avatar').to_return(request_fixture('feed.txt'))
|
2017-11-06 13:54:12 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:account) do
|
|
|
|
Fabricate(:account,
|
2018-04-25 21:12:28 +09:00
|
|
|
avatar_remote_url: 'https://remote.test/valid_avatar',
|
|
|
|
header_remote_url: 'https://remote.test/valid_avatar')
|
2017-11-06 13:54:12 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
let!(:expectation) { account.dup }
|
|
|
|
|
|
|
|
context 'with valid properties' do
|
|
|
|
before do
|
|
|
|
account.save_with_optional_media!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'unchanges avatar, header, avatar_remote_url, and header_remote_url' do
|
|
|
|
expect(account.avatar_remote_url).to eq expectation.avatar_remote_url
|
|
|
|
expect(account.header_remote_url).to eq expectation.header_remote_url
|
|
|
|
expect(account.avatar_file_name).to eq expectation.avatar_file_name
|
|
|
|
expect(account.header_file_name).to eq expectation.header_file_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid properties' do
|
|
|
|
before do
|
2018-04-25 21:12:28 +09:00
|
|
|
account.avatar_remote_url = 'https://remote.test/invalid_avatar'
|
2017-11-06 13:54:12 +09:00
|
|
|
account.save_with_optional_media!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets default avatar, header, avatar_remote_url, and header_remote_url' do
|
2019-10-09 14:10:46 +09:00
|
|
|
expect(account.avatar_remote_url).to eq 'https://remote.test/invalid_avatar'
|
|
|
|
expect(account.header_remote_url).to eq expectation.header_remote_url
|
2023-02-20 14:14:50 +09:00
|
|
|
expect(account.avatar_file_name).to be_nil
|
2022-12-16 01:11:14 +09:00
|
|
|
expect(account.header_file_name).to eq expectation.header_file_name
|
2017-11-06 13:54:12 +09:00
|
|
|
end
|
2016-02-26 23:28:08 +09:00
|
|
|
end
|
2016-02-25 08:17:01 +09:00
|
|
|
end
|
|
|
|
|
2017-11-05 17:20:05 +09:00
|
|
|
describe '#possibly_stale?' do
|
|
|
|
let(:account) { Fabricate(:account, last_webfingered_at: last_webfingered_at) }
|
|
|
|
|
2023-05-04 12:49:08 +09:00
|
|
|
context 'when last_webfingered_at is nil' do
|
2017-11-05 17:20:05 +09:00
|
|
|
let(:last_webfingered_at) { nil }
|
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
expect(account.possibly_stale?).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-10-23 22:29:01 +09:00
|
|
|
context 'when last_webfingered_at is before the threshold' do
|
|
|
|
let(:last_webfingered_at) { (described_class::STALE_THRESHOLD + 1.hour).ago }
|
2017-11-05 17:20:05 +09:00
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
expect(account.possibly_stale?).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-10-23 22:29:01 +09:00
|
|
|
context 'when last_webfingered_at is after the threshold' do
|
|
|
|
let(:last_webfingered_at) { (described_class::STALE_THRESHOLD - 1.hour).ago }
|
2017-11-05 17:20:05 +09:00
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
expect(account.possibly_stale?).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-06 13:54:41 +09:00
|
|
|
describe '#refresh!' do
|
|
|
|
let(:account) { Fabricate(:account, domain: domain) }
|
|
|
|
let(:acct) { account.acct }
|
|
|
|
|
2023-05-04 12:49:08 +09:00
|
|
|
context 'when domain is nil' do
|
2017-11-06 13:54:41 +09:00
|
|
|
let(:domain) { nil }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(account.refresh!).to be_nil
|
|
|
|
end
|
|
|
|
|
2023-11-14 23:52:59 +09:00
|
|
|
it 'does not call ResolveAccountService#call' do
|
|
|
|
service = instance_double(ResolveAccountService, call: nil)
|
|
|
|
allow(ResolveAccountService).to receive(:new).and_return(service)
|
|
|
|
|
2017-11-06 13:54:41 +09:00
|
|
|
account.refresh!
|
2023-11-14 23:52:59 +09:00
|
|
|
|
|
|
|
expect(service).to_not have_received(:call).with(acct)
|
2017-11-06 13:54:41 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 12:49:08 +09:00
|
|
|
context 'when domain is present' do
|
2017-11-06 13:54:41 +09:00
|
|
|
let(:domain) { 'example.com' }
|
|
|
|
|
2018-01-22 22:25:09 +09:00
|
|
|
it 'calls ResolveAccountService#call' do
|
2023-11-14 23:52:59 +09:00
|
|
|
service = instance_double(ResolveAccountService, call: nil)
|
|
|
|
allow(ResolveAccountService).to receive(:new).and_return(service)
|
|
|
|
|
2017-11-06 13:54:41 +09:00
|
|
|
account.refresh!
|
2023-11-14 23:52:59 +09:00
|
|
|
|
|
|
|
expect(service).to have_received(:call).with(acct).once
|
2017-11-06 13:54:41 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-23 01:34:27 +09:00
|
|
|
describe '#to_param' do
|
|
|
|
it 'returns username' do
|
|
|
|
account = Fabricate(:account, username: 'alice')
|
|
|
|
expect(account.to_param).to eq 'alice'
|
2016-02-26 23:28:08 +09:00
|
|
|
end
|
2016-02-25 08:17:01 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#keypair' do
|
2016-02-26 23:28:08 +09:00
|
|
|
it 'returns an RSA key pair' do
|
2017-06-23 01:34:27 +09:00
|
|
|
account = Fabricate(:account)
|
|
|
|
expect(account.keypair).to be_instance_of OpenSSL::PKey::RSA
|
2016-02-26 23:28:08 +09:00
|
|
|
end
|
2016-02-25 08:17:01 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#object_type' do
|
2016-02-26 23:28:08 +09:00
|
|
|
it 'is always a person' do
|
2017-06-23 01:34:27 +09:00
|
|
|
account = Fabricate(:account)
|
|
|
|
expect(account.object_type).to be :person
|
2016-02-26 23:28:08 +09:00
|
|
|
end
|
2016-02-25 08:17:01 +09:00
|
|
|
end
|
|
|
|
|
2016-03-19 20:13:47 +09:00
|
|
|
describe '#favourited?' do
|
2023-02-20 13:24:14 +09:00
|
|
|
subject { Fabricate(:account) }
|
|
|
|
|
2017-04-08 03:18:30 +09:00
|
|
|
let(:original_status) do
|
|
|
|
author = Fabricate(:account, username: 'original')
|
|
|
|
Fabricate(:status, account: author)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the status is a reblog of another status' do
|
|
|
|
let(:original_reblog) do
|
|
|
|
author = Fabricate(:account, username: 'original_reblogger')
|
|
|
|
Fabricate(:status, reblog: original_status, account: author)
|
|
|
|
end
|
|
|
|
|
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-09 01:32:03 +09:00
|
|
|
it 'is true when this account has favourited it' do
|
2017-04-08 03:18:30 +09:00
|
|
|
Fabricate(:favourite, status: original_reblog, account: subject)
|
|
|
|
|
2023-02-20 14:14:50 +09:00
|
|
|
expect(subject.favourited?(original_status)).to be true
|
2017-04-08 03:18:30 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is false when this account has not favourited it' do
|
2023-02-20 14:14:50 +09:00
|
|
|
expect(subject.favourited?(original_status)).to be false
|
2017-04-08 03:18:30 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the status is an original status' 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-09 01:32:03 +09:00
|
|
|
it 'is true when this account has favourited it' do
|
2017-04-08 03:18:30 +09:00
|
|
|
Fabricate(:favourite, status: original_status, account: subject)
|
|
|
|
|
2023-02-20 14:14:50 +09:00
|
|
|
expect(subject.favourited?(original_status)).to be true
|
2017-04-08 03:18:30 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is false when this account has not favourited it' do
|
2023-02-20 14:14:50 +09:00
|
|
|
expect(subject.favourited?(original_status)).to be false
|
2017-04-08 03:18:30 +09:00
|
|
|
end
|
|
|
|
end
|
2016-03-19 20:13:47 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#reblogged?' do
|
2023-02-20 13:24:14 +09:00
|
|
|
subject { Fabricate(:account) }
|
|
|
|
|
2017-04-08 03:18:30 +09:00
|
|
|
let(:original_status) do
|
|
|
|
author = Fabricate(:account, username: 'original')
|
|
|
|
Fabricate(:status, account: author)
|
|
|
|
end
|
|
|
|
|
2018-10-05 00:38:04 +09:00
|
|
|
context 'when the status is a reblog of another status' do
|
2017-04-08 03:18:30 +09:00
|
|
|
let(:original_reblog) do
|
|
|
|
author = Fabricate(:account, username: 'original_reblogger')
|
|
|
|
Fabricate(:status, reblog: original_status, account: author)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is true when this account has reblogged it' do
|
|
|
|
Fabricate(:status, reblog: original_reblog, account: subject)
|
|
|
|
|
2023-02-20 14:14:50 +09:00
|
|
|
expect(subject.reblogged?(original_reblog)).to be true
|
2017-04-08 03:18:30 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is false when this account has not reblogged it' do
|
2023-02-20 14:14:50 +09:00
|
|
|
expect(subject.reblogged?(original_reblog)).to be false
|
2017-04-08 03:18:30 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the status is an original status' do
|
|
|
|
it 'is true when this account has reblogged it' do
|
|
|
|
Fabricate(:status, reblog: original_status, account: subject)
|
|
|
|
|
2023-02-20 14:14:50 +09:00
|
|
|
expect(subject.reblogged?(original_status)).to be true
|
2017-04-08 03:18:30 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is false when this account has not reblogged it' do
|
2023-02-20 14:14:50 +09:00
|
|
|
expect(subject.reblogged?(original_status)).to be false
|
2017-04-08 03:18:30 +09:00
|
|
|
end
|
|
|
|
end
|
2016-03-19 20:13:47 +09:00
|
|
|
end
|
|
|
|
|
2017-04-28 22:10:41 +09:00
|
|
|
describe '#excluded_from_timeline_account_ids' do
|
|
|
|
it 'includes account ids of blockings, blocked_bys and mutes' do
|
|
|
|
account = Fabricate(:account)
|
2017-04-30 21:49:24 +09:00
|
|
|
block = Fabricate(:block, account: account)
|
|
|
|
mute = Fabricate(:mute, account: account)
|
|
|
|
block_by = Fabricate(:block, target_account: account)
|
2017-04-28 22:10:41 +09:00
|
|
|
|
|
|
|
results = account.excluded_from_timeline_account_ids
|
|
|
|
expect(results.size).to eq 3
|
2024-01-12 01:17:45 +09:00
|
|
|
expect(results).to include(
|
|
|
|
block.target_account.id,
|
|
|
|
mute.target_account.id,
|
|
|
|
block_by.account.id
|
|
|
|
)
|
2017-04-28 22:10:41 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-23 01:34:27 +09:00
|
|
|
describe '#excluded_from_timeline_domains' do
|
|
|
|
it 'returns the domains blocked by the account' do
|
|
|
|
account = Fabricate(:account)
|
|
|
|
account.block_domain!('domain')
|
2023-04-27 03:21:54 +09:00
|
|
|
expect(account.excluded_from_timeline_domains).to contain_exactly('domain')
|
2017-06-23 01:34:27 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-09 21:45:01 +09:00
|
|
|
describe '.search_for' do
|
|
|
|
before do
|
|
|
|
_missing = Fabricate(
|
|
|
|
:account,
|
2023-02-19 07:38:14 +09:00
|
|
|
display_name: 'Missing',
|
|
|
|
username: 'missing',
|
|
|
|
domain: 'missing.com'
|
2017-04-09 21:45:01 +09:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-05-26 22:50:33 +09:00
|
|
|
it 'does not return suspended users' do
|
2023-10-19 23:55:06 +09:00
|
|
|
Fabricate(
|
2022-05-26 22:50:33 +09:00
|
|
|
:account,
|
|
|
|
display_name: 'Display Name',
|
|
|
|
username: 'username',
|
|
|
|
domain: 'example.com',
|
|
|
|
suspended: true
|
|
|
|
)
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.search_for('username')
|
2022-05-26 22:50:33 +09:00
|
|
|
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)
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.search_for('username')
|
2022-05-26 22:50:33 +09:00
|
|
|
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)
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.search_for('username')
|
2022-05-26 22:50:33 +09:00
|
|
|
expect(results).to eq []
|
|
|
|
end
|
|
|
|
|
2017-06-23 01:34:27 +09:00
|
|
|
it 'accepts ?, \, : and space as delimiter' do
|
|
|
|
match = Fabricate(
|
|
|
|
:account,
|
|
|
|
display_name: 'A & l & i & c & e',
|
|
|
|
username: 'username',
|
|
|
|
domain: 'example.com'
|
|
|
|
)
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.search_for('A?l\i:c e')
|
2017-06-23 01:34:27 +09:00
|
|
|
expect(results).to eq [match]
|
|
|
|
end
|
|
|
|
|
2017-04-09 21:45:01 +09:00
|
|
|
it 'finds accounts with matching display_name' do
|
2017-06-23 01:34:27 +09:00
|
|
|
match = Fabricate(
|
|
|
|
:account,
|
2023-02-19 07:38:14 +09:00
|
|
|
display_name: 'Display Name',
|
|
|
|
username: 'username',
|
|
|
|
domain: 'example.com'
|
2017-06-23 01:34:27 +09:00
|
|
|
)
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.search_for('display')
|
2017-06-23 01:34:27 +09:00
|
|
|
expect(results).to eq [match]
|
2017-04-09 21:45:01 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'finds accounts with matching username' do
|
2017-06-23 01:34:27 +09:00
|
|
|
match = Fabricate(
|
|
|
|
:account,
|
2023-02-19 07:38:14 +09:00
|
|
|
display_name: 'Display Name',
|
|
|
|
username: 'username',
|
|
|
|
domain: 'example.com'
|
2017-06-23 01:34:27 +09:00
|
|
|
)
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.search_for('username')
|
2017-06-23 01:34:27 +09:00
|
|
|
expect(results).to eq [match]
|
2017-04-09 21:45:01 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'finds accounts with matching domain' do
|
2017-06-23 01:34:27 +09:00
|
|
|
match = Fabricate(
|
|
|
|
:account,
|
2023-02-19 07:38:14 +09:00
|
|
|
display_name: 'Display Name',
|
|
|
|
username: 'username',
|
|
|
|
domain: 'example.com'
|
2017-06-23 01:34:27 +09:00
|
|
|
)
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.search_for('example')
|
2017-06-23 01:34:27 +09:00
|
|
|
expect(results).to eq [match]
|
|
|
|
end
|
|
|
|
|
2023-11-20 18:08:22 +09:00
|
|
|
it 'limits via constant by default' do
|
2023-12-01 20:00:41 +09:00
|
|
|
stub_const('Account::Search::DEFAULT_LIMIT', 1)
|
2023-11-20 18:08:22 +09:00
|
|
|
2.times.each { Fabricate(:account, display_name: 'Display Name') }
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.search_for('display')
|
2023-11-20 18:08:22 +09:00
|
|
|
expect(results.size).to eq 1
|
2017-06-23 01:34:27 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts arbitrary limits' do
|
2023-02-19 07:38:14 +09:00
|
|
|
2.times.each { Fabricate(:account, display_name: 'Display Name') }
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.search_for('display', limit: 1)
|
2017-06-23 01:34:27 +09:00
|
|
|
expect(results.size).to eq 1
|
2017-04-09 21:45:01 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'ranks multiple matches higher' do
|
2017-06-23 01:34:27 +09:00
|
|
|
matches = [
|
2023-02-19 07:38:14 +09:00
|
|
|
{ username: 'username', display_name: 'username' },
|
|
|
|
{ display_name: 'Display Name', username: 'username', domain: 'example.com' },
|
2017-06-23 01:34:27 +09:00
|
|
|
].map(&method(:Fabricate).curry(2).call(:account))
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.search_for('username')
|
2017-06-23 01:34:27 +09:00
|
|
|
expect(results).to eq matches
|
2017-04-09 21:45:01 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.advanced_search_for' do
|
2022-05-26 22:50:33 +09:00
|
|
|
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)
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.advanced_search_for('A?l\i:c e', account, limit: 10, following: true)
|
2022-05-26 22:50:33 +09:00
|
|
|
expect(results).to eq [match]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return non-followed accounts' do
|
2023-10-19 23:55:06 +09:00
|
|
|
Fabricate(
|
2022-05-26 22:50:33 +09:00
|
|
|
:account,
|
|
|
|
display_name: 'A & l & i & c & e',
|
|
|
|
username: 'username',
|
|
|
|
domain: 'example.com'
|
|
|
|
)
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.advanced_search_for('A?l\i:c e', account, limit: 10, following: true)
|
2022-05-26 22:50:33 +09:00
|
|
|
expect(results).to eq []
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return suspended users' do
|
2023-10-19 23:55:06 +09:00
|
|
|
Fabricate(
|
2022-05-26 22:50:33 +09:00
|
|
|
:account,
|
|
|
|
display_name: 'Display Name',
|
|
|
|
username: 'username',
|
|
|
|
domain: 'example.com',
|
|
|
|
suspended: true
|
|
|
|
)
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.advanced_search_for('username', account, limit: 10, following: true)
|
2022-05-26 22:50:33 +09:00
|
|
|
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)
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.advanced_search_for('username', account, limit: 10, following: true)
|
2022-05-26 22:50:33 +09:00
|
|
|
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)
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.advanced_search_for('username', account, limit: 10, following: true)
|
2022-05-26 22:50:33 +09:00
|
|
|
expect(results).to eq []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return suspended users' do
|
2023-10-19 23:55:06 +09:00
|
|
|
Fabricate(
|
2022-05-26 22:50:33 +09:00
|
|
|
:account,
|
|
|
|
display_name: 'Display Name',
|
|
|
|
username: 'username',
|
|
|
|
domain: 'example.com',
|
|
|
|
suspended: true
|
|
|
|
)
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.advanced_search_for('username', account)
|
2022-05-26 22:50:33 +09:00
|
|
|
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)
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.advanced_search_for('username', account)
|
2022-05-26 22:50:33 +09:00
|
|
|
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)
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.advanced_search_for('username', account)
|
2022-05-26 22:50:33 +09:00
|
|
|
expect(results).to eq []
|
|
|
|
end
|
|
|
|
|
2017-06-23 01:34:27 +09:00
|
|
|
it 'accepts ?, \, : and space as delimiter' do
|
|
|
|
match = Fabricate(
|
|
|
|
:account,
|
|
|
|
display_name: 'A & l & i & c & e',
|
|
|
|
username: 'username',
|
|
|
|
domain: 'example.com'
|
|
|
|
)
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.advanced_search_for('A?l\i:c e', account)
|
2017-06-23 01:34:27 +09:00
|
|
|
expect(results).to eq [match]
|
|
|
|
end
|
|
|
|
|
2024-07-22 17:02:31 +09:00
|
|
|
it 'limits result count by default value' do
|
2023-12-01 20:00:41 +09:00
|
|
|
stub_const('Account::Search::DEFAULT_LIMIT', 1)
|
2023-11-20 18:08:22 +09:00
|
|
|
2.times { Fabricate(:account, display_name: 'Display Name') }
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.advanced_search_for('display', account)
|
2023-11-20 18:08:22 +09:00
|
|
|
expect(results.size).to eq 1
|
2017-06-23 01:34:27 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts arbitrary limits' do
|
2023-02-19 07:38:14 +09:00
|
|
|
2.times { Fabricate(:account, display_name: 'Display Name') }
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.advanced_search_for('display', account, limit: 1)
|
2017-06-23 01:34:27 +09:00
|
|
|
expect(results.size).to eq 1
|
|
|
|
end
|
|
|
|
|
2017-04-09 21:45:01 +09:00
|
|
|
it 'ranks followed accounts higher' do
|
2023-02-19 07:38:14 +09:00
|
|
|
match = Fabricate(:account, username: 'Matching')
|
|
|
|
followed_match = Fabricate(:account, username: 'Matcher')
|
2017-04-09 21:45:01 +09:00
|
|
|
Fabricate(:follow, account: account, target_account: followed_match)
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
results = described_class.advanced_search_for('match', account)
|
2017-04-09 21:45:01 +09:00
|
|
|
expect(results).to eq [followed_match, match]
|
|
|
|
expect(results.first.rank).to be > results.last.rank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-30 09:50:23 +09:00
|
|
|
describe '#statuses_count' do
|
|
|
|
subject { Fabricate(:account) }
|
|
|
|
|
|
|
|
it 'counts statuses' do
|
|
|
|
Fabricate(:status, account: subject)
|
|
|
|
Fabricate(:status, account: subject)
|
|
|
|
expect(subject.statuses_count).to eq 2
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not count direct statuses' do
|
|
|
|
Fabricate(:status, account: subject, visibility: :direct)
|
|
|
|
expect(subject.statuses_count).to eq 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is decremented when status is removed' do
|
|
|
|
status = Fabricate(:status, account: subject)
|
|
|
|
expect(subject.statuses_count).to eq 1
|
|
|
|
status.destroy
|
|
|
|
expect(subject.statuses_count).to eq 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is decremented when status is removed when account is not preloaded' do
|
|
|
|
status = Fabricate(:status, account: subject)
|
|
|
|
expect(subject.reload.statuses_count).to eq 1
|
|
|
|
clean_status = Status.find(status.id)
|
|
|
|
expect(clean_status.association(:account).loaded?).to be false
|
|
|
|
clean_status.destroy
|
|
|
|
expect(subject.reload.statuses_count).to eq 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-09 22:00:55 +09:00
|
|
|
describe '.following_map' do
|
|
|
|
it 'returns an hash' do
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.following_map([], 1)).to be_a Hash
|
2017-01-09 22:00:55 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.followed_by_map' do
|
|
|
|
it 'returns an hash' do
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.followed_by_map([], 1)).to be_a Hash
|
2017-01-09 22:00:55 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.blocking_map' do
|
|
|
|
it 'returns an hash' do
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.blocking_map([], 1)).to be_a Hash
|
2017-01-09 22:00:55 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.requested_map' do
|
|
|
|
it 'returns an hash' do
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.requested_map([], 1)).to be_a Hash
|
2017-01-09 22:00:55 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-16 02:50:11 +09:00
|
|
|
describe '.requested_by_map' do
|
|
|
|
it 'returns an hash' do
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.requested_by_map([], 1)).to be_a Hash
|
2022-12-16 02:50:11 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-28 06:51:18 +09:00
|
|
|
describe 'MENTION_RE' do
|
2024-03-14 00:42:39 +09:00
|
|
|
subject { described_class::MENTION_RE }
|
2016-03-28 06:51:18 +09:00
|
|
|
|
|
|
|
it 'matches usernames in the middle of a sentence' do
|
|
|
|
expect(subject.match('Hello to @alice from me')[1]).to eq 'alice'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'matches usernames in the beginning of status' do
|
|
|
|
expect(subject.match('@alice Hey how are you?')[1]).to eq 'alice'
|
|
|
|
end
|
|
|
|
|
2017-03-06 02:08:19 +09:00
|
|
|
it 'matches full usernames' do
|
|
|
|
expect(subject.match('@alice@example.com')[1]).to eq 'alice@example.com'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'matches full usernames with a dot at the end' do
|
|
|
|
expect(subject.match('Hello @alice@example.com.')[1]).to eq 'alice@example.com'
|
|
|
|
end
|
|
|
|
|
2016-03-28 06:51:18 +09:00
|
|
|
it 'matches dot-prepended usernames' do
|
|
|
|
expect(subject.match('.@alice I want everybody to see this')[1]).to eq 'alice'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not match e-mails' do
|
|
|
|
expect(subject.match('Drop me an e-mail at alice@example.com')).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not match URLs' do
|
|
|
|
expect(subject.match('Check this out https://medium.com/@alice/some-article#.abcdef123')).to be_nil
|
|
|
|
end
|
2017-05-06 02:48:22 +09:00
|
|
|
|
2023-10-04 00:01:45 +09:00
|
|
|
it 'does not match URL query string' do
|
2017-05-06 02:48:22 +09:00
|
|
|
expect(subject.match('https://example.com/?x=@alice')).to be_nil
|
|
|
|
end
|
2024-07-25 23:24:19 +09:00
|
|
|
|
|
|
|
it 'matches usernames immediately following the letter ß' do
|
|
|
|
expect(subject.match('Hello toß @alice from me')[1]).to eq 'alice'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'matches usernames containing uppercase characters' do
|
|
|
|
expect(subject.match('Hello to @aLice@Example.com from me')[1]).to eq 'aLice@Example.com'
|
|
|
|
end
|
2016-03-28 06:51:18 +09:00
|
|
|
end
|
2017-04-05 07:29:56 +09:00
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
describe 'Callbacks' do
|
|
|
|
describe 'Stripping content when required' do
|
|
|
|
context 'with a remote account' do
|
|
|
|
subject { Fabricate.build :account, domain: 'host.example', note: ' note ', display_name: ' display name ' }
|
|
|
|
|
|
|
|
it 'preserves content' do
|
|
|
|
expect { subject.valid? }
|
|
|
|
.to not_change(subject, :note)
|
|
|
|
.and not_change(subject, :display_name)
|
|
|
|
end
|
|
|
|
end
|
2024-09-05 18:48:33 +09:00
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
context 'with a local account' do
|
|
|
|
subject { Fabricate.build :account, domain: nil, note:, display_name: }
|
2024-09-05 18:48:33 +09:00
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
context 'with populated fields' do
|
|
|
|
let(:note) { ' note ' }
|
|
|
|
let(:display_name) { ' display name ' }
|
|
|
|
|
|
|
|
it 'strips content' do
|
|
|
|
expect { subject.valid? }
|
|
|
|
.to change(subject, :note).to('note')
|
|
|
|
.and change(subject, :display_name).to('display name')
|
|
|
|
end
|
|
|
|
end
|
2024-09-05 18:48:33 +09:00
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
context 'with empty fields' do
|
|
|
|
let(:note) { nil }
|
|
|
|
let(:display_name) { nil }
|
2024-09-05 18:48:33 +09:00
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
it 'preserves content' do
|
|
|
|
expect { subject.valid? }
|
|
|
|
.to not_change(subject, :note)
|
|
|
|
.and not_change(subject, :display_name)
|
|
|
|
end
|
|
|
|
end
|
2024-09-05 18:48:33 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-10 21:00:40 +09:00
|
|
|
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
|
|
|
|
|
2024-09-13 20:37:15 +09:00
|
|
|
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
|
|
|
|
|
2024-09-04 14:12:40 +09:00
|
|
|
describe 'Normalizations' do
|
|
|
|
describe 'username' do
|
|
|
|
it { is_expected.to normalize(:username).from(" \u3000bob \t \u00a0 \n ").to('bob') }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
describe 'Validations' do
|
2024-09-04 00:30:57 +09:00
|
|
|
it { is_expected.to validate_presence_of(:username) }
|
2017-04-05 07:29:56 +09:00
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
context 'when account is local' do
|
|
|
|
subject { Fabricate.build :account, domain: nil }
|
2017-04-05 07:29:56 +09:00
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
context 'with an existing differently-cased username account' do
|
|
|
|
before { Fabricate :account, username: 'the_doctor' }
|
|
|
|
|
|
|
|
it { is_expected.to_not allow_value('the_Doctor').for(:username) }
|
2017-06-23 01:34:27 +09:00
|
|
|
end
|
2017-06-05 08:03:45 +09:00
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
it { is_expected.to_not allow_value('support').for(:username) }
|
|
|
|
|
2017-06-23 01:34:27 +09:00
|
|
|
it 'is valid when username is reserved but record has already been created' do
|
|
|
|
account = Fabricate.build(:account, username: 'support')
|
|
|
|
account.save(validate: false)
|
|
|
|
expect(account.valid?).to be true
|
|
|
|
end
|
2017-06-08 22:22:01 +09:00
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
context 'with the instance actor' do
|
|
|
|
subject { Fabricate.build :account, id: described_class::INSTANCE_ACTOR_ID, actor_type: 'Application', locked: true }
|
|
|
|
|
|
|
|
it { is_expected.to allow_value('example.com').for(:username) }
|
2019-07-24 21:19:17 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is valid if we are creating a possibly-conflicting instance actor account' do
|
2023-10-18 21:26:22 +09:00
|
|
|
_account = Fabricate(:account, username: 'examplecom')
|
2024-02-19 20:09:43 +09:00
|
|
|
instance_account = Fabricate.build(:account, id: described_class::INSTANCE_ACTOR_ID, actor_type: 'Application', locked: true, username: 'example.com')
|
2023-10-18 21:26:22 +09:00
|
|
|
expect(instance_account.valid?).to be true
|
2019-07-24 21:19:17 +09:00
|
|
|
end
|
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
it { is_expected.to_not allow_values('the-doctor', 'the.doctor').for(:username) }
|
2019-07-24 21:19:17 +09:00
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
it { is_expected.to validate_length_of(:username).is_at_most(described_class::USERNAME_LENGTH_LIMIT) }
|
|
|
|
it { is_expected.to validate_length_of(:display_name).is_at_most(described_class::DISPLAY_NAME_LENGTH_LIMIT) }
|
2017-06-23 01:34:27 +09:00
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
it { is_expected.to_not allow_values(account_note_over_limit).for(:note) }
|
2017-06-23 01:34:27 +09:00
|
|
|
end
|
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
context 'when account is remote' do
|
|
|
|
subject { Fabricate.build :account, domain: 'host.example' }
|
2017-06-23 01:34:27 +09:00
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
context 'when a normalized domain account exists' do
|
|
|
|
subject { Fabricate.build :account, domain: 'xn--r9j5b5b' }
|
2017-06-23 01:34:27 +09:00
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
before { Fabricate(:account, domain: 'にゃん', username: 'username') }
|
2018-11-27 20:28:01 +09:00
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
it { is_expected.to_not allow_values('username', 'Username').for(:username) }
|
2017-06-23 01:34:27 +09:00
|
|
|
end
|
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
it { is_expected.to allow_values('the-doctor', username_over_limit).for(:username) }
|
|
|
|
it { is_expected.to_not allow_values('the doctor').for(:username) }
|
2017-06-23 01:34:27 +09:00
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
it { is_expected.to allow_values(display_name_over_limit).for(:display_name) }
|
2017-06-23 01:34:27 +09:00
|
|
|
|
2024-10-24 21:47:06 +09:00
|
|
|
it { is_expected.to allow_values(account_note_over_limit).for(:note) }
|
2017-04-05 07:29:56 +09:00
|
|
|
end
|
2024-06-11 00:03:41 +09:00
|
|
|
|
|
|
|
def username_over_limit
|
|
|
|
'a' * described_class::USERNAME_LENGTH_LIMIT * 2
|
|
|
|
end
|
|
|
|
|
2024-07-20 00:31:48 +09:00
|
|
|
def display_name_over_limit
|
|
|
|
'a' * described_class::DISPLAY_NAME_LENGTH_LIMIT * 2
|
|
|
|
end
|
|
|
|
|
2024-06-11 00:03:41 +09:00
|
|
|
def account_note_over_limit
|
|
|
|
'a' * described_class::NOTE_LENGTH_LIMIT * 2
|
|
|
|
end
|
2017-04-05 07:29:56 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'scopes' do
|
2024-01-22 22:55:37 +09:00
|
|
|
describe 'matches_uri_prefix' do
|
|
|
|
let!(:alice) { Fabricate :account, domain: 'host.example', uri: 'https://host.example/user/a' }
|
|
|
|
let!(:bob) { Fabricate :account, domain: 'top-level.example', uri: 'https://top-level.example' }
|
|
|
|
|
|
|
|
it 'returns accounts which start with the value' do
|
|
|
|
results = described_class.matches_uri_prefix('https://host.example')
|
|
|
|
|
|
|
|
expect(results.size)
|
|
|
|
.to eq(1)
|
|
|
|
expect(results)
|
|
|
|
.to include(alice)
|
|
|
|
.and not_include(bob)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns accounts which equal the value' do
|
|
|
|
results = described_class.matches_uri_prefix('https://top-level.example')
|
|
|
|
|
|
|
|
expect(results.size)
|
|
|
|
.to eq(1)
|
|
|
|
expect(results)
|
|
|
|
.to include(bob)
|
|
|
|
.and not_include(alice)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-01-19 10:39:30 +09:00
|
|
|
describe 'auditable' do
|
|
|
|
let!(:alice) { Fabricate :account }
|
|
|
|
let!(:bob) { Fabricate :account }
|
|
|
|
|
|
|
|
before do
|
|
|
|
2.times { Fabricate :action_log, account: alice }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns distinct accounts with action log records' do
|
|
|
|
results = described_class.auditable
|
|
|
|
|
|
|
|
expect(results.size)
|
|
|
|
.to eq(1)
|
|
|
|
expect(results)
|
|
|
|
.to include(alice)
|
|
|
|
.and not_include(bob)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-23 01:34:27 +09:00
|
|
|
describe 'alphabetic' do
|
|
|
|
it 'sorts by alphabetic order of domain and username' do
|
|
|
|
matches = [
|
|
|
|
{ username: 'a', domain: 'a' },
|
|
|
|
{ username: 'b', domain: 'a' },
|
|
|
|
{ username: 'a', domain: 'b' },
|
|
|
|
{ username: 'b', domain: 'b' },
|
|
|
|
].map(&method(:Fabricate).curry(2).call(:account))
|
|
|
|
|
2024-03-13 23:11:23 +09:00
|
|
|
expect(described_class.without_internal.alphabetic).to eq matches
|
2017-06-23 01:34:27 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'matches_display_name' do
|
|
|
|
it 'matches display name which starts with the given string' do
|
|
|
|
match = Fabricate(:account, display_name: 'pattern and suffix')
|
|
|
|
Fabricate(:account, display_name: 'prefix and pattern')
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.matches_display_name('pattern')).to eq [match]
|
2017-06-23 01:34:27 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'matches_username' do
|
|
|
|
it 'matches display name which starts with the given string' do
|
|
|
|
match = Fabricate(:account, username: 'pattern_and_suffix')
|
|
|
|
Fabricate(:account, username: 'prefix_and_pattern')
|
|
|
|
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.matches_username('pattern')).to eq [match]
|
2017-06-23 01:34:27 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-22 07:13:10 +09:00
|
|
|
describe 'by_domain_and_subdomains' do
|
|
|
|
it 'returns exact domain matches' do
|
|
|
|
account = Fabricate(:account, domain: 'example.com')
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.by_domain_and_subdomains('example.com')).to eq [account]
|
2019-06-22 07:13:10 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns subdomains' do
|
|
|
|
account = Fabricate(:account, domain: 'foo.example.com')
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.by_domain_and_subdomains('example.com')).to eq [account]
|
2019-06-22 07:13:10 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return partially matching domains' do
|
|
|
|
account = Fabricate(:account, domain: 'grexample.com')
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.by_domain_and_subdomains('example.com')).to_not eq [account]
|
2019-06-22 07:13:10 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-05 07:29:56 +09:00
|
|
|
describe 'remote' do
|
|
|
|
it 'returns an array of accounts who have a domain' do
|
2023-10-18 21:26:22 +09:00
|
|
|
_account = Fabricate(:account, domain: nil)
|
|
|
|
account_with_domain = Fabricate(:account, domain: 'example.com')
|
|
|
|
expect(described_class.remote).to contain_exactly(account_with_domain)
|
2017-04-05 07:29:56 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'local' do
|
|
|
|
it 'returns an array of accounts who do not have a domain' do
|
2023-10-18 21:26:22 +09:00
|
|
|
local_account = Fabricate(:account, domain: nil)
|
|
|
|
_account_with_domain = Fabricate(:account, domain: 'example.com')
|
2024-03-13 23:11:23 +09:00
|
|
|
expect(described_class.without_internal.local).to contain_exactly(local_account)
|
2017-04-05 07:29:56 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-23 01:34:27 +09:00
|
|
|
describe 'partitioned' do
|
|
|
|
it 'returns a relation of accounts partitioned by domain' do
|
2023-02-20 14:14:10 +09:00
|
|
|
matches = %w(a b a b)
|
2017-06-23 01:34:27 +09:00
|
|
|
matches.size.times.to_a.shuffle.each do |index|
|
|
|
|
matches[index] = Fabricate(:account, domain: matches[index])
|
|
|
|
end
|
|
|
|
|
2024-03-13 23:11:23 +09:00
|
|
|
expect(described_class.without_internal.partitioned).to match_array(matches)
|
2017-06-23 01:34:27 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'recent' do
|
|
|
|
it 'returns a relation of accounts sorted by recent creation' do
|
2023-05-03 01:07:16 +09:00
|
|
|
matches = Array.new(2) { Fabricate(:account) }
|
2024-03-13 23:11:23 +09:00
|
|
|
expect(described_class.without_internal.recent).to match_array(matches)
|
2017-06-23 01:34:27 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-05 07:29:56 +09:00
|
|
|
describe 'silenced' do
|
|
|
|
it 'returns an array of accounts who are silenced' do
|
2023-10-18 21:26:22 +09:00
|
|
|
silenced_account = Fabricate(:account, silenced: true)
|
|
|
|
_account = Fabricate(:account, silenced: false)
|
|
|
|
expect(described_class.silenced).to contain_exactly(silenced_account)
|
2017-04-05 07:29:56 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-26 22:50:33 +09:00
|
|
|
describe 'searchable' do
|
|
|
|
let!(:suspended_local) { Fabricate(:account, suspended: true, username: 'suspended_local') }
|
|
|
|
let!(:suspended_remote) { Fabricate(:account, suspended: true, domain: 'example.org', username: 'suspended_remote') }
|
|
|
|
let!(:silenced_local) { Fabricate(:account, silenced: true, username: 'silenced_local') }
|
|
|
|
let!(:silenced_remote) { Fabricate(:account, silenced: true, domain: 'example.org', username: 'silenced_remote') }
|
|
|
|
let!(:unconfirmed) { Fabricate(:user, confirmed_at: nil).account }
|
|
|
|
let!(:unapproved) { Fabricate(:user, approved: false).account }
|
|
|
|
let!(:unconfirmed_unapproved) { Fabricate(:user, confirmed_at: nil, approved: false).account }
|
|
|
|
let!(:local_account) { Fabricate(:account, username: 'local_account') }
|
|
|
|
let!(:remote_account) { Fabricate(:account, domain: 'example.org', username: 'remote_account') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
# Accounts get automatically-approved depending on settings, so ensure they aren't approved
|
|
|
|
unapproved.user.update(approved: false)
|
|
|
|
unconfirmed_unapproved.user.update(approved: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns every usable non-suspended account' do
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.searchable).to contain_exactly(silenced_local, silenced_remote, local_account, remote_account)
|
2023-12-21 18:18:38 +09:00
|
|
|
expect(described_class.searchable).to_not include(suspended_local, suspended_remote, unconfirmed, unapproved)
|
2022-05-26 22:50:33 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not mess with previously-applied scopes' do
|
2023-06-06 20:58:33 +09:00
|
|
|
expect(described_class.where.not(id: remote_account.id).searchable).to contain_exactly(silenced_local, silenced_remote, local_account)
|
2022-05-26 22:50:33 +09:00
|
|
|
end
|
|
|
|
end
|
2017-06-23 01:34:27 +09:00
|
|
|
end
|
2017-04-11 07:38:58 +09:00
|
|
|
|
2017-06-23 01:34:27 +09:00
|
|
|
context 'when is local' do
|
2023-04-30 09:28:52 +09:00
|
|
|
it 'generates keys' do
|
2024-07-15 19:42:18 +09:00
|
|
|
account = described_class.create!(domain: nil, username: 'user_without_keys')
|
|
|
|
|
|
|
|
expect(account)
|
|
|
|
.to be_private_key
|
|
|
|
.and be_public_key
|
|
|
|
expect(account.keypair)
|
|
|
|
.to be_private
|
|
|
|
.and be_public
|
2017-06-23 01:34:27 +09:00
|
|
|
end
|
|
|
|
end
|
2017-04-11 07:38:58 +09:00
|
|
|
|
2017-06-23 01:34:27 +09:00
|
|
|
context 'when is remote' do
|
|
|
|
it 'does not generate keys' do
|
|
|
|
key = OpenSSL::PKey::RSA.new(1024).public_key
|
2024-07-15 19:42:18 +09:00
|
|
|
account = described_class.create!(domain: 'remote', uri: 'https://remote/actor', username: 'remote_user_with_public', public_key: key.to_pem)
|
2017-06-23 01:34:27 +09:00
|
|
|
expect(account.keypair.params).to eq key.params
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'normalizes domain' do
|
2024-07-15 19:42:18 +09:00
|
|
|
account = described_class.create!(domain: 'にゃん', uri: 'https://xn--r9j5b5b/actor', username: 'remote_user_with_idn_domain')
|
2017-06-23 01:34:27 +09:00
|
|
|
expect(account.domain).to eq 'xn--r9j5b5b'
|
2017-04-11 07:38:58 +09:00
|
|
|
end
|
|
|
|
end
|
2017-06-23 01:34:27 +09:00
|
|
|
|
|
|
|
include_examples 'AccountAvatar', :account
|
2020-01-04 09:54:07 +09:00
|
|
|
include_examples 'AccountHeader', :account
|
2020-11-10 00:00:23 +09:00
|
|
|
|
|
|
|
describe '#increment_count!' do
|
|
|
|
subject { Fabricate(:account) }
|
|
|
|
|
|
|
|
it 'increments the count in multi-threaded an environment when account_stat is not yet initialized' do
|
|
|
|
subject
|
|
|
|
|
2024-02-07 23:53:29 +09:00
|
|
|
multi_threaded_execution(15) do
|
|
|
|
described_class.find(subject.id).increment_count!(:followers_count)
|
2020-11-10 00:00:23 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(subject.reload.followers_count).to eq 15
|
|
|
|
end
|
|
|
|
end
|
2016-02-21 06:53:20 +09:00
|
|
|
end
|