Permalink
Browse files

shouldafied restful_authentication test for user.

  • Loading branch information...
1 parent f915efd commit 9f8b417b4ebf51635316fe6a2faa90199ec7846d @technicalpickles technicalpickles committed Aug 7, 2008
Showing with 117 additions and 86 deletions.
  1. +117 −86 test/unit/user_test.rb
View
@@ -1,98 +1,129 @@
require File.dirname(__FILE__) + '/../test_helper'
class UserTest < Test::Unit::TestCase
- fixtures :users
-
- def test_should_create_user
- assert_difference 'User.count' do
- user = create_user
- assert !user.new_record?, "#{user.errors.full_messages.to_sentence}"
+ should_have_many :stars
+ should_have_many :starred_items
+
+ should_require_attributes :login, :password, :password_confirmation #, :email
+ should_ensure_length_in_range :password, (4..40)
+ should_ensure_length_in_range :login, (3..40)
+
+ # TODO test that only some fields are accessible
+
+ context 'A user' do
+ setup do
+ @user = Factory(:user, :login => 'login', :password => 'password', :password_confirmation => 'password')
end
- end
-
- def test_should_require_login
- assert_no_difference 'User.count' do
- u = create_user(:login => nil)
- assert u.errors.on(:login)
+
+ should_require_unique_attributes :login
+ should_allow_values_for :login, 'mylogin', 'woooooo123'
+ should_not_allow_values_for :login, 'my login'
+
+ should_allow_values_for :url, 'http://example.com'
+ should_not_allow_values_for :url, 'example.com'
+
+ should 'be able to authenticate with correct password' do
+ assert_equal @user, User.authenticate('login', 'password')
end
- end
-
- def test_should_require_password
- assert_no_difference 'User.count' do
- u = create_user(:password => nil)
- assert u.errors.on(:password)
+
+ should 'not be able to authenticate with incorrect password' do
+ assert_nil User.authenticate('login', 'notpassword')
end
- end
-
- def test_should_require_password_confirmation
- assert_no_difference 'User.count' do
- u = create_user(:password_confirmation => nil)
- assert u.errors.on(:password_confirmation)
+
+ context 'updating their password' do
+ setup do
+ @user.update_attributes(:password => 'new password', :password_confirmation => 'new password')
+ end
+
+ should 'be able to login with new password' do
+ assert_equal @user, User.authenticate('login', 'new password')
+ end
end
- end
-
- def test_should_require_email
- assert_no_difference 'User.count' do
- u = create_user(:email => nil)
- assert u.errors.on(:email)
+
+ context 'updating their login' do
+ setup do
+ @user.update_attributes(:login => 'newlogin')
+ end
+
+ should_eventually 'not rehash password' do
+ assert_equal @user, User.authenticate('newlogin', 'password')
+ end
end
- end
-
- def test_should_reset_password
- users(:quentin).update_attributes(:password => 'new password', :password_confirmation => 'new password')
- assert_equal users(:quentin), User.authenticate('quentin', 'new password')
- end
-
- def test_should_not_rehash_password
- users(:quentin).update_attributes(:login => 'quentin2')
- assert_equal users(:quentin), User.authenticate('quentin2', 'test')
- end
-
- def test_should_authenticate_user
- assert_equal users(:quentin), User.authenticate('quentin', 'test')
- end
-
- def test_should_set_remember_token
- users(:quentin).remember_me
- assert_not_nil users(:quentin).remember_token
- assert_not_nil users(:quentin).remember_token_expires_at
- end
-
- def test_should_unset_remember_token
- users(:quentin).remember_me
- assert_not_nil users(:quentin).remember_token
- users(:quentin).forget_me
- assert_nil users(:quentin).remember_token
- end
-
- def test_should_remember_me_for_one_week
- before = 1.week.from_now.utc
- users(:quentin).remember_me_for 1.week
- after = 1.week.from_now.utc
- assert_not_nil users(:quentin).remember_token
- assert_not_nil users(:quentin).remember_token_expires_at
- assert users(:quentin).remember_token_expires_at.between?(before, after)
- end
-
- def test_should_remember_me_until_one_week
- time = 1.week.from_now.utc
- users(:quentin).remember_me_until time
- assert_not_nil users(:quentin).remember_token
- assert_not_nil users(:quentin).remember_token_expires_at
- assert_equal users(:quentin).remember_token_expires_at, time
- end
+
+ context 'being remembered for default amount of time' do
+ setup do
+ @before_time = 52.week.from_now.utc
+ @user.remember_me
+ @after_time = 52.weeks.from_now.utc
+ end
+
+ should 'set remember token' do
+ assert_not_nil @user.remember_token
+ end
+
+ should 'set remember token expires at' do
+ assert_not_nil @user.remember_token_expires_at
+ end
+
+ should 'remember for 52 weeks' do
+ assert @user.remember_token_expires_at.between?(@before_time, @after_time)
+ end
+
+ context 'and then being forgotten' do
+ setup do
+ @user.forget_me
+ end
+
+ should 'unset remember token' do
+ assert_nil @user.remember_token
+ end
+ end
+ end
+
+ context 'being remember until 1 week from now' do
+ setup do
+ @time = 1.week.from_now.utc
- def test_should_remember_me_default_two_weeks
- before = 2.weeks.from_now.utc
- users(:quentin).remember_me
- after = 2.weeks.from_now.utc
- assert_not_nil users(:quentin).remember_token
- assert_not_nil users(:quentin).remember_token_expires_at
- assert users(:quentin).remember_token_expires_at.between?(before, after)
+ @user.remember_me_until @time
+ end
+
+ should 'set remember token' do
+ assert_not_nil @user.remember_token
+ end
+
+ should 'set remember token expires at' do
+ assert_not_nil @user.remember_token_expires_at
+ end
+
+ should 'expire in 1 week' do
+ assert_equal @time, @user.remember_token_expires_at
+ end
+ end
+
+ context 'being remembered for 1 week' do
+ setup do
+ @before_time = 1.week.from_now.utc
+ @user.remember_me_for 1.week
+ @after_time = 1.weeks.from_now.utc
+ end
+
+ should 'set remember token' do
+ assert_not_nil @user.remember_token
+ end
+
+ should 'set remember token expires at' do
+ assert_not_nil @user.remember_token_expires_at
+ end
+
+ should 'remember for 1 weeks' do
+ assert @user.remember_token_expires_at.between?(@before_time, @after_time)
+ end
+ end
end
-
-protected
- def create_user(options = {})
- User.create({ :login => 'quire', :email => 'quire@example.com', :password => 'quire', :password_confirmation => 'quire' }.merge(options))
+
+ context 'A non existant user' do
+ should 'not be able to authenticate' do
+ assert_nil User.authenticate('nonexistentlogin', 'password')
+ end
end
end

0 comments on commit 9f8b417

Please sign in to comment.