about summary refs log tree commit diff
path: root/spec/controllers/api/web/embeds_controller_spec.rb
blob: e03f5a37142028fb1dd6f5240d9f441347f47889 (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
# frozen_string_literal: true

require 'rails_helper'

describe Api::Web::EmbedsController do
  render_views

  let(:user) { Fabricate(:user) }

  before { sign_in user }

  describe 'POST #create' do
    subject(:response) { post :create, params: { url: url } }

    subject(:body) { JSON.parse(response.body, symbolize_names: true) }

    context 'when successfully finds status' do
      let(:status) { Fabricate(:status) }
      let(:url) { "http://#{Rails.configuration.x.web_domain}/@#{status.account.username}/#{status.id}" }

      it 'returns a right response' do
        expect(response).to have_http_status 200
        expect(body[:author_name]).to eq status.account.username
      end
    end

    context 'when fails to find status' do
      let(:url) { 'https://host.test/oembed.html' }
      let(:service_instance) { double('fetch_oembed_service') }

      before do
        allow(FetchOEmbedService).to receive(:new) { service_instance }
        allow(service_instance).to receive(:call) { call_result }
      end

      context 'when successfully fetching oembed' do
        let(:call_result) { { result: :ok } }

        it 'returns a right response' do
          expect(response).to have_http_status 200
          expect(body[:result]).to eq 'ok'
        end
      end

      context 'when fails to fetch oembed' do
        let(:call_result) { nil }

        it 'returns a right response' do
          expect(response).to have_http_status 404
        end
      end
    end
  end
end