Permalink
Browse files

updated the activity_logging submodule, and found a (somewhat hackish…

…) way to remove before_filters between specs.
  • Loading branch information...
1 parent 928d0d5 commit 4e8b7b9a38836f05092d0b86489f79749fbf0056 @NoamB committed Feb 9, 2011
View
@@ -28,6 +28,7 @@ Jeweler::RubygemsDotOrgTasks.new
require 'rspec/core'
require 'rspec/core/rake_task'
+
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList['spec/**/*_spec.rb']
end
@@ -6,6 +6,7 @@ def self.included(base)
base.send(:include, InstanceMethods)
Config.after_login << :register_login_time_to_db
Config.before_logout << :register_logout_time_to_db
+ base.after_filter :register_last_activity_time_to_db
end
module InstanceMethods
@@ -14,14 +15,20 @@ def logged_in_users
end
def register_login_time_to_db(user, credentials)
- user.last_login = Time.now.to_s(:db)
+ user.send(:"#{user.sorcery_config.last_login_attribute_name}=", Time.now.to_s(:db))
user.save!(:validate => false)
end
def register_logout_time_to_db(user)
- user.last_logout = Time.now.to_s(:db)
+ user.send(:"#{user.sorcery_config.last_logout_attribute_name}=", Time.now.to_s(:db))
user.save!(:validate => false)
end
+
+ def register_last_activity_time_to_db
+ return if !logged_in?
+ logged_in_user.send(:"#{logged_in_user.sorcery_config.last_activity_attribute_name}=", Time.now.to_s(:db))
+ logged_in_user.save!(:validate => false)
+ end
end
end
end
View
@@ -12,8 +12,8 @@ class Engine < Rails::Engine
end
initializer "extend Controller with sorcery" do |app|
- ActionController::Base.send(:include, Sorcery::Controller)
- ActionController::Base.helper_method :logged_in_user
+ ApplicationController.send(:include, Sorcery::Controller)
+ ApplicationController.helper_method :logged_in_user
end
end
@@ -6,22 +6,27 @@ def self.included(base)
base.extend(ClassMethods)
base.sorcery_config.class_eval do
attr_accessor :last_login_attribute_name, # last login attribute name.
- :last_logout_attribute_name # last logout attribute name.
+ :last_logout_attribute_name, # last logout attribute name.
+ :last_activity_attribute_name, # last activity attribute name.
+ :activity_timeout # how long since last activity is the user defined logged out?
end
base.sorcery_config.instance_eval do
@defaults.merge!(:@last_login_attribute_name => :last_login,
- :@last_logout_attribute_name => :last_logout)
+ :@last_logout_attribute_name => :last_logout,
+ :@last_activity_attribute_name => :last_activity,
+ :@activity_timeout => 10.minutes)
reset!
end
end
module ClassMethods
- # get all users with last login > last logout,
- # which are within session_timeout (if submodule included) or within defined time
+ # get all users with last_activity within timeout
def logged_in_users
config = sorcery_config
- where("#{config.last_login_attribute_name} IS NOT NULL AND (#{config.last_logout_attribute_name} IS NULL OR #{config.last_login_attribute_name}>#{config.last_logout_attribute_name})")
+ where("#{config.last_activity_attribute_name} IS NOT NULL") \
+ .where("#{config.last_logout_attribute_name} IS NULL OR #{config.last_activity_attribute_name} > #{config.last_logout_attribute_name}") \
+ .where("#{config.last_activity_attribute_name} > ? ", config.activity_timeout.seconds.ago)
end
end
end
@@ -1,8 +1,8 @@
class AddActivityLoggingToUsers < ActiveRecord::Migration
def self.up
- add_column :users, :last_login, :datetime, :default => nil
- add_column :users, :last_logout, :datetime, :default => nil
- add_column :users, :last_activity, :datetime, :default => nil
+ add_column :users, :last_login, :datetime, :default => nil
+ add_column :users, :last_logout, :datetime, :default => nil
+ add_column :users, :last_activity, :datetime, :default => nil
add_index :users, [:last_login, :last_logout, :last_activity]
end
@@ -14,54 +14,64 @@
before(:all) do
plugin_model_configure([:activity_logging])
end
-
+
before(:each) do
create_new_user
end
-
+
after(:each) do
User.delete_all
end
-
+
it "should respond to 'logged_in_users'" do
subject.should respond_to(:logged_in_users)
end
-
+
it "'logged_in_users' should be empty when no users are logged in" do
subject.logged_in_users.size.should == 0
end
-
+
it "should log login time on login" do
now = Time.now
login_user
@user.last_login.should_not be_nil
@user.last_login.to_s(:db).should == now.to_s(:db)
end
-
+
it "should log logout time on logout" do
login_user
now = Time.now
logout_user
User.first.last_logout.should_not be_nil
User.first.last_logout.to_s(:db).should == now.to_s(:db)
end
-
+
+ it "should log last activity time when logged in" do
+ login_user
+ now = Time.now
+ get :some_action
+ User.first.last_activity.to_s(:db).should == now.to_s(:db)
+ end
+
it "'logged_in_users' should hold the user object when 1 user is logged in" do
login_user
+ get :some_action
subject.logged_in_users.size.should == 1
subject.logged_in_users[0].should == @user
end
-
+
it "'logged_in_users' should show all logged_in_users, whether they have logged out before or not." do
user1 = create_new_user({:username => 'gizmo1', :email => "bla1@bla.com", :password => 'secret1'})
- subject.send(:register_login_time_to_db, user1, [])
+ login_user(user1)
+ get :some_action
+ clear_user_without_logout
user2 = create_new_user({:username => 'gizmo2', :email => "bla2@bla.com", :password => 'secret2'})
- subject.send(:register_login_time_to_db, user2, [])
+ login_user(user2)
+ get :some_action
+ clear_user_without_logout
user3 = create_new_user({:username => 'gizmo3', :email => "bla3@bla.com", :password => 'secret3'})
- subject.send(:register_login_time_to_db, user3, [])
- subject.send(:register_logout_time_to_db, user3)
- sleep 1
- subject.send(:register_login_time_to_db, user3, [])
+ login_user(user3)
+ get :some_action
subject.logged_in_users.size.should == 3
subject.logged_in_users[0].should == user1
subject.logged_in_users[1].should == user2
View
@@ -62,22 +62,29 @@ def my_instance_method
end
end
+SUBMODUELS_AUTO_ADDED_CONTROLLER_FILTERS = [:register_last_activity_time_to_db, :deny_banned_user, :validate_session]
+
def create_new_user(attributes_hash = nil)
user_attributes_hash = attributes_hash || {:username => 'gizmo', :email => "bla@bla.com", :password => 'secret'}
@user = User.new(user_attributes_hash)
@user.save!
@user
end
-def login_user
- subject.send(:login_user,@user)
- subject.send(:after_login!,@user,['gizmo','secret'])
+def login_user(user = nil)
+ user ||= @user
+ subject.send(:login_user,user)
+ subject.send(:after_login!,user,[user.username,'secret'])
end
def logout_user
subject.send(:logout)
end
+def clear_user_without_logout
+ subject.instance_variable_set(:@logged_in_user,nil)
+end
+
# TODO: rename to sorcery_reload!(subs = [], model_opts = {}, controller_opts = {})
def plugin_model_configure(submodules = [], options = {})
reload_user_class
@@ -86,6 +93,11 @@ def plugin_model_configure(submodules = [], options = {})
::Sorcery::Controller::Config.init!
::Sorcery::Controller::Config.reset!
+ # remove all plugin before_filters so they won't fail other tests.
+ # I don't like this way, but I didn't find another.
+ # hopefully it won't break until Rails 4.
+ ApplicationController._process_action_callbacks.delete_if {|c| SUBMODUELS_AUTO_ADDED_CONTROLLER_FILTERS.include?(c.filter) }
+
# configure
::Sorcery::Controller::Config.submodules = submodules
::Sorcery::Controller::Config.user_class = nil # the next line will reset it to User
@@ -114,6 +126,8 @@ def plugin_set_controller_config_property(property, value)
private
+# reload user class between specs
+# so it will be possible to test the different submodules in isolation
def reload_user_class
Object.send(:remove_const,:User)
load 'user.rb'
@@ -26,7 +26,11 @@
plugin_set_model_config_property(:last_logout_attribute_name, :logout_time)
User.sorcery_config.last_logout_attribute_name.should equal(:logout_time)
end
-
+
+ it "should allow configuration option 'last_activity_attribute_name'" do
+ plugin_set_model_config_property(:last_activity_attribute_name, :activity_time)
+ User.sorcery_config.last_activity_attribute_name.should equal(:activity_time)
+ end
end
end

0 comments on commit 4e8b7b9

Please sign in to comment.