sorcery 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. data/Gemfile +4 -3
  2. data/Gemfile.lock +16 -13
  3. data/LICENSE.txt +1 -1
  4. data/README.rdoc +77 -63
  5. data/Rakefile +7 -13
  6. data/VERSION +1 -1
  7. data/lib/sorcery/controller/submodules/activity_logging.rb +58 -0
  8. data/lib/sorcery/controller/submodules/brute_force_protection.rb +15 -69
  9. data/lib/sorcery/controller/submodules/http_basic_auth.rb +65 -0
  10. data/lib/sorcery/controller/submodules/oauth/oauth1.rb +25 -0
  11. data/lib/sorcery/controller/submodules/oauth/oauth2.rb +23 -0
  12. data/lib/sorcery/controller/submodules/oauth/providers/facebook.rb +64 -0
  13. data/lib/sorcery/controller/submodules/oauth/providers/twitter.rb +61 -0
  14. data/lib/sorcery/controller/submodules/oauth.rb +95 -0
  15. data/lib/sorcery/controller/submodules/remember_me.rb +14 -5
  16. data/lib/sorcery/controller/submodules/session_timeout.rb +10 -2
  17. data/lib/sorcery/controller.rb +40 -22
  18. data/lib/sorcery/crypto_providers/bcrypt.rb +3 -7
  19. data/lib/sorcery/engine.rb +9 -2
  20. data/lib/sorcery/model/submodules/activity_logging.rb +40 -0
  21. data/lib/sorcery/model/submodules/brute_force_protection.rb +79 -0
  22. data/lib/sorcery/model/submodules/oauth.rb +53 -0
  23. data/lib/sorcery/model/submodules/remember_me.rb +6 -2
  24. data/lib/sorcery/model/submodules/reset_password.rb +96 -0
  25. data/lib/sorcery/model/submodules/user_activation.rb +44 -23
  26. data/lib/sorcery/model/temporary_token.rb +22 -0
  27. data/lib/sorcery/model.rb +13 -6
  28. data/lib/sorcery/test_helpers.rb +84 -0
  29. data/lib/sorcery.rb +17 -1
  30. data/sorcery.gemspec +85 -41
  31. data/spec/Gemfile +3 -2
  32. data/spec/Gemfile.lock +15 -2
  33. data/spec/rails3/app_root/.rspec +1 -0
  34. data/spec/rails3/{Gemfile → app_root/Gemfile} +5 -3
  35. data/spec/rails3/{Gemfile.lock → app_root/Gemfile.lock} +25 -2
  36. data/spec/rails3/app_root/app/controllers/application_controller.rb +48 -2
  37. data/spec/rails3/app_root/app/models/authentication.rb +3 -0
  38. data/spec/rails3/app_root/app/models/user.rb +4 -1
  39. data/spec/rails3/app_root/config/application.rb +1 -3
  40. data/spec/rails3/app_root/config/routes.rb +1 -9
  41. data/spec/rails3/app_root/db/migrate/activation/20101224223622_add_activation_to_users.rb +6 -4
  42. data/spec/rails3/app_root/db/migrate/activity_logging/20101224223624_add_activity_logging_to_users.rb +17 -0
  43. data/spec/rails3/app_root/db/migrate/brute_force_protection/20101224223626_add_brute_force_protection_to_users.rb +11 -0
  44. data/spec/rails3/app_root/db/migrate/core/20101224223620_create_users.rb +4 -4
  45. data/spec/rails3/app_root/db/migrate/oauth/20101224223628_create_authentications.rb +14 -0
  46. data/spec/rails3/app_root/db/migrate/reset_password/20101224223622_add_reset_password_to_users.rb +13 -0
  47. data/spec/rails3/app_root/spec/controller_activity_logging_spec.rb +84 -0
  48. data/spec/rails3/app_root/spec/controller_brute_force_protection_spec.rb +65 -0
  49. data/spec/rails3/app_root/spec/controller_http_basic_auth_spec.rb +50 -0
  50. data/spec/rails3/app_root/spec/controller_oauth2_spec.rb +117 -0
  51. data/spec/rails3/app_root/spec/controller_oauth_spec.rb +117 -0
  52. data/spec/rails3/{controller_remember_me_spec.rb → app_root/spec/controller_remember_me_spec.rb} +4 -4
  53. data/spec/rails3/{controller_session_timeout_spec.rb → app_root/spec/controller_session_timeout_spec.rb} +5 -4
  54. data/spec/rails3/{controller_spec.rb → app_root/spec/controller_spec.rb} +23 -16
  55. data/spec/rails3/app_root/spec/spec_helper.orig.rb +27 -0
  56. data/spec/rails3/app_root/spec/spec_helper.rb +61 -0
  57. data/spec/rails3/{user_activation_spec.rb → app_root/spec/user_activation_spec.rb} +62 -22
  58. data/spec/rails3/app_root/spec/user_activity_logging_spec.rb +36 -0
  59. data/spec/rails3/app_root/spec/user_brute_force_protection_spec.rb +76 -0
  60. data/spec/rails3/app_root/spec/user_oauth_spec.rb +39 -0
  61. data/spec/rails3/{user_remember_me_spec.rb → app_root/spec/user_remember_me_spec.rb} +4 -4
  62. data/spec/rails3/app_root/spec/user_reset_password_spec.rb +178 -0
  63. data/spec/rails3/{user_spec.rb → app_root/spec/user_spec.rb} +68 -38
  64. metadata +145 -61
  65. data/features/support/env.rb +0 -13
  66. data/lib/sorcery/model/submodules/password_reset.rb +0 -64
  67. data/spec/rails3/app_root/db/migrate/password_reset/20101224223622_add_password_reset_to_users.rb +0 -9
  68. data/spec/rails3/app_root/test/fixtures/users.yml +0 -9
  69. data/spec/rails3/app_root/test/performance/browsing_test.rb +0 -9
  70. data/spec/rails3/app_root/test/test_helper.rb +0 -13
  71. data/spec/rails3/app_root/test/unit/user_test.rb +0 -8
  72. data/spec/rails3/controller_brute_force_protection_spec.rb +0 -72
  73. data/spec/rails3/spec_helper.rb +0 -115
  74. data/spec/rails3/user_password_reset_spec.rb +0 -76
  75. /data/spec/rails3/{Rakefile → app_root/Rakefile} +0 -0
@@ -0,0 +1,61 @@
1
+ $: << File.join(File.dirname(__FILE__), '..', '..', 'lib' )
2
+
3
+ require 'simplecov'
4
+ SimpleCov.root File.join(File.dirname(__FILE__), "..", "..", "app_root" )
5
+ SimpleCov.start do
6
+ add_filter "/config/"
7
+
8
+ add_group 'Controllers', 'app/controllers'
9
+ add_group 'Models', 'app/models'
10
+ add_group 'Helpers', 'app/helpers'
11
+ add_group 'Libraries', 'lib'
12
+ add_group 'Plugins', 'vendor/plugins'
13
+ add_group 'Migrations', 'db/migrate'
14
+ end
15
+
16
+ require 'spork'
17
+
18
+ Spork.prefork do
19
+ # Set the default environment to sqlite3's in_memory database
20
+ ENV['RAILS_ENV'] ||= 'in_memory'
21
+ ENV['RAILS_ROOT'] = 'app_root'
22
+
23
+ # Load the Rails environment and testing framework
24
+ require "#{File.dirname(__FILE__)}/../config/environment"
25
+ #require "#{File.dirname(__FILE__)}/../../init" # for plugins
26
+ require 'rspec/rails'
27
+
28
+ RSpec.configure do |config|
29
+ config.use_transactional_fixtures = true
30
+ config.use_instantiated_fixtures = false
31
+ config.include RSpec::Rails::ControllerExampleGroup, :example_group => { :file_path => /controller(.)*_spec.rb$/ }
32
+
33
+ config.before(:suite) do
34
+ ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/core")
35
+ end
36
+
37
+ config.after(:suite) do
38
+ ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/core")
39
+ end
40
+ end
41
+
42
+ #----------------------------------------------------------------
43
+ # needed when running individual specs
44
+ require File.join(File.dirname(__FILE__), '..','app','models','user')
45
+ require File.join(File.dirname(__FILE__), '..','app','models','authentication')
46
+
47
+ class TestUser < ActiveRecord::Base
48
+ activate_sorcery!
49
+ end
50
+
51
+ class TestMailer < ActionMailer::Base
52
+
53
+ end
54
+
55
+ include ::Sorcery::TestHelpers
56
+
57
+ end
58
+
59
+ Spork.each_run do
60
+ # This code will be run each time you run your specs.
61
+ end
@@ -1,5 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
- require File.expand_path(File.dirname(__FILE__) + '/app_root/app/mailers/sorcery_mailer')
2
+ require File.expand_path(File.dirname(__FILE__) + '/../app/mailers/sorcery_mailer')
3
3
 
4
4
  describe "User with activation submodule" do
5
5
  before(:all) do
@@ -13,53 +13,53 @@ describe "User with activation submodule" do
13
13
  # ----------------- PLUGIN CONFIGURATION -----------------------
14
14
  describe User, "loaded plugin configuration" do
15
15
  before(:all) do
16
- plugin_model_configure([:user_activation], :sorcery_mailer => ::SorceryMailer)
16
+ sorcery_reload!([:user_activation], :user_activation_mailer => ::SorceryMailer)
17
17
  end
18
18
 
19
19
  after(:each) do
20
20
  User.sorcery_config.reset!
21
- plugin_model_configure([:user_activation], :sorcery_mailer => ::SorceryMailer)
21
+ sorcery_reload!([:user_activation], :user_activation_mailer => ::SorceryMailer)
22
22
  end
23
23
 
24
24
  it "should enable configuration option 'activation_state_attribute_name'" do
25
- plugin_set_model_config_property(:activation_state_attribute_name, :status)
25
+ sorcery_model_property_set(:activation_state_attribute_name, :status)
26
26
  User.sorcery_config.activation_state_attribute_name.should equal(:status)
27
27
  end
28
28
 
29
- it "should enable configuration option 'activation_code_attribute_name'" do
30
- plugin_set_model_config_property(:activation_code_attribute_name, :code)
31
- User.sorcery_config.activation_code_attribute_name.should equal(:code)
29
+ it "should enable configuration option 'activation_token_attribute_name'" do
30
+ sorcery_model_property_set(:activation_token_attribute_name, :code)
31
+ User.sorcery_config.activation_token_attribute_name.should equal(:code)
32
32
  end
33
33
 
34
- it "should enable configuration option 'sorcery_mailer'" do
35
- plugin_set_model_config_property(:sorcery_mailer, TestMailer)
36
- User.sorcery_config.sorcery_mailer.should equal(TestMailer)
34
+ it "should enable configuration option 'user_activation_mailer'" do
35
+ sorcery_model_property_set(:user_activation_mailer, TestMailer)
36
+ User.sorcery_config.user_activation_mailer.should equal(TestMailer)
37
37
  end
38
38
 
39
39
  it "should enable configuration option 'activation_needed_email_method_name'" do
40
- plugin_set_model_config_property(:activation_needed_email_method_name, :my_activation_email)
40
+ sorcery_model_property_set(:activation_needed_email_method_name, :my_activation_email)
41
41
  User.sorcery_config.activation_needed_email_method_name.should equal(:my_activation_email)
42
42
  end
43
43
 
44
44
  it "should enable configuration option 'activation_success_email_method_name'" do
45
- plugin_set_model_config_property(:activation_success_email_method_name, :my_activation_email)
45
+ sorcery_model_property_set(:activation_success_email_method_name, :my_activation_email)
46
46
  User.sorcery_config.activation_success_email_method_name.should equal(:my_activation_email)
47
47
  end
48
48
 
49
49
  it "if mailer is nil on activation, throw exception!" do
50
- expect{plugin_model_configure([:user_activation])}.to raise_error(ArgumentError)
50
+ expect{sorcery_reload!([:user_activation])}.to raise_error(ArgumentError)
51
51
  end
52
52
  end
53
53
 
54
54
  # ----------------- ACTIVATION PROCESS -----------------------
55
55
  describe User, "activation process" do
56
56
  before(:all) do
57
- plugin_model_configure([:user_activation], :sorcery_mailer => ::SorceryMailer)
57
+ sorcery_reload!([:user_activation], :user_activation_mailer => ::SorceryMailer)
58
58
  end
59
59
 
60
60
  it "should generate an activation code on registration" do
61
61
  create_new_user
62
- @user.activation_code.should_not be_nil
62
+ @user.activation_token.should_not be_nil
63
63
  end
64
64
 
65
65
  it "should initialize user state to 'pending'" do
@@ -74,12 +74,12 @@ describe "User with activation submodule" do
74
74
 
75
75
  it "should clear activation code and change state to 'active' on activation" do
76
76
  create_new_user
77
- activation_code = @user.activation_code
77
+ activation_token = @user.activation_token
78
78
  @user.activate!
79
79
  @user2 = User.find(@user.id) # go to db to make sure it was saved and not just in memory
80
- @user2.activation_code.should be_nil
80
+ @user2.activation_token.should be_nil
81
81
  @user2.activation_state.should == "active"
82
- User.find_by_activation_code(activation_code).should be_nil
82
+ User.find_by_activation_token(activation_token).should be_nil
83
83
  end
84
84
 
85
85
  it "should send the user an activation email" do
@@ -113,14 +113,14 @@ describe "User with activation submodule" do
113
113
  end
114
114
 
115
115
  it "activation needed email is optional" do
116
- plugin_set_model_config_property(:activation_needed_email_method_name, nil)
116
+ sorcery_model_property_set(:activation_needed_email_method_name, nil)
117
117
  old_size = ActionMailer::Base.deliveries.size
118
118
  create_new_user
119
119
  ActionMailer::Base.deliveries.size.should == old_size
120
120
  end
121
121
 
122
122
  it "activation success email is optional" do
123
- plugin_set_model_config_property(:activation_success_email_method_name, nil)
123
+ sorcery_model_property_set(:activation_success_email_method_name, nil)
124
124
  create_new_user
125
125
  old_size = ActionMailer::Base.deliveries.size
126
126
  @user.activate!
@@ -130,7 +130,7 @@ describe "User with activation submodule" do
130
130
 
131
131
  describe User, "prevent non-active login feature" do
132
132
  before(:all) do
133
- plugin_model_configure([:user_activation], :sorcery_mailer => ::SorceryMailer)
133
+ sorcery_reload!([:user_activation], :user_activation_mailer => ::SorceryMailer)
134
134
  end
135
135
 
136
136
  it "should not allow a non-active user to authenticate" do
@@ -140,9 +140,49 @@ describe "User with activation submodule" do
140
140
 
141
141
  it "should allow a non-active user to authenticate if configured so" do
142
142
  create_new_user
143
- plugin_set_model_config_property(:prevent_non_active_users_to_login, false)
143
+ sorcery_model_property_set(:prevent_non_active_users_to_login, false)
144
144
  User.authenticate(@user.username,'secret').should be_true
145
145
  end
146
146
  end
147
147
 
148
+ describe User, "load_from_activation_token" do
149
+ before(:all) do
150
+ sorcery_reload!([:user_activation], :user_activation_mailer => ::SorceryMailer)
151
+ end
152
+
153
+ it "load_from_activation_token should return user when token is found" do
154
+ create_new_user
155
+ User.load_from_activation_token(@user.activation_token).should == @user
156
+ end
157
+
158
+ it "load_from_activation_token should NOT return user when token is NOT found" do
159
+ create_new_user
160
+ User.load_from_activation_token("a").should == nil
161
+ end
162
+
163
+ it "load_from_activation_token should return user when token is found and not expired" do
164
+ sorcery_model_property_set(:activation_token_expiration_period, 500)
165
+ create_new_user
166
+ User.load_from_activation_token(@user.activation_token).should == @user
167
+ end
168
+
169
+ it "load_from_activation_token should NOT return user when token is found and expired" do
170
+ sorcery_model_property_set(:activation_token_expiration_period, 0.1)
171
+ create_new_user
172
+ sleep 0.5
173
+ User.load_from_activation_token(@user.activation_token).should == nil
174
+ end
175
+
176
+ it "load_from_activation_token should return nil if token is blank" do
177
+ User.load_from_activation_token(nil).should == nil
178
+ User.load_from_activation_token("").should == nil
179
+ end
180
+
181
+ it "load_from_activation_token should always be valid if expiration period is nil" do
182
+ sorcery_model_property_set(:activation_token_expiration_period, nil)
183
+ create_new_user
184
+ User.load_from_activation_token(@user.activation_token).should == @user
185
+ end
186
+ end
187
+
148
188
  end
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "User with activity logging submodule" do
4
+ before(:all) do
5
+ end
6
+
7
+ after(:all) do
8
+ end
9
+
10
+ # ----------------- PLUGIN CONFIGURATION -----------------------
11
+ describe User, "loaded plugin configuration" do
12
+ before(:all) do
13
+ sorcery_reload!([:activity_logging])
14
+ end
15
+
16
+ after(:each) do
17
+ User.sorcery_config.reset!
18
+ end
19
+
20
+ it "should allow configuration option 'last_login_at_attribute_name'" do
21
+ sorcery_model_property_set(:last_login_at_attribute_name, :login_time)
22
+ User.sorcery_config.last_login_at_attribute_name.should equal(:login_time)
23
+ end
24
+
25
+ it "should allow configuration option 'last_logout_at_attribute_name'" do
26
+ sorcery_model_property_set(:last_logout_at_attribute_name, :logout_time)
27
+ User.sorcery_config.last_logout_at_attribute_name.should equal(:logout_time)
28
+ end
29
+
30
+ it "should allow configuration option 'last_activity_at_attribute_name'" do
31
+ sorcery_model_property_set(:last_activity_at_attribute_name, :activity_time)
32
+ User.sorcery_config.last_activity_at_attribute_name.should equal(:activity_time)
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,76 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "User with brute_force_protection submodule" do
4
+ before(:all) do
5
+ ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/brute_force_protection")
6
+ end
7
+
8
+ after(:all) do
9
+ ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/brute_force_protection")
10
+ end
11
+
12
+ # ----------------- PLUGIN CONFIGURATION -----------------------
13
+ describe User, "loaded plugin configuration" do
14
+
15
+ before(:all) do
16
+ sorcery_reload!([:brute_force_protection])
17
+ create_new_user
18
+ end
19
+
20
+ after(:each) do
21
+ User.sorcery_config.reset!
22
+ end
23
+
24
+ it "should respond to 'failed_logins_count'" do
25
+ @user.should respond_to(:failed_logins_count)
26
+ end
27
+
28
+ it "should respond to 'lock_expires_at'" do
29
+ @user.should respond_to(:failed_logins_count)
30
+ end
31
+
32
+ it "should enable configuration option 'failed_logins_count_attribute_name'" do
33
+ sorcery_model_property_set(:failed_logins_count_attribute_name, :my_count)
34
+ User.sorcery_config.failed_logins_count_attribute_name.should equal(:my_count)
35
+ end
36
+
37
+ it "should enable configuration option 'lock_expires_at_attribute_name'" do
38
+ sorcery_model_property_set(:lock_expires_at_attribute_name, :expires)
39
+ User.sorcery_config.lock_expires_at_attribute_name.should equal(:expires)
40
+ end
41
+
42
+ it "should enable configuration option 'consecutive_login_retries_amount_allowed'" do
43
+ sorcery_model_property_set(:consecutive_login_retries_amount_limit, 34)
44
+ User.sorcery_config.consecutive_login_retries_amount_limit.should equal(34)
45
+ end
46
+
47
+ it "should enable configuration option 'login_lock_time_period'" do
48
+ sorcery_model_property_set(:login_lock_time_period, 2.hours)
49
+ User.sorcery_config.login_lock_time_period.should == 2.hours
50
+ end
51
+ end
52
+
53
+ # ----------------- PLUGIN ACTIVATED -----------------------
54
+ describe User, "when activated with sorcery" do
55
+
56
+ before(:all) do
57
+ sorcery_reload!([:brute_force_protection])
58
+ end
59
+
60
+ before(:each) do
61
+ User.delete_all
62
+ end
63
+
64
+ # it "should increment failed_logins_count on a failed login" do
65
+ # create_new_user
66
+ #
67
+ # end
68
+ #
69
+ # it "should set lock expiry (effectively lock user) when failed_logins_count reaches max within max period" do
70
+ # create_new_user
71
+ # @user.lock_expires_at.should == Time.now.utc + 30
72
+ # end
73
+
74
+ end
75
+
76
+ end
@@ -0,0 +1,39 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "User with oauth submodule" do
4
+ before(:all) do
5
+ ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/oauth")
6
+ end
7
+
8
+ after(:all) do
9
+ ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/oauth")
10
+ end
11
+
12
+ # ----------------- PLUGIN CONFIGURATION -----------------------
13
+ describe User, "loaded plugin configuration" do
14
+
15
+ before(:all) do
16
+ sorcery_reload!([:oauth])
17
+ sorcery_controller_property_set(:oauth_providers, [:twitter])
18
+ sorcery_model_property_set(:authentications_class, Authentication)
19
+ sorcery_controller_oauth_property_set(:twitter, :key, "eYVNBjBDi33aa9GkA3w")
20
+ sorcery_controller_oauth_property_set(:twitter, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
21
+ sorcery_controller_oauth_property_set(:twitter, :callback_url, "http://blabla.com")
22
+ create_new_external_user(:twitter)
23
+ end
24
+
25
+ it "should respond to 'load_from_provider'" do
26
+ User.should respond_to(:load_from_provider)
27
+ end
28
+
29
+ it "'load_from_provider' should load user if exists" do
30
+ User.load_from_provider(:twitter,123).should == @user
31
+ end
32
+
33
+ it "'load_from_provider' should return nil if user doesn't exist" do
34
+ User.load_from_provider(:twitter,980342).should be_nil
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -12,7 +12,7 @@ describe "User with remember_me submodule" do
12
12
  # ----------------- PLUGIN CONFIGURATION -----------------------
13
13
  describe User, "loaded plugin configuration" do
14
14
  before(:all) do
15
- plugin_model_configure([:remember_me])
15
+ sorcery_reload!([:remember_me])
16
16
  end
17
17
 
18
18
  after(:each) do
@@ -20,12 +20,12 @@ describe "User with remember_me submodule" do
20
20
  end
21
21
 
22
22
  it "should allow configuration option 'remember_me_token_attribute_name'" do
23
- plugin_set_model_config_property(:remember_me_token_attribute_name, :my_token)
23
+ sorcery_model_property_set(:remember_me_token_attribute_name, :my_token)
24
24
  User.sorcery_config.remember_me_token_attribute_name.should equal(:my_token)
25
25
  end
26
26
 
27
27
  it "should allow configuration option 'remember_me_token_expires_at_attribute_name'" do
28
- plugin_set_model_config_property(:remember_me_token_expires_at_attribute_name, :my_expires)
28
+ sorcery_model_property_set(:remember_me_token_expires_at_attribute_name, :my_expires)
29
29
  User.sorcery_config.remember_me_token_expires_at_attribute_name.should equal(:my_expires)
30
30
  end
31
31
 
@@ -48,7 +48,7 @@ describe "User with remember_me submodule" do
48
48
 
49
49
  it "should set an expiration based on 'remember_me_for' attribute" do
50
50
  create_new_user
51
- plugin_set_model_config_property(:remember_me_for, 2 * 60 * 60 * 24)
51
+ sorcery_model_property_set(:remember_me_for, 2 * 60 * 60 * 24)
52
52
  @user.remember_me!
53
53
  @user.remember_me_token_expires_at.to_s.should == (Time.now + 2 * 60 * 60 * 24).utc.to_s
54
54
  end
@@ -0,0 +1,178 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "User with reset_password submodule" do
4
+ before(:all) do
5
+ ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/reset_password")
6
+ end
7
+
8
+ after(:all) do
9
+ ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/reset_password")
10
+ end
11
+
12
+ # ----------------- PLUGIN CONFIGURATION -----------------------
13
+ describe User, "loaded plugin configuration" do
14
+
15
+ before(:all) do
16
+ sorcery_reload!([:reset_password], :reset_password_mailer => ::SorceryMailer)
17
+ end
18
+
19
+ after(:each) do
20
+ User.sorcery_config.reset!
21
+ end
22
+
23
+ it "should respond to 'deliver_reset_password_instructions!'" do
24
+ create_new_user
25
+ @user.should respond_to(:deliver_reset_password_instructions!)
26
+ end
27
+
28
+ it "should respond to 'reset_password!" do
29
+ create_new_user
30
+ @user.should respond_to(:reset_password!)
31
+ end
32
+
33
+ it "should respond to 'load_from_reset_password_token'" do
34
+ create_new_user
35
+ User.should respond_to(:load_from_reset_password_token)
36
+ end
37
+
38
+ it "should allow configuration option 'reset_password_token_attribute_name'" do
39
+ sorcery_model_property_set(:reset_password_token_attribute_name, :my_code)
40
+ User.sorcery_config.reset_password_token_attribute_name.should equal(:my_code)
41
+ end
42
+
43
+ it "should allow configuration option 'reset_password_mailer'" do
44
+ sorcery_model_property_set(:reset_password_mailer, TestUser)
45
+ User.sorcery_config.reset_password_mailer.should equal(TestUser)
46
+ end
47
+
48
+ it "should allow configuration option 'reset_password_email_method_name'" do
49
+ sorcery_model_property_set(:reset_password_email_method_name, :my_mailer_method)
50
+ User.sorcery_config.reset_password_email_method_name.should equal(:my_mailer_method)
51
+ end
52
+
53
+ it "should allow configuration option 'reset_password_expiration_period'" do
54
+ sorcery_model_property_set(:reset_password_expiration_period, 16)
55
+ User.sorcery_config.reset_password_expiration_period.should equal(16)
56
+ end
57
+
58
+ it "should allow configuration option 'reset_password_email_sent_at_attribute_name'" do
59
+ sorcery_model_property_set(:reset_password_email_sent_at_attribute_name, :blabla)
60
+ User.sorcery_config.reset_password_email_sent_at_attribute_name.should equal(:blabla)
61
+ end
62
+
63
+ it "should allow configuration option 'reset_password_time_between_emails'" do
64
+ sorcery_model_property_set(:reset_password_time_between_emails, 16)
65
+ User.sorcery_config.reset_password_time_between_emails.should equal(16)
66
+ end
67
+ end
68
+
69
+ # ----------------- PLUGIN ACTIVATED -----------------------
70
+ describe User, "when activated with sorcery" do
71
+
72
+ before(:all) do
73
+ sorcery_reload!([:reset_password], :reset_password_mailer => ::SorceryMailer)
74
+ end
75
+
76
+ before(:each) do
77
+ User.delete_all
78
+ end
79
+
80
+ it "load_from_reset_password_token should return user when token is found" do
81
+ create_new_user
82
+ @user.deliver_reset_password_instructions!
83
+ User.load_from_reset_password_token(@user.reset_password_token).should == @user
84
+ end
85
+
86
+ it "load_from_reset_password_token should NOT return user when token is NOT found" do
87
+ create_new_user
88
+ @user.deliver_reset_password_instructions!
89
+ User.load_from_reset_password_token("a").should == nil
90
+ end
91
+
92
+ it "load_from_reset_password_token should return user when token is found and not expired" do
93
+ create_new_user
94
+ sorcery_model_property_set(:reset_password_expiration_period, 500)
95
+ @user.deliver_reset_password_instructions!
96
+ User.load_from_reset_password_token(@user.reset_password_token).should == @user
97
+ end
98
+
99
+ it "load_from_reset_password_token should NOT return user when token is found and expired" do
100
+ create_new_user
101
+ sorcery_model_property_set(:reset_password_expiration_period, 0.1)
102
+ @user.deliver_reset_password_instructions!
103
+ sleep 0.5
104
+ User.load_from_reset_password_token(@user.reset_password_token).should == nil
105
+ end
106
+
107
+ it "load_from_reset_password_token should always be valid if expiration period is nil" do
108
+ create_new_user
109
+ sorcery_model_property_set(:reset_password_expiration_period, nil)
110
+ @user.deliver_reset_password_instructions!
111
+ User.load_from_reset_password_token(@user.reset_password_token).should == @user
112
+ end
113
+
114
+ it "load_from_reset_password_token should return nil if token is blank" do
115
+ User.load_from_reset_password_token(nil).should == nil
116
+ User.load_from_reset_password_token("").should == nil
117
+ end
118
+
119
+ it "'deliver_reset_password_instructions!' should generate a reset_password_token" do
120
+ create_new_user
121
+ @user.reset_password_token.should be_nil
122
+ @user.deliver_reset_password_instructions!
123
+ @user.reset_password_token.should_not be_nil
124
+ end
125
+
126
+ it "the reset_password_token should be random" do
127
+ create_new_user
128
+ sorcery_model_property_set(:reset_password_time_between_emails, 0)
129
+ @user.deliver_reset_password_instructions!
130
+ old_password_code = @user.reset_password_token
131
+ @user.deliver_reset_password_instructions!
132
+ @user.reset_password_token.should_not == old_password_code
133
+ end
134
+
135
+ it "should send an email on reset" do
136
+ create_new_user
137
+ old_size = ActionMailer::Base.deliveries.size
138
+ @user.deliver_reset_password_instructions!
139
+ ActionMailer::Base.deliveries.size.should == old_size + 1
140
+ end
141
+
142
+ it "when reset_password! is called, should delete reset_password_token" do
143
+ create_new_user
144
+ @user.deliver_reset_password_instructions!
145
+ @user.reset_password_token.should_not be_nil
146
+ @user.reset_password!(:password => "blabulsdf")
147
+ @user.save!
148
+ @user.reset_password_token.should be_nil
149
+ end
150
+
151
+ it "should not send an email if time between emails has not passed since last email" do
152
+ create_new_user
153
+ sorcery_model_property_set(:reset_password_time_between_emails, 10000)
154
+ old_size = ActionMailer::Base.deliveries.size
155
+ @user.deliver_reset_password_instructions!
156
+ ActionMailer::Base.deliveries.size.should == old_size + 1
157
+ @user.deliver_reset_password_instructions!
158
+ ActionMailer::Base.deliveries.size.should == old_size + 1
159
+ end
160
+
161
+ it "should send an email if time between emails has passed since last email" do
162
+ create_new_user
163
+ sorcery_model_property_set(:reset_password_time_between_emails, 0.5)
164
+ old_size = ActionMailer::Base.deliveries.size
165
+ @user.deliver_reset_password_instructions!
166
+ ActionMailer::Base.deliveries.size.should == old_size + 1
167
+ sleep 0.5
168
+ @user.deliver_reset_password_instructions!
169
+ ActionMailer::Base.deliveries.size.should == old_size + 2
170
+ end
171
+
172
+ it "if mailer is nil on activation, throw exception!" do
173
+ expect{sorcery_reload!([:reset_password])}.to raise_error(ArgumentError)
174
+ end
175
+
176
+ end
177
+
178
+ end