about summary refs log tree commit diff
path: root/spec/controllers/api/v1/mutes_controller_spec.rb
blob: 95ec17d6f8905bc5678e0cb06db01526473676d5 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'rails_helper'

RSpec.describe Api::V1::MutesController, type: :controller do
  render_views

  let(:user)   { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
  let(:scopes) { 'read:mutes' }
  let(:token)  { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }

  before { allow(controller).to receive(:doorkeeper_token) { token } }

  describe 'GET #index' do
    it 'limits according to limit parameter' do
      2.times.map { Fabricate(:mute, account: user.account) }
      get :index, params: { limit: 1 }
      expect(body_as_json.size).to eq 1
    end

    it 'queries mutes in range according to max_id' do
      mutes = 2.times.map { Fabricate(:mute, account: user.account) }

      get :index, params: { max_id: mutes[1] }

      expect(body_as_json.size).to eq 1
      expect(body_as_json[0][:id]).to eq mutes[0].target_account_id.to_s
    end

    it 'queries mutes in range according to since_id' do
      mutes = 2.times.map { Fabricate(:mute, account: user.account) }

      get :index, params: { since_id: mutes[0] }

      expect(body_as_json.size).to eq 1
      expect(body_as_json[0][:id]).to eq mutes[1].target_account_id.to_s
    end

    it 'sets pagination header for next path' do
      mutes = 2.times.map { Fabricate(:mute, account: user.account) }
      get :index, params: { limit: 1, since_id: mutes[0] }
      expect(response.headers['Link'].find_link(['rel', 'next']).href).to eq api_v1_mutes_url(limit: 1, max_id: mutes[1])
    end

    it 'sets pagination header for previous path' do
      mute = Fabricate(:mute, account: user.account)
      get :index
      expect(response.headers['Link'].find_link(['rel', 'prev']).href).to eq api_v1_mutes_url(since_id: mute)
    end

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

    context 'with wrong scopes' do
      let(:scopes) { 'write:mutes' }

      it 'returns http forbidden' do
        get :index
        expect(response).to have_http_status(403)
      end
    end
  end

  describe 'GET #details' do
    before do
      Fabricate(:mute, account: user.account, hide_notifications: false)
      get :details, params: { limit: 1 }
    end

    let(:mutes) { JSON.parse(response.body) }

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

    it 'returns one mute' do
      expect(mutes.size).to be(1)
    end

    it 'returns whether the mute hides notifications' do
      expect(mutes.first["hide_notifications"]).to be(false)
    end 
  end
end