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