some fixes to janitor trials, implemented jan trial controller test

This commit is contained in:
albert
2011-01-14 16:45:10 -05:00
parent dceda1b073
commit 3d5873c182
15 changed files with 206 additions and 29 deletions

View File

@@ -1,19 +1,36 @@
class JanitorTrialsController < ApplicationController class JanitorTrialsController < ApplicationController
respond_to :html, :xml, :json
def new def new
@janitor_trial = JanitorTrial.new
respond_with(@janitor_trial)
end end
def edit def edit
@janitor_trial = JanitorTrial.find(params[:id])
respond_with(@janitor_trial)
end end
def index def index
end @search = JanitorTrial.search(params[:search])
@janitor_trials = @search.paginate(:page => params[:page])
def show respond_with(@janitor_trials)
end end
def create def create
@janitor_trial = JanitorTrial.create(params[:janitor_trial])
respond_with(@janitor_trial)
end end
def update 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
end end

View File

@@ -1,10 +1,33 @@
class JanitorTrial < ActiveRecord::Base class JanitorTrial < ActiveRecord::Base
belongs_to :user belongs_to :user
after_create :send_dmail after_create :send_dmail
after_create :promote_user
after_destroy :create_feedback
validates_presence_of :user
def send_dmail 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." 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) Dmail.create_split(:title => "Test Janitor Trial Period", :body => body, :to_id => user_id)
end 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 end

View File

@@ -2,10 +2,15 @@ class UserFeedback < ActiveRecord::Base
set_table_name "user_feedback" set_table_name "user_feedback"
belongs_to :user belongs_to :user
belongs_to :creator, :class_name => "User" belongs_to :creator, :class_name => "User"
before_validation :initialize_creator, :on => :create
attr_accessible :body, :user_id, :is_positive 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 validate :creator_is_privileged
def initialize_creator
self.creator_id = CurrentUser.id
end
def creator_is_privileged def creator_is_privileged
if !creator.is_privileged? if !creator.is_privileged?
errors[:creator] << "must be privileged" errors[:creator] << "must be privileged"

View File

@@ -0,0 +1,22 @@
<h1>IP Bans</h1>
<table>
<thead>
<tr>
<th>IP Address</th>
<th>Banner</th>
<th>Reason</th>
<th></th>
</tr>
</thead>
<tbody>
<% @ip_bans.each do |ip_ban| %>
<tr>
<td><%= ip_ban.ip_addr %></td>
<td><%= ip_ban.creator.name %></td>
<td><%= ip_ban.reason %></td>
<td><%= link_to "Unban", ip_ban_path(ip_ban), :remote => true, :method => :delete, :confirm => "Do your really want to unban #{ip_ban.creator.name}?" %></td>
</tr>
<% end %>
</tbody>
</table>

View File

@@ -0,0 +1,7 @@
<h1>New IP Ban</h1>
<%= simple_form_for(@ip_ban) do |f| %>
<%= f.input :ip_addr %>
<%= f.input :reason %>
<%= f.button :submit %>
<% end %>

View File

View File

View File

View File

@@ -18,7 +18,12 @@ Danbooru::Application.routes.draw do
resources :favorites resources :favorites
resources :forum_posts resources :forum_posts
resources :forum_topics resources :forum_topics
resources :janitor_trials resources :janitor_trials do
member do
put :promote
put :demote
end
end
resources :jobs resources :jobs
resources :ip_bans resources :ip_bans
resources :notes resources :notes

View File

@@ -2,7 +2,6 @@ class CreateJanitorTrials < ActiveRecord::Migration
def self.up def self.up
create_table :janitor_trials do |t| create_table :janitor_trials do |t|
t.column :user_id, :integer, :null => false t.column :user_id, :integer, :null => false
t.column :promoted_at, :datetime
t.timestamps t.timestamps
end end

View File

@@ -0,0 +1,3 @@
Factory.define(:janitor_trial) do |f|
f.user {|x| x.association(:user)}
end

View File

@@ -1,5 +1,5 @@
Factory.define(:user) do |f| Factory.define(:user) do |f|
f.name {Faker::Name.first_name} f.name {rand(1_000_000).to_s}
f.password "password" f.password "password"
f.password_hash {User.sha1("password")} f.password_hash {User.sha1("password")}
f.email {Faker::Internet.email} f.email {Faker::Internet.email}

View File

@@ -1,6 +1,4 @@
Factory.define(:user_feedback) do |f| Factory.define(:user_feedback) do |f|
f.user {|x| x.association(:user)}
f.creator {|x| x.association(:user)}
f.is_positive true f.is_positive true
f.body {Faker::Lorem.words} f.body {Faker::Lorem.words}
end end

View File

@@ -1,8 +1,73 @@
require 'test_helper' require 'test_helper'
class JanitorTrialsControllerTest < ActionController::TestCase class JanitorTrialsControllerTest < ActionController::TestCase
# Replace this with your real tests. context "The janitor trials controller" do
test "the truth" do setup do
assert true @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
end end

View File

@@ -1,23 +1,56 @@
require_relative '../test_helper' require_relative '../test_helper'
class JanitorTrialTest < ActiveSupport::TestCase class JanitorTrialTest < ActiveSupport::TestCase
setup do context "A janitor trial" do
user = Factory.create(:user) setup do
CurrentUser.user = user @admin = Factory.create(:admin_user)
CurrentUser.ip_addr = "127.0.0.1" @user = Factory.create(:user)
MEMCACHE.flush_all CurrentUser.user = @admin
end CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
end
teardown do teardown do
CurrentUser.user = nil CurrentUser.user = nil
CurrentUser.ip_addr = nil CurrentUser.ip_addr = nil
end end
should "create a dmail when testing a new janitor" do context "upon creation" do
admin = Factory.create(:admin_user) should "create a dmail when testing a new janitor" do
user = Factory.create(:user) assert_difference("Dmail.count", 2) do
assert_difference("Dmail.count", 2) do JanitorTrial.create(:user_id => @user.id)
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 end
end end