Gab Social. All are welcome.

This commit is contained in:
robcolbert
2019-07-02 03:10:25 -04:00
commit bd0b5afc92
5366 changed files with 222812 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
require 'rails_helper'
RSpec.describe AccountConversation, type: :model do
let!(:alice) { Fabricate(:account, username: 'alice') }
let!(:bob) { Fabricate(:account, username: 'bob') }
let!(:mark) { Fabricate(:account, username: 'mark') }
describe '.add_status' do
it 'creates new record when no others exist' do
status = Fabricate(:status, account: alice, visibility: :direct)
status.mentions.create(account: bob)
conversation = AccountConversation.add_status(alice, status)
expect(conversation.participant_accounts).to include(bob)
expect(conversation.last_status).to eq status
expect(conversation.status_ids).to eq [status.id]
end
it 'appends to old record when there is a match' do
last_status = Fabricate(:status, account: alice, visibility: :direct)
conversation = AccountConversation.create!(account: alice, conversation: last_status.conversation, participant_account_ids: [bob.id], status_ids: [last_status.id])
status = Fabricate(:status, account: bob, visibility: :direct, thread: last_status)
status.mentions.create(account: alice)
new_conversation = AccountConversation.add_status(alice, status)
expect(new_conversation.id).to eq conversation.id
expect(new_conversation.participant_accounts).to include(bob)
expect(new_conversation.last_status).to eq status
expect(new_conversation.status_ids).to eq [last_status.id, status.id]
end
it 'creates new record when new participants are added' do
last_status = Fabricate(:status, account: alice, visibility: :direct)
conversation = AccountConversation.create!(account: alice, conversation: last_status.conversation, participant_account_ids: [bob.id], status_ids: [last_status.id])
status = Fabricate(:status, account: bob, visibility: :direct, thread: last_status)
status.mentions.create(account: alice)
status.mentions.create(account: mark)
new_conversation = AccountConversation.add_status(alice, status)
expect(new_conversation.id).to_not eq conversation.id
expect(new_conversation.participant_accounts).to include(bob, mark)
expect(new_conversation.last_status).to eq status
expect(new_conversation.status_ids).to eq [status.id]
end
end
describe '.remove_status' do
it 'updates last status to a previous value' do
last_status = Fabricate(:status, account: alice, visibility: :direct)
status = Fabricate(:status, account: alice, visibility: :direct)
conversation = AccountConversation.create!(account: alice, conversation: last_status.conversation, participant_account_ids: [bob.id], status_ids: [status.id, last_status.id])
last_status.mentions.create(account: bob)
last_status.destroy!
conversation.reload
expect(conversation.last_status).to eq status
expect(conversation.status_ids).to eq [status.id]
end
it 'removes the record if no other statuses are referenced' do
last_status = Fabricate(:status, account: alice, visibility: :direct)
conversation = AccountConversation.create!(account: alice, conversation: last_status.conversation, participant_account_ids: [bob.id], status_ids: [last_status.id])
last_status.mentions.create(account: bob)
last_status.destroy!
expect(AccountConversation.where(id: conversation.id).count).to eq 0
end
end
end

View File

@@ -0,0 +1,22 @@
require 'rails_helper'
RSpec.describe AccountDomainBlock, type: :model do
it 'removes blocking cache after creation' do
account = Fabricate(:account)
Rails.cache.write("exclude_domains_for:#{account.id}", 'a.domain.already.blocked')
AccountDomainBlock.create!(account: account, domain: 'a.domain.blocked.later')
expect(Rails.cache.exist?("exclude_domains_for:#{account.id}")).to eq false
end
it 'removes blocking cache after destruction' do
account = Fabricate(:account)
block = AccountDomainBlock.create!(account: account, domain: 'domain')
Rails.cache.write("exclude_domains_for:#{account.id}", 'domain')
block.destroy!
expect(Rails.cache.exist?("exclude_domains_for:#{account.id}")).to eq false
end
end

View File

@@ -0,0 +1,57 @@
require 'rails_helper'
describe AccountFilter do
describe 'with empty params' do
it 'defaults to recent local not-suspended account list' do
filter = described_class.new({})
expect(filter.results).to eq Account.local.recent.without_suspended
end
end
describe 'with invalid params' do
it 'raises with key error' do
filter = described_class.new(wrong: true)
expect { filter.results }.to raise_error(/wrong/)
end
end
describe 'with valid params' do
it 'combines filters on Account' do
filter = described_class.new(
by_domain: 'test.com',
silenced: true,
username: 'test',
display_name: 'name',
email: 'user@example.com',
)
allow(Account).to receive(:where).and_return(Account.none)
allow(Account).to receive(:silenced).and_return(Account.none)
allow(Account).to receive(:matches_display_name).and_return(Account.none)
allow(Account).to receive(:matches_username).and_return(Account.none)
allow(User).to receive(:matches_email).and_return(User.none)
filter.results
expect(Account).to have_received(:where).with(domain: 'test.com')
expect(Account).to have_received(:silenced)
expect(Account).to have_received(:matches_username).with('test')
expect(Account).to have_received(:matches_display_name).with('name')
expect(User).to have_received(:matches_email).with('user@example.com')
end
describe 'that call account methods' do
%i(local remote silenced suspended).each do |option|
it "delegates the #{option} option" do
allow(Account).to receive(option).and_return(Account.none)
filter = described_class.new({ option => true })
filter.results
expect(Account).to have_received(option).at_least(1)
end
end
end
end
end

View File

@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe AccountModerationNote, type: :model do
end

792
spec/models/account_spec.rb Normal file
View File

@@ -0,0 +1,792 @@
require 'rails_helper'
RSpec.describe Account, type: :model do
context do
let(:bob) { Fabricate(:account, username: 'bob') }
subject { Fabricate(:account) }
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)
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
end
describe '#local?' do
it 'returns true when the account is local' do
account = Fabricate(:account, domain: nil)
expect(account.local?).to be true
end
it 'returns false when the account is on a different domain' do
account = Fabricate(:account, domain: 'foreign.tld')
expect(account.local?).to be false
end
end
describe 'Local domain user methods' do
around do |example|
before = Rails.configuration.x.local_domain
example.run
Rails.configuration.x.local_domain = before
end
subject { Fabricate(:account, domain: nil, username: 'alice') }
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
end
describe '#acct' do
it 'returns username for local users' do
account = Fabricate(:account, domain: nil, username: 'alice')
expect(account.acct).to eql 'alice'
end
it 'returns username@domain for foreign users' do
account = Fabricate(:account, domain: 'foreign.tld', username: 'alice')
expect(account.acct).to eql 'alice@foreign.tld'
end
end
describe '#save_with_optional_media!' do
before do
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'))
end
let(:account) do
Fabricate(:account,
avatar_remote_url: 'https://remote.test/valid_avatar',
header_remote_url: 'https://remote.test/valid_avatar')
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
account.avatar_remote_url = 'https://remote.test/invalid_avatar'
account.save_with_optional_media!
end
it 'sets default avatar, header, avatar_remote_url, and header_remote_url' do
expect(account.avatar_remote_url).to eq ''
expect(account.header_remote_url).to eq ''
expect(account.avatar_file_name).to eq nil
expect(account.header_file_name).to eq nil
end
end
end
describe '#subscribed?' do
it 'returns false when no subscription expiration information is present' do
account = Fabricate(:account, subscription_expires_at: nil)
expect(account.subscribed?).to be false
end
it 'returns true when subscription expiration has been set' do
account = Fabricate(:account, subscription_expires_at: 30.days.from_now)
expect(account.subscribed?).to be true
end
end
describe '#possibly_stale?' do
let(:account) { Fabricate(:account, last_webfingered_at: last_webfingered_at) }
context 'last_webfingered_at is nil' do
let(:last_webfingered_at) { nil }
it 'returns true' do
expect(account.possibly_stale?).to be true
end
end
context 'last_webfingered_at is more than 24 hours before' do
let(:last_webfingered_at) { 25.hours.ago }
it 'returns true' do
expect(account.possibly_stale?).to be true
end
end
context 'last_webfingered_at is less than 24 hours before' do
let(:last_webfingered_at) { 23.hours.ago }
it 'returns false' do
expect(account.possibly_stale?).to be false
end
end
end
describe '#refresh!' do
let(:account) { Fabricate(:account, domain: domain) }
let(:acct) { account.acct }
context 'domain is nil' do
let(:domain) { nil }
it 'returns nil' do
expect(account.refresh!).to be_nil
end
it 'calls not ResolveAccountService#call' do
expect_any_instance_of(ResolveAccountService).not_to receive(:call).with(acct)
account.refresh!
end
end
context 'domain is present' do
let(:domain) { 'example.com' }
it 'calls ResolveAccountService#call' do
expect_any_instance_of(ResolveAccountService).to receive(:call).with(acct).once
account.refresh!
end
end
end
describe '#to_param' do
it 'returns username' do
account = Fabricate(:account, username: 'alice')
expect(account.to_param).to eq 'alice'
end
end
describe '#keypair' do
it 'returns an RSA key pair' do
account = Fabricate(:account)
expect(account.keypair).to be_instance_of OpenSSL::PKey::RSA
end
end
describe '#subscription' do
it 'returns an OStatus subscription' do
account = Fabricate(:account)
expect(account.subscription('')).to be_instance_of OStatus2::Subscription
end
end
describe '#object_type' do
it 'is always a person' do
account = Fabricate(:account)
expect(account.object_type).to be :person
end
end
describe '#favourited?' do
let(:original_status) do
author = Fabricate(:account, username: 'original')
Fabricate(:status, account: author)
end
subject { Fabricate(:account) }
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
it 'is is true when this account has favourited it' do
Fabricate(:favourite, status: original_reblog, account: subject)
expect(subject.favourited?(original_status)).to eq true
end
it 'is false when this account has not favourited it' do
expect(subject.favourited?(original_status)).to eq false
end
end
context 'when the status is an original status' do
it 'is is true when this account has favourited it' do
Fabricate(:favourite, status: original_status, account: subject)
expect(subject.favourited?(original_status)).to eq true
end
it 'is false when this account has not favourited it' do
expect(subject.favourited?(original_status)).to eq false
end
end
end
describe '#reblogged?' do
let(:original_status) do
author = Fabricate(:account, username: 'original')
Fabricate(:status, account: author)
end
subject { Fabricate(:account) }
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
it 'is true when this account has reblogged it' do
Fabricate(:status, reblog: original_reblog, account: subject)
expect(subject.reblogged?(original_reblog)).to eq true
end
it 'is false when this account has not reblogged it' do
expect(subject.reblogged?(original_reblog)).to eq false
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)
expect(subject.reblogged?(original_status)).to eq true
end
it 'is false when this account has not reblogged it' do
expect(subject.reblogged?(original_status)).to eq false
end
end
end
describe '#excluded_from_timeline_account_ids' do
it 'includes account ids of blockings, blocked_bys and mutes' do
account = Fabricate(:account)
block = Fabricate(:block, account: account)
mute = Fabricate(:mute, account: account)
block_by = Fabricate(:block, target_account: account)
results = account.excluded_from_timeline_account_ids
expect(results.size).to eq 3
expect(results).to include(block.target_account.id)
expect(results).to include(mute.target_account.id)
expect(results).to include(block_by.account.id)
end
end
describe '#excluded_from_timeline_domains' do
it 'returns the domains blocked by the account' do
account = Fabricate(:account)
account.block_domain!('domain')
expect(account.excluded_from_timeline_domains).to match_array ['domain']
end
end
describe '.search_for' do
before do
_missing = Fabricate(
:account,
display_name: "Missing",
username: "missing",
domain: "missing.com"
)
end
it 'accepts ?, \, : and space as delimiter' do
match = Fabricate(
:account,
display_name: 'A & l & i & c & e',
username: 'username',
domain: 'example.com'
)
results = Account.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 = Account.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 = Account.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 = Account.search_for("example")
expect(results).to eq [match]
end
it 'limits by 10 by default' do
11.times.each { Fabricate(:account, display_name: "Display Name") }
results = Account.search_for("display")
expect(results.size).to eq 10
end
it 'accepts arbitrary limits' do
2.times.each { Fabricate(:account, display_name: "Display Name") }
results = Account.search_for("display", 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 = Account.search_for("username")
expect(results).to eq matches
end
end
describe '.advanced_search_for' do
it 'accepts ?, \, : and space as delimiter' do
account = Fabricate(:account)
match = Fabricate(
:account,
display_name: 'A & l & i & c & e',
username: 'username',
domain: 'example.com'
)
results = Account.advanced_search_for('A?l\i:c e', account)
expect(results).to eq [match]
end
it 'limits by 10 by default' do
11.times { Fabricate(:account, display_name: "Display Name") }
results = Account.search_for("display")
expect(results.size).to eq 10
end
it 'accepts arbitrary limits' do
2.times { Fabricate(:account, display_name: "Display Name") }
results = Account.search_for("display", 1)
expect(results.size).to eq 1
end
it 'ranks followed accounts higher' do
account = Fabricate(:account)
match = Fabricate(:account, username: "Matching")
followed_match = Fabricate(:account, username: "Matcher")
Fabricate(:follow, account: account, target_account: followed_match)
results = Account.advanced_search_for("match", account)
expect(results).to eq [followed_match, match]
expect(results.first.rank).to be > results.last.rank
end
end
describe '.domains' do
it 'returns domains' do
Fabricate(:account, domain: 'domain')
expect(Account.domains).to match_array(['domain'])
end
end
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
describe '.following_map' do
it 'returns an hash' do
expect(Account.following_map([], 1)).to be_a Hash
end
end
describe '.followed_by_map' do
it 'returns an hash' do
expect(Account.followed_by_map([], 1)).to be_a Hash
end
end
describe '.blocking_map' do
it 'returns an hash' do
expect(Account.blocking_map([], 1)).to be_a Hash
end
end
describe '.requested_map' do
it 'returns an hash' do
expect(Account.requested_map([], 1)).to be_a Hash
end
end
describe 'MENTION_RE' do
subject { Account::MENTION_RE }
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
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
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
xit 'does not match URL querystring' do
expect(subject.match('https://example.com/?x=@alice')).to be_nil
end
end
describe 'validations' do
it 'has a valid fabricator' do
account = Fabricate.build(:account)
account.valid?
expect(account).to be_valid
end
it 'is invalid without a username' do
account = Fabricate.build(:account, username: nil)
account.valid?
expect(account).to model_have_error_on_field(:username)
end
it 'squishes the username before validation' do
account = Fabricate(:account, domain: nil, username: " \u3000bob \t \u00a0 \n ")
expect(account.username).to eq 'bob'
end
context 'when is local' do
it 'is invalid if the username is not unique in case-insensitive comparison among local accounts' do
account_1 = Fabricate(:account, username: 'the_doctor')
account_2 = Fabricate.build(:account, username: 'the_Doctor')
account_2.valid?
expect(account_2).to model_have_error_on_field(:username)
end
it 'is invalid if the username is reserved' do
account = Fabricate.build(:account, username: 'support')
account.valid?
expect(account).to model_have_error_on_field(:username)
end
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
it 'is invalid if the username doesn\'t only contains letters, numbers and underscores' do
account = Fabricate.build(:account, username: 'the-doctor')
account.valid?
expect(account).to model_have_error_on_field(:username)
end
it 'is invalid if the username is longer then 30 characters' do
account = Fabricate.build(:account, username: Faker::Lorem.characters(31))
account.valid?
expect(account).to model_have_error_on_field(:username)
end
it 'is invalid if the display name is longer than 30 characters' do
account = Fabricate.build(:account, display_name: Faker::Lorem.characters(31))
account.valid?
expect(account).to model_have_error_on_field(:display_name)
end
it 'is invalid if the note is longer than 500 characters' do
account = Fabricate.build(:account, note: Faker::Lorem.characters(501))
account.valid?
expect(account).to model_have_error_on_field(:note)
end
end
context 'when is remote' do
it 'is invalid if the username is not unique in case-sensitive comparison among accounts in the same normalized domain' do
Fabricate(:account, domain: 'にゃん', username: 'username')
account = Fabricate.build(:account, domain: 'xn--r9j5b5b', username: 'username')
account.valid?
expect(account).to model_have_error_on_field(:username)
end
it 'is valid even if the username is unique only in case-sensitive comparison among accounts in the same normalized domain' do
Fabricate(:account, domain: 'にゃん', username: 'username')
account = Fabricate.build(:account, domain: 'xn--r9j5b5b', username: 'Username')
account.valid?
expect(account).not_to model_have_error_on_field(:username)
end
it 'is valid even if the username contains hyphens' do
account = Fabricate.build(:account, domain: 'domain', username: 'the-doctor')
account.valid?
expect(account).to_not model_have_error_on_field(:username)
end
it 'is invalid if the username doesn\'t only contains letters, numbers, underscores and hyphens' do
account = Fabricate.build(:account, domain: 'domain', username: 'the doctor')
account.valid?
expect(account).to model_have_error_on_field(:username)
end
it 'is valid even if the username is longer then 30 characters' do
account = Fabricate.build(:account, domain: 'domain', username: Faker::Lorem.characters(31))
account.valid?
expect(account).not_to model_have_error_on_field(:username)
end
it 'is valid even if the display name is longer than 30 characters' do
account = Fabricate.build(:account, domain: 'domain', display_name: Faker::Lorem.characters(31))
account.valid?
expect(account).not_to model_have_error_on_field(:display_name)
end
it 'is valid even if the note is longer than 500 characters' do
account = Fabricate.build(:account, domain: 'domain', note: Faker::Lorem.characters(501))
account.valid?
expect(account).not_to model_have_error_on_field(:note)
end
end
end
describe 'scopes' do
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))
expect(Account.alphabetic).to eq matches
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')
expect(Account.matches_display_name('pattern')).to eq [match]
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')
expect(Account.matches_username('pattern')).to eq [match]
end
end
describe 'expiring' do
it 'returns remote accounts with followers whose subscription expiration date is past or not given' do
local = Fabricate(:account, domain: nil)
matches = [
{ domain: 'remote', subscription_expires_at: '2000-01-01T00:00:00Z' },
].map(&method(:Fabricate).curry(2).call(:account))
matches.each(&local.method(:follow!))
Fabricate(:account, domain: 'remote', subscription_expires_at: nil)
local.follow!(Fabricate(:account, domain: 'remote', subscription_expires_at: '2000-01-03T00:00:00Z'))
local.follow!(Fabricate(:account, domain: nil, subscription_expires_at: nil))
expect(Account.expiring('2000-01-02T00:00:00Z').recent).to eq matches.reverse
end
end
describe 'remote' do
it 'returns an array of accounts who have a domain' do
account_1 = Fabricate(:account, domain: nil)
account_2 = Fabricate(:account, domain: 'example.com')
expect(Account.remote).to match_array([account_2])
end
end
describe 'by_domain_accounts' do
it 'returns accounts grouped by domain sorted by accounts' do
2.times { Fabricate(:account, domain: 'example.com') }
Fabricate(:account, domain: 'example2.com')
results = Account.by_domain_accounts
expect(results.length).to eq 2
expect(results.first.domain).to eq 'example.com'
expect(results.first.accounts_count).to eq 2
expect(results.last.domain).to eq 'example2.com'
expect(results.last.accounts_count).to eq 1
end
end
describe 'local' do
it 'returns an array of accounts who do not have a domain' do
account_1 = Fabricate(:account, domain: nil)
account_2 = Fabricate(:account, domain: 'example.com')
expect(Account.local).to match_array([account_1])
end
end
describe 'partitioned' do
it 'returns a relation of accounts partitioned by domain' do
matches = ['a', 'b', 'a', 'b']
matches.size.times.to_a.shuffle.each do |index|
matches[index] = Fabricate(:account, domain: matches[index])
end
expect(Account.partitioned).to match_array(matches)
end
end
describe 'recent' do
it 'returns a relation of accounts sorted by recent creation' do
matches = 2.times.map { Fabricate(:account) }
expect(Account.recent).to match_array(matches)
end
end
describe 'silenced' do
it 'returns an array of accounts who are silenced' do
account_1 = Fabricate(:account, silenced: true)
account_2 = Fabricate(:account, silenced: false)
expect(Account.silenced).to match_array([account_1])
end
end
describe 'suspended' do
it 'returns an array of accounts who are suspended' do
account_1 = Fabricate(:account, suspended: true)
account_2 = Fabricate(:account, suspended: false)
expect(Account.suspended).to match_array([account_1])
end
end
end
context 'when is local' do
# Test disabled because test environment omits autogenerating keys for performance
xit 'generates keys' do
account = Account.create!(domain: nil, username: Faker::Internet.user_name(nil, ['_']))
expect(account.keypair.private?).to eq true
end
end
context 'when is remote' do
it 'does not generate keys' do
key = OpenSSL::PKey::RSA.new(1024).public_key
account = Account.create!(domain: 'remote', username: Faker::Internet.user_name(nil, ['_']), public_key: key.to_pem)
expect(account.keypair.params).to eq key.params
end
it 'normalizes domain' do
account = Account.create!(domain: 'にゃん', username: Faker::Internet.user_name(nil, ['_']))
expect(account.domain).to eq 'xn--r9j5b5b'
end
end
include_examples 'AccountAvatar', :account
end

View File

@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe AccountStat, type: :model do
end

View File

@@ -0,0 +1,38 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe AccountTagStat, type: :model do
key = 'accounts_count'
let(:account_tag_stat) { Fabricate(:tag).account_tag_stat }
describe '#increment_count!' do
it 'calls #update' do
args = { key => account_tag_stat.public_send(key) + 1 }
expect(account_tag_stat).to receive(:update).with(args)
account_tag_stat.increment_count!(key)
end
it 'increments value by 1' do
expect do
account_tag_stat.increment_count!(key)
end.to change { account_tag_stat.accounts_count }.by(1)
end
end
describe '#decrement_count!' do
it 'calls #update' do
args = { key => [account_tag_stat.public_send(key) - 1, 0].max }
expect(account_tag_stat).to receive(:update).with(args)
account_tag_stat.decrement_count!(key)
end
it 'decrements value by 1' do
account_tag_stat.update(key => 1)
expect do
account_tag_stat.decrement_count!(key)
end.to change { account_tag_stat.accounts_count }.by(-1)
end
end
end

View File

@@ -0,0 +1,131 @@
require 'rails_helper'
RSpec.describe Admin::AccountAction, type: :model do
let(:account_action) { described_class.new }
describe '#save!' do
subject { account_action.save! }
let(:account) { Fabricate(:account, user: Fabricate(:user, admin: true)) }
let(:target_account) { Fabricate(:account, user: Fabricate(:user)) }
let(:type) { 'disable' }
before do
account_action.assign_attributes(
type: type,
current_account: account,
target_account: target_account
)
end
context 'type is "disable"' do
let(:type) { 'disable' }
it 'disable user' do
subject
expect(target_account.user).to be_disabled
end
end
context 'type is "silence"' do
let(:type) { 'silence' }
it 'silences account' do
subject
expect(target_account).to be_silenced
end
end
context 'type is "suspend"' do
let(:type) { 'suspend' }
it 'suspends account' do
subject
expect(target_account).to be_suspended
end
it 'queues Admin::SuspensionWorker by 1' do
Sidekiq::Testing.fake! do
expect do
subject
end.to change { Admin::SuspensionWorker.jobs.size }.by 1
end
end
end
it 'creates Admin::ActionLog' do
expect do
subject
end.to change { Admin::ActionLog.count }.by 1
end
it 'calls queue_email!' do
expect(account_action).to receive(:queue_email!)
subject
end
it 'calls process_reports!' do
expect(account_action).to receive(:process_reports!)
subject
end
end
describe '#report' do
subject { account_action.report }
context 'report_id.present?' do
before do
account_action.report_id = Fabricate(:report).id
end
it 'returns Report' do
expect(subject).to be_instance_of Report
end
end
context '!report_id.present?' do
it 'returns nil' do
expect(subject).to be_nil
end
end
end
describe '#with_report?' do
subject { account_action.with_report? }
context '!report.nil?' do
before do
account_action.report_id = Fabricate(:report).id
end
it 'returns true' do
expect(subject).to be true
end
end
context '!(!report.nil?)' do
it 'returns false' do
expect(subject).to be false
end
end
end
describe '.types_for_account' do
subject { described_class.types_for_account(account) }
context 'account.local?' do
let(:account) { Fabricate(:account, domain: nil) }
it 'returns ["none", "disable", "silence", "suspend"]' do
expect(subject).to eq %w(none disable silence suspend)
end
end
context '!account.local?' do
let(:account) { Fabricate(:account, domain: 'hoge.com') }
it 'returns ["silence", "suspend"]' do
expect(subject).to eq %w(silence suspend)
end
end
end
end

View File

@@ -0,0 +1,12 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Admin::ActionLog, type: :model do
describe '#action' do
it 'returns action' do
action_log = described_class.new(action: 'hoge')
expect(action_log.action).to be :hoge
end
end
end

View File

@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe Backup, type: :model do
end

47
spec/models/block_spec.rb Normal file
View File

@@ -0,0 +1,47 @@
require 'rails_helper'
RSpec.describe Block, type: :model do
describe 'validations' do
it 'has a valid fabricator' do
block = Fabricate.build(:block)
expect(block).to be_valid
end
it 'is invalid without an account' do
block = Fabricate.build(:block, account: nil)
block.valid?
expect(block).to model_have_error_on_field(:account)
end
it 'is invalid without a target_account' do
block = Fabricate.build(:block, target_account: nil)
block.valid?
expect(block).to model_have_error_on_field(:target_account)
end
end
it 'removes blocking cache after creation' do
account = Fabricate(:account)
target_account = Fabricate(:account)
Rails.cache.write("exclude_account_ids_for:#{account.id}", [])
Rails.cache.write("exclude_account_ids_for:#{target_account.id}", [])
Block.create!(account: account, target_account: target_account)
expect(Rails.cache.exist?("exclude_account_ids_for:#{account.id}")).to eq false
expect(Rails.cache.exist?("exclude_account_ids_for:#{target_account.id}")).to eq false
end
it 'removes blocking cache after destruction' do
account = Fabricate(:account)
target_account = Fabricate(:account)
block = Block.create!(account: account, target_account: target_account)
Rails.cache.write("exclude_account_ids_for:#{account.id}", [target_account.id])
Rails.cache.write("exclude_account_ids_for:#{target_account.id}", [account.id])
block.destroy!
expect(Rails.cache.exist?("exclude_account_ids_for:#{account.id}")).to eq false
expect(Rails.cache.exist?("exclude_account_ids_for:#{target_account.id}")).to eq false
end
end

View File

@@ -0,0 +1,109 @@
# frozen_string_literal: true
require 'rails_helper'
describe AccountFinderConcern do
describe 'local finders' do
before do
@account = Fabricate(:account, username: 'Alice')
end
describe '.find_local' do
it 'returns case-insensitive result' do
expect(Account.find_local('alice')).to eq(@account)
end
it 'returns correctly cased result' do
expect(Account.find_local('Alice')).to eq(@account)
end
it 'returns nil without a match' do
expect(Account.find_local('a_ice')).to be_nil
end
it 'returns nil for regex style username value' do
expect(Account.find_local('al%')).to be_nil
end
it 'returns nil for nil username value' do
expect(Account.find_local(nil)).to be_nil
end
it 'returns nil for blank username value' do
expect(Account.find_local('')).to be_nil
end
end
describe '.find_local!' do
it 'returns matching result' do
expect(Account.find_local!('alice')).to eq(@account)
end
it 'raises on non-matching result' do
expect { Account.find_local!('missing') }.to raise_error(ActiveRecord::RecordNotFound)
end
it 'raises with blank username' do
expect { Account.find_local!('') }.to raise_error(ActiveRecord::RecordNotFound)
end
it 'raises with nil username' do
expect { Account.find_local!(nil) }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
describe 'remote finders' do
before do
@account = Fabricate(:account, username: 'Alice', domain: 'gab.com')
end
describe '.find_remote' do
it 'returns exact match result' do
expect(Account.find_remote('alice', 'gab.com')).to eq(@account)
end
it 'returns case-insensitive result' do
expect(Account.find_remote('ALICE', 'GAB.COM')).to eq(@account)
end
it 'returns nil when username does not match' do
expect(Account.find_remote('a_ice', 'gab.com')).to be_nil
end
it 'returns nil when domain does not match' do
expect(Account.find_remote('alice', 'g_b.com')).to be_nil
end
it 'returns nil for regex style domain value' do
expect(Account.find_remote('alice', 'm%')).to be_nil
end
it 'returns nil for nil username value' do
expect(Account.find_remote(nil, 'domain')).to be_nil
end
it 'returns nil for blank username value' do
expect(Account.find_remote('', 'domain')).to be_nil
end
end
describe '.find_remote!' do
it 'returns matching result' do
expect(Account.find_remote!('alice', 'gab.com')).to eq(@account)
end
it 'raises on non-matching result' do
expect { Account.find_remote!('missing', 'gab.host') }.to raise_error(ActiveRecord::RecordNotFound)
end
it 'raises with blank username' do
expect { Account.find_remote!('', '') }.to raise_error(ActiveRecord::RecordNotFound)
end
it 'raises with nil username' do
expect { Account.find_remote!(nil, nil) }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
end

View File

@@ -0,0 +1,613 @@
require 'rails_helper'
describe AccountInteractions do
let(:account) { Fabricate(:account, username: 'account') }
let(:account_id) { account.id }
let(:account_ids) { [account_id] }
let(:target_account) { Fabricate(:account, username: 'target') }
let(:target_account_id) { target_account.id }
let(:target_account_ids) { [target_account_id] }
describe '.following_map' do
subject { Account.following_map(target_account_ids, account_id) }
context 'account with Follow' do
it 'returns { target_account_id => true }' do
Fabricate(:follow, account: account, target_account: target_account)
is_expected.to eq(target_account_id => { reblogs: true })
end
end
context 'account without Follow' do
it 'returns {}' do
is_expected.to eq({})
end
end
end
describe '.followed_by_map' do
subject { Account.followed_by_map(target_account_ids, account_id) }
context 'account with Follow' do
it 'returns { target_account_id => true }' do
Fabricate(:follow, account: target_account, target_account: account)
is_expected.to eq(target_account_id => true)
end
end
context 'account without Follow' do
it 'returns {}' do
is_expected.to eq({})
end
end
end
describe '.blocking_map' do
subject { Account.blocking_map(target_account_ids, account_id) }
context 'account with Block' do
it 'returns { target_account_id => true }' do
Fabricate(:block, account: account, target_account: target_account)
is_expected.to eq(target_account_id => true)
end
end
context 'account without Block' do
it 'returns {}' do
is_expected.to eq({})
end
end
end
describe '.muting_map' do
subject { Account.muting_map(target_account_ids, account_id) }
context 'account with Mute' do
before do
Fabricate(:mute, target_account: target_account, account: account, hide_notifications: hide)
end
context 'if Mute#hide_notifications?' do
let(:hide) { true }
it 'returns { target_account_id => { notifications: true } }' do
is_expected.to eq(target_account_id => { notifications: true })
end
end
context 'unless Mute#hide_notifications?' do
let(:hide) { false }
it 'returns { target_account_id => { notifications: false } }' do
is_expected.to eq(target_account_id => { notifications: false })
end
end
end
context 'account without Mute' do
it 'returns {}' do
is_expected.to eq({})
end
end
end
describe '#follow!' do
it 'creates and returns Follow' do
expect do
expect(account.follow!(target_account)).to be_kind_of Follow
end.to change { account.following.count }.by 1
end
end
describe '#block' do
it 'creates and returns Block' do
expect do
expect(account.block!(target_account)).to be_kind_of Block
end.to change { account.block_relationships.count }.by 1
end
end
describe '#mute!' do
subject { account.mute!(target_account, notifications: arg_notifications) }
context 'Mute does not exist yet' do
context 'arg :notifications is nil' do
let(:arg_notifications) { nil }
it 'creates Mute, and returns Mute' do
expect do
expect(subject).to be_kind_of Mute
end.to change { account.mute_relationships.count }.by 1
end
end
context 'arg :notifications is false' do
let(:arg_notifications) { false }
it 'creates Mute, and returns Mute' do
expect do
expect(subject).to be_kind_of Mute
end.to change { account.mute_relationships.count }.by 1
end
end
context 'arg :notifications is true' do
let(:arg_notifications) { true }
it 'creates Mute, and returns Mute' do
expect do
expect(subject).to be_kind_of Mute
end.to change { account.mute_relationships.count }.by 1
end
end
end
context 'Mute already exists' do
before do
account.mute_relationships << mute
end
let(:mute) do
Fabricate(:mute,
account: account,
target_account: target_account,
hide_notifications: hide_notifications)
end
context 'mute.hide_notifications is true' do
let(:hide_notifications) { true }
context 'arg :notifications is nil' do
let(:arg_notifications) { nil }
it 'returns Mute without updating mute.hide_notifications' do
expect do
expect(subject).to be_kind_of Mute
end.not_to change { mute.reload.hide_notifications? }.from(true)
end
end
context 'arg :notifications is false' do
let(:arg_notifications) { false }
it 'returns Mute, and updates mute.hide_notifications false' do
expect do
expect(subject).to be_kind_of Mute
end.to change { mute.reload.hide_notifications? }.from(true).to(false)
end
end
context 'arg :notifications is true' do
let(:arg_notifications) { true }
it 'returns Mute without updating mute.hide_notifications' do
expect do
expect(subject).to be_kind_of Mute
end.not_to change { mute.reload.hide_notifications? }.from(true)
end
end
end
context 'mute.hide_notifications is false' do
let(:hide_notifications) { false }
context 'arg :notifications is nil' do
let(:arg_notifications) { nil }
it 'returns Mute, and updates mute.hide_notifications true' do
expect do
expect(subject).to be_kind_of Mute
end.to change { mute.reload.hide_notifications? }.from(false).to(true)
end
end
context 'arg :notifications is false' do
let(:arg_notifications) { false }
it 'returns Mute without updating mute.hide_notifications' do
expect do
expect(subject).to be_kind_of Mute
end.not_to change { mute.reload.hide_notifications? }.from(false)
end
end
context 'arg :notifications is true' do
let(:arg_notifications) { true }
it 'returns Mute, and updates mute.hide_notifications true' do
expect do
expect(subject).to be_kind_of Mute
end.to change { mute.reload.hide_notifications? }.from(false).to(true)
end
end
end
end
end
describe '#mute_conversation!' do
let(:conversation) { Fabricate(:conversation) }
subject { account.mute_conversation!(conversation) }
it 'creates and returns ConversationMute' do
expect do
is_expected.to be_kind_of ConversationMute
end.to change { account.conversation_mutes.count }.by 1
end
end
describe '#block_domain!' do
let(:domain) { 'example.com' }
subject { account.block_domain!(domain) }
it 'creates and returns AccountDomainBlock' do
expect do
is_expected.to be_kind_of AccountDomainBlock
end.to change { account.domain_blocks.count }.by 1
end
end
describe '#unfollow!' do
subject { account.unfollow!(target_account) }
context 'following target_account' do
it 'returns destroyed Follow' do
account.active_relationships.create(target_account: target_account)
is_expected.to be_kind_of Follow
expect(subject).to be_destroyed
end
end
context 'not following target_account' do
it 'returns nil' do
is_expected.to be_nil
end
end
end
describe '#unblock!' do
subject { account.unblock!(target_account) }
context 'blocking target_account' do
it 'returns destroyed Block' do
account.block_relationships.create(target_account: target_account)
is_expected.to be_kind_of Block
expect(subject).to be_destroyed
end
end
context 'not blocking target_account' do
it 'returns nil' do
is_expected.to be_nil
end
end
end
describe '#unmute!' do
subject { account.unmute!(target_account) }
context 'muting target_account' do
it 'returns destroyed Mute' do
account.mute_relationships.create(target_account: target_account)
is_expected.to be_kind_of Mute
expect(subject).to be_destroyed
end
end
context 'not muting target_account' do
it 'returns nil' do
is_expected.to be_nil
end
end
end
describe '#unmute_conversation!' do
let(:conversation) { Fabricate(:conversation) }
subject { account.unmute_conversation!(conversation) }
context 'muting the conversation' do
it 'returns destroyed ConversationMute' do
account.conversation_mutes.create(conversation: conversation)
is_expected.to be_kind_of ConversationMute
expect(subject).to be_destroyed
end
end
context 'not muting the conversation' do
it 'returns nil' do
is_expected.to be nil
end
end
end
describe '#unblock_domain!' do
let(:domain) { 'example.com' }
subject { account.unblock_domain!(domain) }
context 'blocking the domain' do
it 'returns destroyed AccountDomainBlock' do
account_domain_block = Fabricate(:account_domain_block, domain: domain)
account.domain_blocks << account_domain_block
is_expected.to be_kind_of AccountDomainBlock
expect(subject).to be_destroyed
end
end
context 'unblocking the domain' do
it 'returns nil' do
is_expected.to be_nil
end
end
end
describe '#following?' do
subject { account.following?(target_account) }
context 'following target_account' do
it 'returns true' do
account.active_relationships.create(target_account: target_account)
is_expected.to be true
end
end
context 'not following target_account' do
it 'returns false' do
is_expected.to be false
end
end
end
describe '#blocking?' do
subject { account.blocking?(target_account) }
context 'blocking target_account' do
it 'returns true' do
account.block_relationships.create(target_account: target_account)
is_expected.to be true
end
end
context 'not blocking target_account' do
it 'returns false' do
is_expected.to be false
end
end
end
describe '#domain_blocking?' do
let(:domain) { 'example.com' }
subject { account.domain_blocking?(domain) }
context 'blocking the domain' do
it' returns true' do
account_domain_block = Fabricate(:account_domain_block, domain: domain)
account.domain_blocks << account_domain_block
is_expected.to be true
end
end
context 'not blocking the domain' do
it 'returns false' do
is_expected.to be false
end
end
end
describe '#muting?' do
subject { account.muting?(target_account) }
context 'muting target_account' do
it 'returns true' do
mute = Fabricate(:mute, account: account, target_account: target_account)
account.mute_relationships << mute
is_expected.to be true
end
end
context 'not muting target_account' do
it 'returns false' do
is_expected.to be false
end
end
end
describe '#muting_conversation?' do
let(:conversation) { Fabricate(:conversation) }
subject { account.muting_conversation?(conversation) }
context 'muting the conversation' do
it 'returns true' do
account.conversation_mutes.create(conversation: conversation)
is_expected.to be true
end
end
context 'not muting the conversation' do
it 'returns false' do
is_expected.to be false
end
end
end
describe '#muting_notifications?' do
before do
mute = Fabricate(:mute, target_account: target_account, account: account, hide_notifications: hide)
account.mute_relationships << mute
end
subject { account.muting_notifications?(target_account) }
context 'muting notifications of target_account' do
let(:hide) { true }
it 'returns true' do
is_expected.to be true
end
end
context 'not muting notifications of target_account' do
let(:hide) { false }
it 'returns false' do
is_expected.to be false
end
end
end
describe '#requested?' do
subject { account.requested?(target_account) }
context 'requested by target_account' do
it 'returns true' do
Fabricate(:follow_request, account: account, target_account: target_account)
is_expected.to be true
end
end
context 'not requested by target_account' do
it 'returns false' do
is_expected.to be false
end
end
end
describe '#favourited?' do
let(:status) { Fabricate(:status, account: account, favourites: favourites) }
subject { account.favourited?(status) }
context 'favorited' do
let(:favourites) { [Fabricate(:favourite, account: account)] }
it 'returns true' do
is_expected.to be true
end
end
context 'not favorited' do
let(:favourites) { [] }
it 'returns false' do
is_expected.to be false
end
end
end
describe '#reblogged?' do
let(:status) { Fabricate(:status, account: account, reblogs: reblogs) }
subject { account.reblogged?(status) }
context 'reblogged' do
let(:reblogs) { [Fabricate(:status, account: account)] }
it 'returns true' do
is_expected.to be true
end
end
context 'not reblogged' do
let(:reblogs) { [] }
it 'returns false' do
is_expected.to be false
end
end
end
describe '#pinned?' do
let(:status) { Fabricate(:status, account: account) }
subject { account.pinned?(status) }
context 'pinned' do
it 'returns true' do
Fabricate(:status_pin, account: account, status: status)
is_expected.to be true
end
end
context 'not pinned' do
it 'returns false' do
is_expected.to be false
end
end
end
describe 'muting an account' do
let(:me) { Fabricate(:account, username: 'Me') }
let(:you) { Fabricate(:account, username: 'You') }
context 'with the notifications option unspecified' do
before do
me.mute!(you)
end
it 'defaults to muting notifications' do
expect(me.muting_notifications?(you)).to be true
end
end
context 'with the notifications option set to false' do
before do
me.mute!(you, notifications: false)
end
it 'does not mute notifications' do
expect(me.muting_notifications?(you)).to be false
end
end
context 'with the notifications option set to true' do
before do
me.mute!(you, notifications: true)
end
it 'does mute notifications' do
expect(me.muting_notifications?(you)).to be true
end
end
end
describe 'ignoring reblogs from an account' do
before do
@me = Fabricate(:account, username: 'Me')
@you = Fabricate(:account, username: 'You')
end
context 'with the reblogs option unspecified' do
before do
@me.follow!(@you)
end
it 'defaults to showing reblogs' do
expect(@me.muting_reblogs?(@you)).to be(false)
end
end
context 'with the reblogs option set to false' do
before do
@me.follow!(@you, reblogs: false)
end
it 'does mute reblogs' do
expect(@me.muting_reblogs?(@you)).to be(true)
end
end
context 'with the reblogs option set to true' do
before do
@me.follow!(@you, reblogs: true)
end
it 'does not mute reblogs' do
expect(@me.muting_reblogs?(@you)).to be(false)
end
end
end
end

View File

@@ -0,0 +1,219 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Remotable do
class Foo
def initialize
@attrs = {}
end
def [](arg)
@attrs[arg]
end
def []=(arg1, arg2)
@attrs[arg1] = arg2
end
def hoge=(arg); end
def hoge_file_name=(arg); end
def has_attribute?(arg); end
def self.attachment_definitions
{ hoge: nil }
end
end
context 'Remotable module is included' do
before do
class Foo
include Remotable
remotable_attachment :hoge, 1.kilobyte
end
end
let(:attribute_name) { "#{hoge}_remote_url".to_sym }
let(:code) { 200 }
let(:file) { 'filename="foo.txt"' }
let(:foo) { Foo.new }
let(:headers) { { 'content-disposition' => file } }
let(:hoge) { :hoge }
let(:url) { 'https://google.com' }
let(:request) do
stub_request(:get, url)
.to_return(status: code, headers: headers)
end
it 'defines a method #hoge_remote_url=' do
expect(foo).to respond_to(:hoge_remote_url=)
end
it 'defines a method #reset_hoge!' do
expect(foo).to respond_to(:reset_hoge!)
end
describe '#hoge_remote_url' do
before do
request
end
it 'always returns arg' do
[nil, '', [], {}].each do |arg|
expect(foo.hoge_remote_url = arg).to be arg
end
end
context 'Addressable::URI::InvalidURIError raised' do
it 'makes no request' do
allow(Addressable::URI).to receive_message_chain(:parse, :normalize)
.with(url).with(no_args).and_raise(Addressable::URI::InvalidURIError)
foo.hoge_remote_url = url
expect(request).not_to have_been_requested
end
end
context 'scheme is neither http nor https' do
let(:url) { 'ftp://google.com' }
it 'makes no request' do
foo.hoge_remote_url = url
expect(request).not_to have_been_requested
end
end
context 'parsed_url.host is empty' do
it 'makes no request' do
parsed_url = double(scheme: 'https', host: double(blank?: true))
allow(Addressable::URI).to receive_message_chain(:parse, :normalize)
.with(url).with(no_args).and_return(parsed_url)
foo.hoge_remote_url = url
expect(request).not_to have_been_requested
end
end
context 'parsed_url.host is nil' do
it 'makes no request' do
parsed_url = Addressable::URI.parse('https:https://example.com/path/file.png')
allow(Addressable::URI).to receive_message_chain(:parse, :normalize)
.with(url).with(no_args).and_return(parsed_url)
foo.hoge_remote_url = url
expect(request).not_to have_been_requested
end
end
context 'foo[attribute_name] == url' do
it 'makes no request' do
allow(foo).to receive(:[]).with(attribute_name).and_return(url)
foo.hoge_remote_url = url
expect(request).not_to have_been_requested
end
end
context "scheme is https, parsed_url.host isn't empty, and foo[attribute_name] != url" do
it 'makes a request' do
foo.hoge_remote_url = url
expect(request).to have_been_requested
end
context 'response.code != 200' do
let(:code) { 500 }
it 'calls not send' do
expect(foo).not_to receive(:send).with("#{hoge}=", any_args)
expect(foo).not_to receive(:send).with("#{hoge}_file_name=", any_args)
foo.hoge_remote_url = url
end
end
context 'response.code == 200' do
let(:code) { 200 }
context 'response contains headers["content-disposition"]' do
let(:file) { 'filename="foo.txt"' }
let(:headers) { { 'content-disposition' => file } }
it 'calls send' do
string_io = StringIO.new('')
extname = '.txt'
basename = '0123456789abcdef'
allow(SecureRandom).to receive(:hex).and_return(basename)
allow(StringIO).to receive(:new).with(anything).and_return(string_io)
expect(foo).to receive(:send).with("#{hoge}=", string_io)
expect(foo).to receive(:send).with("#{hoge}_file_name=", basename + extname)
foo.hoge_remote_url = url
end
end
context 'if has_attribute?' do
it 'calls foo[attribute_name] = url' do
allow(foo).to receive(:has_attribute?).with(attribute_name).and_return(true)
expect(foo).to receive('[]=').with(attribute_name, url)
foo.hoge_remote_url = url
end
end
context 'unless has_attribute?' do
it 'calls not foo[attribute_name] = url' do
allow(foo).to receive(:has_attribute?)
.with(attribute_name).and_return(false)
expect(foo).not_to receive('[]=').with(attribute_name, url)
foo.hoge_remote_url = url
end
end
end
context 'an error raised during the request' do
let(:request) { stub_request(:get, url).to_raise(error_class) }
error_classes = [
HTTP::TimeoutError,
HTTP::ConnectionError,
OpenSSL::SSL::SSLError,
Paperclip::Errors::NotIdentifiedByImageMagickError,
Addressable::URI::InvalidURIError,
]
error_classes.each do |error_class|
let(:error_class) { error_class }
it 'calls Rails.logger.debug' do
expect(Rails.logger).to receive(:debug).with(/^Error fetching remote #{hoge}: /)
foo.hoge_remote_url = url
end
end
end
end
end
describe '#reset_hoge!' do
context 'if url.blank?' do
it 'returns nil, without clearing foo[attribute_name] and calling #hoge_remote_url=' do
url = nil
expect(foo).not_to receive(:send).with(:hoge_remote_url=, url)
foo[attribute_name] = url
expect(foo.reset_hoge!).to be_nil
expect(foo[attribute_name]).to be_nil
end
end
context 'unless url.blank?' do
it 'clears foo[attribute_name] and calls #hoge_remote_url=' do
foo[attribute_name] = url
expect(foo).to receive(:send).with(:hoge_remote_url=, url)
foo.reset_hoge!
expect(foo[attribute_name]).to be ''
end
end
end
end
end

View File

@@ -0,0 +1,132 @@
# frozen_string_literal: true
require 'rails_helper'
describe StatusThreadingConcern do
describe '#ancestors' do
let!(:alice) { Fabricate(:account, username: 'alice') }
let!(:bob) { Fabricate(:account, username: 'bob', domain: 'example.com') }
let!(:jeff) { Fabricate(:account, username: 'jeff') }
let!(:status) { Fabricate(:status, account: alice) }
let!(:reply1) { Fabricate(:status, thread: status, account: jeff) }
let!(:reply2) { Fabricate(:status, thread: reply1, account: bob) }
let!(:reply3) { Fabricate(:status, thread: reply2, account: alice) }
let!(:viewer) { Fabricate(:account, username: 'viewer') }
it 'returns conversation history' do
expect(reply3.ancestors(4)).to include(status, reply1, reply2)
end
it 'does not return conversation history user is not allowed to see' do
reply1.update(visibility: :private)
status.update(visibility: :direct)
expect(reply3.ancestors(4, viewer)).to_not include(reply1, status)
end
it 'does not return conversation history from blocked users' do
viewer.block!(jeff)
expect(reply3.ancestors(4, viewer)).to_not include(reply1)
end
it 'does not return conversation history from muted users' do
viewer.mute!(jeff)
expect(reply3.ancestors(4, viewer)).to_not include(reply1)
end
it 'does not return conversation history from silenced and not followed users' do
jeff.silence!
expect(reply3.ancestors(4, viewer)).to_not include(reply1)
end
it 'does not return conversation history from blocked domains' do
viewer.block_domain!('example.com')
expect(reply3.ancestors(4, viewer)).to_not include(reply2)
end
it 'ignores deleted records' do
first_status = Fabricate(:status, account: bob)
second_status = Fabricate(:status, thread: first_status, account: alice)
# Create cache and delete cached record
second_status.ancestors(4)
first_status.destroy
expect(second_status.ancestors(4)).to eq([])
end
it 'can return more records than previously requested' do
first_status = Fabricate(:status, account: bob)
second_status = Fabricate(:status, thread: first_status, account: alice)
third_status = Fabricate(:status, thread: second_status, account: alice)
# Create cache
second_status.ancestors(1)
expect(third_status.ancestors(2)).to eq([first_status, second_status])
end
it 'can return fewer records than previously requested' do
first_status = Fabricate(:status, account: bob)
second_status = Fabricate(:status, thread: first_status, account: alice)
third_status = Fabricate(:status, thread: second_status, account: alice)
# Create cache
second_status.ancestors(2)
expect(third_status.ancestors(1)).to eq([second_status])
end
end
describe '#descendants' do
let!(:alice) { Fabricate(:account, username: 'alice') }
let!(:bob) { Fabricate(:account, username: 'bob', domain: 'example.com') }
let!(:jeff) { Fabricate(:account, username: 'jeff') }
let!(:status) { Fabricate(:status, account: alice) }
let!(:reply1) { Fabricate(:status, thread: status, account: alice) }
let!(:reply2) { Fabricate(:status, thread: status, account: bob) }
let!(:reply3) { Fabricate(:status, thread: reply1, account: jeff) }
let!(:viewer) { Fabricate(:account, username: 'viewer') }
it 'returns replies' do
expect(status.descendants(4)).to include(reply1, reply2, reply3)
end
it 'does not return replies user is not allowed to see' do
reply1.update(visibility: :private)
reply3.update(visibility: :direct)
expect(status.descendants(4, viewer)).to_not include(reply1, reply3)
end
it 'does not return replies from blocked users' do
viewer.block!(jeff)
expect(status.descendants(4, viewer)).to_not include(reply3)
end
it 'does not return replies from muted users' do
viewer.mute!(jeff)
expect(status.descendants(4, viewer)).to_not include(reply3)
end
it 'does not return replies from silenced and not followed users' do
jeff.silence!
expect(status.descendants(4, viewer)).to_not include(reply3)
end
it 'does not return replies from blocked domains' do
viewer.block_domain!('example.com')
expect(status.descendants(4, viewer)).to_not include(reply2)
end
it 'promotes self-replies to the top while leaving the rest in order' do
a = Fabricate(:status, account: alice)
d = Fabricate(:status, account: jeff, thread: a)
e = Fabricate(:status, account: bob, thread: d)
c = Fabricate(:status, account: alice, thread: a)
f = Fabricate(:status, account: bob, thread: c)
expect(a.descendants(20)).to eq [c, d, e, f]
end
end
end

View File

@@ -0,0 +1,63 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Streamable do
class Parent
def title; end
def target; end
def thread; end
def self.has_one(*); end
def self.after_create; end
end
class Child < Parent
include Streamable
end
child = Child.new
describe '#title' do
it 'calls Parent#title' do
expect_any_instance_of(Parent).to receive(:title)
child.title
end
end
describe '#content' do
it 'calls #title' do
expect_any_instance_of(Parent).to receive(:title)
child.content
end
end
describe '#target' do
it 'calls Parent#target' do
expect_any_instance_of(Parent).to receive(:target)
child.target
end
end
describe '#object_type' do
it 'returns :activity' do
expect(child.object_type).to eq :activity
end
end
describe '#thread' do
it 'calls Parent#thread' do
expect_any_instance_of(Parent).to receive(:thread)
child.thread
end
end
describe '#hidden?' do
it 'returns false' do
expect(child.hidden?).to be false
end
end
end

View File

@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe ConversationMute, type: :model do
end

View File

@@ -0,0 +1,13 @@
require 'rails_helper'
RSpec.describe Conversation, type: :model do
describe '#local?' do
it 'returns true when URI is nil' do
expect(Fabricate(:conversation).local?).to be true
end
it 'returns false when URI is not nil' do
expect(Fabricate(:conversation, uri: 'abc').local?).to be false
end
end
end

View File

@@ -0,0 +1,70 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe CustomEmojiFilter do
describe '#results' do
let!(:custom_emoji_0) { Fabricate(:custom_emoji, domain: 'a') }
let!(:custom_emoji_1) { Fabricate(:custom_emoji, domain: 'b') }
let!(:custom_emoji_2) { Fabricate(:custom_emoji, domain: nil, shortcode: 'hoge') }
subject { described_class.new(params).results }
context 'params have values' do
context 'local' do
let(:params) { { local: true } }
it 'returns ActiveRecord::Relation' do
expect(subject).to be_kind_of(ActiveRecord::Relation)
expect(subject).to match_array([custom_emoji_2])
end
end
context 'remote' do
let(:params) { { remote: true } }
it 'returns ActiveRecord::Relation' do
expect(subject).to be_kind_of(ActiveRecord::Relation)
expect(subject).to match_array([custom_emoji_0, custom_emoji_1])
end
end
context 'by_domain' do
let(:params) { { by_domain: 'a' } }
it 'returns ActiveRecord::Relation' do
expect(subject).to be_kind_of(ActiveRecord::Relation)
expect(subject).to match_array([custom_emoji_0])
end
end
context 'shortcode' do
let(:params) { { shortcode: 'hoge' } }
it 'returns ActiveRecord::Relation' do
expect(subject).to be_kind_of(ActiveRecord::Relation)
expect(subject).to match_array([custom_emoji_2])
end
end
context 'else' do
let(:params) { { else: 'else' } }
it 'raises RuntimeError' do
expect do
subject
end.to raise_error(RuntimeError, /Unknown filter: else/)
end
end
end
context 'params without value' do
let(:params) { { hoge: nil } }
it 'returns ActiveRecord::Relation' do
expect(subject).to be_kind_of(ActiveRecord::Relation)
expect(subject).to match_array([custom_emoji_0, custom_emoji_1, custom_emoji_2])
end
end
end
end

View File

@@ -0,0 +1,87 @@
require 'rails_helper'
RSpec.describe CustomEmoji, type: :model do
describe '#search' do
let(:custom_emoji) { Fabricate(:custom_emoji, shortcode: shortcode) }
subject { described_class.search(search_term) }
context 'shortcode is exact' do
let(:shortcode) { 'blobpats' }
let(:search_term) { 'blobpats' }
it 'finds emoji' do
is_expected.to include(custom_emoji)
end
end
context 'shortcode is partial' do
let(:shortcode) { 'blobpats' }
let(:search_term) { 'blob' }
it 'finds emoji' do
is_expected.to include(custom_emoji)
end
end
end
describe '#local?' do
let(:custom_emoji) { Fabricate(:custom_emoji, domain: domain) }
subject { custom_emoji.local? }
context 'domain is nil' do
let(:domain) { nil }
it 'returns true' do
is_expected.to be true
end
end
context 'domain is present' do
let(:domain) { 'example.com' }
it 'returns false' do
is_expected.to be false
end
end
end
describe '#object_type' do
it 'returns :emoji' do
custom_emoji = Fabricate(:custom_emoji)
expect(custom_emoji.object_type).to be :emoji
end
end
describe '.from_text' do
let!(:emojo) { Fabricate(:custom_emoji) }
subject { described_class.from_text(text, nil) }
context 'with plain text' do
let(:text) { 'Hello :coolcat:' }
it 'returns records used via shortcodes in text' do
is_expected.to include(emojo)
end
end
context 'with html' do
let(:text) { '<p>Hello :coolcat:</p>' }
it 'returns records used via shortcodes in text' do
is_expected.to include(emojo)
end
end
end
describe 'pre_validation' do
let(:custom_emoji) { Fabricate(:custom_emoji, domain: 'wWw.GaB.cOm') }
it 'should downcase' do
custom_emoji.valid?
expect(custom_emoji.domain).to eq('www.gab.com')
end
end
end

View File

@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe CustomFilter, type: :model do
end

View File

@@ -0,0 +1,70 @@
require 'rails_helper'
RSpec.describe DomainBlock, type: :model do
describe 'validations' do
it 'has a valid fabricator' do
domain_block = Fabricate.build(:domain_block)
expect(domain_block).to be_valid
end
it 'is invalid without a domain' do
domain_block = Fabricate.build(:domain_block, domain: nil)
domain_block.valid?
expect(domain_block).to model_have_error_on_field(:domain)
end
it 'is invalid if the same normalized domain already exists' do
domain_block_1 = Fabricate(:domain_block, domain: 'にゃん')
domain_block_2 = Fabricate.build(:domain_block, domain: 'xn--r9j5b5b')
domain_block_2.valid?
expect(domain_block_2).to model_have_error_on_field(:domain)
end
end
describe 'blocked?' do
it 'returns true if the domain is suspended' do
Fabricate(:domain_block, domain: 'domain', severity: :suspend)
expect(DomainBlock.blocked?('domain')).to eq true
end
it 'returns false even if the domain is silenced' do
Fabricate(:domain_block, domain: 'domain', severity: :silence)
expect(DomainBlock.blocked?('domain')).to eq false
end
it 'returns false if the domain is not suspended nor silenced' do
expect(DomainBlock.blocked?('domain')).to eq false
end
end
describe 'stricter_than?' do
it 'returns true if the new block has suspend severity while the old has lower severity' do
suspend = DomainBlock.new(domain: 'domain', severity: :suspend)
silence = DomainBlock.new(domain: 'domain', severity: :silence)
noop = DomainBlock.new(domain: 'domain', severity: :noop)
expect(suspend.stricter_than?(silence)).to be true
expect(suspend.stricter_than?(noop)).to be true
end
it 'returns false if the new block has lower severity than the old one' do
suspend = DomainBlock.new(domain: 'domain', severity: :suspend)
silence = DomainBlock.new(domain: 'domain', severity: :silence)
noop = DomainBlock.new(domain: 'domain', severity: :noop)
expect(silence.stricter_than?(suspend)).to be false
expect(noop.stricter_than?(suspend)).to be false
expect(noop.stricter_than?(silence)).to be false
end
it 'returns false if the new block does is less strict regarding reports' do
older = DomainBlock.new(domain: 'domain', severity: :silence, reject_reports: true)
newer = DomainBlock.new(domain: 'domain', severity: :silence, reject_reports: false)
expect(newer.stricter_than?(older)).to be false
end
it 'returns false if the new block does is less strict regarding media' do
older = DomainBlock.new(domain: 'domain', severity: :silence, reject_media: true)
newer = DomainBlock.new(domain: 'domain', severity: :silence, reject_media: false)
expect(newer.stricter_than?(older)).to be false
end
end
end

View File

@@ -0,0 +1,22 @@
require 'rails_helper'
RSpec.describe EmailDomainBlock, type: :model do
describe 'validations' do
it 'has a valid fabricator' do
email_domain_block = Fabricate.build(:email_domain_block)
expect(email_domain_block).to be_valid
end
end
describe 'block?' do
it 'returns true if the domain is registed' do
Fabricate(:email_domain_block, domain: 'example.com')
expect(EmailDomainBlock.block?('nyarn@example.com')).to eq true
end
it 'returns true if the domain is not registed' do
Fabricate(:email_domain_block, domain: 'example.com')
expect(EmailDomainBlock.block?('nyarn@example.net')).to eq false
end
end
end

View File

@@ -0,0 +1,66 @@
require 'rails_helper'
describe Export do
let(:account) { Fabricate(:account) }
let(:target_accounts) do
[ {}, { username: 'one', domain: 'local.host' } ].map(&method(:Fabricate).curry(2).call(:account))
end
describe 'to_csv' do
it 'returns a csv of the blocked accounts' do
target_accounts.each(&account.method(:block!))
export = Export.new(account).to_blocked_accounts_csv
results = export.strip.split
expect(results.size).to eq 2
expect(results.first).to eq 'one@local.host'
end
it 'returns a csv of the muted accounts' do
target_accounts.each(&account.method(:mute!))
export = Export.new(account).to_muted_accounts_csv
results = export.strip.split("\n")
expect(results.size).to eq 3
expect(results.first).to eq 'Account address,Hide notifications'
expect(results.second).to eq 'one@local.host,true'
end
it 'returns a csv of the following accounts' do
target_accounts.each(&account.method(:follow!))
export = Export.new(account).to_following_accounts_csv
results = export.strip.split("\n")
expect(results.size).to eq 3
expect(results.first).to eq 'Account address,Show reposts'
expect(results.second).to eq 'one@local.host,true'
end
end
describe 'total_storage' do
it 'returns the total size of the media attachments' do
media_attachment = Fabricate(:media_attachment, account: account)
expect(Export.new(account).total_storage).to eq media_attachment.file_file_size || 0
end
end
describe 'total_follows' do
it 'returns the total number of the followed accounts' do
target_accounts.each(&account.method(:follow!))
expect(Export.new(account.reload).total_follows).to eq 2
end
it 'returns the total number of the blocked accounts' do
target_accounts.each(&account.method(:block!))
expect(Export.new(account.reload).total_blocks).to eq 2
end
it 'returns the total number of the muted accounts' do
target_accounts.each(&account.method(:mute!))
expect(Export.new(account.reload).total_mutes).to eq 2
end
end
end

View File

@@ -0,0 +1,29 @@
require 'rails_helper'
RSpec.describe Favourite, type: :model do
let(:account) { Fabricate(:account) }
context 'when status is a reblog' do
let(:reblog) { Fabricate(:status, reblog: nil) }
let(:status) { Fabricate(:status, reblog: reblog) }
it 'invalidates if the reblogged status is already a favourite' do
Favourite.create!(account: account, status: reblog)
expect(Favourite.new(account: account, status: status).valid?).to eq false
end
it 'replaces status with the reblogged one if it is a reblog' do
favourite = Favourite.create!(account: account, status: status)
expect(favourite.status).to eq reblog
end
end
context 'when status is not a reblog' do
let(:status) { Fabricate(:status, reblog: nil) }
it 'saves with the specified status' do
favourite = Favourite.create!(account: account, status: status)
expect(favourite.status).to eq status
end
end
end

View File

@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe FeaturedTag, type: :model do
end

View File

@@ -0,0 +1,30 @@
require 'rails_helper'
RSpec.describe FollowRequest, type: :model do
describe '#authorize!' do
let(:follow_request) { Fabricate(:follow_request, account: account, target_account: target_account) }
let(:account) { Fabricate(:account) }
let(:target_account) { Fabricate(:account) }
it 'calls Account#follow!, MergeWorker.perform_async, and #destroy!' do
expect(account).to receive(:follow!).with(target_account, reblogs: true, uri: follow_request.uri)
expect(MergeWorker).to receive(:perform_async).with(target_account.id, account.id)
expect(follow_request).to receive(:destroy!)
follow_request.authorize!
end
it 'correctly passes show_reblogs when true' do
follow_request = Fabricate.create(:follow_request, show_reblogs: true)
follow_request.authorize!
target = follow_request.target_account
expect(follow_request.account.muting_reblogs?(target)).to be false
end
it 'correctly passes show_reblogs when false' do
follow_request = Fabricate.create(:follow_request, show_reblogs: false)
follow_request.authorize!
target = follow_request.target_account
expect(follow_request.account.muting_reblogs?(target)).to be true
end
end
end

View File

@@ -0,0 +1,70 @@
require 'rails_helper'
RSpec.describe Follow, type: :model do
let(:alice) { Fabricate(:account, username: 'alice') }
let(:bob) { Fabricate(:account, username: 'bob') }
describe 'validations' do
subject { Follow.new(account: alice, target_account: bob) }
it 'has a valid fabricator' do
follow = Fabricate.build(:follow)
expect(follow).to be_valid
end
it 'is invalid without an account' do
follow = Fabricate.build(:follow, account: nil)
follow.valid?
expect(follow).to model_have_error_on_field(:account)
end
it 'is invalid without a target_account' do
follow = Fabricate.build(:follow, target_account: nil)
follow.valid?
expect(follow).to model_have_error_on_field(:target_account)
end
it 'is invalid if account already follows too many people' do
alice.update(following_count: FollowLimitValidator::LIMIT)
expect(subject).to_not be_valid
expect(subject).to model_have_error_on_field(:base)
end
it 'is valid if account is only on the brink of following too many people' do
alice.update(following_count: FollowLimitValidator::LIMIT - 1)
expect(subject).to be_valid
expect(subject).to_not model_have_error_on_field(:base)
end
end
describe 'recent' do
it 'sorts so that more recent follows comes earlier' do
follow0 = Follow.create!(account: alice, target_account: bob)
follow1 = Follow.create!(account: bob, target_account: alice)
a = Follow.recent.to_a
expect(a.size).to eq 2
expect(a[0]).to eq follow1
expect(a[1]).to eq follow0
end
end
describe 'revoke_request!' do
let(:follow) { Fabricate(:follow, account: account, target_account: target_account) }
let(:account) { Fabricate(:account) }
let(:target_account) { Fabricate(:account) }
it 'revokes the follow relation' do
follow.revoke_request!
expect(account.following?(target_account)).to be false
end
it 'creates a follow request' do
follow.revoke_request!
expect(account.requested?(target_account)).to be true
end
end
end

View File

@@ -0,0 +1,52 @@
require 'rails_helper'
describe Form::StatusBatch do
let(:form) { Form::StatusBatch.new(action: action, status_ids: status_ids) }
let(:status) { Fabricate(:status) }
describe 'with nsfw action' do
let(:status_ids) { [status.id, nonsensitive_status.id, sensitive_status.id] }
let(:nonsensitive_status) { Fabricate(:status, sensitive: false) }
let(:sensitive_status) { Fabricate(:status, sensitive: true) }
let!(:shown_media_attachment) { Fabricate(:media_attachment, status: nonsensitive_status) }
let!(:hidden_media_attachment) { Fabricate(:media_attachment, status: sensitive_status) }
context 'nsfw_on' do
let(:action) { 'nsfw_on' }
it { expect(form.save).to be true }
it { expect { form.save }.to change { nonsensitive_status.reload.sensitive }.from(false).to(true) }
it { expect { form.save }.not_to change { sensitive_status.reload.sensitive } }
it { expect { form.save }.not_to change { status.reload.sensitive } }
end
context 'nsfw_off' do
let(:action) { 'nsfw_off' }
it { expect(form.save).to be true }
it { expect { form.save }.to change { sensitive_status.reload.sensitive }.from(true).to(false) }
it { expect { form.save }.not_to change { nonsensitive_status.reload.sensitive } }
it { expect { form.save }.not_to change { status.reload.sensitive } }
end
end
describe 'with delete action' do
let(:status_ids) { [status.id] }
let(:action) { 'delete' }
let!(:another_status) { Fabricate(:status) }
before do
allow(RemovalWorker).to receive(:perform_async)
end
it 'call RemovalWorker' do
form.save
expect(RemovalWorker).to have_received(:perform_async).with(status.id)
end
it 'do not call RemovalWorker' do
form.save
expect(RemovalWorker).not_to have_received(:perform_async).with(another_status.id)
end
end
end

View File

@@ -0,0 +1,45 @@
require 'rails_helper'
RSpec.describe HomeFeed, type: :model do
let(:account) { Fabricate(:account) }
subject { described_class.new(account) }
describe '#get' do
before do
Fabricate(:status, account: account, id: 1)
Fabricate(:status, account: account, id: 2)
Fabricate(:status, account: account, id: 3)
Fabricate(:status, account: account, id: 10)
end
context 'when feed is generated' do
before do
Redis.current.zadd(
FeedManager.instance.key(:home, account.id),
[[4, 4], [3, 3], [2, 2], [1, 1]]
)
end
it 'gets statuses with ids in the range from redis' do
results = subject.get(3)
expect(results.map(&:id)).to eq [3, 2]
expect(results.first.attributes.keys).to eq %w(id updated_at)
end
end
context 'when feed is being generated' do
before do
Redis.current.set("account:#{account.id}:regeneration", true)
end
it 'gets statuses with ids in the range from database' do
results = subject.get(3)
expect(results.map(&:id)).to eq [10, 3, 2]
expect(results.first.attributes.keys).to include('id', 'updated_at')
end
end
end
end

View File

@@ -0,0 +1,16 @@
require 'rails_helper'
RSpec.describe Identity, type: :model do
describe '.find_for_oauth' do
let(:auth) { Fabricate(:identity, user: Fabricate(:user)) }
it 'calls .find_or_create_by' do
expect(described_class).to receive(:find_or_create_by).with(uid: auth.uid, provider: auth.provider)
described_class.find_for_oauth(auth)
end
it 'returns an instance of Identity' do
expect(described_class.find_for_oauth(auth)).to be_instance_of Identity
end
end
end

View File

@@ -0,0 +1,24 @@
require 'rails_helper'
RSpec.describe Import, type: :model do
let (:account) { Fabricate(:account) }
let (:type) { 'following' }
let (:data) { attachment_fixture('imports.txt') }
describe 'validations' do
it 'has a valid parameters' do
import = Import.create(account: account, type: type, data: data)
expect(import).to be_valid
end
it 'is invalid without an type' do
import = Import.create(account: account, data: data)
expect(import).to model_have_error_on_field(:type)
end
it 'is invalid without a data' do
import = Import.create(account: account, type: type)
expect(import).to model_have_error_on_field(:data)
end
end
end

View File

@@ -0,0 +1,30 @@
require 'rails_helper'
RSpec.describe Invite, type: :model do
describe '#valid_for_use?' do
it 'returns true when there are no limitations' do
invite = Invite.new(max_uses: nil, expires_at: nil)
expect(invite.valid_for_use?).to be true
end
it 'returns true when not expired' do
invite = Invite.new(max_uses: nil, expires_at: 1.hour.from_now)
expect(invite.valid_for_use?).to be true
end
it 'returns false when expired' do
invite = Invite.new(max_uses: nil, expires_at: 1.hour.ago)
expect(invite.valid_for_use?).to be false
end
it 'returns true when uses still available' do
invite = Invite.new(max_uses: 250, uses: 249, expires_at: nil)
expect(invite.valid_for_use?).to be true
end
it 'returns false when maximum uses reached' do
invite = Invite.new(max_uses: 250, uses: 250, expires_at: nil)
expect(invite.valid_for_use?).to be false
end
end
end

View File

@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe ListAccount, type: :model do
end

4
spec/models/list_spec.rb Normal file
View File

@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe List, type: :model do
end

View File

@@ -0,0 +1,145 @@
require 'rails_helper'
RSpec.describe MediaAttachment, type: :model do
describe 'local?' do
let(:media_attachment) { Fabricate(:media_attachment, remote_url: remote_url) }
subject { media_attachment.local? }
context 'remote_url is blank' do
let(:remote_url) { '' }
it 'returns true' do
is_expected.to be true
end
end
context 'remote_url is present' do
let(:remote_url) { 'remote_url' }
it 'returns false' do
is_expected.to be false
end
end
end
describe 'needs_redownload?' do
let(:media_attachment) { Fabricate(:media_attachment, remote_url: remote_url, file: file) }
subject { media_attachment.needs_redownload? }
context 'file is blank' do
let(:file) { nil }
context 'remote_url is blank' do
let(:remote_url) { '' }
it 'returns false' do
is_expected.to be false
end
end
context 'remote_url is present' do
let(:remote_url) { 'remote_url' }
it 'returns true' do
is_expected.to be true
end
end
end
context 'file is present' do
let(:file) { attachment_fixture('avatar.gif') }
context 'remote_url is blank' do
let(:remote_url) { '' }
it 'returns false' do
is_expected.to be false
end
end
context 'remote_url is present' do
let(:remote_url) { 'remote_url' }
it 'returns true' do
is_expected.to be false
end
end
end
end
describe '#to_param' do
let(:media_attachment) { Fabricate(:media_attachment) }
let(:shortcode) { media_attachment.shortcode }
it 'returns shortcode' do
expect(media_attachment.to_param).to eq shortcode
end
end
describe 'animated gif conversion' do
let(:media) { MediaAttachment.create(account: Fabricate(:account), file: attachment_fixture('avatar.gif')) }
it 'sets type to gifv' do
expect(media.type).to eq 'gifv'
end
it 'converts original file to mp4' do
expect(media.file_content_type).to eq 'video/mp4'
end
it 'sets meta' do
expect(media.file.meta["original"]["width"]).to eq 128
expect(media.file.meta["original"]["height"]).to eq 128
end
end
describe 'non-animated gif non-conversion' do
fixtures = [
{ filename: 'attachment.gif', width: 600, height: 400, aspect: 1.5 },
{ filename: 'mini-static.gif', width: 32, height: 32, aspect: 1.0 },
]
fixtures.each do |fixture|
context fixture[:filename] do
let(:media) { MediaAttachment.create(account: Fabricate(:account), file: attachment_fixture(fixture[:filename])) }
it 'sets type to image' do
expect(media.type).to eq 'image'
end
it 'leaves original file as-is' do
expect(media.file_content_type).to eq 'image/gif'
end
it 'sets meta' do
expect(media.file.meta["original"]["width"]).to eq fixture[:width]
expect(media.file.meta["original"]["height"]).to eq fixture[:height]
expect(media.file.meta["original"]["aspect"]).to eq fixture[:aspect]
end
end
end
end
describe 'jpeg' do
let(:media) { MediaAttachment.create(account: Fabricate(:account), file: attachment_fixture('attachment.jpg')) }
it 'sets meta for different style' do
expect(media.file.meta["original"]["width"]).to eq 600
expect(media.file.meta["original"]["height"]).to eq 400
expect(media.file.meta["original"]["aspect"]).to eq 1.5
expect(media.file.meta["small"]["width"]).to eq 490
expect(media.file.meta["small"]["height"]).to eq 327
expect(media.file.meta["small"]["aspect"]).to eq 490.0 / 327
end
end
describe 'descriptions for remote attachments' do
it 'are cut off at 140 characters' do
media = Fabricate(:media_attachment, description: 'foo' * 1000, remote_url: 'http://example.com/blah.jpg')
expect(media.description.size).to be <= 420
end
end
end

View File

@@ -0,0 +1,22 @@
require 'rails_helper'
RSpec.describe Mention, type: :model do
describe 'validations' do
it 'has a valid fabricator' do
mention = Fabricate.build(:mention)
expect(mention).to be_valid
end
it 'is invalid without an account' do
mention = Fabricate.build(:mention, account: nil)
mention.valid?
expect(mention).to model_have_error_on_field(:account)
end
it 'is invalid without a status' do
mention = Fabricate.build(:mention, status: nil)
mention.valid?
expect(mention).to model_have_error_on_field(:status)
end
end
end

4
spec/models/mute_spec.rb Normal file
View File

@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe Mute, type: :model do
end

View File

@@ -0,0 +1,129 @@
require 'rails_helper'
RSpec.describe Notification, type: :model do
describe '#target_status' do
let(:notification) { Fabricate(:notification, activity: activity) }
let(:status) { Fabricate(:status) }
let(:reblog) { Fabricate(:status, reblog: status) }
let(:favourite) { Fabricate(:favourite, status: status) }
let(:mention) { Fabricate(:mention, status: status) }
context 'activity is reblog' do
let(:activity) { reblog }
it 'returns status' do
expect(notification.target_status).to eq status
end
end
context 'activity is favourite' do
let(:type) { :favourite }
let(:activity) { favourite }
it 'returns status' do
expect(notification.target_status).to eq status
end
end
context 'activity is mention' do
let(:activity) { mention }
it 'returns status' do
expect(notification.target_status).to eq status
end
end
end
describe '#browserable?' do
let(:notification) { Fabricate(:notification) }
subject { notification.browserable? }
context 'type is :follow_request' do
before do
allow(notification).to receive(:type).and_return(:follow_request)
end
it 'returns false' do
is_expected.to be false
end
end
context 'type is not :follow_request' do
before do
allow(notification).to receive(:type).and_return(:else)
end
it 'returns true' do
is_expected.to be true
end
end
end
describe '#type' do
it 'returns :reblog for a Status' do
notification = Notification.new(activity: Status.new)
expect(notification.type).to eq :reblog
end
it 'returns :mention for a Mention' do
notification = Notification.new(activity: Mention.new)
expect(notification.type).to eq :mention
end
it 'returns :favourite for a Favourite' do
notification = Notification.new(activity: Favourite.new)
expect(notification.type).to eq :favourite
end
it 'returns :follow for a Follow' do
notification = Notification.new(activity: Follow.new)
expect(notification.type).to eq :follow
end
end
describe '.reload_stale_associations!' do
context 'account_ids are empty' do
let(:cached_items) { [] }
subject { described_class.reload_stale_associations!(cached_items) }
it 'returns nil' do
is_expected.to be nil
end
end
context 'account_ids are present' do
before do
allow(accounts_with_ids).to receive(:[]).with(stale_account1.id).and_return(account1)
allow(accounts_with_ids).to receive(:[]).with(stale_account2.id).and_return(account2)
allow(Account).to receive_message_chain(:where, :includes, :each_with_object).and_return(accounts_with_ids)
end
let(:cached_items) do
[
Fabricate(:notification, activity: Fabricate(:status)),
Fabricate(:notification, activity: Fabricate(:follow)),
]
end
let(:stale_account1) { cached_items[0].from_account }
let(:stale_account2) { cached_items[1].from_account }
let(:account1) { Fabricate(:account) }
let(:account2) { Fabricate(:account) }
let(:accounts_with_ids) { { account1.id => account1, account2.id => account2 } }
it 'reloads associations' do
expect(cached_items[0].from_account).to be stale_account1
expect(cached_items[1].from_account).to be stale_account2
described_class.reload_stale_associations!(cached_items)
expect(cached_items[0].from_account).to be account1
expect(cached_items[1].from_account).to be account2
end
end
end
end

5
spec/models/poll_spec.rb Normal file
View File

@@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe Poll, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe PollVote, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe PreviewCard, type: :model do
end

View File

@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe Relay, type: :model do
end

View File

@@ -0,0 +1,67 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe RemoteFollow do
before do
stub_request(:get, 'https://quitter.no/.well-known/webfinger?resource=acct:robcolbert@quitter.no').to_return(request_fixture('webfinger.txt'))
end
let(:attrs) { nil }
let(:remote_follow) { described_class.new(attrs) }
describe '.initialize' do
subject { remote_follow.acct }
context 'attrs with acct' do
let(:attrs) { { acct: 'robcolbert@quitter.no' } }
it 'returns acct' do
is_expected.to eq 'robcolbert@quitter.no'
end
end
context 'attrs without acct' do
let(:attrs) { {} }
it do
is_expected.to be_nil
end
end
end
describe '#valid?' do
subject { remote_follow.valid? }
context 'attrs with acct' do
let(:attrs) { { acct: 'robcolbert@quitter.no' } }
it do
is_expected.to be true
end
end
context 'attrs without acct' do
let(:attrs) { {} }
it do
is_expected.to be false
end
end
end
describe '#subscribe_address_for' do
before do
remote_follow.valid?
end
let(:attrs) { { acct: 'robcolbert@quitter.no' } }
let(:account) { Fabricate(:account, username: 'alice') }
subject { remote_follow.subscribe_address_for(account) }
it 'returns subscribe address' do
is_expected.to eq 'https://quitter.no/main/ostatussub?profile=alice%40cb6e6126.ngrok.io'
end
end
end

View File

@@ -0,0 +1,143 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe RemoteProfile do
let(:remote_profile) { RemoteProfile.new(body) }
let(:body) do
<<-XML
<feed xmlns="http://www.w3.org/2005/Atom">
<author>John</author>
XML
end
describe '.initialize' do
it 'calls Nokogiri::XML.parse' do
expect(Nokogiri::XML).to receive(:parse).with(body, nil, 'utf-8')
RemoteProfile.new(body)
end
it 'sets document' do
remote_profile = RemoteProfile.new(body)
expect(remote_profile).not_to be nil
end
end
describe '#root' do
let(:document) { remote_profile.document }
it 'callse document.at_xpath' do
expect(document).to receive(:at_xpath).with(
'/atom:feed|/atom:entry',
atom: OStatus::TagManager::XMLNS
)
remote_profile.root
end
end
describe '#author' do
let(:root) { remote_profile.root }
it 'calls root.at_xpath' do
expect(root).to receive(:at_xpath).with(
'./atom:author|./dfrn:owner',
atom: OStatus::TagManager::XMLNS,
dfrn: OStatus::TagManager::DFRN_XMLNS
)
remote_profile.author
end
end
describe '#hub_link' do
let(:root) { remote_profile.root }
it 'calls #link_href_from_xml' do
expect(remote_profile).to receive(:link_href_from_xml).with(root, 'hub')
remote_profile.hub_link
end
end
describe '#display_name' do
let(:author) { remote_profile.author }
it 'calls author.at_xpath.content' do
expect(author).to receive_message_chain(:at_xpath, :content).with(
'./poco:displayName',
poco: OStatus::TagManager::POCO_XMLNS
).with(no_args)
remote_profile.display_name
end
end
describe '#note' do
let(:author) { remote_profile.author }
it 'calls author.at_xpath.content' do
expect(author).to receive_message_chain(:at_xpath, :content).with(
'./atom:summary|./poco:note',
atom: OStatus::TagManager::XMLNS,
poco: OStatus::TagManager::POCO_XMLNS
).with(no_args)
remote_profile.note
end
end
describe '#scope' do
let(:author) { remote_profile.author }
it 'calls author.at_xpath.content' do
expect(author).to receive_message_chain(:at_xpath, :content).with(
'./gabsocial:scope',
gabsocial: OStatus::TagManager::GABSCL_XMLNS
).with(no_args)
remote_profile.scope
end
end
describe '#avatar' do
let(:author) { remote_profile.author }
it 'calls #link_href_from_xml' do
expect(remote_profile).to receive(:link_href_from_xml).with(author, 'avatar')
remote_profile.avatar
end
end
describe '#header' do
let(:author) { remote_profile.author }
it 'calls #link_href_from_xml' do
expect(remote_profile).to receive(:link_href_from_xml).with(author, 'header')
remote_profile.header
end
end
describe '#locked?' do
before do
allow(remote_profile).to receive(:scope).and_return(scope)
end
subject { remote_profile.locked? }
context 'scope is private' do
let(:scope) { 'private' }
it 'returns true' do
is_expected.to be true
end
end
context 'scope is not private' do
let(:scope) { 'public' }
it 'returns false' do
is_expected.to be false
end
end
end
end

View File

@@ -0,0 +1,32 @@
require 'rails_helper'
describe ReportFilter do
describe 'with empty params' do
it 'defaults to unresolved reports list' do
filter = ReportFilter.new({})
expect(filter.results).to eq Report.unresolved
end
end
describe 'with invalid params' do
it 'raises with key error' do
filter = ReportFilter.new(wrong: true)
expect { filter.results }.to raise_error(/wrong/)
end
end
describe 'with valid params' do
it 'combines filters on Report' do
filter = ReportFilter.new(account_id: '123', resolved: true, target_account_id: '456')
allow(Report).to receive(:where).and_return(Report.none)
allow(Report).to receive(:resolved).and_return(Report.none)
filter.results
expect(Report).to have_received(:where).with(account_id: '123')
expect(Report).to have_received(:where).with(target_account_id: '456')
expect(Report).to have_received(:resolved)
end
end
end

133
spec/models/report_spec.rb Normal file
View File

@@ -0,0 +1,133 @@
require 'rails_helper'
describe Report do
describe 'statuses' do
it 'returns the statuses for the report' do
status = Fabricate(:status)
_other = Fabricate(:status)
report = Fabricate(:report, status_ids: [status.id])
expect(report.statuses).to eq [status]
end
end
describe 'media_attachments' do
it 'returns media attachments from statuses' do
status = Fabricate(:status)
media_attachment = Fabricate(:media_attachment, status: status)
_other_media_attachment = Fabricate(:media_attachment)
report = Fabricate(:report, status_ids: [status.id])
expect(report.media_attachments).to eq [media_attachment]
end
end
describe 'assign_to_self!' do
subject { report.assigned_account_id }
let(:report) { Fabricate(:report, assigned_account_id: original_account) }
let(:original_account) { Fabricate(:account) }
let(:current_account) { Fabricate(:account) }
before do
report.assign_to_self!(current_account)
end
it 'assigns to a given account' do
is_expected.to eq current_account.id
end
end
describe 'unassign!' do
subject { report.assigned_account_id }
let(:report) { Fabricate(:report, assigned_account_id: account.id) }
let(:account) { Fabricate(:account) }
before do
report.unassign!
end
it 'unassigns' do
is_expected.to be_nil
end
end
describe 'resolve!' do
subject(:report) { Fabricate(:report, action_taken: false, action_taken_by_account_id: nil) }
let(:acting_account) { Fabricate(:account) }
before do
report.resolve!(acting_account)
end
it 'records action taken' do
expect(report).to have_attributes(action_taken: true, action_taken_by_account_id: acting_account.id)
end
end
describe 'unresolve!' do
subject(:report) { Fabricate(:report, action_taken: true, action_taken_by_account_id: acting_account.id) }
let(:acting_account) { Fabricate(:account) }
before do
report.unresolve!
end
it 'unresolves' do
expect(report).to have_attributes(action_taken: false, action_taken_by_account_id: nil)
end
end
describe 'unresolved?' do
subject { report.unresolved? }
let(:report) { Fabricate(:report, action_taken: action_taken) }
context 'if action is taken' do
let(:action_taken) { true }
it { is_expected.to be false }
end
context 'if action not is taken' do
let(:action_taken) { false }
it { is_expected.to be true }
end
end
describe 'history' do
subject(:action_logs) { report.history }
let(:report) { Fabricate(:report, target_account_id: target_account.id, status_ids: [status.id], created_at: 3.days.ago, updated_at: 1.day.ago) }
let(:target_account) { Fabricate(:account) }
let(:status) { Fabricate(:status) }
before do
Fabricate('Admin::ActionLog', target_type: 'Report', account_id: target_account.id, target_id: report.id, created_at: 2.days.ago)
Fabricate('Admin::ActionLog', target_type: 'Account', account_id: target_account.id, target_id: report.target_account_id, created_at: 2.days.ago)
Fabricate('Admin::ActionLog', target_type: 'Status', account_id: target_account.id, target_id: status.id, created_at: 2.days.ago)
end
it 'returns right logs' do
expect(action_logs.count).to eq 3
end
end
describe 'validatiions' do
it 'has a valid fabricator' do
report = Fabricate(:report)
report.valid?
expect(report).to be_valid
end
it 'is invalid if comment is longer than 1000 characters' do
report = Fabricate.build(:report, comment: Faker::Lorem.characters(1001))
report.valid?
expect(report).to model_have_error_on_field(:comment)
end
end
end

View File

@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe ScheduledStatus, type: :model do
end

View File

@@ -0,0 +1,127 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe SessionActivation, type: :model do
describe '#detection' do
let(:session_activation) { Fabricate(:session_activation, user_agent: 'Chrome/62.0.3202.89') }
it 'sets a Browser instance as detection' do
expect(session_activation.detection).to be_kind_of Browser::Chrome
end
end
describe '#browser' do
before do
allow(session_activation).to receive(:detection).and_return(detection)
end
let(:detection) { double(id: 1) }
let(:session_activation) { Fabricate(:session_activation) }
it 'returns detection.id' do
expect(session_activation.browser).to be 1
end
end
describe '#platform' do
before do
allow(session_activation).to receive(:detection).and_return(detection)
end
let(:session_activation) { Fabricate(:session_activation) }
let(:detection) { double(platform: double(id: 1)) }
it 'returns detection.platform.id' do
expect(session_activation.platform).to be 1
end
end
describe '.active?' do
subject { described_class.active?(id) }
context 'id is absent' do
let(:id) { nil }
it 'returns nil' do
is_expected.to be nil
end
end
context 'id is present' do
let(:id) { '1' }
let!(:session_activation) { Fabricate(:session_activation, session_id: id) }
context 'id exists as session_id' do
it 'returns true' do
is_expected.to be true
end
end
context 'id does not exist as session_id' do
before do
session_activation.update!(session_id: '2')
end
it 'returns false' do
is_expected.to be false
end
end
end
end
describe '.activate' do
let(:options) { { user: Fabricate(:user), session_id: '1' } }
it 'calls create! and purge_old' do
expect(described_class).to receive(:create!).with(options)
expect(described_class).to receive(:purge_old)
described_class.activate(options)
end
it 'returns an instance of SessionActivation' do
expect(described_class.activate(options)).to be_kind_of SessionActivation
end
end
describe '.deactivate' do
context 'id is absent' do
let(:id) { nil }
it 'returns nil' do
expect(described_class.deactivate(id)).to be nil
end
end
context 'id exists' do
let(:id) { '1' }
it 'calls where.destroy_all' do
expect(described_class).to receive_message_chain(:where, :destroy_all)
.with(session_id: id).with(no_args)
described_class.deactivate(id)
end
end
end
describe '.purge_old' do
it 'calls order.offset.destroy_all' do
expect(described_class).to receive_message_chain(:order, :offset, :destroy_all)
.with('created_at desc').with(Rails.configuration.x.max_session_activations).with(no_args)
described_class.purge_old
end
end
describe '.exclusive' do
let(:id) { '1' }
it 'calls where.destroy_all' do
expect(described_class).to receive_message_chain(:where, :destroy_all)
.with('session_id != ?', id).with(no_args)
described_class.exclusive(id)
end
end
end

187
spec/models/setting_spec.rb Normal file
View File

@@ -0,0 +1,187 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Setting, type: :model do
describe '#to_param' do
let(:setting) { Fabricate(:setting, var: var) }
let(:var) { 'var' }
it 'returns setting.var' do
expect(setting.to_param).to eq var
end
end
describe '.[]' do
before do
allow(described_class).to receive(:rails_initialized?).and_return(rails_initialized)
end
let(:key) { 'key' }
context 'rails_initialized? is falsey' do
let(:rails_initialized) { false }
it 'calls RailsSettings::Base#[]' do
expect(RailsSettings::Base).to receive(:[]).with(key)
described_class[key]
end
end
context 'rails_initialized? is truthy' do
before do
allow(RailsSettings::Base).to receive(:cache_key).with(key, nil).and_return(cache_key)
end
let(:rails_initialized) { true }
let(:cache_key) { 'cache-key' }
let(:cache_value) { 'cache-value' }
it 'calls not RailsSettings::Base#[]' do
expect(RailsSettings::Base).not_to receive(:[]).with(key)
described_class[key]
end
context 'Rails.cache does not exists' do
before do
allow(RailsSettings::Settings).to receive(:object).with(key).and_return(object)
allow(described_class).to receive(:default_settings).and_return(default_settings)
allow_any_instance_of(Settings::ScopedSettings).to receive(:thing_scoped).and_return(records)
Rails.cache.delete(cache_key)
end
let(:object) { nil }
let(:default_value) { 'default_value' }
let(:default_settings) { { key => default_value } }
let(:records) { [Fabricate(:setting, var: key, value: nil)] }
it 'calls RailsSettings::Settings.object' do
expect(RailsSettings::Settings).to receive(:object).with(key)
described_class[key]
end
context 'RailsSettings::Settings.object returns truthy' do
let(:object) { db_val }
let(:db_val) { double(value: 'db_val') }
context 'default_value is a Hash' do
let(:default_value) { { default_value: 'default_value' } }
it 'calls default_value.with_indifferent_access.merge!' do
expect(default_value).to receive_message_chain(:with_indifferent_access, :merge!)
.with(db_val.value)
described_class[key]
end
end
context 'default_value is not a Hash' do
let(:default_value) { 'default_value' }
it 'returns db_val.value' do
expect(described_class[key]).to be db_val.value
end
end
end
context 'RailsSettings::Settings.object returns falsey' do
let(:object) { nil }
it 'returns default_settings[key]' do
expect(described_class[key]).to be default_settings[key]
end
end
end
context 'Rails.cache exists' do
before do
Rails.cache.write(cache_key, cache_value)
end
it 'does not query the database' do
expect do |callback|
ActiveSupport::Notifications.subscribed callback, 'sql.active_record' do
described_class[key]
end
end.not_to yield_control
end
it 'returns the cached value' do
expect(described_class[key]).to eq cache_value
end
end
end
end
describe '.all_as_records' do
before do
allow_any_instance_of(Settings::ScopedSettings).to receive(:thing_scoped).and_return(records)
allow(described_class).to receive(:default_settings).and_return(default_settings)
end
let(:key) { 'key' }
let(:default_value) { 'default_value' }
let(:default_settings) { { key => default_value } }
let(:original_setting) { Fabricate(:setting, var: key, value: nil) }
let(:records) { [original_setting] }
it 'returns a Hash' do
expect(described_class.all_as_records).to be_kind_of Hash
end
context 'records includes Setting with var as the key' do
let(:records) { [original_setting] }
it 'includes the original Setting' do
setting = described_class.all_as_records[key]
expect(setting).to eq original_setting
end
end
context 'records includes nothing' do
let(:records) { [] }
context 'default_value is not a Hash' do
it 'includes Setting with value of default_value' do
setting = described_class.all_as_records[key]
expect(setting).to be_kind_of Setting
expect(setting).to have_attributes(var: key)
expect(setting).to have_attributes(value: 'default_value')
end
end
context 'default_value is a Hash' do
let(:default_value) { { 'foo' => 'fuga' } }
it 'returns {}' do
expect(described_class.all_as_records).to eq({})
end
end
end
end
describe '.default_settings' do
before do
allow(RailsSettings::Default).to receive(:enabled?).and_return(enabled)
end
subject { described_class.default_settings }
context 'RailsSettings::Default.enabled? is false' do
let(:enabled) { false }
it 'returns {}' do
is_expected.to eq({})
end
end
context 'RailsSettings::Settings.enabled? is true' do
let(:enabled) { true }
it 'returns instance of RailsSettings::Default' do
is_expected.to be_kind_of RailsSettings::Default
end
end
end
end

View File

@@ -0,0 +1,13 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe SiteUpload, type: :model do
describe '#cache_key' do
let(:site_upload) { SiteUpload.new(var: 'var') }
it 'returns cache_key' do
expect(site_upload.cache_key).to eq 'site_uploads/var'
end
end
end

View File

@@ -0,0 +1,72 @@
require 'rails_helper'
RSpec.describe StatusPin, type: :model do
describe 'validations' do
it 'allows pins of own statuses' do
account = Fabricate(:account)
status = Fabricate(:status, account: account)
expect(StatusPin.new(account: account, status: status).save).to be true
end
it 'does not allow pins of statuses by someone else' do
account = Fabricate(:account)
status = Fabricate(:status)
expect(StatusPin.new(account: account, status: status).save).to be false
end
it 'does not allow pins of reblogs' do
account = Fabricate(:account)
status = Fabricate(:status, account: account)
reblog = Fabricate(:status, reblog: status)
expect(StatusPin.new(account: account, status: reblog).save).to be false
end
it 'does not allow pins of private statuses' do
account = Fabricate(:account)
status = Fabricate(:status, account: account, visibility: :private)
expect(StatusPin.new(account: account, status: status).save).to be false
end
it 'does not allow pins of direct statuses' do
account = Fabricate(:account)
status = Fabricate(:status, account: account, visibility: :direct)
expect(StatusPin.new(account: account, status: status).save).to be false
end
max_pins = 5
it 'does not allow pins above the max' do
account = Fabricate(:account)
status = []
(max_pins + 1).times do |i|
status[i] = Fabricate(:status, account: account)
end
max_pins.times do |i|
expect(StatusPin.new(account: account, status: status[i]).save).to be true
end
expect(StatusPin.new(account: account, status: status[max_pins]).save).to be false
end
it 'allows pins above the max for remote accounts' do
account = Fabricate(:account, domain: 'remote.test', username: 'bob', url: 'https://remote.test/')
status = []
(max_pins + 1).times do |i|
status[i] = Fabricate(:status, account: account)
end
max_pins.times do |i|
expect(StatusPin.new(account: account, status: status[i]).save).to be true
end
expect(StatusPin.new(account: account, status: status[max_pins]).save).to be true
end
end
end

696
spec/models/status_spec.rb Normal file
View File

@@ -0,0 +1,696 @@
require 'rails_helper'
RSpec.describe Status, type: :model do
let(:alice) { Fabricate(:account, username: 'alice') }
let(:bob) { Fabricate(:account, username: 'bob') }
let(:other) { Fabricate(:status, account: bob, text: 'Skulls for the skull god! The enemy\'s gates are sideways!') }
subject { Fabricate(:status, account: alice) }
describe '#local?' do
it 'returns true when no remote URI is set' do
expect(subject.local?).to be true
end
it 'returns false if a remote URI is set' do
alice.update(domain: 'example.com')
subject.save
expect(subject.local?).to be false
end
it 'returns true if a URI is set and `local` is true' do
subject.update(uri: 'example.com', local: true)
expect(subject.local?).to be true
end
end
describe '#reblog?' do
it 'returns true when the status reblogs another status' do
subject.reblog = other
expect(subject.reblog?).to be true
end
it 'returns false if the status is self-contained' do
expect(subject.reblog?).to be false
end
end
describe '#reply?' do
it 'returns true if the status references another' do
subject.thread = other
expect(subject.reply?).to be true
end
it 'returns false if the status is self-contained' do
expect(subject.reply?).to be false
end
end
describe '#verb' do
context 'if destroyed?' do
it 'returns :delete' do
subject.destroy!
expect(subject.verb).to be :delete
end
end
context 'unless destroyed?' do
context 'if reblog?' do
it 'returns :share' do
subject.reblog = other
expect(subject.verb).to be :share
end
end
context 'unless reblog?' do
it 'returns :post' do
subject.reblog = nil
expect(subject.verb).to be :post
end
end
end
end
describe '#object_type' do
it 'is note when the status is self-contained' do
expect(subject.object_type).to be :note
end
it 'is comment when the status replies to another' do
subject.thread = other
expect(subject.object_type).to be :comment
end
end
describe '#title' do
# rubocop:disable Style/InterpolationCheck
let(:account) { subject.account }
context 'if destroyed?' do
it 'returns "#{account.acct} deleted status"' do
subject.destroy!
expect(subject.title).to eq "#{account.acct} deleted status"
end
end
context 'unless destroyed?' do
context 'if reblog?' do
it 'returns "#{account.acct} shared a status by #{reblog.account.acct}"' do
reblog = subject.reblog = other
expect(subject.title).to eq "#{account.acct} shared a status by #{reblog.account.acct}"
end
end
context 'unless reblog?' do
it 'returns "New status by #{account.acct}"' do
subject.reblog = nil
expect(subject.title).to eq "New status by #{account.acct}"
end
end
end
end
describe '#hidden?' do
context 'if private_visibility?' do
it 'returns true' do
subject.visibility = :private
expect(subject.hidden?).to be true
end
end
context 'if direct_visibility?' do
it 'returns true' do
subject.visibility = :direct
expect(subject.hidden?).to be true
end
end
context 'if public_visibility?' do
it 'returns false' do
subject.visibility = :public
expect(subject.hidden?).to be false
end
end
context 'if unlisted_visibility?' do
it 'returns false' do
subject.visibility = :unlisted
expect(subject.hidden?).to be false
end
end
end
describe '#content' do
it 'returns the text of the status if it is not a reblog' do
expect(subject.content).to eql subject.text
end
it 'returns the text of the reblogged status' do
subject.reblog = other
expect(subject.content).to eql other.text
end
end
describe '#target' do
it 'returns nil if the status is self-contained' do
expect(subject.target).to be_nil
end
it 'returns nil if the status is a reply' do
subject.thread = other
expect(subject.target).to be_nil
end
it 'returns the reblogged status' do
subject.reblog = other
expect(subject.target).to eq other
end
end
describe '#reblogs_count' do
it 'is the number of reblogs' do
Fabricate(:status, account: bob, reblog: subject)
Fabricate(:status, account: alice, reblog: subject)
expect(subject.reblogs_count).to eq 2
end
it 'is decremented when reblog is removed' do
reblog = Fabricate(:status, account: bob, reblog: subject)
expect(subject.reblogs_count).to eq 1
reblog.destroy
expect(subject.reblogs_count).to eq 0
end
it 'does not fail when original is deleted before reblog' do
reblog = Fabricate(:status, account: bob, reblog: subject)
expect(subject.reblogs_count).to eq 1
expect { subject.destroy }.to_not raise_error
expect(Status.find_by(id: reblog.id)).to be_nil
end
end
describe '#replies_count' do
it 'is the number of replies' do
reply = Fabricate(:status, account: bob, thread: subject)
expect(subject.replies_count).to eq 1
end
it 'is decremented when reply is removed' do
reply = Fabricate(:status, account: bob, thread: subject)
expect(subject.replies_count).to eq 1
reply.destroy
expect(subject.replies_count).to eq 0
end
end
describe '#favourites_count' do
it 'is the number of favorites' do
Fabricate(:favourite, account: bob, status: subject)
Fabricate(:favourite, account: alice, status: subject)
expect(subject.favourites_count).to eq 2
end
it 'is decremented when favourite is removed' do
favourite = Fabricate(:favourite, account: bob, status: subject)
expect(subject.favourites_count).to eq 1
favourite.destroy
expect(subject.favourites_count).to eq 0
end
end
describe '#proper' do
it 'is itself for original statuses' do
expect(subject.proper).to eq subject
end
it 'is the source status for reblogs' do
subject.reblog = other
expect(subject.proper).to eq other
end
end
describe '.mutes_map' do
let(:status) { Fabricate(:status) }
let(:account) { Fabricate(:account) }
subject { Status.mutes_map([status.conversation.id], account) }
it 'returns a hash' do
expect(subject).to be_a Hash
end
it 'contains true value' do
account.mute_conversation!(status.conversation)
expect(subject[status.conversation.id]).to be true
end
end
describe '.favourites_map' do
let(:status) { Fabricate(:status) }
let(:account) { Fabricate(:account) }
subject { Status.favourites_map([status], account) }
it 'returns a hash' do
expect(subject).to be_a Hash
end
it 'contains true value' do
Fabricate(:favourite, status: status, account: account)
expect(subject[status.id]).to be true
end
end
describe '.reblogs_map' do
let(:status) { Fabricate(:status) }
let(:account) { Fabricate(:account) }
subject { Status.reblogs_map([status], account) }
it 'returns a hash' do
expect(subject).to be_a Hash
end
it 'contains true value' do
Fabricate(:status, account: account, reblog: status)
expect(subject[status.id]).to be true
end
end
describe '.in_chosen_languages' do
context 'for accounts with language filters' do
let(:user) { Fabricate(:user, chosen_languages: ['en']) }
it 'does not include statuses in not in chosen languages' do
status = Fabricate(:status, language: 'de')
expect(Status.in_chosen_languages(user.account)).not_to include status
end
it 'includes status with unknown language' do
status = Fabricate(:status, language: nil)
expect(Status.in_chosen_languages(user.account)).to include status
end
end
end
describe '.as_home_timeline' do
let(:account) { Fabricate(:account) }
let(:followed) { Fabricate(:account) }
let(:not_followed) { Fabricate(:account) }
before do
Fabricate(:follow, account: account, target_account: followed)
@self_status = Fabricate(:status, account: account, visibility: :public)
@self_direct_status = Fabricate(:status, account: account, visibility: :direct)
@followed_status = Fabricate(:status, account: followed, visibility: :public)
@followed_direct_status = Fabricate(:status, account: followed, visibility: :direct)
@not_followed_status = Fabricate(:status, account: not_followed, visibility: :public)
@results = Status.as_home_timeline(account)
end
it 'includes statuses from self' do
expect(@results).to include(@self_status)
end
it 'does not include direct statuses from self' do
expect(@results).to_not include(@self_direct_status)
end
it 'includes statuses from followed' do
expect(@results).to include(@followed_status)
end
it 'does not include direct statuses mentioning recipient from followed' do
Fabricate(:mention, account: account, status: @followed_direct_status)
expect(@results).to_not include(@followed_direct_status)
end
it 'does not include direct statuses not mentioning recipient from followed' do
expect(@results).not_to include(@followed_direct_status)
end
it 'does not include statuses from non-followed' do
expect(@results).not_to include(@not_followed_status)
end
end
describe '.as_direct_timeline' do
let(:account) { Fabricate(:account) }
let(:followed) { Fabricate(:account) }
let(:not_followed) { Fabricate(:account) }
before do
Fabricate(:follow, account: account, target_account: followed)
@self_public_status = Fabricate(:status, account: account, visibility: :public)
@self_direct_status = Fabricate(:status, account: account, visibility: :direct)
@followed_public_status = Fabricate(:status, account: followed, visibility: :public)
@followed_direct_status = Fabricate(:status, account: followed, visibility: :direct)
@not_followed_direct_status = Fabricate(:status, account: not_followed, visibility: :direct)
@results = Status.as_direct_timeline(account)
end
it 'does not include public statuses from self' do
expect(@results).to_not include(@self_public_status)
end
it 'includes direct statuses from self' do
expect(@results).to include(@self_direct_status)
end
it 'does not include public statuses from followed' do
expect(@results).to_not include(@followed_public_status)
end
it 'does not include direct statuses not mentioning recipient from followed' do
expect(@results).to_not include(@followed_direct_status)
end
it 'does not include direct statuses not mentioning recipient from non-followed' do
expect(@results).to_not include(@not_followed_direct_status)
end
it 'includes direct statuses mentioning recipient from followed' do
Fabricate(:mention, account: account, status: @followed_direct_status)
results2 = Status.as_direct_timeline(account)
expect(results2).to include(@followed_direct_status)
end
it 'includes direct statuses mentioning recipient from non-followed' do
Fabricate(:mention, account: account, status: @not_followed_direct_status)
results2 = Status.as_direct_timeline(account)
expect(results2).to include(@not_followed_direct_status)
end
end
describe '.as_public_timeline' do
it 'only includes statuses with public visibility' do
public_status = Fabricate(:status, visibility: :public)
private_status = Fabricate(:status, visibility: :private)
results = Status.as_public_timeline
expect(results).to include(public_status)
expect(results).not_to include(private_status)
end
it 'does not include replies' do
status = Fabricate(:status)
reply = Fabricate(:status, in_reply_to_id: status.id)
results = Status.as_public_timeline
expect(results).to include(status)
expect(results).not_to include(reply)
end
it 'does not include reposts' do
status = Fabricate(:status)
repost = Fabricate(:status, reblog_of_id: status.id)
results = Status.as_public_timeline
expect(results).to include(status)
expect(results).not_to include(repost)
end
it 'filters out silenced accounts' do
account = Fabricate(:account)
silenced_account = Fabricate(:account, silenced: true)
status = Fabricate(:status, account: account)
silenced_status = Fabricate(:status, account: silenced_account)
results = Status.as_public_timeline
expect(results).to include(status)
expect(results).not_to include(silenced_status)
end
context 'without local_only option' do
let(:viewer) { nil }
let!(:local_account) { Fabricate(:account, domain: nil) }
let!(:remote_account) { Fabricate(:account, domain: 'test.com') }
let!(:local_status) { Fabricate(:status, account: local_account) }
let!(:remote_status) { Fabricate(:status, account: remote_account) }
subject { Status.as_public_timeline(viewer, false) }
context 'without a viewer' do
let(:viewer) { nil }
it 'includes remote instances statuses' do
expect(subject).to include(remote_status)
end
it 'includes local statuses' do
expect(subject).to include(local_status)
end
end
context 'with a viewer' do
let(:viewer) { Fabricate(:account, username: 'viewer') }
it 'includes remote instances statuses' do
expect(subject).to include(remote_status)
end
it 'includes local statuses' do
expect(subject).to include(local_status)
end
end
end
context 'with a local_only option set' do
let!(:local_account) { Fabricate(:account, domain: nil) }
let!(:remote_account) { Fabricate(:account, domain: 'test.com') }
let!(:local_status) { Fabricate(:status, account: local_account) }
let!(:remote_status) { Fabricate(:status, account: remote_account) }
subject { Status.as_public_timeline(viewer, true) }
context 'without a viewer' do
let(:viewer) { nil }
it 'does not include remote instances statuses' do
expect(subject).to include(local_status)
expect(subject).not_to include(remote_status)
end
end
context 'with a viewer' do
let(:viewer) { Fabricate(:account, username: 'viewer') }
it 'does not include remote instances statuses' do
expect(subject).to include(local_status)
expect(subject).not_to include(remote_status)
end
it 'is not affected by personal domain blocks' do
viewer.block_domain!('test.com')
expect(subject).to include(local_status)
expect(subject).not_to include(remote_status)
end
end
end
describe 'with an account passed in' do
before do
@account = Fabricate(:account)
end
it 'excludes statuses from accounts blocked by the account' do
blocked = Fabricate(:account)
Fabricate(:block, account: @account, target_account: blocked)
blocked_status = Fabricate(:status, account: blocked)
results = Status.as_public_timeline(@account)
expect(results).not_to include(blocked_status)
end
it 'excludes statuses from accounts who have blocked the account' do
blocked = Fabricate(:account)
Fabricate(:block, account: blocked, target_account: @account)
blocked_status = Fabricate(:status, account: blocked)
results = Status.as_public_timeline(@account)
expect(results).not_to include(blocked_status)
end
it 'excludes statuses from accounts muted by the account' do
muted = Fabricate(:account)
Fabricate(:mute, account: @account, target_account: muted)
muted_status = Fabricate(:status, account: muted)
results = Status.as_public_timeline(@account)
expect(results).not_to include(muted_status)
end
it 'excludes statuses from accounts from personally blocked domains' do
blocked = Fabricate(:account, domain: 'example.com')
@account.block_domain!(blocked.domain)
blocked_status = Fabricate(:status, account: blocked)
results = Status.as_public_timeline(@account)
expect(results).not_to include(blocked_status)
end
context 'with language preferences' do
it 'excludes statuses in languages not allowed by the account user' do
user = Fabricate(:user, chosen_languages: [:en, :es])
@account.update(user: user)
en_status = Fabricate(:status, language: 'en')
es_status = Fabricate(:status, language: 'es')
fr_status = Fabricate(:status, language: 'fr')
results = Status.as_public_timeline(@account)
expect(results).to include(en_status)
expect(results).to include(es_status)
expect(results).not_to include(fr_status)
end
it 'includes all languages when user does not have a setting' do
user = Fabricate(:user, chosen_languages: nil)
@account.update(user: user)
en_status = Fabricate(:status, language: 'en')
es_status = Fabricate(:status, language: 'es')
results = Status.as_public_timeline(@account)
expect(results).to include(en_status)
expect(results).to include(es_status)
end
it 'includes all languages when account does not have a user' do
expect(@account.user).to be_nil
en_status = Fabricate(:status, language: 'en')
es_status = Fabricate(:status, language: 'es')
results = Status.as_public_timeline(@account)
expect(results).to include(en_status)
expect(results).to include(es_status)
end
end
end
end
describe '.as_tag_timeline' do
it 'includes statuses with a tag' do
tag = Fabricate(:tag)
status = Fabricate(:status, tags: [tag])
other = Fabricate(:status)
results = Status.as_tag_timeline(tag)
expect(results).to include(status)
expect(results).not_to include(other)
end
it 'allows replies to be included' do
original = Fabricate(:status)
tag = Fabricate(:tag)
status = Fabricate(:status, tags: [tag], in_reply_to_id: original.id)
results = Status.as_tag_timeline(tag)
expect(results).to include(status)
end
end
describe '.permitted_for' do
subject { described_class.permitted_for(target_account, account).pluck(:visibility) }
let(:target_account) { alice }
let(:account) { bob }
let!(:public_status) { Fabricate(:status, account: target_account, visibility: 'public') }
let!(:unlisted_status) { Fabricate(:status, account: target_account, visibility: 'unlisted') }
let!(:private_status) { Fabricate(:status, account: target_account, visibility: 'private') }
let!(:direct_status) do
Fabricate(:status, account: target_account, visibility: 'direct').tap do |status|
Fabricate(:mention, status: status, account: account)
end
end
let!(:other_direct_status) do
Fabricate(:status, account: target_account, visibility: 'direct').tap do |status|
Fabricate(:mention, status: status)
end
end
context 'given nil' do
let(:account) { nil }
let(:direct_status) { nil }
it { is_expected.to eq(%w(unlisted public)) }
end
context 'given blocked account' do
before do
target_account.block!(account)
end
it { is_expected.to be_empty }
end
context 'given same account' do
let(:account) { target_account }
it { is_expected.to eq(%w(direct direct private unlisted public)) }
end
context 'given followed account' do
before do
account.follow!(target_account)
end
it { is_expected.to eq(%w(direct private unlisted public)) }
end
context 'given unfollowed account' do
it { is_expected.to eq(%w(direct unlisted public)) }
end
end
describe 'before_validation' do
it 'sets account being replied to correctly over intermediary nodes' do
first_status = Fabricate(:status, account: bob)
intermediary = Fabricate(:status, thread: first_status, account: alice)
final = Fabricate(:status, thread: intermediary, account: alice)
expect(final.in_reply_to_account_id).to eq bob.id
end
it 'creates new conversation for stand-alone status' do
expect(Status.create(account: alice, text: 'First').conversation_id).to_not be_nil
end
it 'keeps conversation of parent node' do
parent = Fabricate(:status, text: 'First')
expect(Status.create(account: alice, thread: parent, text: 'Response').conversation_id).to eq parent.conversation_id
end
it 'sets `local` to true for status by local account' do
expect(Status.create(account: alice, text: 'foo').local).to be true
end
it 'sets `local` to false for status by remote account' do
alice.update(domain: 'example.com')
expect(Status.create(account: alice, text: 'foo').local).to be false
end
end
describe 'validation' do
it 'disallow empty uri for remote status' do
alice.update(domain: 'example.com')
status = Fabricate.build(:status, uri: '', account: alice)
expect(status).to model_have_error_on_field(:uri)
end
end
describe 'after_create' do
it 'saves ActivityPub uri as uri for local status' do
status = Status.create(account: alice, text: 'foo')
status.reload
expect(status.uri).to start_with('https://')
end
end
end

View File

@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe StatusStat, type: :model do
end

View File

@@ -0,0 +1,192 @@
require 'rails_helper'
RSpec.describe StreamEntry, type: :model do
let(:alice) { Fabricate(:account, username: 'alice') }
let(:bob) { Fabricate(:account, username: 'bob') }
let(:status) { Fabricate(:status, account: alice) }
let(:reblog) { Fabricate(:status, account: bob, reblog: status) }
let(:reply) { Fabricate(:status, account: bob, thread: status) }
let(:stream_entry) { Fabricate(:stream_entry, activity: activity) }
let(:activity) { reblog }
describe '#object_type' do
before do
allow(stream_entry).to receive(:orphaned?).and_return(orphaned)
allow(stream_entry).to receive(:targeted?).and_return(targeted)
end
subject { stream_entry.object_type }
context 'orphaned? is true' do
let(:orphaned) { true }
let(:targeted) { false }
it 'returns :activity' do
is_expected.to be :activity
end
end
context 'targeted? is true' do
let(:orphaned) { false }
let(:targeted) { true }
it 'returns :activity' do
is_expected.to be :activity
end
end
context 'orphaned? and targeted? are false' do
let(:orphaned) { false }
let(:targeted) { false }
context 'activity is reblog' do
let(:activity) { reblog }
it 'returns :note' do
is_expected.to be :note
end
end
context 'activity is reply' do
let(:activity) { reply }
it 'returns :comment' do
is_expected.to be :comment
end
end
end
end
describe '#verb' do
before do
allow(stream_entry).to receive(:orphaned?).and_return(orphaned)
end
subject { stream_entry.verb }
context 'orphaned? is true' do
let(:orphaned) { true }
it 'returns :delete' do
is_expected.to be :delete
end
end
context 'orphaned? is false' do
let(:orphaned) { false }
context 'activity is reblog' do
let(:activity) { reblog }
it 'returns :share' do
is_expected.to be :share
end
end
context 'activity is reply' do
let(:activity) { reply }
it 'returns :post' do
is_expected.to be :post
end
end
end
end
describe '#mentions' do
before do
allow(stream_entry).to receive(:orphaned?).and_return(orphaned)
end
subject { stream_entry.mentions }
context 'orphaned? is true' do
let(:orphaned) { true }
it 'returns []' do
is_expected.to eq []
end
end
context 'orphaned? is false' do
before do
reblog.mentions << Fabricate(:mention, account: alice)
reblog.mentions << Fabricate(:mention, account: bob)
end
let(:orphaned) { false }
it 'returns [Account] includes alice and bob' do
is_expected.to eq [alice, bob]
end
end
end
describe '#targeted?' do
it 'returns true for a reblog' do
expect(reblog.stream_entry.targeted?).to be true
end
it 'returns false otherwise' do
expect(status.stream_entry.targeted?).to be false
end
end
describe '#threaded?' do
it 'returns true for a reply' do
expect(reply.stream_entry.threaded?).to be true
end
it 'returns false otherwise' do
expect(status.stream_entry.threaded?).to be false
end
end
describe 'delegated methods' do
context 'with a nil status' do
subject { described_class.new(status: nil) }
it 'returns nil for target' do
expect(subject.target).to be_nil
end
it 'returns nil for title' do
expect(subject.title).to be_nil
end
it 'returns nil for content' do
expect(subject.content).to be_nil
end
it 'returns nil for thread' do
expect(subject.thread).to be_nil
end
end
context 'with a real status' do
let(:original) { Fabricate(:status, text: 'Test status') }
let(:status) { Fabricate(:status, reblog: original, thread: original) }
subject { described_class.new(status: status) }
it 'delegates target' do
expect(status.target).not_to be_nil
expect(subject.target).to eq(status.target)
end
it 'delegates title' do
expect(status.title).not_to be_nil
expect(subject.title).to eq(status.title)
end
it 'delegates content' do
expect(status.content).not_to be_nil
expect(subject.content).to eq(status.content)
end
it 'delegates thread' do
expect(status.thread).not_to be_nil
expect(subject.thread).to eq(status.thread)
end
end
end
end

View File

@@ -0,0 +1,67 @@
require 'rails_helper'
RSpec.describe Subscription, type: :model do
let(:alice) { Fabricate(:account, username: 'alice') }
subject { Fabricate(:subscription, account: alice) }
describe '#expired?' do
it 'return true when expires_at is past' do
subject.expires_at = 2.days.ago
expect(subject.expired?).to be true
end
it 'return false when expires_at is future' do
subject.expires_at = 2.days.from_now
expect(subject.expired?).to be false
end
end
describe 'lease_seconds' do
it 'returns the time remaining until expiration' do
datetime = 1.day.from_now
subscription = Subscription.new(expires_at: datetime)
travel_to(datetime - 12.hours) do
expect(subscription.lease_seconds).to eq(12.hours)
end
end
end
describe 'lease_seconds=' do
it 'sets expires_at to min expiration when small value is provided' do
subscription = Subscription.new
datetime = 1.day.from_now
too_low = Subscription::MIN_EXPIRATION - 1000
travel_to(datetime) do
subscription.lease_seconds = too_low
end
expected = datetime + Subscription::MIN_EXPIRATION.seconds
expect(subscription.expires_at).to be_within(1.0).of(expected)
end
it 'sets expires_at to value when valid value is provided' do
subscription = Subscription.new
datetime = 1.day.from_now
valid = Subscription::MIN_EXPIRATION + 1000
travel_to(datetime) do
subscription.lease_seconds = valid
end
expected = datetime + valid.seconds
expect(subscription.expires_at).to be_within(1.0).of(expected)
end
it 'sets expires_at to max expiration when large value is provided' do
subscription = Subscription.new
datetime = 1.day.from_now
too_high = Subscription::MAX_EXPIRATION + 1000
travel_to(datetime) do
subscription.lease_seconds = too_high
end
expected = datetime + Subscription::MAX_EXPIRATION.seconds
expect(subscription.expires_at).to be_within(1.0).of(expected)
end
end
end

73
spec/models/tag_spec.rb Normal file
View File

@@ -0,0 +1,73 @@
require 'rails_helper'
RSpec.describe Tag, type: :model do
describe 'validations' do
it 'invalid with #' do
expect(Tag.new(name: '#hello_world')).to_not be_valid
end
it 'invalid with .' do
expect(Tag.new(name: '.abcdef123')).to_not be_valid
end
it 'invalid with spaces' do
expect(Tag.new(name: 'hello world')).to_not be_valid
end
it 'valid with ' do
expect(Tag.new(name: '')).to be_valid
end
end
describe 'HASHTAG_RE' do
subject { Tag::HASHTAG_RE }
it 'does not match URLs with anchors with non-hashtag characters' do
expect(subject.match('Check this out https://medium.com/@alice/some-article#.abcdef123')).to be_nil
end
it 'does not match URLs with hashtag-like anchors' do
expect(subject.match('https://en.wikipedia.org/wiki/Ghostbusters_(song)#Lawsuit')).to be_nil
end
it 'matches #' do
expect(subject.match('this is #')).to_not be_nil
end
end
describe '#to_param' do
it 'returns name' do
tag = Fabricate(:tag, name: 'foo')
expect(tag.to_param).to eq 'foo'
end
end
describe '.search_for' do
it 'finds tag records with matching names' do
tag = Fabricate(:tag, name: "match")
_miss_tag = Fabricate(:tag, name: "miss")
results = Tag.search_for("match")
expect(results).to eq [tag]
end
it 'finds tag records in case insensitive' do
tag = Fabricate(:tag, name: "MATCH")
_miss_tag = Fabricate(:tag, name: "miss")
results = Tag.search_for("match")
expect(results).to eq [tag]
end
it 'finds the exact matching tag as the first item' do
similar_tag = Fabricate(:tag, name: "matchlater")
tag = Fabricate(:tag, name: "match")
results = Tag.search_for("match")
expect(results).to eq [tag, similar_tag]
end
end
end

View File

@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe UserInviteRequest, type: :model do
end

529
spec/models/user_spec.rb Normal file
View File

@@ -0,0 +1,529 @@
require 'rails_helper'
require 'devise_two_factor/spec_helpers'
RSpec.describe User, type: :model do
it_behaves_like 'two_factor_backupable'
describe 'otp_secret' do
it 'is encrypted with OTP_SECRET environment variable' do
user = Fabricate(:user,
encrypted_otp_secret: "Fttsy7QAa0edaDfdfSz094rRLAxc8cJweDQ4BsWH/zozcdVA8o9GLqcKhn2b\nGi/V\n",
encrypted_otp_secret_iv: 'rys3THICkr60BoWC',
encrypted_otp_secret_salt: '_LMkAGvdg7a+sDIKjI3mR2Q==')
expect(user.otp_secret).to eq 'anotpsecretthatshouldbeencrypted'
end
end
describe 'validations' do
it 'is invalid without an account' do
user = Fabricate.build(:user, account: nil)
user.valid?
expect(user).to model_have_error_on_field(:account)
end
it 'is invalid without a valid locale' do
user = Fabricate.build(:user, locale: 'toto')
user.valid?
expect(user).to model_have_error_on_field(:locale)
end
it 'is invalid without a valid email' do
user = Fabricate.build(:user, email: 'john@')
user.valid?
expect(user).to model_have_error_on_field(:email)
end
it 'is valid with an invalid e-mail that has already been saved' do
user = Fabricate.build(:user, email: 'invalid-email')
user.save(validate: false)
expect(user.valid?).to be true
end
it 'cleans out empty string from languages' do
user = Fabricate.build(:user, chosen_languages: [''])
user.valid?
expect(user.chosen_languages).to eq nil
end
end
describe 'scopes' do
describe 'recent' do
it 'returns an array of recent users ordered by id' do
user_1 = Fabricate(:user)
user_2 = Fabricate(:user)
expect(User.recent).to eq [user_2, user_1]
end
end
describe 'admins' do
it 'returns an array of users who are admin' do
user_1 = Fabricate(:user, admin: false)
user_2 = Fabricate(:user, admin: true)
expect(User.admins).to match_array([user_2])
end
end
describe 'confirmed' do
it 'returns an array of users who are confirmed' do
user_1 = Fabricate(:user, confirmed_at: nil)
user_2 = Fabricate(:user, confirmed_at: Time.zone.now)
expect(User.confirmed).to match_array([user_2])
end
end
describe 'inactive' do
it 'returns a relation of inactive users' do
specified = Fabricate(:user, current_sign_in_at: 15.days.ago)
Fabricate(:user, current_sign_in_at: 6.days.ago)
expect(User.inactive).to match_array([specified])
end
end
describe 'matches_email' do
it 'returns a relation of users whose email starts with the given string' do
specified = Fabricate(:user, email: 'specified@spec')
Fabricate(:user, email: 'unspecified@spec')
expect(User.matches_email('specified')).to match_array([specified])
end
end
end
let(:account) { Fabricate(:account, username: 'alice') }
let(:password) { 'abcd1234' }
describe 'blacklist' do
around(:each) do |example|
old_blacklist = Rails.configuration.x.email_blacklist
Rails.configuration.x.email_domains_blacklist = 'mvrht.com'
example.run
Rails.configuration.x.email_domains_blacklist = old_blacklist
end
it 'should allow a non-blacklisted user to be created' do
user = User.new(email: 'foo@example.com', account: account, password: password, agreement: true)
expect(user.valid?).to be_truthy
end
it 'should not allow a blacklisted user to be created' do
user = User.new(email: 'foo@mvrht.com', account: account, password: password, agreement: true)
expect(user.valid?).to be_falsey
end
it 'should not allow a subdomain blacklisted user to be created' do
user = User.new(email: 'foo@mvrht.com.topdomain.tld', account: account, password: password, agreement: true)
expect(user.valid?).to be_falsey
end
end
describe '#confirmed?' do
it 'returns true when a confirmed_at is set' do
user = Fabricate.build(:user, confirmed_at: Time.now.utc)
expect(user.confirmed?).to be true
end
it 'returns false if a confirmed_at is nil' do
user = Fabricate.build(:user, confirmed_at: nil)
expect(user.confirmed?).to be false
end
end
describe '#confirm' do
it 'sets email to unconfirmed_email' do
user = Fabricate.build(:user, confirmed_at: Time.now.utc, unconfirmed_email: 'new-email@example.com')
user.confirm
expect(user.email).to eq 'new-email@example.com'
end
end
describe '#disable_two_factor!' do
it 'saves false for otp_required_for_login' do
user = Fabricate.build(:user, otp_required_for_login: true)
user.disable_two_factor!
expect(user.reload.otp_required_for_login).to be false
end
it 'saves cleared otp_backup_codes' do
user = Fabricate.build(:user, otp_backup_codes: %w(dummy dummy))
user.disable_two_factor!
expect(user.reload.otp_backup_codes.empty?).to be true
end
end
describe '#send_confirmation_instructions' do
around do |example|
queue_adapter = ActiveJob::Base.queue_adapter
example.run
ActiveJob::Base.queue_adapter = queue_adapter
end
it 'delivers confirmation instructions later' do
user = Fabricate(:user)
ActiveJob::Base.queue_adapter = :test
expect { user.send_confirmation_instructions }.to have_enqueued_job(ActionMailer::DeliveryJob)
end
end
describe 'settings' do
it 'is instance of Settings::ScopedSettings' do
user = Fabricate(:user)
expect(user.settings).to be_kind_of Settings::ScopedSettings
end
end
describe '#setting_default_privacy' do
it 'returns default privacy setting if user has configured' do
user = Fabricate(:user)
user.settings[:default_privacy] = 'unlisted'
expect(user.setting_default_privacy).to eq 'unlisted'
end
it "returns 'private' if user has not configured default privacy setting and account is locked" do
user = Fabricate(:user, account: Fabricate(:account, locked: true))
expect(user.setting_default_privacy).to eq 'private'
end
it "returns 'public' if user has not configured default privacy setting and account is not locked" do
user = Fabricate(:user, account: Fabricate(:account, locked: false))
expect(user.setting_default_privacy).to eq 'public'
end
end
describe 'whitelist' do
around(:each) do |example|
old_whitelist = Rails.configuration.x.email_whitelist
Rails.configuration.x.email_domains_whitelist = 'gab.space'
example.run
Rails.configuration.x.email_domains_whitelist = old_whitelist
end
it 'should not allow a user to be created unless they are whitelisted' do
user = User.new(email: 'foo@example.com', account: account, password: password, agreement: true)
expect(user.valid?).to be_falsey
end
it 'should allow a user to be created if they are whitelisted' do
user = User.new(email: 'foo@gab.space', account: account, password: password, agreement: true)
expect(user.valid?).to be_truthy
end
it 'should not allow a user with a whitelisted top domain as subdomain in their email address to be created' do
user = User.new(email: 'foo@gab.space.userdomain.com', account: account, password: password, agreement: true)
expect(user.valid?).to be_falsey
end
context do
around do |example|
old_blacklist = Rails.configuration.x.email_blacklist
example.run
Rails.configuration.x.email_domains_blacklist = old_blacklist
end
it 'should not allow a user to be created with a specific blacklisted subdomain even if the top domain is whitelisted' do
Rails.configuration.x.email_domains_blacklist = 'blacklisted.gab.space'
user = User.new(email: 'foo@blacklisted.gab.space', account: account, password: password)
expect(user.valid?).to be_falsey
end
end
end
it_behaves_like 'Settings-extended' do
def create!
User.create!(account: Fabricate(:account), email: 'foo@gab.space', password: 'abcd1234', agreement: true)
end
def fabricate
Fabricate(:user)
end
end
describe 'token_for_app' do
let(:user) { Fabricate(:user) }
let(:app) { Fabricate(:application, owner: user) }
it 'returns a token' do
expect(user.token_for_app(app)).to be_a(Doorkeeper::AccessToken)
end
it 'persists a token' do
t = user.token_for_app(app)
expect(user.token_for_app(app)).to eql(t)
end
it 'is nil if user does not own app' do
app.update!(owner: nil)
expect(user.token_for_app(app)).to be_nil
end
end
describe '#role' do
it 'returns admin for admin' do
user = User.new(admin: true)
expect(user.role).to eq 'admin'
end
it 'returns moderator for moderator' do
user = User.new(moderator: true)
expect(user.role).to eq 'moderator'
end
it 'returns user otherwise' do
user = User.new
expect(user.role).to eq 'user'
end
end
describe '#role?' do
it 'returns false when invalid role requested' do
user = User.new(admin: true)
expect(user.role?('disabled')).to be false
end
it 'returns true when exact role match' do
user = User.new
mod = User.new(moderator: true)
admin = User.new(admin: true)
expect(user.role?('user')).to be true
expect(mod.role?('moderator')).to be true
expect(admin.role?('admin')).to be true
end
it 'returns true when role higher than needed' do
mod = User.new(moderator: true)
admin = User.new(admin: true)
expect(mod.role?('user')).to be true
expect(admin.role?('user')).to be true
expect(admin.role?('moderator')).to be true
end
end
describe '#disable!' do
subject(:user) { Fabricate(:user, disabled: false, current_sign_in_at: current_sign_in_at, last_sign_in_at: nil) }
let(:current_sign_in_at) { Time.zone.now }
before do
user.disable!
end
it 'disables user' do
expect(user).to have_attributes(disabled: true, current_sign_in_at: nil, last_sign_in_at: current_sign_in_at)
end
end
describe '#disable!' do
subject(:user) { Fabricate(:user, disabled: false, current_sign_in_at: current_sign_in_at, last_sign_in_at: nil) }
let(:current_sign_in_at) { Time.zone.now }
before do
user.disable!
end
it 'disables user' do
expect(user).to have_attributes(disabled: true, current_sign_in_at: nil, last_sign_in_at: current_sign_in_at)
end
end
describe '#enable!' do
subject(:user) { Fabricate(:user, disabled: true) }
before do
user.enable!
end
it 'enables user' do
expect(user).to have_attributes(disabled: false)
end
end
describe '#confirm!' do
subject(:user) { Fabricate(:user, confirmed_at: confirmed_at) }
before do
ActionMailer::Base.deliveries.clear
user.confirm!
end
after { ActionMailer::Base.deliveries.clear }
context 'when user is new' do
let(:confirmed_at) { nil }
it 'confirms user' do
expect(user.confirmed_at).to be_present
end
it 'delivers mails' do
expect(ActionMailer::Base.deliveries.count).to eq 2
end
end
context 'when user is not new' do
let(:confirmed_at) { Time.zone.now }
it 'confirms user' do
expect(user.confirmed_at).to be_present
end
it 'does not deliver mail' do
expect(ActionMailer::Base.deliveries.count).to eq 0
end
end
end
describe '#promote!' do
subject(:user) { Fabricate(:user, admin: is_admin, moderator: is_moderator) }
before do
user.promote!
end
context 'when user is an admin' do
let(:is_admin) { true }
context 'when user is a moderator' do
let(:is_moderator) { true }
it 'changes moderator filed false' do
expect(user).to be_admin
expect(user).not_to be_moderator
end
end
context 'when user is not a moderator' do
let(:is_moderator) { false }
it 'does not change status' do
expect(user).to be_admin
expect(user).not_to be_moderator
end
end
end
context 'when user is not admin' do
let(:is_admin) { false }
context 'when user is a moderator' do
let(:is_moderator) { true }
it 'changes user into an admin' do
expect(user).to be_admin
expect(user).not_to be_moderator
end
end
context 'when user is not a moderator' do
let(:is_moderator) { false }
it 'changes user into a moderator' do
expect(user).not_to be_admin
expect(user).to be_moderator
end
end
end
end
describe '#demote!' do
subject(:user) { Fabricate(:user, admin: admin, moderator: moderator) }
before do
user.demote!
end
context 'when user is an admin' do
let(:admin) { true }
context 'when user is a moderator' do
let(:moderator) { true }
it 'changes user into a moderator' do
expect(user).not_to be_admin
expect(user).to be_moderator
end
end
context 'when user is not a moderator' do
let(:moderator) { false }
it 'changes user into a moderator' do
expect(user).not_to be_admin
expect(user).to be_moderator
end
end
end
context 'when user is not an admin' do
let(:admin) { false }
context 'when user is a moderator' do
let(:moderator) { true }
it 'changes user into a plain user' do
expect(user).not_to be_admin
expect(user).not_to be_moderator
end
end
context 'when user is not a moderator' do
let(:moderator) { false }
it 'does not change any fields' do
expect(user).not_to be_admin
expect(user).not_to be_moderator
end
end
end
end
describe '#active_for_authentication?' do
subject { user.active_for_authentication? }
let(:user) { Fabricate(:user, disabled: disabled, confirmed_at: confirmed_at) }
context 'when user is disabled' do
let(:disabled) { true }
context 'when user is confirmed' do
let(:confirmed_at) { Time.zone.now }
it { is_expected.to be true }
end
context 'when user is not confirmed' do
let(:confirmed_at) { nil }
it { is_expected.to be false }
end
end
context 'when user is not disabled' do
let(:disabled) { false }
context 'when user is confirmed' do
let(:confirmed_at) { Time.zone.now }
it { is_expected.to be true }
end
context 'when user is not confirmed' do
let(:confirmed_at) { nil }
it { is_expected.to be false }
end
end
end
end

View File

@@ -0,0 +1,16 @@
require 'rails_helper'
RSpec.describe Web::PushSubscription, type: :model do
let(:alerts) { { mention: true, reblog: false, follow: true, follow_request: false, favourite: true } }
let(:push_subscription) { Web::PushSubscription.new(data: { alerts: alerts }) }
describe '#pushable?' do
it 'obeys alert settings' do
expect(push_subscription.send(:pushable?, Notification.new(activity_type: 'Mention'))).to eq true
expect(push_subscription.send(:pushable?, Notification.new(activity_type: 'Status'))).to eq false
expect(push_subscription.send(:pushable?, Notification.new(activity_type: 'Follow'))).to eq true
expect(push_subscription.send(:pushable?, Notification.new(activity_type: 'FollowRequest'))).to eq false
expect(push_subscription.send(:pushable?, Notification.new(activity_type: 'Favourite'))).to eq true
end
end
end

View File

@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe Web::Setting, type: :model do
end