forked from adamcooke/authie
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add testing for controller methods (including adding a delegate class…
… for managing the inteaction between controller & Authie)
- Loading branch information
Adam Cooke
committed
Feb 24, 2015
1 parent
43acd45
commit 0347601
Showing
6 changed files
with
163 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |