MS IZ added spec from admin page, moved it to its own controller where it belongs

This commit is contained in:
maxwell 2011-02-18 11:35:24 -08:00
parent c3e101112d
commit 0bf3cae54a
8 changed files with 105 additions and 15 deletions

View file

@ -0,0 +1,24 @@
class AdminsController < ApplicationController
before_filter :authenticate_user!
before_filter :redirect_unless_admin
def user_search
user = params[:user] || {}
user = user.delete_if {|key, value| value.blank? }
params[:user] = user
if user.keys.count == 0
@users = []
else
@users = User.where(params[:user]).all || []
end
render 'user_search'
end
def admin_inviter
Invitation.create_invitee(:identifier => params[:identifier])
flash[:notice] = "invitation sent to #{params[:identifier]}"
redirect_to 'admins/user_search'
end
end

View file

@ -55,4 +55,10 @@ class ApplicationController < ActionController::Base
def clear_gc_stats
GC.clear_stats if GC.respond_to?(:clear_stats)
end
def redirect_unless_admin
unless AppConfig[:admins].include?(current_user.username)
redirect_to root_url
end
end
end

View file

@ -1,6 +1,6 @@
class StatisticsController < ApplicationController
before_filter :authenticate_user!
before_filter :redirect_unauthorized
before_filter :redirect_unless_admin
def index
@statistics = Statistic.find(:all, :order => 'created_at DESC').paginate(:page => params[:page], :per_page => 15)
@ -19,7 +19,6 @@ class StatisticsController < ApplicationController
:axis_labels => [(0..@distribution.length-1).to_a.map{|d| d%10==0 ? d : ''},
(0..10).to_a.map!{|int| int.to_f/10}]
)
end
def generate_single
@ -47,11 +46,4 @@ class StatisticsController < ApplicationController
redirect_to 'statistics/user_search'
end
private
def redirect_unauthorized
unless AppConfig[:admins].include?(current_user.username)
redirect_to root_url
end
end
end

View file

@ -1,6 +1,6 @@
%h3
- form_tag 'admin_inviter', :method => :get do
= form_tag 'admin_inviter', :method => :get do
email to invite:
= text_field_tag 'identifier'
= submit_tag 'invite'
@ -9,7 +9,7 @@
%h3
user search
- form_tag 'user_search', :method => :get do
= form_tag 'user_search', :method => :get do
username:
= text_field_tag 'user[username]', params[:user][:username]
@ -27,7 +27,7 @@
= "#{@users.count} users found"
%br
%br
- for user in @users
= for user in @users
= user.inspect
%br
= user.person.inspect

View file

@ -12,8 +12,8 @@ Diaspora::Application.routes.draw do
match 'services/finder/:provider' => 'services#finder', :as => 'friend_finder'
resources :services
match 'statistics/user_search' => 'statistics#user_search'
match 'statistics/admin_inviter' => 'statistics#admin_inviter'
match 'admins/user_search' => 'admins#user_search'
match 'admins/admin_inviter' => 'admins#admin_inviter'
match 'statistics/generate_single' => 'statistics#generate_single'
resources :statistics

View file

@ -0,0 +1,64 @@
require 'spec_helper'
describe AdminsController do
render_views
before do
@user = alice
sign_in :user, @user
end
it 'is behind redirect_unless_admin' do
get :user_search
response.should be_redirect
end
context 'admin signed in' do
before do
AppConfig[:admins] = [alice.username]
end
describe '#user_search' do
it 'succeeds' do
get :user_search
response.should be_success
end
it 'assings users to an empty array if nothing is searched for' do
get :user_search
assigns[:users].should == []
end
it 'should search on username' do
get :user_search, :user => {:username => @user.username}
assigns[:users].should == [@user]
end
it 'should search on email' do
get :user_search, :user => {:email => @user.email}
assigns[:users].should == [@user]
end
it 'should search on invitation_identifier' do
@user.invitation_identifier = "La@foo.com"
@user.save!
get :user_search, :user => {:invitation_identifier => @user.invitation_identifier}
assigns[:users].should == [@user]
end
it 'should search on invitation_token' do
@user.invitation_token = "akjsdhflhasdf"
@user.save
get :user_search, :user => {:invitation_token => @user.invitation_token}
assigns[:users].should == [@user]
end
end
describe '#admin_inviter' do
it 'invites a user' do
Invitation.should_receive(:create_invitee).with(:identifier => 'bob@moms.com')
get :admin_inviter, :identifier => 'bob@moms.com'
response.should be_redirect
end
end
end
end

View file

@ -0,0 +1,4 @@
require 'spec_helper'
describe ApplicationController do
end

View file

@ -35,7 +35,7 @@ describe StatisticsController do
end
end
describe '#redirect_unauthorized' do
describe ' sets a before filter to use #redirect_unless_admin' do
it 'redirects for non admins' do
AppConfig[:admins] = ['bob']
get :index