123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- 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 '#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 '.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
|