~cytrogen/masto-fe

ref: d9adda1a991dde52949489208c948b0502af8a3f masto-fe/spec/requests/api/web/embeds_spec.rb -rw-r--r-- 4.2 KiB
d9adda1a — Claire Merge commit '71db616fed817893d0efa363f0e7dbfcf23866a0' into glitch-soc/merge-upstream 2 years ago
                                                                                
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe '/api/web/embed' do
  subject { get "/api/web/embeds/#{id}", headers: headers }

  context 'when accessed anonymously' do
    let(:headers) { {} }

    context 'when the requested status is local' do
      let(:id) { status.id }

      context 'when the requested status is public' do
        let(:status) { Fabricate(:status, visibility: :public) }

        it 'returns JSON with an html attribute' do
          subject

          expect(response).to have_http_status(200)
          expect(body_as_json[:html]).to be_present
        end
      end

      context 'when the requested status is private' do
        let(:status) { Fabricate(:status, visibility: :private) }

        it 'returns http not found' do
          subject

          expect(response).to have_http_status(404)
        end
      end
    end

    context 'when the requested status is remote' do
      let(:remote_account) { Fabricate(:account, domain: 'example.com') }
      let(:status)         { Fabricate(:status, visibility: :public, account: remote_account, url: 'https://example.com/statuses/1') }
      let(:id)             { status.id }

      it 'returns http not found' do
        subject

        expect(response).to have_http_status(404)
      end
    end

    context 'when the requested status does not exist' do
      let(:id) { -1 }

      it 'returns http not found' do
        subject

        expect(response).to have_http_status(404)
      end
    end
  end

  context 'with an API token' do
    let(:user)    { Fabricate(:user) }
    let(:token)   { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read') }
    let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }

    context 'when the requested status is local' do
      let(:id) { status.id }

      context 'when the requested status is public' do
        let(:status) { Fabricate(:status, visibility: :public) }

        it 'returns JSON with an html attribute' do
          subject

          expect(response).to have_http_status(200)
          expect(body_as_json[:html]).to be_present
        end

        context 'when the requesting user is blocked' do
          before do
            status.account.block!(user.account)
          end

          it 'returns http not found' do
            subject

            expect(response).to have_http_status(404)
          end
        end
      end

      context 'when the requested status is private' do
        let(:status) { Fabricate(:status, visibility: :private) }

        before do
          user.account.follow!(status.account)
        end

        it 'returns http not found' do
          subject

          expect(response).to have_http_status(404)
        end
      end
    end

    context 'when the requested status is remote' do
      let(:remote_account) { Fabricate(:account, domain: 'example.com') }
      let(:status)         { Fabricate(:status, visibility: :public, account: remote_account, url: 'https://example.com/statuses/1') }
      let(:id)             { status.id }

      let(:service_instance) { instance_double(FetchOEmbedService) }

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

      context 'when the requesting user is blocked' do
        before do
          status.account.block!(user.account)
        end

        it 'returns http not found' do
          subject

          expect(response).to have_http_status(404)
        end
      end

      context 'when successfully fetching OEmbed' do
        let(:call_result) { { html: 'ok' } }

        it 'returns JSON with an html attribute' do
          subject

          expect(response).to have_http_status(200)
          expect(body_as_json[:html]).to be_present
        end
      end

      context 'when failing to fetch OEmbed' do
        let(:call_result) { nil }

        it 'returns http not found' do
          subject

          expect(response).to have_http_status(404)
        end
      end
    end

    context 'when the requested status does not exist' do
      let(:id) { -1 }

      it 'returns http not found' do
        subject

        expect(response).to have_http_status(404)
      end
    end
  end
end