forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement EmailBlackList (mastodon#5109)
* Implement BlacklistedEmailDomain * Use Faker::Internet.domain_name * Remove note column * Add frozen_string_literal comment * Delete unnecessary codes * Sort alphabetically * Change of wording * Rename BlacklistedEmailDomain to EmailDomainBlock
- Loading branch information
Showing
15 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
class EmailDomainBlocksController < BaseController | ||
before_action :set_email_domain_block, only: [:show, :destroy] | ||
|
||
def index | ||
@email_domain_blocks = EmailDomainBlock.page(params[:page]) | ||
end | ||
|
||
def new | ||
@email_domain_block = EmailDomainBlock.new | ||
end | ||
|
||
def create | ||
@email_domain_block = EmailDomainBlock.new(resource_params) | ||
|
||
if @email_domain_block.save | ||
redirect_to admin_email_domain_blocks_path, notice: I18n.t('admin.email_domain_blocks.created_msg') | ||
else | ||
render :new | ||
end | ||
end | ||
|
||
def destroy | ||
@email_domain_block.destroy | ||
redirect_to admin_email_domain_blocks_path, notice: I18n.t('admin.email_domain_blocks.destroyed_msg') | ||
end | ||
|
||
private | ||
|
||
def set_email_domain_block | ||
@email_domain_block = EmailDomainBlock.find(params[:id]) | ||
end | ||
|
||
def resource_params | ||
params.require(:email_domain_block).permit(:domain) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
# == Schema Information | ||
# | ||
# Table name: email_domain_blocks | ||
# | ||
# id :integer not null, primary key | ||
# domain :string not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
|
||
class EmailDomainBlock < ApplicationRecord | ||
def self.block?(email) | ||
domain = email.gsub(/.+@([^.]+)/, '\1') | ||
where(domain: domain).exists? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
app/views/admin/email_domain_blocks/_email_domain_block.html.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
%tr | ||
%td.domain | ||
%samp= email_domain_block.domain | ||
%td | ||
= table_link_to 'trash', t('admin.email_domain_blocks.delete'), admin_email_domain_block_path(email_domain_block), method: :delete |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
- content_for :page_title do | ||
= t('admin.email_domain_blocks.title') | ||
|
||
%table.table | ||
%thead | ||
%tr | ||
%th= t('admin.email_domain_blocks.domain') | ||
%th | ||
%tbody | ||
= render @email_domain_blocks | ||
|
||
= paginate @email_domain_blocks | ||
= link_to t('admin.email_domain_blocks.add_new'), new_admin_email_domain_block_path, class: 'button' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
- content_for :page_title do | ||
= t('.title') | ||
|
||
= simple_form_for @email_domain_block, url: admin_email_domain_blocks_path do |f| | ||
= render 'shared/error_messages', object: @email_domain_block | ||
|
||
= f.input :domain, placeholder: t('admin.email_domain_blocks.domain') | ||
|
||
.actions | ||
= f.button :button, t('.create'), type: :submit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CreateEmailDomainBlocks < ActiveRecord::Migration[5.1] | ||
def change | ||
create_table :email_domain_blocks do |t| | ||
t.string :domain, null: false | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
spec/controllers/admin/email_domain_blocks_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Admin::EmailDomainBlocksController, type: :controller do | ||
render_views | ||
|
||
before do | ||
sign_in Fabricate(:user, admin: true), scope: :user | ||
end | ||
|
||
describe 'GET #index' do | ||
around do |example| | ||
default_per_page = EmailDomainBlock.default_per_page | ||
EmailDomainBlock.paginates_per 1 | ||
example.run | ||
EmailDomainBlock.paginates_per default_per_page | ||
end | ||
|
||
it 'renders email blacks' do | ||
2.times { Fabricate(:email_domain_block) } | ||
|
||
get :index, params: { page: 2 } | ||
|
||
assigned = assigns(:email_domain_blocks) | ||
expect(assigned.count).to eq 1 | ||
expect(assigned.klass).to be EmailDomainBlock | ||
expect(response).to have_http_status(:success) | ||
end | ||
end | ||
|
||
describe 'GET #new' do | ||
it 'assigns a new email black' do | ||
get :new | ||
|
||
expect(assigns(:email_domain_block)).to be_instance_of(EmailDomainBlock) | ||
expect(response).to have_http_status(:success) | ||
end | ||
end | ||
|
||
describe 'POST #create' do | ||
it 'blocks the domain when succeeded to save' do | ||
post :create, params: { email_domain_block: { domain: 'example.com'} } | ||
|
||
expect(flash[:notice]).to eq I18n.t('admin.email_domain_blocks.created_msg') | ||
expect(response).to redirect_to(admin_email_domain_blocks_path) | ||
end | ||
end | ||
|
||
describe 'DELETE #destroy' do | ||
it 'unblocks the domain' do | ||
email_domain_block = Fabricate(:email_domain_block) | ||
delete :destroy, params: { id: email_domain_block.id } | ||
|
||
expect(flash[:notice]).to eq I18n.t('admin.email_domain_blocks.destroyed_msg') | ||
expect(response).to redirect_to(admin_email_domain_blocks_path) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Fabricator(:email_domain_block) do | ||
domain { sequence(:domain) { |i| "#{i}#{Faker::Internet.domain_name}" } } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe EmailDomainBlock, type: :model do | ||
describe 'validations' do | ||
it 'has a valid fabricator' do | ||
email_domain_block = Fabricate.build(:email_domain_block) | ||
expect(email_domain_block).to be_valid | ||
end | ||
end | ||
|
||
describe 'block?' do | ||
it 'returns true if the domain is registed' do | ||
Fabricate(:email_domain_block, domain: 'example.com') | ||
expect(EmailDomainBlock.block?('nyarn@example.com')).to eq true | ||
end | ||
it 'returns true if the domain is not registed' do | ||
Fabricate(:email_domain_block, domain: 'domain') | ||
expect(EmailDomainBlock.block?('example')).to eq false | ||
end | ||
end | ||
end |