Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## users.yml
- quentin:
- id: 1
- login: quentin
- email: quentin@example.com
- account_id: 1
- first_name: Quentin
- last_name: Tarantino
- salt: 356a192b7913b04c54574d18c28d46e6395428ab # SHA1('0')
- crypted_password: 20c6cdf408c5349e7b962d029f9859aadb009488 # 'monkey'
- created_at: <%= 5.days.ago.to_s :db %>
- remember_token_expires_at: <%= 1.days.from_now.to_s %>
- remember_token: 77de68daecd823babbb58edb1c8e14d7106e83bb
- aaron:
- id: 2
- account_id: 1
- login: aaron
- email: aaron@example.com
- salt: da4b9237bacccdf19c0760cab7aec4a8359010b0 # SHA1('1')
- crypted_password: 518a0ee1698b571410a9ab9b2f2c06821a085869 # 'monkey'
- created_at: <%= 1.days.ago.to_s :db %>
- remember_token_expires_at:
- remember_token:
- old_password_holder:
- id: 3
- account_id: 1
- login: old_password_holder
- email: salty_dog@example.com
- salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd
- crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test
- created_at: <%= 1.days.ago.to_s :db %>
- ## user_test.rb
- require File.dirname(__FILE__) + '/../test_helper'
- class UserTest < Test::Unit::TestCase
- # Be sure to include AuthenticatedTestHelper in test/test_helper.rb instead.
- # Then, you can remove it from this and the functional test.
- #include AuthenticatedTestHelper
- 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}"
- end
- end
- def test_should_require_login
- assert_no_difference 'User.count' do
- u = create_user(:login => nil)
- assert u.errors.on(:login)
- end
- end
- def test_should_require_password
- assert_no_difference 'User.count' do
- u = create_user(:password => nil)
- assert u.errors.on(:password)
- 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)
- end
- end
- def test_should_require_email
- assert_no_difference 'User.count' do
- u = create_user(:email => nil)
- assert u.errors.on(:email)
- 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', 'monkey')
- end
- def test_should_authenticate_user
- assert_equal users(:quentin), User.authenticate('quentin', 'monkey')
- 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
- 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)
- end
- protected
- def create_user(options = {})
- record = User.new({:account_id => 1, :login => 'quire', :first_name => 'John',
- :last_name => 'Doe', :is_admin => true, :email => 'quire@example.com',
- :password => 'quire69', :password_confirmation => 'quire69' }.merge(options))
- record.save
- record
- end
- end
- ## models/user.rb
- require 'digest/sha1'
- class User < ActiveRecord::Base
- include Authentication
- include Authentication::ByPassword
- include Authentication::ByCookieToken
- belongs_to :account
- has_many :encounters
- has_one :address, :as => :addressable
- validates_associated :account
- validates_presence_of :login
- validates_length_of :login, :within => 3..40
- validates_uniqueness_of :login, :case_sensitive => false
- validates_format_of :login, :with => RE_LOGIN_OK, :message => MSG_LOGIN_BAD
- validates_format_of :first_name, :with => RE_NAME_OK, :message => MSG_NAME_BAD, :allow_nil => false
- validates_length_of :first_name, :within => 3..100
- validates_format_of :last_name, :with => RE_NAME_OK, :message => MSG_NAME_BAD, :allow_nil => false
- validates_length_of :last_name, :within => 3..100
- validates_presence_of :email
- validates_length_of :email, :within => 6..100 #r@a.wk
- validates_uniqueness_of :email, :case_sensitive => false
- validates_format_of :email, :with => RE_EMAIL_OK, :message => MSG_EMAIL_BAD
- # HACK HACK HACK -- how to do attr_accessible from here?
- # prevents a user from submitting a crafted form that bypasses activation
- # anything else you want your user to change should be added here.
- attr_accessible :login, :email, :first_name, :last_name, :password, :password_confirmation, :organization
- def full_name
- result = first_name + ' '
- unless middle_initial.blank?
- result += middle_initial + '. '
- end
- result += last_name
- result
- end
- # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
- #
- # uff. this is really an authorization, not authentication routine.
- # We really need a Dispatch Chain here or something.
- # This will also let us return a human error message.
- #
- def self.authenticate(login, password)
- u = find_by_login(login) # need to get the salt
- u && u.authenticated?(password) ? u : nil
- end
- protected
- end
- ## rake test:units
- Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader
- Started
- ........E...........
- Finished in 1.813008 seconds.
- 1) Error:
- test_should_create_user(UserTest):
- ActiveRecord::StatementInvalid: Mysql::Error: Column 'account_id' cannot be null: INSERT INTO `users` (`salt`, `updated_at`, `crypted_password`, `account_id`, `remember_token_expires_at`, `is_admin`, `remember_token`, `first_name`, `middle_initial`, `login`, `last_name`, `email`, `created_at`) VALUES('764c4dd12678d5d5d416db6da20a053c44901496', '2008-06-09 21:17:45', '028b520074a553f29f63230e122fa2b0e74c5f2f', NULL, NULL, 0, NULL, 'John', NULL, 'quire', 'Doe', 'quire@example.com', '2008-06-09 21:17:45')
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:147:in `log'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/mysql_adapter.rb:299:in `execute'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:161:in `insert_sql'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/mysql_adapter.rb:309:in `insert_sql'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:44:in `insert_without_query_dirty'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/query_cache.rb:19:in `insert'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:2506:in `create_without_callbacks'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/callbacks.rb:220:in `create_without_timestamps'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/timestamp.rb:29:in `create'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:2472:in `create_or_update_without_callbacks'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/callbacks.rb:207:in `create_or_update'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:2200:in `save_without_validation'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/validations.rb:901:in `save_without_dirty'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/dirty.rb:75:in `save_without_transactions'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:106:in `save'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:66:in `transaction'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:79:in `transaction'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:98:in `transaction'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:106:in `save'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:118:in `rollback_active_record_state!'
- /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:106:in `save'
- ./test/unit/user_test.rb:102:in `create_user'
- ./test/unit/user_test.rb:11:in `test_should_create_user'
- /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/core_ext/test/unit/assertions.rb:41:in `assert_difference'
- ./test/unit/user_test.rb:10:in `test_should_create_user'
- /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
- /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/testing/setup_and_teardown.rb:33:in `run'
- 20 tests, 32 assertions, 0 failures, 1 errors
- rake aborted!
- Command failed with status (1): [/usr/bin/ruby1.8 -Ilib:test "/usr/lib/ruby...]
- (See full trace by running task with --trace)
Add Comment
Please, Sign In to add comment