ban fixes

This commit is contained in:
albert
2011-10-27 19:35:43 -04:00
parent 3871188bde
commit 3372a93ef8
6 changed files with 30 additions and 26 deletions

View File

@@ -19,13 +19,12 @@ class BansController < ApplicationController
end end
def create def create
@ban = Ban.new(params[:ban]) @ban = Ban.create(params[:ban])
@ban.banner_id = CurrentUser.id
if @ban.save if @ban.errors.any?
redirect_to ban_path(@ban), :notice => "Ban created"
else
render :action => "new" render :action => "new"
else
redirect_to ban_path(@ban), :notice => "Ban created"
end end
end end
@@ -34,6 +33,7 @@ class BansController < ApplicationController
if @ban.update_attributes(params[:ban]) if @ban.update_attributes(params[:ban])
redirect_to ban_path(@ban), :notice => "Ban updated" redirect_to ban_path(@ban), :notice => "Ban updated"
else else
puts @ban.errors.full_messages
render :action => "edit" render :action => "edit"
end end
end end

View File

@@ -5,11 +5,16 @@ class Ban < ActiveRecord::Base
attr_accessible :reason, :duration, :user_id, :user_name attr_accessible :reason, :duration, :user_id, :user_name
validate :user_is_inferior validate :user_is_inferior
validates_presence_of :user_id, :reason, :duration validates_presence_of :user_id, :reason, :duration
before_validation :initialize_banner_id, :on => :create
def self.is_banned?(user) def self.is_banned?(user)
exists?(["user_id = ? AND expires_at > ?", user.id, Time.now]) exists?(["user_id = ? AND expires_at > ?", user.id, Time.now])
end end
def initialize_banner_id
self.banner_id = CurrentUser.id
end
def user_is_inferior def user_is_inferior
if user if user
if user.is_admin? if user.is_admin?

View File

@@ -1,6 +1,4 @@
Factory.define(:ban) do |f| 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.reason {Faker::Lorem.words.join(" ")}
f.duration 60 f.duration 60
end end

View File

@@ -3,10 +3,11 @@ require 'test_helper'
class BansControllerTest < ActionController::TestCase class BansControllerTest < ActionController::TestCase
context "A bans controller" do context "A bans controller" do
setup do setup do
CurrentUser.user = Factory.create(:user) @mod = Factory.create(:moderator_user)
CurrentUser.user = @mod
CurrentUser.ip_addr = "127.0.0.1" CurrentUser.ip_addr = "127.0.0.1"
@ban = Factory.create(:ban) @user = Factory.create(:user)
@user = Factory.create(:moderator_user) @ban = Factory.create(:ban, :user_id => @user.id)
end end
teardown do teardown do
@@ -15,12 +16,12 @@ class BansControllerTest < ActionController::TestCase
end end
should "get the new page" do should "get the new page" do
get :new, {}, {:user_id => @user.id} get :new, {}, {:user_id => @mod.id}
assert_response :success assert_response :success
end end
should "get the edit page" do 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 assert_response :success
end end
@@ -36,22 +37,22 @@ class BansControllerTest < ActionController::TestCase
should "create a ban" do should "create a ban" do
assert_difference("Ban.count", 1) 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 end
ban = Ban.last ban = Ban.last
assert_redirected_to(ban_path(ban)) assert_redirected_to(ban_path(ban))
end end
should "update a ban" do should "update a ban" do
post :update, {:id => @ban.id, :ban => {:reason => "xxx"}}, {:user_id => @user.id} post :update, {:id => @ban.id, :ban => {:reason => "xxx", :duration => 60}}, {:user_id => @mod.id}
ban = Ban.last @ban.reload
assert_equal("xxx", ban.reason) assert_equal("xxx", @ban.reason)
assert_redirected_to(ban_path(ban)) assert_redirected_to(ban_path(@ban))
end end
should "destroy a ban" do should "destroy a ban" do
assert_difference("Ban.count", -1) 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 end
assert_redirected_to(bans_path) assert_redirected_to(bans_path)
end end

View File

@@ -31,8 +31,8 @@ class PostDisapprovalTest < ActiveSupport::TestCase
end end
should "remove the associated post from alice's moderation queue" do 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(false).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_2.id))
end end
end end
@@ -43,8 +43,8 @@ class PostDisapprovalTest < ActiveSupport::TestCase
end end
should "not remove the associated post from brittony's moderation queue" do 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(false).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_2.id))
end end
end end
end end

View File

@@ -109,7 +109,7 @@ class UploadTest < ActiveSupport::TestCase
context "resizer" do context "resizer" do
teardown 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/medium/test.*.jpg"))
FileUtils.rm_f(Dir.glob("#{Rails.root}/public/data/large/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")) 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) @upload.calculate_dimensions(@upload.file_path)
assert_nothing_raised {@upload.generate_resizes(@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(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(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(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
end end