From dceda1b073538ccc486592862654ced300b8d50f Mon Sep 17 00:00:00 2001 From: albert Date: Fri, 14 Jan 2011 15:50:17 -0500 Subject: [PATCH] added ip bans controller --- app/controllers/ip_bans_controller.rb | 21 ++++++++ app/models/ip_ban.rb | 9 +++- app/views/ip_bans/index.html.erb | 0 app/views/ip_bans/new.html.erb | 0 config/routes.rb | 1 + test/factories/ip_ban.rb | 2 +- test/functional/ip_bans_controller_test.rb | 56 ++++++++++++++++++++++ test/unit/ip_ban_test.rb | 2 +- 8 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 app/views/ip_bans/index.html.erb create mode 100644 app/views/ip_bans/new.html.erb create mode 100644 test/functional/ip_bans_controller_test.rb diff --git a/app/controllers/ip_bans_controller.rb b/app/controllers/ip_bans_controller.rb index 89b0577d8..580a6dec1 100644 --- a/app/controllers/ip_bans_controller.rb +++ b/app/controllers/ip_bans_controller.rb @@ -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 diff --git a/app/models/ip_ban.rb b/app/models/ip_ban.rb index c42018ae1..6548e8aa2 100644 --- a/app/models/ip_ban.rb +++ b/app/models/ip_ban.rb @@ -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 diff --git a/app/views/ip_bans/index.html.erb b/app/views/ip_bans/index.html.erb new file mode 100644 index 000000000..e69de29bb diff --git a/app/views/ip_bans/new.html.erb b/app/views/ip_bans/new.html.erb new file mode 100644 index 000000000..e69de29bb diff --git a/config/routes.rb b/config/routes.rb index 850aac47d..5a2593ae0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/test/factories/ip_ban.rb b/test/factories/ip_ban.rb index 4dc7109c4..a767bc09a 100644 --- a/test/factories/ip_ban.rb +++ b/test/factories/ip_ban.rb @@ -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 diff --git a/test/functional/ip_bans_controller_test.rb b/test/functional/ip_bans_controller_test.rb new file mode 100644 index 000000000..27513ddec --- /dev/null +++ b/test/functional/ip_bans_controller_test.rb @@ -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 diff --git a/test/unit/ip_ban_test.rb b/test/unit/ip_ban_test.rb index c130adf36..7ac376db1 100644 --- a/test/unit/ip_ban_test.rb +++ b/test/unit/ip_ban_test.rb @@ -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