~cytrogen/masto-fe

ref: 5ea3e8e765e0a0ae8e60c3e4e0b03cd3604ffdf1 masto-fe/spec/controllers/well_known/webfinger_controller_spec.rb -rw-r--r-- 5.9 KiB
5ea3e8e7 — github-actions[bot] New Crowdin Translations (automated) (#27144) 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# frozen_string_literal: true

require 'rails_helper'

describe WellKnown::WebfingerController do
  include RoutingHelper

  render_views

  describe 'GET #show' do
    subject(:perform_show!) do
      get :show, params: { resource: resource }, format: :json
    end

    let(:alternate_domains) { [] }
    let(:alice) { Fabricate(:account, username: 'alice') }
    let(:resource) { nil }

    around(:each) do |example|
      tmp = Rails.configuration.x.alternate_domains
      Rails.configuration.x.alternate_domains = alternate_domains
      example.run
      Rails.configuration.x.alternate_domains = tmp
    end

    shared_examples 'a successful response' do
      it 'returns http success' do
        expect(response).to have_http_status(200)
      end

      it 'does not set a Vary header' do
        expect(response.headers['Vary']).to be_nil
      end

      it 'returns application/jrd+json' do
        expect(response.media_type).to eq 'application/jrd+json'
      end

      it 'returns links for the account' do
        json = body_as_json
        expect(json[:subject]).to eq 'acct:alice@cb6e6126.ngrok.io'
        expect(json[:aliases]).to include('https://cb6e6126.ngrok.io/@alice', 'https://cb6e6126.ngrok.io/users/alice')
      end
    end

    context 'when an account exists' do
      let(:resource) { alice.to_webfinger_s }

      before do
        perform_show!
      end

      it_behaves_like 'a successful response'
    end

    context 'when an account is temporarily suspended' do
      let(:resource) { alice.to_webfinger_s }

      before do
        alice.suspend!
        perform_show!
      end

      it_behaves_like 'a successful response'
    end

    context 'when an account is permanently suspended or deleted' do
      let(:resource) { alice.to_webfinger_s }

      before do
        alice.suspend!
        alice.deletion_request.destroy
        perform_show!
      end

      it 'returns http gone' do
        expect(response).to have_http_status(410)
      end
    end

    context 'when an account is not found' do
      let(:resource) { 'acct:not@existing.com' }

      before do
        perform_show!
      end

      it 'returns http not found' do
        expect(response).to have_http_status(404)
      end
    end

    context 'with an alternate domain' do
      let(:alternate_domains) { ['foo.org'] }

      before do
        perform_show!
      end

      context 'when an account exists' do
        let(:resource) do
          username, = alice.to_webfinger_s.split('@')
          "#{username}@foo.org"
        end

        it_behaves_like 'a successful response'
      end

      context 'when the domain is wrong' do
        let(:resource) do
          username, = alice.to_webfinger_s.split('@')
          "#{username}@bar.org"
        end

        it 'returns http not found' do
          expect(response).to have_http_status(404)
        end
      end
    end

    context 'when the old name scheme is used to query the instance actor' do
      let(:resource) do
        "#{Rails.configuration.x.local_domain}@#{Rails.configuration.x.local_domain}"
      end

      before do
        perform_show!
      end

      it 'returns http success' do
        expect(response).to have_http_status(200)
      end

      it 'does not set a Vary header' do
        expect(response.headers['Vary']).to be_nil
      end

      it 'returns application/jrd+json' do
        expect(response.media_type).to eq 'application/jrd+json'
      end

      it 'returns links for the internal account' do
        json = body_as_json
        expect(json[:subject]).to eq 'acct:mastodon.internal@cb6e6126.ngrok.io'
        expect(json[:aliases]).to eq ['https://cb6e6126.ngrok.io/actor']
      end
    end

    context 'with no resource parameter' do
      let(:resource) { nil }

      before do
        perform_show!
      end

      it 'returns http bad request' do
        expect(response).to have_http_status(400)
      end
    end

    context 'with a nonsense parameter' do
      let(:resource) { 'df/:dfkj' }

      before do
        perform_show!
      end

      it 'returns http bad request' do
        expect(response).to have_http_status(400)
      end
    end

    context 'when an account has an avatar' do
      let(:alice) { Fabricate(:account, username: 'alice', avatar: attachment_fixture('attachment.jpg')) }
      let(:resource) { alice.to_webfinger_s }

      it 'returns avatar in response' do
        perform_show!

        avatar_link = get_avatar_link(body_as_json)
        expect(avatar_link).to_not be_nil
        expect(avatar_link[:type]).to eq alice.avatar.content_type
        expect(avatar_link[:href]).to eq full_asset_url(alice.avatar)
      end

      context 'with limited federation mode' do
        before do
          allow(Rails.configuration.x).to receive(:limited_federation_mode).and_return(true)
        end

        it 'does not return avatar in response' do
          perform_show!

          avatar_link = get_avatar_link(body_as_json)
          expect(avatar_link).to be_nil
        end
      end

      context 'when enabling DISALLOW_UNAUTHENTICATED_API_ACCESS' do
        around do |example|
          ClimateControl.modify DISALLOW_UNAUTHENTICATED_API_ACCESS: 'true' do
            example.run
          end
        end

        it 'does not return avatar in response' do
          perform_show!

          avatar_link = get_avatar_link(body_as_json)
          expect(avatar_link).to be_nil
        end
      end
    end

    context 'when an account does not have an avatar' do
      let(:alice) { Fabricate(:account, username: 'alice', avatar: nil) }
      let(:resource) { alice.to_webfinger_s }

      before do
        perform_show!
      end

      it 'does not return avatar in response' do
        avatar_link = get_avatar_link(body_as_json)
        expect(avatar_link).to be_nil
      end
    end
  end

  private

  def get_avatar_link(json)
    json[:links].find { |link| link[:rel] == 'http://webfinger.net/rel/avatar' }
  end
end