2010-03-26 06:27:19 -04:00
|
|
|
require 'test_helper'
|
2009-11-24 12:18:42 -05:00
|
|
|
|
2013-01-22 20:17:17 -05:00
|
|
|
class TrackableHooksTest < ActionDispatch::IntegrationTest
|
2009-11-24 12:18:42 -05:00
|
|
|
|
|
|
|
test "current and last sign in timestamps are updated on each sign in" do
|
|
|
|
user = create_user
|
|
|
|
assert_nil user.current_sign_in_at
|
|
|
|
assert_nil user.last_sign_in_at
|
|
|
|
|
|
|
|
sign_in_as_user
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
assert_kind_of Time, user.current_sign_in_at
|
|
|
|
assert_kind_of Time, user.last_sign_in_at
|
|
|
|
|
|
|
|
assert_equal user.current_sign_in_at, user.last_sign_in_at
|
|
|
|
assert user.current_sign_in_at >= user.created_at
|
|
|
|
|
|
|
|
visit destroy_user_session_path
|
|
|
|
new_time = 2.seconds.from_now
|
|
|
|
Time.stubs(:now).returns(new_time)
|
|
|
|
|
|
|
|
sign_in_as_user
|
|
|
|
user.reload
|
|
|
|
assert user.current_sign_in_at > user.last_sign_in_at
|
|
|
|
end
|
|
|
|
|
|
|
|
test "current and last sign in remote ip are updated on each sign in" do
|
|
|
|
user = create_user
|
|
|
|
assert_nil user.current_sign_in_ip
|
|
|
|
assert_nil user.last_sign_in_ip
|
|
|
|
|
|
|
|
sign_in_as_user
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
assert_equal "127.0.0.1", user.current_sign_in_ip
|
|
|
|
assert_equal "127.0.0.1", user.last_sign_in_ip
|
|
|
|
end
|
2012-01-09 16:08:39 -05:00
|
|
|
|
2011-10-22 09:57:18 -04:00
|
|
|
test "current remote ip returns original ip behind a non transparent proxy" do
|
|
|
|
user = create_user
|
2012-01-09 16:08:39 -05:00
|
|
|
|
2012-01-10 09:43:15 -05:00
|
|
|
arbitrary_ip = '200.121.1.69'
|
2011-10-22 09:57:18 -04:00
|
|
|
sign_in_as_user do
|
|
|
|
header 'HTTP_X_FORWARDED_FOR', arbitrary_ip
|
|
|
|
end
|
|
|
|
user.reload
|
|
|
|
assert_equal arbitrary_ip, user.current_sign_in_ip
|
|
|
|
end
|
2009-11-24 12:18:42 -05:00
|
|
|
|
|
|
|
test "increase sign in count" do
|
|
|
|
user = create_user
|
2010-03-26 07:55:37 -04:00
|
|
|
assert_equal 0, user.sign_in_count
|
2009-11-24 12:18:42 -05:00
|
|
|
|
|
|
|
sign_in_as_user
|
|
|
|
user.reload
|
|
|
|
assert_equal 1, user.sign_in_count
|
|
|
|
|
|
|
|
visit destroy_user_session_path
|
|
|
|
sign_in_as_user
|
|
|
|
user.reload
|
|
|
|
assert_equal 2, user.sign_in_count
|
|
|
|
end
|
|
|
|
|
2010-01-05 07:44:13 -05:00
|
|
|
test "does not update anything if user has signed out along the way" do
|
2014-02-25 11:42:55 -05:00
|
|
|
swap Devise, allow_unconfirmed_access_for: 0.days do
|
|
|
|
user = create_user(confirm: false)
|
2009-11-24 12:29:46 -05:00
|
|
|
sign_in_as_user
|
2009-11-24 12:18:42 -05:00
|
|
|
|
2009-11-24 12:29:46 -05:00
|
|
|
user.reload
|
|
|
|
assert_nil user.current_sign_in_at
|
|
|
|
assert_nil user.last_sign_in_at
|
|
|
|
end
|
2009-11-24 12:18:42 -05:00
|
|
|
end
|
2012-01-09 16:08:39 -05:00
|
|
|
|
2011-05-04 09:21:12 -04:00
|
|
|
test "do not track if devise.skip_trackable is set" do
|
2011-05-04 08:44:37 -04:00
|
|
|
user = create_user
|
|
|
|
sign_in_as_user do
|
2011-05-04 09:21:12 -04:00
|
|
|
header 'devise.skip_trackable', '1'
|
2011-05-04 08:44:37 -04:00
|
|
|
end
|
|
|
|
user.reload
|
|
|
|
assert_equal 0, user.sign_in_count
|
|
|
|
visit destroy_user_session_path
|
2012-01-09 16:08:39 -05:00
|
|
|
|
2011-05-04 08:44:37 -04:00
|
|
|
sign_in_as_user do
|
2011-05-04 09:21:12 -04:00
|
|
|
header 'devise.skip_trackable', false
|
2011-05-04 08:44:37 -04:00
|
|
|
end
|
|
|
|
user.reload
|
|
|
|
assert_equal 1, user.sign_in_count
|
|
|
|
end
|
2011-05-04 09:21:12 -04:00
|
|
|
|
2009-11-24 12:18:42 -05:00
|
|
|
end
|