Skip to content

Commit

Permalink
add testing for controller methods (including adding a delegate class…
Browse files Browse the repository at this point in the history
… for

managing the inteaction between controller & Authie)
  • Loading branch information
Adam Cooke committed Feb 24, 2015
1 parent 43acd45 commit 0347601
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 28 deletions.
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ task :default do
$LOAD_PATH.unshift(File.expand_path('../test', __FILE__))
require 'test_helper'
require 'tests/session_test'
require 'tests/controller_delegate_test'
require 'tests/controller_extension_test'
end
61 changes: 61 additions & 0 deletions lib/authie/controller_delegate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module Authie
class ControllerDelegate

def initialize(controller)
@controller = controller
end

# Set a random browser ID for this browser.
def set_browser_id
until cookies[:browser_id]
proposed_browser_id = SecureRandom.uuid
unless Session.where(:browser_id => proposed_browser_id).exists?
cookies[:browser_id] = {:value => proposed_browser_id, :expires => 20.years.from_now}
end
end
end

# Touch the auth session on each request if logged in
def touch_auth_session
if logged_in?
auth_session.touch!
end
end

# Return the currently logged in user object
def current_user
auth_session.user
end

# Set the currently logged in user
def current_user=(user)
if user
unless logged_in?
@auth_session = Session.start(@controller, :user => user)
end
@current_user = user
else
auth_session.destroy if logged_in?
@current_user = nil
end
end

# Is anyone currently logged in?
def logged_in?
auth_session.is_a?(Session)
end

# Return the currently logged in user session
def auth_session
@auth_session ||= Session.get_session(@controller)
end

private

# Return cookies for the controller
def cookies
@controller.send(:cookies)
end

end
end
41 changes: 13 additions & 28 deletions lib/authie/controller_extension.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'authie/controller_delegate'

module Authie
module ControllerExtension

Expand All @@ -8,49 +10,32 @@ def self.included(base)

private

# Set a random browser ID for this browser.
def auth_session_delegate
@auth_session_delegate ||= Authie::ControllerDelegate.new(self)
end

def set_browser_id
until cookies[:browser_id]
proposed_browser_id = SecureRandom.uuid
unless Session.where(:browser_id => proposed_browser_id).exists?
cookies[:browser_id] = {:value => proposed_browser_id, :expires => 20.years.from_now}
end
end
auth_session_delegate.set_browser_id
end

# Touch the auth session on each request if logged in
def touch_auth_session
if logged_in?
auth_session.touch!
end
auth_session_delegate.touch_auth_session
end

# Return the currently logged in user object
def current_user
auth_session.user
auth_session_delegate.current_user
end

# Set the currently logged in user
def current_user=(user)
if user
unless logged_in?
@auth_session = Session.start(self, :user => user)
end
@current_user = user
else
auth_session.destroy if logged_in?
@current_user = nil
end
def current_user=(*args)
auth_session_delegate.current_user(*args)
end

# Is anyone currently logged in?
def logged_in?
auth_session.is_a?(Session)
auth_session_delegate.logged_in?
end

# Return the currently logged in user session
def auth_session
@auth_session ||= Session.get_session(self)
auth_session_delegate.auth_session
end

end
Expand Down
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class User < ActiveRecord::Base
end

class FakeController

def initialize(options = {})
@options = options
end
Expand All @@ -24,6 +25,7 @@ def cookies
def request
@request ||= FakeRequest.new(@options)
end

end

class FakeRequest
Expand Down
39 changes: 39 additions & 0 deletions test/tests/controller_delegate_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'authie/controller_delegate'

class ControllerDelegateTest < Minitest::Test

def setup
@controller = FakeController.new
@delegate = Authie::ControllerDelegate.new(@controller)
end

def test_users_can_be_logged_in
# Test nobody is logged in by default
assert_equal false, @delegate.logged_in?
# Create a user and log them in
user = User.create(:username => 'tester')
@delegate.current_user = user
# Test that we're now logged in
assert_equal true, @delegate.logged_in?
assert_equal user, @delegate.current_user
assert_equal Authie::Session, @delegate.auth_session.class
end

def test_browser_id_can_be_set
# Test that there's no browser ID to begin
assert_equal nil, @controller.cookies[:browser_id]
# Set the browser ID
@delegate.set_browser_id
# Test the browser ID looks like a UUID
assert @controller.cookies[:browser_id] =~ /\A[a-f0-9\-]{36}\z/
end

def test_touching_auth_sessions
@delegate.current_user = User.create(:username => 'dave')
assert_equal nil, @delegate.auth_session.last_activity_at
@delegate.touch_auth_session
assert_equal Time, @delegate.auth_session.last_activity_at.class
assert_equal '127.0.0.1', @delegate.auth_session.last_activity_ip
end

end
46 changes: 46 additions & 0 deletions test/tests/controller_extension_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'authie/controller_extension'

class ExtendedController < FakeController

def self.before_filters
@before_filters ||= []
end

def self.before_filter(*names)
names.each { |n| before_filters << n }
end

def self.helper_methods
@helper_methods ||= []
end

def self.helper_method(*names)
names.each { |n| helper_methods << n }
end

include Authie::ControllerExtension

end

class ControllerExtensionTest < Minitest::Test

def setup
@controller = ExtendedController.new
end

def test_the_delegate_is_added
assert_equal Authie::ControllerDelegate, @controller.send(:auth_session_delegate).class
end

def test_before_filters_are_added
assert @controller.class.before_filters.include?(:set_browser_id)
assert @controller.class.before_filters.include?(:touch_auth_session)
end

def test_helper_methods_are_added
assert @controller.class.helper_methods.include?(:logged_in?)
assert @controller.class.helper_methods.include?(:current_user)
assert @controller.class.helper_methods.include?(:auth_session)
end

end

0 comments on commit 0347601

Please sign in to comment.