about summary refs log tree commit diff
path: root/spec/controllers/api/salmon_controller_spec.rb
blob: 8ce4913a52d57aa78911daefccd53658d9ce00f3 (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
56
57
58
59
60
61
62
63
64
65
require 'rails_helper'

RSpec.describe Api::SalmonController, type: :controller do
  render_views

  let(:account) { Fabricate(:user, account: Fabricate(:account, username: 'catsrgr8')).account }

  before do
    stub_request(:get, "https://quitter.no/.well-known/host-meta").to_return(request_fixture('.host-meta.txt'))
    stub_request(:get, "https://quitter.no/.well-known/webfinger?resource=acct:gargron@quitter.no").to_return(request_fixture('webfinger.txt'))
    stub_request(:get, "https://quitter.no/api/statuses/user_timeline/7477.atom").to_return(request_fixture('feed.txt'))
    stub_request(:get, "https://quitter.no/avatar/7477-300-20160211190340.png").to_return(request_fixture('avatar.txt'))
  end

  describe 'POST #update' do
    context 'with valid post data' do
      before do
        post :update, params: { id: account.id }, body: File.read(File.join(Rails.root, 'spec', 'fixtures', 'salmon', 'mention.xml'))
      end

      it 'contains XML in the request body' do
        expect(request.body.read).to be_a String
      end

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

      it 'creates remote account' do
        expect(Account.find_by(username: 'gargron', domain: 'quitter.no')).to_not be_nil
      end

      it 'creates status' do
        expect(Status.find_by(uri: 'tag:quitter.no,2016-03-20:noticeId=1276923:objectType=note')).to_not be_nil
      end

      it 'creates mention for target account' do
        expect(account.mentions.count).to eq 1
      end
    end

    context 'with empty post data' do
      before do
        post :update, params: { id: account.id }, body: ''
      end

      it 'returns http client error' do
        expect(response).to have_http_status(400)
      end
    end

    context 'with invalid post data' do
      before do
        service = double(call: false)
        allow(VerifySalmonService).to receive(:new).and_return(service)

        post :update, params: { id: account.id }, body: File.read(File.join(Rails.root, 'spec', 'fixtures', 'salmon', 'mention.xml'))
      end

      it 'returns http client error' do
        expect(response).to have_http_status(401)
      end
    end
  end
end