wristband 0.0.0 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/Gemfile +2 -1
  2. data/Gemfile.lock +3 -3
  3. data/README.md +121 -38
  4. data/Rakefile +1 -0
  5. data/VERSION +1 -1
  6. data/app/controllers/application_controller.rb +0 -6
  7. data/app/controllers/passwords_controller.rb +43 -0
  8. data/app/controllers/sessions_controller.rb +6 -7
  9. data/app/controllers/users_controller.rb +1 -26
  10. data/app/helpers/form_helper.rb +8 -0
  11. data/app/mailers/user_mailer.rb +4 -26
  12. data/app/models/user.rb +1 -2
  13. data/app/views/passwords/edit.html.haml +7 -0
  14. data/app/views/passwords/new.html.haml +15 -0
  15. data/app/views/sessions/new.html.haml +1 -5
  16. data/app/views/user_mailer/password_reset.html.haml +7 -0
  17. data/app/views/user_mailer/password_reset.text.haml +6 -0
  18. data/config/environment.rb +1 -0
  19. data/config/initializers/formatted_form_builder.rb +141 -0
  20. data/config/routes.rb +6 -9
  21. data/db/migrate/01_create_users_table.rb +5 -3
  22. data/db/schema.rb +7 -4
  23. data/lib/generators/wristband/wristband_generator.rb +26 -15
  24. data/lib/wristband/application_extensions.rb +1 -0
  25. data/lib/wristband/user_extensions.rb +7 -17
  26. data/lib/wristband.rb +1 -3
  27. data/test/dummy/user.rb +1 -1
  28. data/test/functional/passwords_controller_test.rb +62 -0
  29. data/test/functional/sessions_controller_test.rb +93 -0
  30. data/test/test_helper.rb +1 -5
  31. data/test/unit/has_authorities_test.rb +1 -8
  32. data/test/unit/session_user_test.rb +46 -4
  33. data/test/unit/user_mailer_test.rb +18 -0
  34. data/test/unit/user_test.rb +1 -72
  35. data/test/unit/wristband_test.rb +74 -0
  36. data/wristband.gemspec +27 -10
  37. metadata +62 -29
  38. data/app/views/user_mailer/email_verification.text.html.rhtml +0 -7
  39. data/app/views/user_mailer/email_verification.text.plain.rhtml +0 -9
  40. data/app/views/user_mailer/forgot_password.text.html.rhtml +0 -10
  41. data/app/views/user_mailer/forgot_password.text.plain.rhtml +0 -10
@@ -2,10 +2,9 @@ class CreateUsersTable < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :users do |t|
4
4
  t.string :email
5
- t.string :email_validation_key
6
- t.datetime :validated_at
7
- t.string :password_crypt, :limit => 40
5
+ t.string :password_hash, :limit => 40
8
6
  t.string :password_salt, :limit => 40
7
+ t.string :perishable_token
9
8
  t.string :remember_token
10
9
  t.string :role
11
10
  t.timestamps
@@ -18,6 +17,9 @@ class CreateUsersTable < ActiveRecord::Migration
18
17
  # t.string :zip
19
18
  # t.string :ip
20
19
  end
20
+ add_index :users, :email
21
+ add_index :users, :perishable_token
22
+ add_index :users, :remember_token
21
23
  end
22
24
 
23
25
  def self.down
data/db/schema.rb CHANGED
@@ -14,14 +14,17 @@ ActiveRecord::Schema.define(:version => 1) do
14
14
 
15
15
  create_table "users", :force => true do |t|
16
16
  t.string "email"
17
- t.string "email_validation_key"
18
- t.datetime "validated_at"
19
- t.string "password_crypt", :limit => 40
20
- t.string "password_salt", :limit => 40
17
+ t.string "password_hash", :limit => 40
18
+ t.string "password_salt", :limit => 40
19
+ t.string "perishable_token"
21
20
  t.string "remember_token"
22
21
  t.string "role"
23
22
  t.datetime "created_at"
24
23
  t.datetime "updated_at"
25
24
  end
26
25
 
26
+ add_index "users", ["email"], :name => "index_users_on_email"
27
+ add_index "users", ["perishable_token"], :name => "index_users_on_perishable_token"
28
+ add_index "users", ["remember_token"], :name => "index_users_on_remember_token"
29
+
27
30
  end
@@ -4,29 +4,50 @@ class WristbandGenerator < Rails::Generators::Base
4
4
  source_root File.expand_path('../../../..', __FILE__)
5
5
 
6
6
  def generate_models
7
+ puts "\t== Models =="
7
8
  copy_file 'app/models/user.rb', 'app/models/user.rb'
8
9
  copy_file 'app/models/session_user.rb', 'app/models/session_user.rb'
9
10
  end
10
11
 
11
12
  def generate_controllers
13
+ puts "\t== Controllers =="
12
14
  copy_file 'app/controllers/users_controller.rb', 'app/controllers/users_controller.rb'
13
15
  copy_file 'app/controllers/sessions_controller.rb', 'app/controllers/sessions_controller.rb'
16
+ copy_file 'app/controllers/passwords_controller.rb', 'app/controllers/passwords_controller.rb'
14
17
  end
15
18
 
16
19
  def generate_views
20
+ puts "\t== Views =="
17
21
  copy_file 'app/views/users/show.html.haml', 'app/views/users/show.html.haml'
18
22
  copy_file 'app/views/sessions/new.html.haml', 'app/views/sessions/new.html.haml'
19
-
23
+ copy_file 'app/views/passwords/new.html.haml', 'app/views/passwords/new.html.haml'
24
+ copy_file 'app/views/passwords/edit.html.haml', 'app/views/passwords/edit.html.haml'
20
25
  end
26
+
21
27
  def generate_user_mailer
28
+ puts "\t== User Mailer =="
22
29
  copy_file 'app/mailers/user_mailer.rb', 'app/mailers/user_mailer.rb'
23
- copy_file 'app/views/user_mailer/forgot_password.text.html.rhtml', 'app/views/user_mailer/forgot_password.text.html.rhtml'
24
- copy_file 'app/views/user_mailer/forgot_password.text.plain.rhtml', 'app/views/user_mailer/forgot_password.text.plain.rhtml'
25
- copy_file 'app/views/user_mailer/email_verification.text.html.rhtml', 'app/views/user_mailer/email_verification.text.html.rhtml'
26
- copy_file 'app/views/user_mailer/email_verification.text.plain.rhtml', 'app/views/user_mailer/email_verification.text.plain.rhtml'
30
+ copy_file'app/views/user_mailer/password_reset.html.haml', 'app/views/user_mailer/password_reset.html.haml'
31
+ copy_file 'app/views/user_mailer/password_reset.text.haml', 'app/views/user_mailer/password_reset.text.haml'
27
32
  end
28
33
 
34
+ def generate_tests
35
+ puts "\t== Test helper and Dummies =="
36
+ copy_file 'test/test_helper.rb', 'test/test_helper.rb'
37
+ copy_file 'test/dummy/user.rb', 'test/dummy/user.rb'
38
+
39
+ puts "\t== Unit tests =="
40
+ copy_file 'test/unit/user_test.rb', 'test/unit/user_test.rb'
41
+ copy_file 'test/unit/session_user_test.rb', 'test/unit/session_user_test.rb'
42
+ copy_file 'test/unit/user_mailer_test.rb', 'test/unit/user_mailer_test.rb'
43
+
44
+ puts "\t== Functional tests =="
45
+ copy_file 'test/functional/sessions_controller_test.rb', 'test/functional/sessions_controller_test.rb'
46
+ copy_file 'test/functional/passwords_controller_test.rb', 'test/functional/passwords_controller_test.rb'
47
+ end
48
+
29
49
  def generate_migration
50
+ puts "\t== Migration =="
30
51
  destination = File.expand_path('db/migrate/01_create_users_table.rb', self.destination_root)
31
52
  migration_dir = File.dirname(destination)
32
53
  destination = self.class.migration_exists?(migration_dir, 'create_users_table')
@@ -43,14 +64,4 @@ class WristbandGenerator < Rails::Generators::Base
43
64
  require "rails/generators/#{orm}"
44
65
  "#{orm.to_s.camelize}::Generators::Base".constantize.next_migration_number(dirname)
45
66
  end
46
-
47
- # def generate_routes
48
- # route "get '/login', :to => 'sessions#new'"
49
- # route "post '/login', :to => 'sessions#create'"
50
- # route "get '/logout', :to => 'sessions#destroy'"
51
- # route "match '/forgot_password', :to => 'users#forgot_password'"
52
- # route "resources :users"
53
- # route "match '/register', :to => 'users#new'"
54
- # end
55
-
56
67
  end
@@ -27,6 +27,7 @@ module Wristband
27
27
  # Logs a user out and deletes the remember_token.
28
28
  def logout
29
29
  current_user.update_attribute(:remember_token, nil) if current_user
30
+ self.current_user = nil
30
31
  cookies.delete(:login_token)
31
32
  reset_session
32
33
  end
@@ -28,16 +28,7 @@ module Wristband
28
28
  end
29
29
  end
30
30
  end
31
-
32
- def verify_email!(email_validation_key)
33
- if user = find_by_email_validation_key(email_validation_key)
34
- user.update_attribute(:validated_at, Time.now.to_s(:db))
35
- user
36
- else
37
- raise UserVerificationError, 'We were not able to verify your account or it may have been verified already. Please contact us for assistance.'.t
38
- end
39
- end
40
-
31
+
41
32
  def roles_for_select
42
33
  self.class.wristband[:roles].collect{ |k| [ k.to_s.titleize, k.to_s] }
43
34
  end
@@ -72,16 +63,15 @@ module Wristband
72
63
  self.send(self.class.wristband[:password_column]) == Wristband::Support.encrypt_with_salt(string, self.password_salt)
73
64
  end
74
65
 
75
- def password_crypted?
76
- self.password_salt and !self.password_salt.empty?
77
- end
78
-
79
- def password_crypt=(value)
80
- if (value != read_attribute(:password_crypt))
66
+ def password_hash=(value)
67
+ if (value != read_attribute(:password_hash))
81
68
  initialize_token
82
69
  end
70
+ write_attribute(:password_hash, value)
71
+ end
83
72
 
84
- write_attribute(:password_crypt, value)
73
+ def reset_perishable_token!
74
+ update_attribute(:perishable_token, Wristband::Support.random_salt.gsub(/[^A-Za-z0-9]/,''))
85
75
  end
86
76
 
87
77
  end
data/lib/wristband.rb CHANGED
@@ -26,7 +26,7 @@ module Wristband
26
26
  class_eval do
27
27
  include Wristband::UserExtensions
28
28
 
29
- options[:password_column] ||= :password_crypt
29
+ options[:password_column] ||= :password_hash
30
30
 
31
31
  # These two are used on the login form
32
32
  attr_accessor :password
@@ -82,5 +82,3 @@ end
82
82
 
83
83
  ActiveRecord::Base.send(:extend, Wristband::ClassMethods)
84
84
  ActionController::Base.send(:include, Wristband::ApplicationExtensions)
85
-
86
- class UserVerificationError < StandardError; end
data/test/dummy/user.rb CHANGED
@@ -4,7 +4,7 @@ TestDummy.declare(User) do
4
4
  end
5
5
 
6
6
  dummy :password do
7
- 'passpass'
7
+ 'password'
8
8
  end
9
9
 
10
10
  dummy :role do
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+
3
+ class PasswordsControllerTest < ActionController::TestCase
4
+ def test_new
5
+ get :new
6
+ assert_response :success
7
+ assert_template 'new'
8
+ end
9
+
10
+ def test_create
11
+ user = a User
12
+ assert_created user
13
+ assert_difference 'ActionMailer::Base.deliveries.size', 1 do
14
+ post :create, :email => user.email
15
+ assert_equal 'Email to reset password successfully sent.', flash[:notice]
16
+ assert_redirected_to login_path
17
+ user.reload
18
+ assert_not_nil user.perishable_token
19
+ end
20
+ response = ActionMailer::Base.deliveries.last
21
+ assert_equal 2, response.parts.length
22
+ response.parts.each do |part|
23
+ assert_match /#{user.perishable_token}/, part.body.to_s
24
+ end
25
+ assert_equal user.email, response.to[0]
26
+ end
27
+
28
+ def test_edit
29
+ user = a User
30
+ user.reset_perishable_token!
31
+ get :edit, :id => user.perishable_token
32
+ assert_response :success
33
+ assert_template 'edit'
34
+ end
35
+
36
+ def test_edit_redirects_on_invalid_perishable_token
37
+ user = a User
38
+ user.reset_perishable_token!
39
+ get :edit, :id => user.perishable_token + 'bogus'
40
+ assert_redirected_to login_path
41
+ end
42
+
43
+ def test_update
44
+ user = a User
45
+ user.reset_perishable_token!
46
+ assert !user.password_match?('newpassword')
47
+ put :update, :id => user.perishable_token, :user => {:password => 'newpassword', :password_confirmation => 'newpassword'}
48
+ assert_redirected_to user_path(user)
49
+ user.reload
50
+ assert_nil user.perishable_token
51
+ assert user.password_match?('newpassword')
52
+ end
53
+
54
+ def test_update_redirects_on_invalid_perishable_token
55
+ user = a User
56
+ user.reset_perishable_token!
57
+ put :update, :id => user.perishable_token + 'bogus', :user => {:password => 'newpassword', :password_confirmation => 'newpassword'}
58
+ assert_redirected_to login_path
59
+ user.reload
60
+ assert_not_nil user.perishable_token
61
+ end
62
+ end
@@ -0,0 +1,93 @@
1
+ require 'test_helper'
2
+
3
+ class SessionsControllerTest < ActionController::TestCase
4
+ def setup
5
+ @regular_user = a User
6
+ @regular_user.update_attribute(:role, 'regular_user')
7
+ assert_created @regular_user
8
+
9
+ assert !@regular_user.is_admin?
10
+ end
11
+
12
+ # >> Login -----------------------------------------------------------
13
+
14
+ def test_get_new
15
+ get :new
16
+ assert_response :success
17
+ assert_template 'new'
18
+ assert !@controller.logged_in?
19
+ assert_nil @controller.current_user
20
+ end
21
+
22
+ def test_should_login_with_email_and_redirect
23
+ post :create, :session_user => { :email => @regular_user.email, :password => @regular_user.password }
24
+ assert_equal "Welcome, you are now logged in.", flash[:notice]
25
+ assert_equal session[:user_id], @regular_user.id
26
+ assert_redirected_to user_path(@regular_user)
27
+ assert @controller.logged_in?
28
+ assert_equal @controller.current_user, @regular_user
29
+ end
30
+
31
+ def test_login_errors
32
+ post :create, :session_user => { :email => 'a', :password => 'b' }
33
+ assert_equal "Login failed. Did you mistype?", flash[:alert]
34
+ assert_nil session[:user]
35
+ assert_response :success
36
+ assert_template 'new'
37
+ assert !@controller.logged_in?
38
+ assert_nil @controller.current_user
39
+
40
+ assert !assigns(:session_user).valid?
41
+ assert_errors_on assigns(:session_user), :email, :password
42
+ assert assigns(:session_user).errors[:email].include?("The email address you entered is not valid")
43
+ assert assigns(:session_user).errors[:email].include?("The email address you entered is to short")
44
+ assert assigns(:session_user).errors[:password].include?("The password you entered is too short (minimum is 4 characters)")
45
+ end
46
+
47
+ def test_new_redirects_if_logged_in
48
+ login_as(@regular_user)
49
+ assert_equal session[:user_id], @regular_user.id
50
+ assert @controller.logged_in?
51
+ assert_equal @controller.current_user, @regular_user
52
+
53
+ get :new
54
+ assert_redirected_to user_path(@regular_user)
55
+ end
56
+
57
+ def test_create_redirects_if_logged_in
58
+ login_as(@regular_user)
59
+ assert_equal session[:user_id], @regular_user.id
60
+ assert @controller.logged_in?
61
+ assert_equal @controller.current_user, @regular_user
62
+
63
+ post :create, :session_user => { :email => @regular_user.email, :password => @regular_user.password }
64
+ assert_redirected_to user_path(@regular_user)
65
+ end
66
+
67
+ # >> Logout -----------------------------------------------------------
68
+
69
+ def test_should_logout
70
+ login_as(@regular_user)
71
+ assert_equal session[:user_id], @regular_user.id
72
+ assert @controller.logged_in?
73
+ assert_equal @controller.current_user, @regular_user
74
+
75
+ get :destroy
76
+ assert_redirected_to login_path
77
+ assert_nil cookies[:login_token]
78
+ assert_nil session[:user_id]
79
+ @regular_user.reload
80
+ assert_nil @regular_user.remember_token
81
+ assert !@controller.logged_in?
82
+ assert_nil @controller.current_user
83
+ end
84
+
85
+ # >> Remember me -----------------------------------------------------------
86
+
87
+ def test_remember_me
88
+ post :create, :session_user => { :email => @regular_user.email, :password => @regular_user.password , :remember_me => '1' }
89
+ assert_equal request.session[:user_id], @regular_user.id
90
+ assert_not_nil assigns(:session_user).user.remember_token
91
+ assert_equal assigns(:session_user).user.remember_token, cookies['login_token']
92
+ end
93
+ end
data/test/test_helper.rb CHANGED
@@ -3,7 +3,7 @@ require File.expand_path('../../config/environment', __FILE__)
3
3
  require 'rails/test_help'
4
4
 
5
5
  class ActiveSupport::TestCase
6
- self.use_transactional_fixtures = false
6
+ self.use_transactional_fixtures = true
7
7
  self.use_instantiated_fixtures = false
8
8
 
9
9
  def data_path(name)
@@ -42,10 +42,8 @@ class ActiveSupport::TestCase
42
42
  if (v != map[k])
43
43
  a << k
44
44
  end
45
-
46
45
  a
47
46
  end
48
-
49
47
  assert_equal map, result_map, "Difference: #{map.slice(*differences).inspect} vs #{result_map.slice(*differences).inspect}"
50
48
  end
51
49
 
@@ -64,9 +62,7 @@ class ActiveSupport::TestCase
64
62
  end
65
63
 
66
64
  session[:user_id] = user.id
67
-
68
65
  token = Wristband::Support.encrypt_with_salt(user.id.to_s, Time.now.to_f.to_s)
69
-
70
66
  cookies[:login_token] = {
71
67
  :value => token,
72
68
  :expires => 2.weeks.from_now.utc
@@ -33,16 +33,9 @@ class NewUser < ActiveRecord::Base
33
33
  columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
34
34
  end
35
35
 
36
- column :email, :string
37
- column :to_email, :string
38
- column :article_id, :integer
39
- column :message, :text
40
36
  column :email
41
- column :email_validation_key
42
- column :validated_at
43
- column :password_crypt
37
+ column :password_hash
44
38
  column :password_salt
45
- column :remember_token
46
39
  column :role
47
40
 
48
41
  wristband :has_authorities => true
@@ -1,8 +1,50 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class SessionUserTest < ActiveSupport::TestCase
4
- # Replace this with your real tests.
5
- test "the truth" do
6
- assert true
4
+
5
+ def test_create_defaults
6
+ session_user = SessionUser.new(
7
+ :email => 'test@test.com',
8
+ :password => 'password'
9
+ )
10
+ assert session_user.valid?
11
+ assert_equal session_user.email, 'test@test.com'
12
+ assert_equal session_user.password, 'password'
13
+ assert_nil session_user.user
7
14
  end
8
- end
15
+
16
+ def test_create_requirements
17
+ session_user = SessionUser.new
18
+ assert !session_user.valid?
19
+ assert_errors_on session_user, :email, :password
20
+ assert session_user.errors[:email].include?("Please enter your email address")
21
+ assert session_user.errors[:email].include?("The email address you entered is not valid")
22
+ assert session_user.errors[:email].include?("The email address you entered is to short")
23
+ assert session_user.errors[:password].include?("Please choose a password")
24
+ assert session_user.errors[:password].include?("The password you entered is too short (minimum is 4 characters)")
25
+ end
26
+
27
+ def test_successful_authentication
28
+ user = a User
29
+ session_user = SessionUser.create(
30
+ :email => user.email,
31
+ :password => user.password
32
+ )
33
+ assert session_user.valid?
34
+ assert_equal session_user.email, user.email
35
+ assert_equal session_user.password, user.password
36
+ assert_equal session_user.user, user
37
+ end
38
+
39
+ def test_failed_authentication
40
+ user = a User
41
+ session_user = SessionUser.create(
42
+ :email => user.email,
43
+ :password => '-bugus-'
44
+ )
45
+ assert session_user.valid?
46
+ assert_equal session_user.email, user.email
47
+ assert_equal session_user.password, '-bugus-'
48
+ assert_nil session_user.user
49
+ end
50
+ end
@@ -0,0 +1,18 @@
1
+ require 'test_helper'
2
+
3
+ class UserMailerTest < ActionMailer::TestCase
4
+
5
+ def test_password_reset
6
+ user = a User
7
+ user.reset_perishable_token!
8
+ assert_emails 1 do
9
+ response = UserMailer.password_reset(user).deliver
10
+ assert_equal "You have requested a new password", response.subject
11
+ assert_equal 2, response.parts.length
12
+ response.parts.each do |part|
13
+ assert_match /#{user.perishable_token}/, part.body.to_s
14
+ end
15
+ assert_equal user.email, response.to[0]
16
+ end
17
+ end
18
+ end
@@ -8,7 +8,7 @@ class UserTest < ActiveSupport::TestCase
8
8
  :password => 'tester',
9
9
  :password_confirmation => 'tester'
10
10
  )
11
- user.role = :admin
11
+ user.role = 'admin'
12
12
  user.save
13
13
  assert_created user
14
14
  end
@@ -31,75 +31,4 @@ class UserTest < ActiveSupport::TestCase
31
31
  assert_created user
32
32
  end
33
33
 
34
- def test_user_instance_methods
35
- @user = a User
36
- %w{
37
- has_authority_to?
38
- has_objections_to?
39
- initialize_salt
40
- initialize_token
41
- encrypt_password
42
- password_match?
43
- password_crypted?
44
- password_crypt=
45
- is_admin?
46
- is_regular_user?
47
- }.each do |method|
48
- assert @user.respond_to?(method), "On '#{method}' method"
49
- end
50
- end
51
-
52
- def test_user_class_methods
53
- @user = a User
54
- %w{
55
- authenticate
56
- execute_authentication_chain
57
- verify_email!
58
- wristband
59
- }.each do |method|
60
- assert User.respond_to?(method), "On '#{method}' method"
61
- end
62
- end
63
-
64
- def test_user_class_private_methods
65
- %w{
66
- random_string
67
- encrypt_with_salt
68
- random_salt
69
- }.each do |method|
70
- assert User.private_methods.include?(method), "On '#{method}' method"
71
- end
72
- end
73
-
74
- def test_assigned_options
75
- assert_equal User.wristband[:login_with_fields], [:email]
76
- assert_equal User.wristband[:before_authentication_chain], []
77
- assert_equal User.wristband[:after_authentication_chain], []
78
- assert_equal User.wristband[:password_column], :password_crypt
79
- assert_equal User.wristband[:roles], [:admin, :regular_user]
80
- end
81
-
82
- def test_authentication_by_email
83
- @user = a User
84
- assert_equal @user, User.authenticate(@user.email, 'passpass')
85
- end
86
-
87
- def test_authentication_fails
88
- @user = a User
89
- assert_nil User.authenticate('-bugus-', 'passpass')
90
- assert_nil User.authenticate(@user.email, '-bugus-')
91
- end
92
-
93
- def test_password_match
94
- @user = a User
95
- assert @user.password_match?('passpass')
96
- end
97
-
98
- def test_user_roles
99
- @user = a User
100
- @user.update_attribute(:role, :regular_user)
101
- assert @user.is_regular_user?
102
- @user.update_attribute(:role, :admin)
103
- assert @user.is_admin?
104
- end
105
34
  end
@@ -0,0 +1,74 @@
1
+ require 'test_helper'
2
+
3
+ class WristbandTest < ActiveSupport::TestCase
4
+
5
+ def test_user_instance_methods
6
+ @user = a User
7
+ %w{
8
+ has_authority_to?
9
+ has_objections_to?
10
+ initialize_salt
11
+ initialize_token
12
+ encrypt_password
13
+ password_match?
14
+ password_hash=
15
+ is_admin?
16
+ is_regular_user?
17
+ }.each do |method|
18
+ assert @user.respond_to?(method), "On '#{method}' method"
19
+ end
20
+ end
21
+
22
+ def test_user_class_methods
23
+ @user = a User
24
+ %w{
25
+ authenticate
26
+ execute_authentication_chain
27
+ wristband
28
+ }.each do |method|
29
+ assert User.respond_to?(method), "On '#{method}' method"
30
+ end
31
+ end
32
+
33
+ def test_user_class_private_methods
34
+ %w{
35
+ random_string
36
+ encrypt_with_salt
37
+ random_salt
38
+ }.each do |method|
39
+ assert User.private_methods.include?(method.to_sym), "On '#{method}' method"
40
+ end
41
+ end
42
+
43
+ def test_assigned_options
44
+ assert_equal User.wristband[:login_with_fields], [:email]
45
+ assert_equal User.wristband[:before_authentication_chain], []
46
+ assert_equal User.wristband[:after_authentication_chain], []
47
+ assert_equal User.wristband[:password_column], :password_hash
48
+ assert_equal User.wristband[:roles], ['admin', 'regular_user']
49
+ end
50
+
51
+ def test_authentication_by_email
52
+ @user = a User
53
+ assert_equal @user, User.authenticate(@user.email, @user.password)
54
+ end
55
+
56
+ def test_authentication_fails
57
+ @user = a User
58
+ assert_nil User.authenticate('-bugus-', @user.password)
59
+ assert_nil User.authenticate(@user.email, '-bugus-')
60
+ end
61
+
62
+ def test_password_match
63
+ @user = a User
64
+ assert @user.password_match?('password')
65
+ end
66
+
67
+ def test_user_roles
68
+ @user = a User
69
+ @user.update_attribute(:role, 'regular_user')
70
+ assert @user.is_regular_user?
71
+ @user.update_attribute(:role, 'admin')
72
+ assert @user.is_admin?
73
+ end
74
+ end