~cytrogen/masto-fe

ref: 48ef7d0f067943b5e608eaee7bba4e277bea83db masto-fe/spec/requests/api/v1/domain_blocks_spec.rb -rw-r--r-- 2.9 KiB
48ef7d0f — renovate[bot] Update libretranslate/libretranslate Docker tag to v1.3.12 (#27214) 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
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Domain blocks' do
  let(:user)    { Fabricate(:user) }
  let(:token)   { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  let(:scopes)  { 'read:blocks write:blocks' }
  let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }

  describe 'GET /api/v1/domain_blocks' do
    subject do
      get '/api/v1/domain_blocks', headers: headers, params: params
    end

    let(:blocked_domains) { ['example.com', 'example.net', 'example.org', 'example.com.br'] }
    let(:params) { {} }

    before do
      blocked_domains.each { |domain| user.account.block_domain!(domain) }
    end

    it_behaves_like 'forbidden for wrong scope', 'write:blocks'

    it 'returns http success' do
      subject

      expect(response).to have_http_status(200)
    end

    it 'returns the domains blocked by the requesting user' do
      subject

      expect(body_as_json).to match_array(blocked_domains)
    end

    context 'with limit param' do
      let(:params) { { limit: 2 } }

      it 'returns only the requested number of blocked domains' do
        subject

        expect(body_as_json.size).to eq(params[:limit])
      end
    end
  end

  describe 'POST /api/v1/domain_blocks' do
    subject do
      post '/api/v1/domain_blocks', headers: headers, params: params
    end

    let(:params) { { domain: 'example.com' } }

    it_behaves_like 'forbidden for wrong scope', 'read read:blocks'

    it 'returns http success' do
      subject

      expect(response).to have_http_status(200)
    end

    it 'creates a domain block' do
      subject

      expect(user.account.domain_blocking?(params[:domain])).to be(true)
    end

    context 'when no domain name is given' do
      let(:params) { { domain: '' } }

      it 'returns http unprocessable entity' do
        subject

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

    context 'when the given domain name is invalid' do
      let(:params) { { domain: 'example com' } }

      it 'returns unprocessable entity' do
        subject

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

  describe 'DELETE /api/v1/domain_blocks' do
    subject do
      delete '/api/v1/domain_blocks/', headers: headers, params: params
    end

    let(:params) { { domain: 'example.com' } }

    before do
      user.account.block_domain!('example.com')
    end

    it_behaves_like 'forbidden for wrong scope', 'read read:blocks'

    it 'returns http success' do
      subject

      expect(response).to have_http_status(200)
    end

    it 'deletes the specified domain block' do
      subject

      expect(user.account.domain_blocking?('example.com')).to be(false)
    end

    context 'when the given domain name is not blocked' do
      let(:params) { { domain: 'example.org' } }

      it 'returns http success' do
        subject

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