2023-03-05 01:12:54 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
RSpec.describe 'Email Domain Blocks' do
|
2023-05-22 20:28:11 +09:00
|
|
|
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) }
|
2023-03-05 01:12:54 +09:00
|
|
|
let(:account) { Fabricate(:account) }
|
2023-05-22 20:28:11 +09:00
|
|
|
let(:scopes) { 'admin:read:email_domain_blocks admin:write:email_domain_blocks' }
|
2023-06-09 21:00:14 +09:00
|
|
|
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
|
2023-03-05 01:12:54 +09:00
|
|
|
|
2023-05-22 20:28:11 +09:00
|
|
|
shared_examples 'forbidden for wrong scope' do |wrong_scope|
|
|
|
|
let(:scopes) { wrong_scope }
|
|
|
|
|
|
|
|
it 'returns http forbidden' do
|
2023-06-09 21:00:14 +09:00
|
|
|
subject
|
|
|
|
|
2023-05-22 20:28:11 +09:00
|
|
|
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
|
2023-06-09 21:00:14 +09:00
|
|
|
subject
|
|
|
|
|
2023-05-22 20:28:11 +09:00
|
|
|
expect(response).to have_http_status(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
describe 'GET /api/v1/admin/email_domain_blocks' do
|
|
|
|
subject do
|
|
|
|
get '/api/v1/admin/email_domain_blocks', headers: headers, params: params
|
2023-05-22 20:28:11 +09:00
|
|
|
end
|
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
let(:params) { {} }
|
2023-05-22 20:28:11 +09:00
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
it_behaves_like 'forbidden for wrong scope', 'read:statuses'
|
|
|
|
it_behaves_like 'forbidden for wrong role', ''
|
|
|
|
it_behaves_like 'forbidden for wrong role', 'Moderator'
|
2023-05-22 20:28:11 +09:00
|
|
|
|
2023-03-05 01:12:54 +09:00
|
|
|
it 'returns http success' do
|
2023-06-09 21:00:14 +09:00
|
|
|
subject
|
2023-03-05 01:12:54 +09:00
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
2023-05-22 20:28:11 +09:00
|
|
|
|
|
|
|
context 'when there is no email domain block' do
|
|
|
|
it 'returns an empty list' do
|
2023-06-09 21:00:14 +09:00
|
|
|
subject
|
2023-05-22 20:28:11 +09:00
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
expect(body_as_json).to be_empty
|
2023-05-22 20:28:11 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are email domain blocks' do
|
2023-06-09 21:00:14 +09:00
|
|
|
let!(:email_domain_blocks) { Fabricate.times(5, :email_domain_block) }
|
2023-05-22 20:28:11 +09:00
|
|
|
let(:blocked_email_domains) { email_domain_blocks.pluck(:domain) }
|
|
|
|
|
|
|
|
it 'return the correct blocked email domains' do
|
2023-06-09 21:00:14 +09:00
|
|
|
subject
|
2023-05-22 20:28:11 +09:00
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
expect(body_as_json.pluck(:domain)).to match_array(blocked_email_domains)
|
2023-05-22 20:28:11 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with limit param' do
|
|
|
|
let(:params) { { limit: 2 } }
|
|
|
|
|
|
|
|
it 'returns only the requested number of email domain blocks' do
|
2023-06-09 21:00:14 +09:00
|
|
|
subject
|
2023-05-22 20:28:11 +09:00
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
expect(body_as_json.size).to eq(params[:limit])
|
2023-05-22 20:28:11 +09:00
|
|
|
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
|
2023-06-09 21:00:14 +09:00
|
|
|
subject
|
2023-05-22 20:28:11 +09:00
|
|
|
|
|
|
|
email_domain_blocks_ids = email_domain_blocks.pluck(:id).map(&:to_s)
|
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
expect(body_as_json.pluck(:id)).to match_array(email_domain_blocks_ids[2..])
|
2023-05-22 20:28:11 +09:00
|
|
|
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
|
2023-06-09 21:00:14 +09:00
|
|
|
subject
|
2023-05-22 20:28:11 +09:00
|
|
|
|
|
|
|
email_domain_blocks_ids = email_domain_blocks.pluck(:id).map(&:to_s)
|
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
expect(body_as_json.pluck(:id)).to match_array(email_domain_blocks_ids[..2])
|
2023-05-22 20:28:11 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
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
|
2023-05-22 20:28:11 +09:00
|
|
|
end
|
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
let!(:email_domain_block) { Fabricate(:email_domain_block) }
|
2023-05-22 20:28:11 +09:00
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
it_behaves_like 'forbidden for wrong scope', 'read:statuses'
|
|
|
|
it_behaves_like 'forbidden for wrong role', ''
|
|
|
|
it_behaves_like 'forbidden for wrong role', 'Moderator'
|
2023-05-22 20:28:11 +09:00
|
|
|
|
|
|
|
context 'when email domain block exists' do
|
|
|
|
it 'returns http success' do
|
2023-06-09 21:00:14 +09:00
|
|
|
subject
|
2023-05-22 20:28:11 +09:00
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the correct blocked domain' do
|
2023-06-09 21:00:14 +09:00
|
|
|
subject
|
2023-05-22 20:28:11 +09:00
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
expect(body_as_json[:domain]).to eq(email_domain_block.domain)
|
2023-05-22 20:28:11 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when email domain block does not exist' do
|
|
|
|
it 'returns http not found' do
|
2023-06-09 21:00:14 +09:00
|
|
|
get '/api/v1/admin/email_domain_blocks/-1', headers: headers
|
2023-05-22 20:28:11 +09:00
|
|
|
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
describe 'POST /api/v1/admin/email_domain_blocks' do
|
|
|
|
subject do
|
|
|
|
post '/api/v1/admin/email_domain_blocks', headers: headers, params: params
|
2023-05-22 20:28:11 +09:00
|
|
|
end
|
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
let(:params) { { domain: 'example.com' } }
|
2023-05-22 20:28:11 +09:00
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
it_behaves_like 'forbidden for wrong scope', 'read:statuses'
|
|
|
|
it_behaves_like 'forbidden for wrong role', ''
|
|
|
|
it_behaves_like 'forbidden for wrong role', 'Moderator'
|
2023-05-22 20:28:11 +09:00
|
|
|
|
|
|
|
it 'returns http success' do
|
2023-06-09 21:00:14 +09:00
|
|
|
subject
|
2023-05-22 20:28:11 +09:00
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the correct blocked email domain' do
|
2023-06-09 21:00:14 +09:00
|
|
|
subject
|
2023-05-22 20:28:11 +09:00
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
expect(body_as_json[:domain]).to eq(params[:domain])
|
2023-05-22 20:28:11 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when domain param is not provided' do
|
|
|
|
let(:params) { { domain: '' } }
|
|
|
|
|
|
|
|
it 'returns http unprocessable entity' do
|
2023-06-09 21:00:14 +09:00
|
|
|
subject
|
2023-05-22 20:28:11 +09:00
|
|
|
|
|
|
|
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
|
2023-06-09 21:00:14 +09:00
|
|
|
subject
|
2023-05-22 20:28:11 +09:00
|
|
|
|
|
|
|
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
|
2023-06-09 21:00:14 +09:00
|
|
|
subject
|
2023-05-22 20:28:11 +09:00
|
|
|
|
|
|
|
expect(response).to have_http_status(422)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
describe 'DELETE /api/v1/admin/email_domain_blocks' do
|
|
|
|
subject do
|
|
|
|
delete "/api/v1/admin/email_domain_blocks/#{email_domain_block.id}", headers: headers
|
2023-05-22 20:28:11 +09:00
|
|
|
end
|
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
let!(:email_domain_block) { Fabricate(:email_domain_block) }
|
2023-05-22 20:28:11 +09:00
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
it_behaves_like 'forbidden for wrong scope', 'read:statuses'
|
|
|
|
it_behaves_like 'forbidden for wrong role', ''
|
|
|
|
it_behaves_like 'forbidden for wrong role', 'Moderator'
|
2023-05-22 20:28:11 +09:00
|
|
|
|
|
|
|
it 'returns http success' do
|
2023-06-09 21:00:14 +09:00
|
|
|
subject
|
2023-05-22 20:28:11 +09:00
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an empty body' do
|
2023-06-09 21:00:14 +09:00
|
|
|
subject
|
2023-05-22 20:28:11 +09:00
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
expect(body_as_json).to be_empty
|
2023-05-22 20:28:11 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'deletes email domain block' do
|
2023-06-09 21:00:14 +09:00
|
|
|
subject
|
2023-05-22 20:28:11 +09:00
|
|
|
|
2023-06-09 21:00:14 +09:00
|
|
|
expect(EmailDomainBlock.find_by(id: email_domain_block.id)).to be_nil
|
2023-05-22 20:28:11 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when email domain block does not exist' do
|
|
|
|
it 'returns http not found' do
|
2023-06-09 21:00:14 +09:00
|
|
|
delete '/api/v1/admin/email_domain_blocks/-1', headers: headers
|
2023-05-22 20:28:11 +09:00
|
|
|
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
end
|
|
|
|
end
|
2023-03-05 01:12:54 +09:00
|
|
|
end
|
|
|
|
end
|