From 0bf3cae54a785966fb8d258041e66bd45cd87813 Mon Sep 17 00:00:00 2001
From: maxwell <maxwell@joindiaspora.com>
Date: Fri, 18 Feb 2011 11:35:24 -0800
Subject: [PATCH] MS IZ added spec from admin page, moved it to its own
 controller where it belongs

---
 app/controllers/admins_controller.rb          | 24 +++++++
 app/controllers/application_controller.rb     |  6 ++
 app/controllers/statistics_controller.rb      | 10 +--
 .../user_search.html.haml                     |  6 +-
 config/routes.rb                              |  4 +-
 spec/controllers/admins_controller_spec.rb    | 64 +++++++++++++++++++
 .../application_controller_spec.rb            |  4 ++
 .../controllers/statistics_controller_spec.rb |  2 +-
 8 files changed, 105 insertions(+), 15 deletions(-)
 create mode 100644 app/controllers/admins_controller.rb
 rename app/views/{statistics => admins}/user_search.html.haml (86%)
 create mode 100644 spec/controllers/admins_controller_spec.rb
 create mode 100644 spec/controllers/application_controller_spec.rb

diff --git a/app/controllers/admins_controller.rb b/app/controllers/admins_controller.rb
new file mode 100644
index 0000000000..a9b3b25cd4
--- /dev/null
+++ b/app/controllers/admins_controller.rb
@@ -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
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 4289ee9f33..57e0cb8a36 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -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
diff --git a/app/controllers/statistics_controller.rb b/app/controllers/statistics_controller.rb
index 5b1cdfc197..568a11400e 100644
--- a/app/controllers/statistics_controller.rb
+++ b/app/controllers/statistics_controller.rb
@@ -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
-
diff --git a/app/views/statistics/user_search.html.haml b/app/views/admins/user_search.html.haml
similarity index 86%
rename from app/views/statistics/user_search.html.haml
rename to app/views/admins/user_search.html.haml
index 3736277ab8..e6672c8a89 100644
--- a/app/views/statistics/user_search.html.haml
+++ b/app/views/admins/user_search.html.haml
@@ -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
diff --git a/config/routes.rb b/config/routes.rb
index 06e1242aa3..a3471c3aaf 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -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
 
diff --git a/spec/controllers/admins_controller_spec.rb b/spec/controllers/admins_controller_spec.rb
new file mode 100644
index 0000000000..1aaef3716d
--- /dev/null
+++ b/spec/controllers/admins_controller_spec.rb
@@ -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
diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb
new file mode 100644
index 0000000000..01847eef4f
--- /dev/null
+++ b/spec/controllers/application_controller_spec.rb
@@ -0,0 +1,4 @@
+require 'spec_helper'
+
+describe ApplicationController do
+end
diff --git a/spec/controllers/statistics_controller_spec.rb b/spec/controllers/statistics_controller_spec.rb
index 560c6d3d67..d9bf0857e1 100644
--- a/spec/controllers/statistics_controller_spec.rb
+++ b/spec/controllers/statistics_controller_spec.rb
@@ -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
-- 
GitLab