From 3d5873c182eeeb1659090042deb650ac50834fac Mon Sep 17 00:00:00 2001 From: albert Date: Fri, 14 Jan 2011 16:45:10 -0500 Subject: [PATCH] some fixes to janitor trials, implemented jan trial controller test --- app/controllers/janitor_trials_controller.rb | 27 +++++-- app/models/janitor_trial.rb | 23 ++++++ app/models/user_feedback.rb | 7 +- app/views/ip_bans/index.html.erb | 22 ++++++ app/views/ip_bans/new.html.erb | 7 ++ app/views/janitor_trials/edit.html.erb | 0 app/views/janitor_trials/index.html.erb | 0 app/views/janitor_trials/new.html.erb | 0 config/routes.rb | 7 +- .../20100309211553_create_janitor_trials.rb | 1 - test/factories/janitor_trial.rb | 3 + test/factories/user.rb | 2 +- test/factories/user_feedback.rb | 2 - .../janitor_trials_controller_test.rb | 71 ++++++++++++++++++- test/unit/janitor_trial_test.rb | 63 ++++++++++++---- 15 files changed, 206 insertions(+), 29 deletions(-) create mode 100644 app/views/janitor_trials/edit.html.erb create mode 100644 app/views/janitor_trials/index.html.erb create mode 100644 app/views/janitor_trials/new.html.erb create mode 100644 test/factories/janitor_trial.rb diff --git a/app/controllers/janitor_trials_controller.rb b/app/controllers/janitor_trials_controller.rb index 64edaa45a..ec488a249 100644 --- a/app/controllers/janitor_trials_controller.rb +++ b/app/controllers/janitor_trials_controller.rb @@ -1,19 +1,36 @@ class JanitorTrialsController < ApplicationController + respond_to :html, :xml, :json + def new + @janitor_trial = JanitorTrial.new + respond_with(@janitor_trial) end def edit + @janitor_trial = JanitorTrial.find(params[:id]) + respond_with(@janitor_trial) end def index - end - - def show + @search = JanitorTrial.search(params[:search]) + @janitor_trials = @search.paginate(:page => params[:page]) + respond_with(@janitor_trials) end def create + @janitor_trial = JanitorTrial.create(params[:janitor_trial]) + respond_with(@janitor_trial) end - def update - end + def promote + @janitor_trial = JanitorTrial.find(params[:id]) + @janitor_trial.promote! + respond_with(@janitor_trial) + end + + def demote + @janitor_trial = JanitorTrial.find(params[:id]) + @janitor_trial.demote! + respond_with(@janitor_trial) + end end diff --git a/app/models/janitor_trial.rb b/app/models/janitor_trial.rb index 02d7ddef8..7bc988bcb 100644 --- a/app/models/janitor_trial.rb +++ b/app/models/janitor_trial.rb @@ -1,10 +1,33 @@ class JanitorTrial < ActiveRecord::Base belongs_to :user after_create :send_dmail + after_create :promote_user + after_destroy :create_feedback + validates_presence_of :user def send_dmail body = "You have been selected as a test janitor. You can now approve pending posts and have access to the moderation interface.\n\nOver the next several weeks your approvals will be monitored. If the majority of them are quality uploads, then you will be promoted to full janitor status which grants you the ability to delete and undelete posts, ban users, and revert tag changes from vandals. If you fail the trial period, you will be demoted back to your original level and you'll receive a negative user record indicating you previously attempted and failed a test janitor trial.\n\nThere is a minimum quota of 5 approvals a week to indicate that you are being active. Remember, the goal isn't to approve as much as possible. It's to filter out borderline-quality art.\n\nIf you have any questions please respond to this message." Dmail.create_split(:title => "Test Janitor Trial Period", :body => body, :to_id => user_id) end + + def promote_user + user.update_attribute(:is_janitor, true) + end + + def create_feedback + user.feedback.create( + :is_positive => false, + :body => "Demoted from janitor trial" + ) + end + + def promote! + destroy + end + + def demote! + user.update_attribute(:is_janitor, false) + destroy + end end diff --git a/app/models/user_feedback.rb b/app/models/user_feedback.rb index 7bdbf136b..c8504f419 100644 --- a/app/models/user_feedback.rb +++ b/app/models/user_feedback.rb @@ -2,10 +2,15 @@ class UserFeedback < ActiveRecord::Base set_table_name "user_feedback" belongs_to :user belongs_to :creator, :class_name => "User" + before_validation :initialize_creator, :on => :create attr_accessible :body, :user_id, :is_positive - validates_presence_of :user_id, :creator_id, :body + validates_presence_of :user, :creator, :body validate :creator_is_privileged + def initialize_creator + self.creator_id = CurrentUser.id + end + def creator_is_privileged if !creator.is_privileged? errors[:creator] << "must be privileged" diff --git a/app/views/ip_bans/index.html.erb b/app/views/ip_bans/index.html.erb index e69de29bb..693cf2c79 100644 --- a/app/views/ip_bans/index.html.erb +++ b/app/views/ip_bans/index.html.erb @@ -0,0 +1,22 @@ +

IP Bans

+ + + + + + + + + + + + <% @ip_bans.each do |ip_ban| %> + + + + + + + <% end %> + +
IP AddressBannerReason
<%= ip_ban.ip_addr %><%= ip_ban.creator.name %><%= ip_ban.reason %><%= link_to "Unban", ip_ban_path(ip_ban), :remote => true, :method => :delete, :confirm => "Do your really want to unban #{ip_ban.creator.name}?" %>
diff --git a/app/views/ip_bans/new.html.erb b/app/views/ip_bans/new.html.erb index e69de29bb..e1a05b1a8 100644 --- a/app/views/ip_bans/new.html.erb +++ b/app/views/ip_bans/new.html.erb @@ -0,0 +1,7 @@ +

New IP Ban

+ +<%= simple_form_for(@ip_ban) do |f| %> + <%= f.input :ip_addr %> + <%= f.input :reason %> + <%= f.button :submit %> +<% end %> diff --git a/app/views/janitor_trials/edit.html.erb b/app/views/janitor_trials/edit.html.erb new file mode 100644 index 000000000..e69de29bb diff --git a/app/views/janitor_trials/index.html.erb b/app/views/janitor_trials/index.html.erb new file mode 100644 index 000000000..e69de29bb diff --git a/app/views/janitor_trials/new.html.erb b/app/views/janitor_trials/new.html.erb new file mode 100644 index 000000000..e69de29bb diff --git a/config/routes.rb b/config/routes.rb index 5a2593ae0..924927c2a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,7 +18,12 @@ Danbooru::Application.routes.draw do resources :favorites resources :forum_posts resources :forum_topics - resources :janitor_trials + resources :janitor_trials do + member do + put :promote + put :demote + end + end resources :jobs resources :ip_bans resources :notes diff --git a/db/migrate/20100309211553_create_janitor_trials.rb b/db/migrate/20100309211553_create_janitor_trials.rb index ebff7352d..1a0b83af2 100644 --- a/db/migrate/20100309211553_create_janitor_trials.rb +++ b/db/migrate/20100309211553_create_janitor_trials.rb @@ -2,7 +2,6 @@ class CreateJanitorTrials < ActiveRecord::Migration def self.up create_table :janitor_trials do |t| t.column :user_id, :integer, :null => false - t.column :promoted_at, :datetime t.timestamps end diff --git a/test/factories/janitor_trial.rb b/test/factories/janitor_trial.rb new file mode 100644 index 000000000..b863d9b91 --- /dev/null +++ b/test/factories/janitor_trial.rb @@ -0,0 +1,3 @@ +Factory.define(:janitor_trial) do |f| + f.user {|x| x.association(:user)} +end diff --git a/test/factories/user.rb b/test/factories/user.rb index 78fb946e5..85c8d523d 100644 --- a/test/factories/user.rb +++ b/test/factories/user.rb @@ -1,5 +1,5 @@ Factory.define(:user) do |f| - f.name {Faker::Name.first_name} + f.name {rand(1_000_000).to_s} f.password "password" f.password_hash {User.sha1("password")} f.email {Faker::Internet.email} diff --git a/test/factories/user_feedback.rb b/test/factories/user_feedback.rb index cbc8136c9..03c15fe77 100644 --- a/test/factories/user_feedback.rb +++ b/test/factories/user_feedback.rb @@ -1,6 +1,4 @@ Factory.define(:user_feedback) do |f| - f.user {|x| x.association(:user)} - f.creator {|x| x.association(:user)} f.is_positive true f.body {Faker::Lorem.words} end diff --git a/test/functional/janitor_trials_controller_test.rb b/test/functional/janitor_trials_controller_test.rb index a316910c9..8830b61c6 100644 --- a/test/functional/janitor_trials_controller_test.rb +++ b/test/functional/janitor_trials_controller_test.rb @@ -1,8 +1,73 @@ require 'test_helper' class JanitorTrialsControllerTest < ActionController::TestCase - # Replace this with your real tests. - test "the truth" do - assert true + context "The janitor trials controller" do + setup do + @admin = Factory.create(:admin_user) + @user = Factory.create(: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 janitor trial" do + assert_difference("JanitorTrial.count", 1) do + post :create, {:janitor_trial => {:user_id => @user.id}}, {:user_id => @admin.id} + end + end + end + + context "promote action" do + setup do + @janitor_trial = Factory.create(:janitor_trial, :user_id => @user.id) + end + + should "promote the janitor trial" do + assert_difference("JanitorTrial.count", -1) do + post :promote, {:id => @janitor_trial.id}, {:user_id => @admin.id} + end + @user.reload + assert(@user.is_janitor?) + end + end + + context "demote action" do + setup do + @janitor_trial = Factory.create(:janitor_trial, :user_id => @user.id) + end + + should "demote the janitor trial" do + assert_difference("JanitorTrial.count", -1) do + post :demote, {:id => @janitor_trial.id}, {:user_id => @admin.id} + end + @user.reload + assert(!@user.is_janitor?) + end + end + + context "index action" do + setup do + Factory.create(:janitor_trial) + 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 => {:user_name_equals => @user.name}}, {:user_id => @admin.id} + assert_response :success + end + end + end end end diff --git a/test/unit/janitor_trial_test.rb b/test/unit/janitor_trial_test.rb index 5fd5c6938..782d639e1 100644 --- a/test/unit/janitor_trial_test.rb +++ b/test/unit/janitor_trial_test.rb @@ -1,23 +1,56 @@ require_relative '../test_helper' class JanitorTrialTest < ActiveSupport::TestCase - setup do - user = Factory.create(:user) - CurrentUser.user = user - CurrentUser.ip_addr = "127.0.0.1" - MEMCACHE.flush_all - end + context "A janitor trial" do + setup do + @admin = Factory.create(:admin_user) + @user = Factory.create(:user) + CurrentUser.user = @admin + CurrentUser.ip_addr = "127.0.0.1" + MEMCACHE.flush_all + end - teardown do - CurrentUser.user = nil - CurrentUser.ip_addr = nil - end + teardown do + CurrentUser.user = nil + CurrentUser.ip_addr = nil + end - should "create a dmail when testing a new janitor" do - admin = Factory.create(:admin_user) - user = Factory.create(:user) - assert_difference("Dmail.count", 2) do - JanitorTrial.create(:user_id => user.id) + context "upon creation" do + should "create a dmail when testing a new janitor" do + assert_difference("Dmail.count", 2) do + JanitorTrial.create(:user_id => @user.id) + end + end + + should "toggle the janitor flag on the user" do + janitor_trial = JanitorTrial.create(:user_id => @user.id) + @user.reload + assert(@user.is_janitor?) + end + end + + context "upon demotion" do + setup do + @janitor_trial = Factory.create(:janitor_trial, :user_id => @user.id) + end + + should "create a negative feedback record" do + assert_difference("UserFeedback.count", 1) do + @janitor_trial.demote! + end + end + end + + context "upon promotion" do + setup do + @janitor_trial = Factory.create(:janitor_trial, :user_id => @user.id) + end + + should "destroy the trial object" do + assert_difference("JanitorTrial.count", -1) do + @janitor_trial.promote! + end + end end end end