From 3372a93ef832b01f048b405caeca56ea5884f4c6 Mon Sep 17 00:00:00 2001 From: albert Date: Thu, 27 Oct 2011 19:35:43 -0400 Subject: [PATCH] ban fixes --- app/controllers/bans_controller.rb | 10 +++++----- app/models/ban.rb | 5 +++++ test/factories/ban.rb | 2 -- test/functional/bans_controller_test.rb | 23 ++++++++++++----------- test/unit/post_disapproval_test.rb | 8 ++++---- test/unit/upload_test.rb | 8 ++++---- 6 files changed, 30 insertions(+), 26 deletions(-) diff --git a/app/controllers/bans_controller.rb b/app/controllers/bans_controller.rb index 255c3738c..862df3e98 100644 --- a/app/controllers/bans_controller.rb +++ b/app/controllers/bans_controller.rb @@ -19,13 +19,12 @@ class BansController < ApplicationController end def create - @ban = Ban.new(params[:ban]) - @ban.banner_id = CurrentUser.id + @ban = Ban.create(params[:ban]) - if @ban.save - redirect_to ban_path(@ban), :notice => "Ban created" - else + if @ban.errors.any? render :action => "new" + else + redirect_to ban_path(@ban), :notice => "Ban created" end end @@ -34,6 +33,7 @@ class BansController < ApplicationController if @ban.update_attributes(params[:ban]) redirect_to ban_path(@ban), :notice => "Ban updated" else + puts @ban.errors.full_messages render :action => "edit" end end diff --git a/app/models/ban.rb b/app/models/ban.rb index 93a5417d5..e9da57f77 100644 --- a/app/models/ban.rb +++ b/app/models/ban.rb @@ -5,11 +5,16 @@ class Ban < ActiveRecord::Base attr_accessible :reason, :duration, :user_id, :user_name validate :user_is_inferior validates_presence_of :user_id, :reason, :duration + before_validation :initialize_banner_id, :on => :create def self.is_banned?(user) exists?(["user_id = ? AND expires_at > ?", user.id, Time.now]) end + def initialize_banner_id + self.banner_id = CurrentUser.id + end + def user_is_inferior if user if user.is_admin? diff --git a/test/factories/ban.rb b/test/factories/ban.rb index 9ef20b80e..63e69c289 100644 --- a/test/factories/ban.rb +++ b/test/factories/ban.rb @@ -1,6 +1,4 @@ Factory.define(:ban) do |f| - f.user {|x| x.association(:user)} - f.banner {|x| x.association(:admin_user)} f.reason {Faker::Lorem.words.join(" ")} f.duration 60 end diff --git a/test/functional/bans_controller_test.rb b/test/functional/bans_controller_test.rb index 1ac314e0d..d58a60a5f 100644 --- a/test/functional/bans_controller_test.rb +++ b/test/functional/bans_controller_test.rb @@ -3,10 +3,11 @@ require 'test_helper' class BansControllerTest < ActionController::TestCase context "A bans controller" do setup do - CurrentUser.user = Factory.create(:user) + @mod = Factory.create(:moderator_user) + CurrentUser.user = @mod CurrentUser.ip_addr = "127.0.0.1" - @ban = Factory.create(:ban) - @user = Factory.create(:moderator_user) + @user = Factory.create(:user) + @ban = Factory.create(:ban, :user_id => @user.id) end teardown do @@ -15,12 +16,12 @@ class BansControllerTest < ActionController::TestCase end should "get the new page" do - get :new, {}, {:user_id => @user.id} + get :new, {}, {:user_id => @mod.id} assert_response :success end should "get the edit page" do - get :edit, {:id => @ban.id}, {:user_id => @user.id} + get :edit, {:id => @ban.id}, {:user_id => @mod.id} assert_response :success end @@ -36,22 +37,22 @@ class BansControllerTest < ActionController::TestCase should "create a ban" do assert_difference("Ban.count", 1) do - post :create, {:ban => Factory.attributes_for(:ban)}, {:user_id => @user.id} + post :create, {:ban => {:duration => 60, :reason => "xxx", :user_id => @user.id}}, {:user_id => @mod.id} end ban = Ban.last assert_redirected_to(ban_path(ban)) end should "update a ban" do - post :update, {:id => @ban.id, :ban => {:reason => "xxx"}}, {:user_id => @user.id} - ban = Ban.last - assert_equal("xxx", ban.reason) - assert_redirected_to(ban_path(ban)) + post :update, {:id => @ban.id, :ban => {:reason => "xxx", :duration => 60}}, {:user_id => @mod.id} + @ban.reload + assert_equal("xxx", @ban.reason) + assert_redirected_to(ban_path(@ban)) end should "destroy a ban" do assert_difference("Ban.count", -1) do - post :destroy, {:id => @ban.id}, {:user_id => @user.id} + post :destroy, {:id => @ban.id}, {:user_id => @mod.id} end assert_redirected_to(bans_path) end diff --git a/test/unit/post_disapproval_test.rb b/test/unit/post_disapproval_test.rb index 263ae6586..323d2ef75 100644 --- a/test/unit/post_disapproval_test.rb +++ b/test/unit/post_disapproval_test.rb @@ -31,8 +31,8 @@ class PostDisapprovalTest < ActiveSupport::TestCase end should "remove the associated post from alice's moderation queue" do - assert(!Post.available_for_moderation.map(&:id).include?(@post_1.id)) - assert(Post.available_for_moderation.map(&:id).include?(@post_2.id)) + assert(!Post.available_for_moderation(false).map(&:id).include?(@post_1.id)) + assert(Post.available_for_moderation(false).map(&:id).include?(@post_2.id)) end end @@ -43,8 +43,8 @@ class PostDisapprovalTest < ActiveSupport::TestCase end should "not remove the associated post from brittony's moderation queue" do - assert(Post.available_for_moderation.map(&:id).include?(@post_1.id)) - assert(Post.available_for_moderation.map(&:id).include?(@post_2.id)) + assert(Post.available_for_moderation(false).map(&:id).include?(@post_1.id)) + assert(Post.available_for_moderation(false).map(&:id).include?(@post_2.id)) end end end diff --git a/test/unit/upload_test.rb b/test/unit/upload_test.rb index 61d0480d7..2ce873224 100644 --- a/test/unit/upload_test.rb +++ b/test/unit/upload_test.rb @@ -109,7 +109,7 @@ class UploadTest < ActiveSupport::TestCase context "resizer" do teardown do - FileUtils.rm_f(Dir.glob("#{Rails.root}/public/data/thumb/test.*.jpg")) + FileUtils.rm_f(Dir.glob("#{Rails.root}/public/data/preview/test.*.jpg")) FileUtils.rm_f(Dir.glob("#{Rails.root}/public/data/medium/test.*.jpg")) FileUtils.rm_f(Dir.glob("#{Rails.root}/public/data/large/test.*.jpg")) FileUtils.rm_f(Dir.glob("#{Rails.root}/public/data/original/test.*.jpg")) @@ -121,11 +121,11 @@ class UploadTest < ActiveSupport::TestCase @upload.calculate_dimensions(@upload.file_path) assert_nothing_raised {@upload.generate_resizes(@upload.file_path)} assert(File.exists?(@upload.resized_file_path_for(Danbooru.config.small_image_width))) - assert_equal(5613, File.size(@upload.resized_file_path_for(Danbooru.config.small_image_width))) + assert_equal(5677, File.size(@upload.resized_file_path_for(Danbooru.config.small_image_width))) assert(File.exists?(@upload.resized_file_path_for(Danbooru.config.medium_image_width))) - assert_equal(42990, File.size(@upload.resized_file_path_for(Danbooru.config.medium_image_width))) + assert_equal(43470, File.size(@upload.resized_file_path_for(Danbooru.config.medium_image_width))) assert(File.exists?(@upload.resized_file_path_for(Danbooru.config.large_image_width))) - assert_equal(197046, File.size(@upload.resized_file_path_for(Danbooru.config.large_image_width))) + assert_equal(198695, File.size(@upload.resized_file_path_for(Danbooru.config.large_image_width))) end end