~cytrogen/masto-fe

ref: ae2790d84fcaa38fd205705157743ecb963ce8ae masto-fe/spec/requests/api/v1/admin/email_domain_blocks_spec.rb -rw-r--r-- 6.1 KiB
ae2790d8 — renovate[bot] Update babel monorepo to v7.22.5 (#25405) 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
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Email Domain Blocks' do
  let(:role)    { UserRole.find_by(name: 'Admin') }
  let(:user)    { Fabricate(:user, role: role) }
  let(:token)   { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  let(:account) { Fabricate(:account) }
  let(:scopes)  { 'admin:read:email_domain_blocks admin:write:email_domain_blocks' }
  let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }

  shared_examples 'forbidden for wrong scope' do |wrong_scope|
    let(:scopes) { wrong_scope }

    it 'returns http forbidden' do
      subject

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

  shared_examples 'forbidden for wrong role' do |wrong_role|
    let(:role) { UserRole.find_by(name: wrong_role) }

    it 'returns http forbidden' do
      subject

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

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

    let(:params) { {} }

    it_behaves_like 'forbidden for wrong scope', 'read:statuses'
    it_behaves_like 'forbidden for wrong role', ''
    it_behaves_like 'forbidden for wrong role', 'Moderator'

    it 'returns http success' do
      subject

      expect(response).to have_http_status(200)
    end

    context 'when there is no email domain block' do
      it 'returns an empty list' do
        subject

        expect(body_as_json).to be_empty
      end
    end

    context 'when there are email domain blocks' do
      let!(:email_domain_blocks)  { Fabricate.times(5, :email_domain_block) }
      let(:blocked_email_domains) { email_domain_blocks.pluck(:domain) }

      it 'return the correct blocked email domains' do
        subject

        expect(body_as_json.pluck(:domain)).to match_array(blocked_email_domains)
      end

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

        it 'returns only the requested number of email domain blocks' do
          subject

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

      context 'with since_id param' do
        let(:params) { { since_id: email_domain_blocks[1].id } }

        it 'returns only the email domain blocks after since_id' do
          subject

          email_domain_blocks_ids = email_domain_blocks.pluck(:id).map(&:to_s)

          expect(body_as_json.pluck(:id)).to match_array(email_domain_blocks_ids[2..])
        end
      end

      context 'with max_id param' do
        let(:params) { { max_id: email_domain_blocks[3].id } }

        it 'returns only the email domain blocks before max_id' do
          subject

          email_domain_blocks_ids = email_domain_blocks.pluck(:id).map(&:to_s)

          expect(body_as_json.pluck(:id)).to match_array(email_domain_blocks_ids[..2])
        end
      end
    end
  end

  describe 'GET /api/v1/admin/email_domain_blocks/:id' do
    subject do
      get "/api/v1/admin/email_domain_blocks/#{email_domain_block.id}", headers: headers
    end

    let!(:email_domain_block) { Fabricate(:email_domain_block) }

    it_behaves_like 'forbidden for wrong scope', 'read:statuses'
    it_behaves_like 'forbidden for wrong role', ''
    it_behaves_like 'forbidden for wrong role', 'Moderator'

    context 'when email domain block exists' do
      it 'returns http success' do
        subject

        expect(response).to have_http_status(200)
      end

      it 'returns the correct blocked domain' do
        subject

        expect(body_as_json[:domain]).to eq(email_domain_block.domain)
      end
    end

    context 'when email domain block does not exist' do
      it 'returns http not found' do
        get '/api/v1/admin/email_domain_blocks/-1', headers: headers

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

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

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

    it_behaves_like 'forbidden for wrong scope', 'read:statuses'
    it_behaves_like 'forbidden for wrong role', ''
    it_behaves_like 'forbidden for wrong role', 'Moderator'

    it 'returns http success' do
      subject

      expect(response).to have_http_status(200)
    end

    it 'returns the correct blocked email domain' do
      subject

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

    context 'when domain param is not provided' do
      let(:params) { { domain: '' } }

      it 'returns http unprocessable entity' do
        subject

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

    context 'when provided domain name has an invalid character' do
      let(:params) { { domain: 'do\uD800.com' } }

      it 'returns http unprocessable entity' do
        subject

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

    context 'when provided domain is already blocked' do
      before do
        EmailDomainBlock.create(params)
      end

      it 'returns http unprocessable entity' do
        subject

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

  describe 'DELETE /api/v1/admin/email_domain_blocks' do
    subject do
      delete "/api/v1/admin/email_domain_blocks/#{email_domain_block.id}", headers: headers
    end

    let!(:email_domain_block) { Fabricate(:email_domain_block) }

    it_behaves_like 'forbidden for wrong scope', 'read:statuses'
    it_behaves_like 'forbidden for wrong role', ''
    it_behaves_like 'forbidden for wrong role', 'Moderator'

    it 'returns http success' do
      subject

      expect(response).to have_http_status(200)
    end

    it 'returns an empty body' do
      subject

      expect(body_as_json).to be_empty
    end

    it 'deletes email domain block' do
      subject

      expect(EmailDomainBlock.find_by(id: email_domain_block.id)).to be_nil
    end

    context 'when email domain block does not exist' do
      it 'returns http not found' do
        delete '/api/v1/admin/email_domain_blocks/-1', headers: headers

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