about summary refs log tree commit diff
path: root/spec/lib/stream_entry_finder_spec.rb
blob: 64e03c36af79d589c278f77d8da6bc69cc884465 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true

require 'rails_helper'

describe StreamEntryFinder do
  include RoutingHelper

  describe '#stream_entry' do
    context 'with a status url' do
      let(:status) { Fabricate(:status) }
      let(:url) { short_account_status_url(account_username: status.account.username, id: status.id) }
      subject { described_class.new(url) }

      it 'finds the stream entry' do
        expect(subject.stream_entry).to eq(status.stream_entry)
      end

      it 'raises an error if action is not :show' do
        recognized = Rails.application.routes.recognize_path(url)
        expect(recognized).to receive(:[]).with(:action).and_return(:create)
        expect(Rails.application.routes).to receive(:recognize_path).with(url).and_return(recognized)

        expect { subject.stream_entry }.to raise_error(ActiveRecord::RecordNotFound)
      end
    end

    context 'with a stream entry url' do
      let(:stream_entry) { Fabricate(:stream_entry) }
      let(:url) { account_stream_entry_url(stream_entry.account, stream_entry) }
      subject { described_class.new(url) }

      it 'finds the stream entry' do
        expect(subject.stream_entry).to eq(stream_entry)
      end
    end

    context 'with a plausible url' do
      let(:url) { 'https://example.com/users/test/updates/123/embed' }
      subject { described_class.new(url) }

      it 'raises an error' do
        expect { subject.stream_entry }.to raise_error(ActiveRecord::RecordNotFound)
      end
    end

    context 'with an unrecognized url' do
      let(:url) { 'https://example.com/about' }
      subject { described_class.new(url) }

      it 'raises an error' do
        expect { subject.stream_entry }.to raise_error(ActiveRecord::RecordNotFound)
      end
    end
  end
end