Guest User

Untitled

a guest
Apr 27th, 2018
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.81 KB | None | 0 0
  1. ## users.yml
  2.  
  3. quentin:
  4. id: 1
  5. login: quentin
  6. email: quentin@example.com
  7. account_id: 1
  8. first_name: Quentin
  9. last_name: Tarantino
  10. salt: 356a192b7913b04c54574d18c28d46e6395428ab # SHA1('0')
  11. crypted_password: 20c6cdf408c5349e7b962d029f9859aadb009488 # 'monkey'
  12. created_at: <%= 5.days.ago.to_s :db %>
  13. remember_token_expires_at: <%= 1.days.from_now.to_s %>
  14. remember_token: 77de68daecd823babbb58edb1c8e14d7106e83bb
  15.  
  16. aaron:
  17. id: 2
  18. account_id: 1
  19. login: aaron
  20. email: aaron@example.com
  21. salt: da4b9237bacccdf19c0760cab7aec4a8359010b0 # SHA1('1')
  22. crypted_password: 518a0ee1698b571410a9ab9b2f2c06821a085869 # 'monkey'
  23. created_at: <%= 1.days.ago.to_s :db %>
  24. remember_token_expires_at:
  25. remember_token:
  26.  
  27.  
  28. old_password_holder:
  29. id: 3
  30. account_id: 1
  31. login: old_password_holder
  32. email: salty_dog@example.com
  33. salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd
  34. crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test
  35. created_at: <%= 1.days.ago.to_s :db %>
  36.  
  37.  
  38. ## user_test.rb
  39.  
  40. require File.dirname(__FILE__) + '/../test_helper'
  41.  
  42. class UserTest < Test::Unit::TestCase
  43. # Be sure to include AuthenticatedTestHelper in test/test_helper.rb instead.
  44. # Then, you can remove it from this and the functional test.
  45. #include AuthenticatedTestHelper
  46. fixtures :users
  47.  
  48. def test_should_create_user
  49. assert_difference 'User.count' do
  50. user = create_user
  51. assert !user.new_record?, "#{user.errors.full_messages.to_sentence}"
  52. end
  53. end
  54.  
  55. def test_should_require_login
  56. assert_no_difference 'User.count' do
  57. u = create_user(:login => nil)
  58. assert u.errors.on(:login)
  59. end
  60. end
  61.  
  62. def test_should_require_password
  63. assert_no_difference 'User.count' do
  64. u = create_user(:password => nil)
  65. assert u.errors.on(:password)
  66. end
  67. end
  68.  
  69. def test_should_require_password_confirmation
  70. assert_no_difference 'User.count' do
  71. u = create_user(:password_confirmation => nil)
  72. assert u.errors.on(:password_confirmation)
  73. end
  74. end
  75.  
  76. def test_should_require_email
  77. assert_no_difference 'User.count' do
  78. u = create_user(:email => nil)
  79. assert u.errors.on(:email)
  80. end
  81. end
  82.  
  83. def test_should_reset_password
  84. users(:quentin).update_attributes(:password => 'new password', :password_confirmation => 'new password')
  85. assert_equal users(:quentin), User.authenticate('quentin', 'new password')
  86. end
  87.  
  88. def test_should_not_rehash_password
  89. users(:quentin).update_attributes(:login => 'quentin2')
  90. assert_equal users(:quentin), User.authenticate('quentin2', 'monkey')
  91. end
  92.  
  93. def test_should_authenticate_user
  94. assert_equal users(:quentin), User.authenticate('quentin', 'monkey')
  95. end
  96.  
  97. def test_should_set_remember_token
  98. users(:quentin).remember_me
  99. assert_not_nil users(:quentin).remember_token
  100. assert_not_nil users(:quentin).remember_token_expires_at
  101. end
  102.  
  103. def test_should_unset_remember_token
  104. users(:quentin).remember_me
  105. assert_not_nil users(:quentin).remember_token
  106. users(:quentin).forget_me
  107. assert_nil users(:quentin).remember_token
  108. end
  109.  
  110. def test_should_remember_me_for_one_week
  111. before = 1.week.from_now.utc
  112. users(:quentin).remember_me_for 1.week
  113. after = 1.week.from_now.utc
  114. assert_not_nil users(:quentin).remember_token
  115. assert_not_nil users(:quentin).remember_token_expires_at
  116. assert users(:quentin).remember_token_expires_at.between?(before, after)
  117. end
  118.  
  119. def test_should_remember_me_until_one_week
  120. time = 1.week.from_now.utc
  121. users(:quentin).remember_me_until time
  122. assert_not_nil users(:quentin).remember_token
  123. assert_not_nil users(:quentin).remember_token_expires_at
  124. assert_equal users(:quentin).remember_token_expires_at, time
  125. end
  126.  
  127. def test_should_remember_me_default_two_weeks
  128. before = 2.weeks.from_now.utc
  129. users(:quentin).remember_me
  130. after = 2.weeks.from_now.utc
  131. assert_not_nil users(:quentin).remember_token
  132. assert_not_nil users(:quentin).remember_token_expires_at
  133. assert users(:quentin).remember_token_expires_at.between?(before, after)
  134. end
  135.  
  136. protected
  137. def create_user(options = {})
  138. record = User.new({:account_id => 1, :login => 'quire', :first_name => 'John',
  139. :last_name => 'Doe', :is_admin => true, :email => 'quire@example.com',
  140. :password => 'quire69', :password_confirmation => 'quire69' }.merge(options))
  141. record.save
  142. record
  143. end
  144. end
  145.  
  146. ## models/user.rb
  147.  
  148. require 'digest/sha1'
  149.  
  150. class User < ActiveRecord::Base
  151. include Authentication
  152. include Authentication::ByPassword
  153. include Authentication::ByCookieToken
  154.  
  155. belongs_to :account
  156. has_many :encounters
  157. has_one :address, :as => :addressable
  158.  
  159. validates_associated :account
  160.  
  161. validates_presence_of :login
  162. validates_length_of :login, :within => 3..40
  163. validates_uniqueness_of :login, :case_sensitive => false
  164. validates_format_of :login, :with => RE_LOGIN_OK, :message => MSG_LOGIN_BAD
  165.  
  166. validates_format_of :first_name, :with => RE_NAME_OK, :message => MSG_NAME_BAD, :allow_nil => false
  167. validates_length_of :first_name, :within => 3..100
  168.  
  169. validates_format_of :last_name, :with => RE_NAME_OK, :message => MSG_NAME_BAD, :allow_nil => false
  170. validates_length_of :last_name, :within => 3..100
  171.  
  172. validates_presence_of :email
  173. validates_length_of :email, :within => 6..100 #r@a.wk
  174. validates_uniqueness_of :email, :case_sensitive => false
  175. validates_format_of :email, :with => RE_EMAIL_OK, :message => MSG_EMAIL_BAD
  176.  
  177. # HACK HACK HACK -- how to do attr_accessible from here?
  178. # prevents a user from submitting a crafted form that bypasses activation
  179. # anything else you want your user to change should be added here.
  180. attr_accessible :login, :email, :first_name, :last_name, :password, :password_confirmation, :organization
  181.  
  182. def full_name
  183. result = first_name + ' '
  184. unless middle_initial.blank?
  185. result += middle_initial + '. '
  186. end
  187. result += last_name
  188. result
  189. end
  190.  
  191. # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
  192. #
  193. # uff. this is really an authorization, not authentication routine.
  194. # We really need a Dispatch Chain here or something.
  195. # This will also let us return a human error message.
  196. #
  197. def self.authenticate(login, password)
  198. u = find_by_login(login) # need to get the salt
  199. u && u.authenticated?(password) ? u : nil
  200. end
  201.  
  202. protected
  203.  
  204. end
  205.  
  206. ## rake test:units
  207.  
  208.  
  209. Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader
  210. Started
  211. ........E...........
  212. Finished in 1.813008 seconds.
  213.  
  214. 1) Error:
  215. test_should_create_user(UserTest):
  216. 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')
  217. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:147:in `log'
  218. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/mysql_adapter.rb:299:in `execute'
  219. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:161:in `insert_sql'
  220. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/mysql_adapter.rb:309:in `insert_sql'
  221. /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'
  222. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/query_cache.rb:19:in `insert'
  223. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:2506:in `create_without_callbacks'
  224. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/callbacks.rb:220:in `create_without_timestamps'
  225. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/timestamp.rb:29:in `create'
  226. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:2472:in `create_or_update_without_callbacks'
  227. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/callbacks.rb:207:in `create_or_update'
  228. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:2200:in `save_without_validation'
  229. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/validations.rb:901:in `save_without_dirty'
  230. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/dirty.rb:75:in `save_without_transactions'
  231. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:106:in `save'
  232. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:66:in `transaction'
  233. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:79:in `transaction'
  234. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:98:in `transaction'
  235. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:106:in `save'
  236. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:118:in `rollback_active_record_state!'
  237. /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:106:in `save'
  238. ./test/unit/user_test.rb:102:in `create_user'
  239. ./test/unit/user_test.rb:11:in `test_should_create_user'
  240. /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/core_ext/test/unit/assertions.rb:41:in `assert_difference'
  241. ./test/unit/user_test.rb:10:in `test_should_create_user'
  242. /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
  243. /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/testing/setup_and_teardown.rb:33:in `run'
  244.  
  245. 20 tests, 32 assertions, 0 failures, 1 errors
  246. rake aborted!
  247. Command failed with status (1): [/usr/bin/ruby1.8 -Ilib:test "/usr/lib/ruby...]
  248.  
  249. (See full trace by running task with --trace)
Add Comment
Please, Sign In to add comment