Skip to content
This repository has been archived by the owner on Jun 7, 2021. It is now read-only.

Commit

Permalink
add authorizations controller test
Browse files Browse the repository at this point in the history
  • Loading branch information
bborn committed Dec 27, 2013
1 parent 8a02e9c commit 603a952
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 27 deletions.
3 changes: 2 additions & 1 deletion app/controllers/authorizations_controller.rb
Expand Up @@ -11,7 +11,8 @@ def create
flash[:notice] = t('authorizations.create.success_existing_user', :provider => provider_name)
elsif @auth.valid?
flash[:notice] = t('authorizations.create.success_message', :provider => provider_name)
UserSession.create(@auth.user, true)
user_session = UserSession.create(@auth.user, true)
self.current_user = user_session.user
end

if logged_in?
Expand Down
7 changes: 3 additions & 4 deletions app/controllers/sessions_controller.rb
@@ -1,11 +1,11 @@
# This controller handles the login/logout function of the site.
# This controller handles the login/logout function of the site.
class SessionsController < BaseController

skip_before_filter :store_location, :only => [:new, :create]

def index
redirect_to :action => "new"
end
end

def new
redirect_to user_path(current_user) and return if current_user
Expand All @@ -16,9 +16,8 @@ def create
@user_session = UserSession.new(:login => params[:email], :password => params[:password], :remember_me => params[:remember_me])

if @user_session.save
self.current_user = @user_session.record #if current_user has been called before this, it will ne nil, so we have to make to reset it

current_user = @user_session.record #if current_user has been called before this, it will ne nil, so we have to make to reset it

flash[:notice] = :thanks_youre_now_logged_in.l
redirect_back_or_default(dashboard_user_path(current_user))
else
Expand Down
22 changes: 11 additions & 11 deletions lib/community_engine/authenticated_system.rb
@@ -1,19 +1,19 @@
module AuthenticatedSystem
def update_last_seen_at
return unless logged_in?
User.update_all ['sb_last_seen_at = ?', Time.now.utc], ['id = ?', current_user.id]
User.update_all ['sb_last_seen_at = ?', Time.now.utc], ['id = ?', current_user.id]
current_user.sb_last_seen_at = Time.now.utc
end

def login_by_token
end

protected
# Returns true or false if the user is logged in.
def logged_in?
current_user ? true : false
end

# Accesses the current user from the session.
def current_user
return @current_user if defined?(@current_user)
Expand All @@ -23,7 +23,7 @@ def current_user
# Create a user session without credentials.
def current_user=(user)
return if current_user # Use act_as_user= to switch to another user account
@current_user_session = UserSession.create(user, true)
@current_user_session = UserSession.create(user)
@current_user = @current_user_session.record
end

Expand Down Expand Up @@ -76,9 +76,9 @@ def admin?
logged_in? && current_user.admin?
end
def moderator?
logged_in? && current_user.moderator?
logged_in? && current_user.moderator?
end

# Redirect as appropriate when an access request fails.
#
# The default action is to redirect to the login screen.
Expand All @@ -99,19 +99,19 @@ def access_denied
render :text => "Couldn't authenticate you", :status => '401 Unauthorized'
end
accepts.js do
store_location
store_location
render :update do |page|
page.redirect_to login_path
end and return false
end
end
end
false
end

# Inclusion hook to make #current_user and #logged_in?
# available as ActionView helper methods.
def self.included(base)
base.send :helper_method, :current_user, :current_user_session, :logged_in?, :admin?, :moderator?
base.send :helper_method, :current_user=, :current_user, :current_user_session, :logged_in?, :admin?, :moderator?
end

private
Expand Down
60 changes: 60 additions & 0 deletions test/functional/authorizations_controller_test.rb
@@ -0,0 +1,60 @@
require 'test_helper'

class AuthorizationsControllerTest < ActionController::TestCase
fixtures :all

setup do
OmniAuth.config.test_mode = true
end



test 'should create new authorization and log in' do

set_ommniauth

get :create

user = UserSession.find.record
assert_redirected_to user_path(user)
end

test 'should find existing authorization and log in' do
quentin = users(:quentin)
Authorization.create_from_hash(auth_hash(quentin.email), quentin)
set_ommniauth(quentin.email)

get :create

assert_redirected_to user_path(quentin)
end

test 'should authorize existing logged-in user' do
quentin = users(:quentin)
login_as :quentin

set_ommniauth(quentin.email)

get :create

assert_redirected_to user_path(quentin)
end


def set_ommniauth(email=nil)
OmniAuth.config.mock_auth[:facebook] = auth_hash(email)
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
end

def auth_hash(email='email@example.com')
{
'provider' => 'facebook',
"info" => {
'nickname' => 'Omniauth-user',
'email' => email
},
'uid' => '123545'
}
end

end
6 changes: 3 additions & 3 deletions test/functional/sessions_controller_test.rb
@@ -1,6 +1,6 @@
require 'test_helper'

class SessionsControllerTest < ActionController::TestCase
class SessionsControllerTest < ActionController::TestCase
fixtures :all

def test_should_login_and_redirect
Expand All @@ -21,7 +21,7 @@ def test_should_logout
assert_nil UserSession.find
assert_response :redirect
end

def test_should_delete_token_on_logout
login_as :quentin
get :destroy
Expand All @@ -42,7 +42,7 @@ def test_should_fail_cookie_login
@request.cookies["user_credentials"] = {:value => {:value => 'invalid_token'}, :expires => nil}
assert !@controller.send(:logged_in?)
end

def test_should_login_with_reset_password
quentin = users(:quentin)
quentin.reset_password
Expand Down
8 changes: 0 additions & 8 deletions test/test_helper.rb
Expand Up @@ -17,14 +17,6 @@
ActiveSupport::TestCase.fixture_path = (Rails.root + "../fixtures").to_s #we want a string here, not a Pathname
ActionController::IntegrationTest.fixture_path = ActiveSupport::TestCase.fixture_path

# OmniAuth.config.test_mode = true
# OmniAuth.config.mock_auth[:default] = {
# 'uid' => '123545'
# 'nickname' => 'Omniauth-user'
# 'email' => 'email@example.com'
# }


class ActionController::TestCase
setup :activate_authlogic
end
Expand Down

0 comments on commit 603a952

Please sign in to comment.