added ip bans controller
This commit is contained in:
@@ -1,2 +1,23 @@
|
||||
class IpBansController < ApplicationController
|
||||
before_filter :admin_only
|
||||
|
||||
def new
|
||||
@ip_ban = IpBan.new
|
||||
end
|
||||
|
||||
def create
|
||||
@ip_ban = IpBan.create(params[:ip_ban])
|
||||
redirect_to ip_bans_path
|
||||
end
|
||||
|
||||
def index
|
||||
@search = IpBan.search(params[:search])
|
||||
@ip_bans = @search.paginate(:page => params[:page])
|
||||
end
|
||||
|
||||
def destroy
|
||||
@ip_ban = IpBan.find(params[:id])
|
||||
@ip_ban.destroy
|
||||
redirect_to ip_bans_path
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
class IpBan < ActiveRecord::Base
|
||||
belongs_to :creator, :class_name => "User"
|
||||
validates_presence_of :reason
|
||||
before_validation :initialize_creator, :on => :create
|
||||
validates_presence_of :reason, :creator
|
||||
validates_uniqueness_of :ip_addr
|
||||
|
||||
def self.is_banned?(ip_addr)
|
||||
exists?(["ip_addr = ?", ip_addr])
|
||||
end
|
||||
|
||||
def self.search(user_ids)
|
||||
def self.query(user_ids)
|
||||
comments = count_by_ip_addr("comments", user_ids, "creator_id", "ip_addr")
|
||||
notes = count_by_ip_addr("note_versions", user_ids, "updater_id", "updater_ip_addr")
|
||||
pools = count_by_ip_addr("pool_versions", user_ids, "updater_id", "updater_ip_addr")
|
||||
@@ -24,4 +25,8 @@ class IpBan < ActiveRecord::Base
|
||||
def self.count_by_ip_addr(table, user_ids, user_id_field = "user_id", ip_addr_field = "ip_addr")
|
||||
select_all_sql("SELECT #{ip_addr_field}, count(*) FROM #{table} WHERE #{user_id_field} IN (?) GROUP BY #{ip_addr_field} ORDER BY count(*) DESC", user_ids)
|
||||
end
|
||||
|
||||
def initialize_creator
|
||||
self.creator_id = CurrentUser.id
|
||||
end
|
||||
end
|
||||
|
||||
0
app/views/ip_bans/index.html.erb
Normal file
0
app/views/ip_bans/index.html.erb
Normal file
0
app/views/ip_bans/new.html.erb
Normal file
0
app/views/ip_bans/new.html.erb
Normal file
@@ -20,6 +20,7 @@ Danbooru::Application.routes.draw do
|
||||
resources :forum_topics
|
||||
resources :janitor_trials
|
||||
resources :jobs
|
||||
resources :ip_bans
|
||||
resources :notes
|
||||
resources :note_versions
|
||||
resources :pools do
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
Factory.define(:ip_ban) do |f|
|
||||
f.creator {|x| x.association(:user)}
|
||||
f.reason {Faker::Lorem.words}
|
||||
f.reason {Faker::Lorem.words.join(" ")}
|
||||
f.ip_addr "127.0.0.1"
|
||||
end
|
||||
|
||||
56
test/functional/ip_bans_controller_test.rb
Normal file
56
test/functional/ip_bans_controller_test.rb
Normal file
@@ -0,0 +1,56 @@
|
||||
require 'test_helper'
|
||||
|
||||
class IpBansControllerTest < ActionController::TestCase
|
||||
context "The ip bans controller" do
|
||||
setup do
|
||||
@admin = Factory.create(:admin_user)
|
||||
CurrentUser.user = @admin
|
||||
CurrentUser.ip_addr = "127.0.0.1"
|
||||
end
|
||||
|
||||
context "new action" do
|
||||
should "render" do
|
||||
get :new, {}, {:user_id => @admin.id}
|
||||
assert_response :success
|
||||
end
|
||||
end
|
||||
|
||||
context "create action" do
|
||||
should "create a new ip ban" do
|
||||
assert_difference("IpBan.count", 1) do
|
||||
post :create, {:ip_ban => {:ip_addr => "1.2.3.4", :reason => "xyz"}}, {:user_id => @admin.id}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "index action" do
|
||||
setup do
|
||||
Factory.create(:ip_ban)
|
||||
end
|
||||
|
||||
should "render" do
|
||||
get :index, {}, {:user_id => @admin.id}
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
context "with search parameters" do
|
||||
should "render" do
|
||||
get :index, {:search => {:ip_addr_equals => "1.2.3.4"}}, {:user_id => @admin.id}
|
||||
assert_response :success
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "destroy action" do
|
||||
setup do
|
||||
@ip_ban = Factory.create(:ip_ban)
|
||||
end
|
||||
|
||||
should "destroy an ip ban" do
|
||||
assert_difference("IpBan.count", -1) do
|
||||
post :destroy, {:id => @ip_ban.id}, {:user_id => @admin.id}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -22,7 +22,7 @@ class IpBanTest < ActiveSupport::TestCase
|
||||
should "be able to count any updates from a user, groupiny by IP address" do
|
||||
CurrentUser.scoped(@user, "1.2.3.4") do
|
||||
comment = Factory.create(:comment, :body => "aaa")
|
||||
counts = IpBan.search([comment.creator_id])
|
||||
counts = IpBan.query([comment.creator_id])
|
||||
assert_equal([{"ip_addr" => "1.2.3.4", "count" => "1"}], counts["comments"])
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user