account_spec.rb 22.02 KiB
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'
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
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 'https://remote.test/invalid_avatar' expect(account.header_remote_url).to eq expectation.header_remote_url expect(account.avatar_file_name).to eq nil expect(account.header_file_name).to eq nil end end end describe '#possibly_stale?' do let(:account) { Fabricate(:account, last_webfingered_at: last_webfingered_at) } context 'last_webfingered_at is nil' do