Skip to content
Extraits de code Groupes Projets
Valider 0bf3cae5 rédigé par maxwell's avatar maxwell
Parcourir les fichiers

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

parent c3e10111
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
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
...@@ -55,4 +55,10 @@ class ApplicationController < ActionController::Base ...@@ -55,4 +55,10 @@ class ApplicationController < ActionController::Base
def clear_gc_stats def clear_gc_stats
GC.clear_stats if GC.respond_to?(:clear_stats) GC.clear_stats if GC.respond_to?(:clear_stats)
end end
def redirect_unless_admin
unless AppConfig[:admins].include?(current_user.username)
redirect_to root_url
end
end
end end
class StatisticsController < ApplicationController class StatisticsController < ApplicationController
before_filter :authenticate_user! before_filter :authenticate_user!
before_filter :redirect_unauthorized before_filter :redirect_unless_admin
def index def index
@statistics = Statistic.find(:all, :order => 'created_at DESC').paginate(:page => params[:page], :per_page => 15) @statistics = Statistic.find(:all, :order => 'created_at DESC').paginate(:page => params[:page], :per_page => 15)
...@@ -19,7 +19,6 @@ class StatisticsController < ApplicationController ...@@ -19,7 +19,6 @@ class StatisticsController < ApplicationController
:axis_labels => [(0..@distribution.length-1).to_a.map{|d| d%10==0 ? d : ''}, :axis_labels => [(0..@distribution.length-1).to_a.map{|d| d%10==0 ? d : ''},
(0..10).to_a.map!{|int| int.to_f/10}] (0..10).to_a.map!{|int| int.to_f/10}]
) )
end end
def generate_single def generate_single
...@@ -47,11 +46,4 @@ class StatisticsController < ApplicationController ...@@ -47,11 +46,4 @@ class StatisticsController < ApplicationController
redirect_to 'statistics/user_search' redirect_to 'statistics/user_search'
end end
private
def redirect_unauthorized
unless AppConfig[:admins].include?(current_user.username)
redirect_to root_url
end
end
end end
%h3 %h3
- form_tag 'admin_inviter', :method => :get do = form_tag 'admin_inviter', :method => :get do
email to invite: email to invite:
= text_field_tag 'identifier' = text_field_tag 'identifier'
= submit_tag 'invite' = submit_tag 'invite'
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
%h3 %h3
user search user search
- form_tag 'user_search', :method => :get do = form_tag 'user_search', :method => :get do
username: username:
= text_field_tag 'user[username]', params[:user][:username] = text_field_tag 'user[username]', params[:user][:username]
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
= "#{@users.count} users found" = "#{@users.count} users found"
%br %br
%br %br
- for user in @users = for user in @users
= user.inspect = user.inspect
%br %br
= user.person.inspect = user.person.inspect
......
...@@ -12,8 +12,8 @@ Diaspora::Application.routes.draw do ...@@ -12,8 +12,8 @@ Diaspora::Application.routes.draw do
match 'services/finder/:provider' => 'services#finder', :as => 'friend_finder' match 'services/finder/:provider' => 'services#finder', :as => 'friend_finder'
resources :services resources :services
match 'statistics/user_search' => 'statistics#user_search' match 'admins/user_search' => 'admins#user_search'
match 'statistics/admin_inviter' => 'statistics#admin_inviter' match 'admins/admin_inviter' => 'admins#admin_inviter'
match 'statistics/generate_single' => 'statistics#generate_single' match 'statistics/generate_single' => 'statistics#generate_single'
resources :statistics resources :statistics
......
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
require 'spec_helper'
describe ApplicationController do
end
...@@ -35,7 +35,7 @@ describe StatisticsController do ...@@ -35,7 +35,7 @@ describe StatisticsController do
end end
end end
describe '#redirect_unauthorized' do describe ' sets a before filter to use #redirect_unless_admin' do
it 'redirects for non admins' do it 'redirects for non admins' do
AppConfig[:admins] = ['bob'] AppConfig[:admins] = ['bob']
get :index get :index
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter