sorcery 0.8.5 → 0.8.6

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sorcery might be problematic. Click here for more details.

Files changed (119) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +60 -4
  4. data/CHANGELOG.md +15 -1
  5. data/Gemfile +9 -18
  6. data/Gemfile.rails4 +8 -10
  7. data/README.md +31 -11
  8. data/VERSION +1 -1
  9. data/gemfiles/active_record-rails41.gemfile +6 -0
  10. data/gemfiles/mongo_mapper-rails41.gemfile +8 -0
  11. data/gemfiles/mongoid-rails41.gemfile +11 -0
  12. data/lib/sorcery.rb +20 -28
  13. data/lib/sorcery/controller.rb +6 -11
  14. data/lib/sorcery/controller/submodules/external.rb +30 -15
  15. data/lib/sorcery/controller/submodules/session_timeout.rb +1 -1
  16. data/lib/sorcery/model.rb +102 -70
  17. data/lib/sorcery/model/adapters/active_record.rb +7 -2
  18. data/lib/sorcery/model/adapters/datamapper.rb +123 -0
  19. data/lib/sorcery/model/adapters/mongo_mapper.rb +8 -4
  20. data/lib/sorcery/model/adapters/mongoid.rb +6 -6
  21. data/lib/sorcery/model/submodules/activity_logging.rb +24 -0
  22. data/lib/sorcery/model/submodules/brute_force_protection.rb +16 -0
  23. data/lib/sorcery/model/submodules/remember_me.rb +19 -4
  24. data/lib/sorcery/model/submodules/reset_password.rb +30 -13
  25. data/lib/sorcery/model/submodules/user_activation.rb +53 -22
  26. data/lib/sorcery/{controller/submodules/external/protocols → protocols}/certs/ca-bundle.crt +0 -0
  27. data/lib/sorcery/protocols/oauth.rb +42 -0
  28. data/lib/sorcery/protocols/oauth2.rb +47 -0
  29. data/lib/sorcery/providers/base.rb +27 -0
  30. data/lib/sorcery/providers/facebook.rb +63 -0
  31. data/lib/sorcery/providers/github.rb +51 -0
  32. data/lib/sorcery/providers/google.rb +51 -0
  33. data/lib/sorcery/providers/linkedin.rb +66 -0
  34. data/lib/sorcery/providers/liveid.rb +53 -0
  35. data/lib/sorcery/providers/twitter.rb +59 -0
  36. data/lib/sorcery/providers/vk.rb +61 -0
  37. data/lib/sorcery/providers/xing.rb +64 -0
  38. data/lib/sorcery/test_helpers/internal.rb +3 -3
  39. data/lib/sorcery/test_helpers/internal/rails.rb +14 -3
  40. data/lib/sorcery/test_helpers/rails.rb +1 -10
  41. data/lib/sorcery/test_helpers/rails/controller.rb +17 -0
  42. data/lib/sorcery/test_helpers/rails/integration.rb +26 -0
  43. data/sorcery.gemspec +14 -18
  44. data/spec/active_record/controller_activity_logging_spec.rb +5 -116
  45. data/spec/active_record/controller_brute_force_protection_spec.rb +69 -47
  46. data/spec/active_record/controller_http_basic_auth_spec.rb +24 -18
  47. data/spec/active_record/controller_oauth2_spec.rb +112 -187
  48. data/spec/active_record/controller_oauth_spec.rb +41 -37
  49. data/spec/active_record/controller_remember_me_spec.rb +39 -38
  50. data/spec/active_record/controller_session_timeout_spec.rb +31 -16
  51. data/spec/active_record/controller_spec.rb +4 -178
  52. data/spec/active_record/integration_spec.rb +1 -1
  53. data/spec/active_record/user_activation_spec.rb +1 -1
  54. data/spec/active_record/user_activity_logging_spec.rb +1 -1
  55. data/spec/active_record/user_brute_force_protection_spec.rb +1 -1
  56. data/spec/active_record/user_oauth_spec.rb +1 -1
  57. data/spec/active_record/user_remember_me_spec.rb +1 -1
  58. data/spec/active_record/user_reset_password_spec.rb +1 -1
  59. data/spec/active_record/user_spec.rb +7 -8
  60. data/spec/datamapper/controller_activity_logging_spec.rb +17 -0
  61. data/spec/datamapper/controller_spec.rb +8 -0
  62. data/spec/datamapper/user_activation_spec.rb +10 -0
  63. data/spec/datamapper/user_activity_logging_spec.rb +9 -0
  64. data/spec/datamapper/user_brute_force_protection_spec.rb +9 -0
  65. data/spec/datamapper/user_oauth_spec.rb +9 -0
  66. data/spec/datamapper/user_remember_me_spec.rb +8 -0
  67. data/spec/datamapper/user_reset_password_spec.rb +8 -0
  68. data/spec/datamapper/user_spec.rb +27 -0
  69. data/spec/mongo_mapper/controller_spec.rb +4 -171
  70. data/spec/mongo_mapper/user_activation_spec.rb +1 -2
  71. data/spec/mongo_mapper/user_activity_logging_spec.rb +1 -1
  72. data/spec/mongo_mapper/user_brute_force_protection_spec.rb +1 -1
  73. data/spec/mongo_mapper/user_oauth_spec.rb +1 -1
  74. data/spec/mongo_mapper/user_remember_me_spec.rb +1 -1
  75. data/spec/mongo_mapper/user_reset_password_spec.rb +1 -1
  76. data/spec/mongo_mapper/user_spec.rb +7 -8
  77. data/spec/mongoid/controller_activity_logging_spec.rb +4 -99
  78. data/spec/mongoid/controller_spec.rb +4 -182
  79. data/spec/mongoid/user_activation_spec.rb +1 -2
  80. data/spec/mongoid/user_activity_logging_spec.rb +1 -2
  81. data/spec/mongoid/user_brute_force_protection_spec.rb +1 -2
  82. data/spec/mongoid/user_oauth_spec.rb +1 -2
  83. data/spec/mongoid/user_remember_me_spec.rb +1 -2
  84. data/spec/mongoid/user_reset_password_spec.rb +1 -2
  85. data/spec/mongoid/user_spec.rb +8 -9
  86. data/spec/orm/active_record.rb +2 -0
  87. data/spec/orm/datamapper.rb +34 -0
  88. data/spec/orm/mongo_mapper.rb +1 -0
  89. data/spec/orm/mongoid.rb +1 -0
  90. data/spec/rails_app/app/controllers/sorcery_controller.rb +64 -59
  91. data/spec/rails_app/app/datamapper/authentication.rb +8 -0
  92. data/spec/rails_app/app/datamapper/user.rb +7 -0
  93. data/spec/rails_app/config/routes.rb +18 -13
  94. data/spec/shared_examples/controller_activity_logging_shared_examples.rb +125 -0
  95. data/spec/shared_examples/controller_oauth2_shared_examples.rb +32 -36
  96. data/spec/shared_examples/controller_oauth_shared_examples.rb +19 -26
  97. data/spec/shared_examples/controller_shared_examples.rb +203 -0
  98. data/spec/shared_examples/user_activation_shared_examples.rb +107 -90
  99. data/spec/shared_examples/user_activity_logging_shared_examples.rb +10 -10
  100. data/spec/shared_examples/user_brute_force_protection_shared_examples.rb +14 -13
  101. data/spec/shared_examples/user_oauth_shared_examples.rb +23 -15
  102. data/spec/shared_examples/user_remember_me_shared_examples.rb +32 -23
  103. data/spec/shared_examples/user_reset_password_shared_examples.rb +136 -115
  104. data/spec/shared_examples/user_shared_examples.rb +206 -146
  105. data/spec/sorcery_crypto_providers_spec.rb +28 -28
  106. data/spec/spec_helper.rb +15 -6
  107. metadata +83 -127
  108. data/lib/sorcery/controller/submodules/external/protocols/oauth1.rb +0 -46
  109. data/lib/sorcery/controller/submodules/external/protocols/oauth2.rb +0 -50
  110. data/lib/sorcery/controller/submodules/external/providers/base.rb +0 -21
  111. data/lib/sorcery/controller/submodules/external/providers/facebook.rb +0 -99
  112. data/lib/sorcery/controller/submodules/external/providers/github.rb +0 -93
  113. data/lib/sorcery/controller/submodules/external/providers/google.rb +0 -92
  114. data/lib/sorcery/controller/submodules/external/providers/linkedin.rb +0 -103
  115. data/lib/sorcery/controller/submodules/external/providers/liveid.rb +0 -93
  116. data/lib/sorcery/controller/submodules/external/providers/twitter.rb +0 -94
  117. data/lib/sorcery/controller/submodules/external/providers/vk.rb +0 -101
  118. data/lib/sorcery/controller/submodules/external/providers/xing.rb +0 -98
  119. data/lib/sorcery/test_helpers.rb +0 -5
@@ -4,22 +4,24 @@ require 'shared_examples/controller_oauth_shared_examples'
4
4
  require 'ostruct'
5
5
 
6
6
  def stub_all_oauth_requests!
7
- @consumer = OAuth::Consumer.new("key","secret", :site => "http://myapi.com")
8
- OAuth::Consumer.stub(:new).and_return(@consumer)
9
-
10
- @req_token = OAuth::RequestToken.new(@consumer)
11
- @consumer.stub(:get_request_token).and_return(@req_token)
12
- @acc_token = OAuth::AccessToken.new(@consumer)
13
- @req_token.stub(:get_access_token).and_return(@acc_token)
14
- session[:request_token] = @req_token.token
15
- session[:request_token_secret] = @req_token.secret
16
- OAuth::RequestToken.stub(:new).and_return(@req_token)
7
+ consumer = OAuth::Consumer.new("key","secret", :site => "http://myapi.com")
8
+ req_token = OAuth::RequestToken.new(consumer)
9
+ acc_token = OAuth::AccessToken.new(consumer)
10
+
17
11
  response = OpenStruct.new()
18
12
  response.body = {"following"=>false, "listed_count"=>0, "profile_link_color"=>"0084B4", "profile_image_url"=>"http://a1.twimg.com/profile_images/536178575/noamb_normal.jpg", "description"=>"Programmer/Heavy Metal Fan/New Father", "status"=>{"text"=>"coming soon to sorcery gem: twitter and facebook authentication support.", "truncated"=>false, "favorited"=>false, "source"=>"web", "geo"=>nil, "in_reply_to_screen_name"=>nil, "in_reply_to_user_id"=>nil, "in_reply_to_status_id_str"=>nil, "created_at"=>"Sun Mar 06 23:01:12 +0000 2011", "contributors"=>nil, "place"=>nil, "retweeted"=>false, "in_reply_to_status_id"=>nil, "in_reply_to_user_id_str"=>nil, "coordinates"=>nil, "retweet_count"=>0, "id"=>44533012284706816, "id_str"=>"44533012284706816"}, "show_all_inline_media"=>false, "geo_enabled"=>true, "profile_sidebar_border_color"=>"a8c7f7", "url"=>nil, "followers_count"=>10, "screen_name"=>"nbenari", "profile_use_background_image"=>true, "location"=>"Israel", "statuses_count"=>25, "profile_background_color"=>"022330", "lang"=>"en", "verified"=>false, "notifications"=>false, "profile_background_image_url"=>"http://a3.twimg.com/profile_background_images/104087198/04042010339.jpg", "favourites_count"=>5, "created_at"=>"Fri Nov 20 21:58:19 +0000 2009", "is_translator"=>false, "contributors_enabled"=>false, "protected"=>false, "follow_request_sent"=>false, "time_zone"=>"Greenland", "profile_text_color"=>"333333", "name"=>"Noam Ben Ari", "friends_count"=>10, "profile_sidebar_fill_color"=>"C0DFEC", "id"=>123, "id_str"=>"91434812", "profile_background_tile"=>false, "utc_offset"=>-10800}.to_json
19
- @acc_token.stub(:get).and_return(response)
13
+
14
+ session[:request_token] = req_token.token
15
+ session[:request_token_secret] = req_token.secret
16
+
17
+ allow(OAuth::Consumer).to receive(:new) { consumer }
18
+ allow(consumer).to receive(:get_request_token) { req_token }
19
+ allow(req_token).to receive(:get_access_token) { acc_token }
20
+ allow(OAuth::RequestToken).to receive(:new) { req_token }
21
+ allow(acc_token).to receive(:get) { response }
20
22
  end
21
23
 
22
- describe SorceryController do
24
+ describe SorceryController, :active_record => true do
23
25
  before(:all) do
24
26
  ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/external")
25
27
  User.reset_column_information
@@ -53,8 +55,8 @@ describe SorceryController do
53
55
  it "login_at redirects correctly" do
54
56
  create_new_user
55
57
  get :login_at_test
56
- response.should be_a_redirect
57
- response.should redirect_to("http://myapi.com/oauth/authorize?oauth_callback=http%3A%2F%2Ftest.host%2Foauth%2Ftwitter%2Fcallback&oauth_token=")
58
+ expect(response).to be_a_redirect
59
+ expect(response).to redirect_to("http://myapi.com/oauth/authorize?oauth_callback=http%3A%2F%2Ftest.host%2Foauth%2Ftwitter%2Fcallback&oauth_token=")
58
60
  end
59
61
  after do
60
62
  sorcery_controller_external_property_set(:twitter, :callback_url, "http://blabla.com")
@@ -65,8 +67,8 @@ describe SorceryController do
65
67
  it "login_at redirects correctly", pending: true do
66
68
  create_new_user
67
69
  get :login_at_test
68
- response.should be_a_redirect
69
- response.should redirect_to("http://myapi.com/oauth/authorize?oauth_callback=http%3A%2F%2Fblabla.com&oauth_token=")
70
+ expect(response).to be_a_redirect
71
+ expect(response).to redirect_to("http://myapi.com/oauth/authorize?oauth_callback=http%3A%2F%2Fblabla.com&oauth_token=")
70
72
  end
71
73
  end
72
74
 
@@ -74,22 +76,22 @@ describe SorceryController do
74
76
  sorcery_model_property_set(:authentications_class, Authentication)
75
77
  create_new_external_user(:twitter)
76
78
  get :test_login_from, :oauth_verifier => "blablaRERASDFcxvSDFA"
77
- flash[:notice].should == "Success!"
79
+ expect(flash[:notice]).to eq "Success!"
78
80
  end
79
81
 
80
82
  it "'login_from' fails if user doesn't exist" do
81
83
  sorcery_model_property_set(:authentications_class, Authentication)
82
84
  create_new_user
83
85
  get :test_login_from, :oauth_verifier => "blablaRERASDFcxvSDFA"
84
- flash[:alert].should == "Failed!"
86
+ expect(flash[:alert]).to eq "Failed!"
85
87
  end
86
88
 
87
- it "on successful 'login_from' the user should be redirected to the url he originally wanted" do
89
+ it "on successful 'login_from' the user is redirected to the url he originally wanted" do
88
90
  sorcery_model_property_set(:authentications_class, Authentication)
89
91
  create_new_external_user(:twitter)
90
92
  get :test_return_to_with_external, {}, :return_to_url => "fuu"
91
- response.should redirect_to("fuu")
92
- flash[:notice].should == "Success!"
93
+ expect(response).to redirect_to("fuu")
94
+ expect(flash[:notice]).to eq "Success!"
93
95
  end
94
96
 
95
97
  end
@@ -113,18 +115,18 @@ describe SorceryController do
113
115
  Authentication.delete_all
114
116
  end
115
117
 
116
- it "should not send activation email to external users" do
118
+ it "does not send activation email to external users" do
117
119
  old_size = ActionMailer::Base.deliveries.size
118
120
  create_new_external_user(:twitter)
119
- ActionMailer::Base.deliveries.size.should == old_size
121
+ expect(ActionMailer::Base.deliveries.size).to eq old_size
120
122
  end
121
123
 
122
- it "should not send external users an activation success email" do
124
+ it "does not send external users an activation success email" do
123
125
  sorcery_model_property_set(:activation_success_email_method_name, nil)
124
126
  create_new_external_user(:twitter)
125
127
  old_size = ActionMailer::Base.deliveries.size
126
128
  @user.activate!
127
- ActionMailer::Base.deliveries.size.should == old_size
129
+ expect(ActionMailer::Base.deliveries.size).to eq old_size
128
130
  end
129
131
  end
130
132
 
@@ -152,19 +154,19 @@ describe SorceryController do
152
154
  create_new_external_user(:twitter)
153
155
  end
154
156
 
155
- it "should register login time" do
157
+ it "registers login time" do
156
158
  now = Time.now.in_time_zone
157
159
  get :test_login_from
158
- User.last.last_login_at.should_not be_nil
159
- User.last.last_login_at.to_s(:db).should >= now.to_s(:db)
160
- User.last.last_login_at.to_s(:db).should <= (now+2).to_s(:db)
160
+ expect(User.last.last_login_at).not_to be_nil
161
+ expect(User.last.last_login_at.to_s(:db)).to be >= now.to_s(:db)
162
+ expect(User.last.last_login_at.to_s(:db)).to be <= (now+2).to_s(:db)
161
163
  end
162
164
 
163
- it "should not register login time if configured so" do
165
+ it "does not register login time if configured so" do
164
166
  sorcery_controller_property_set(:register_login_time, false)
165
167
  now = Time.now.in_time_zone
166
168
  get :test_login_from
167
- User.last.last_login_at.should be_nil
169
+ expect(User.last.last_login_at).to be_nil
168
170
  end
169
171
  end
170
172
  end
@@ -194,18 +196,20 @@ describe SorceryController do
194
196
  Timecop.return
195
197
  end
196
198
 
197
- it "should not reset session before session timeout" do
199
+ it "does not reset session before session timeout" do
198
200
  get :test_login_from
199
- session[:user_id].should_not be_nil
200
- flash[:notice].should == "Success!"
201
+
202
+ expect(session[:user_id]).not_to be_nil
203
+ expect(flash[:notice]).to eq "Success!"
201
204
  end
202
205
 
203
- it "should reset session after session timeout" do
206
+ it "resets session after session timeout" do
204
207
  get :test_login_from
205
208
  Timecop.travel(Time.now.in_time_zone+0.6)
206
209
  get :test_should_be_logged_in
207
- session[:user_id].should be_nil
208
- response.should be_a_redirect
210
+
211
+ expect(session[:user_id]).to be_nil
212
+ expect(response).to be_a_redirect
209
213
  end
210
214
  end
211
215
  end
@@ -1,9 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe SorceryController do
3
+ describe SorceryController, :active_record => true do
4
+
5
+ let!(:user) { create_new_user }
4
6
 
5
7
  # ----------------- REMEMBER ME -----------------------
6
- describe SorceryController, "with remember me features" do
8
+ context "with remember me features" do
7
9
 
8
10
  before(:all) do
9
11
  ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/remember_me")
@@ -11,10 +13,6 @@ describe SorceryController do
11
13
  sorcery_reload!([:remember_me])
12
14
  end
13
15
 
14
- before(:each) do
15
- create_new_user
16
- end
17
-
18
16
  after(:all) do
19
17
  ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/remember_me")
20
18
  end
@@ -25,72 +23,75 @@ describe SorceryController do
25
23
  User.delete_all
26
24
  end
27
25
 
28
- it "should set cookie on remember_me!" do
26
+ it "sets cookie on remember_me!" do
29
27
  post :test_login_with_remember, :email => 'bla@bla.com', :password => 'secret'
30
- # @request.cookies.merge!(cookies)
31
- # cookies = ActionDispatch::Cookies::CookieJar.build(@request)
32
- cookies.signed["remember_me_token"].should == assigns[:current_user].remember_me_token
28
+
29
+ expect(cookies.signed["remember_me_token"]).to eq assigns[:current_user].remember_me_token
33
30
  end
34
31
 
35
- it "should clear cookie on forget_me!" do
32
+ it "clears cookie on forget_me!" do
36
33
  cookies["remember_me_token"] == {:value => 'asd54234dsfsd43534', :expires => 3600}
37
34
  get :test_logout
38
- cookies["remember_me_token"].should be_nil
35
+
36
+ expect(cookies["remember_me_token"]).to be_nil
39
37
  end
40
38
 
41
- it "login(email,password,remember_me) should login and remember" do
39
+ it "login(email,password,remember_me) logs user in and remembers" do
42
40
  post :test_login_with_remember_in_login, :email => 'bla@bla.com', :password => 'secret', :remember => "1"
43
- # cookies = ActionDispatch::Cookies::CookieJar.build(@request)
44
- cookies.signed["remember_me_token"].should_not be_nil
45
- cookies.signed["remember_me_token"].should == assigns[:user].remember_me_token
41
+
42
+ expect(cookies.signed["remember_me_token"]).not_to be_nil
43
+ expect(cookies.signed["remember_me_token"]).to eq assigns[:user].remember_me_token
46
44
  end
47
45
 
48
- it "logout should also forget_me!" do
49
- session[:user_id] = @user.id
46
+ it "logout also calls forget_me!" do
47
+ session[:user_id] = user.id
50
48
  get :test_logout_with_remember
51
- cookies["remember_me_token"].should be_nil
49
+
50
+ expect(cookies["remember_me_token"]).to be_nil
52
51
  end
53
52
 
54
- it "should login_from_cookie" do
55
- session[:user_id] = @user.id
53
+ it "logs user in from cookie" do
54
+ session[:user_id] = user.id
56
55
  subject.remember_me!
57
56
  subject.instance_eval do
58
- @current_user = nil
57
+ remove_instance_variable :@current_user
59
58
  end
60
59
  session[:user_id] = nil
61
60
  get :test_login_from_cookie
62
- assigns[:current_user].should == @user
61
+
62
+ expect(assigns[:current_user]).to eq user
63
63
  end
64
64
 
65
- it "should not remember_me! when not asked to, even if third parameter is used" do
65
+ it "doest not remember_me! when not asked to, even if third parameter is used" do
66
66
  post :test_login_with_remember_in_login, :email => 'bla@bla.com', :password => 'secret', :remember => "0"
67
- cookies["remember_me_token"].should be_nil
67
+
68
+ expect(cookies["remember_me_token"]).to be_nil
68
69
  end
69
70
 
70
- it "should not remember_me! when not asked to" do
71
+ it "doest not remember_me! when not asked to" do
71
72
  post :test_login, :email => 'bla@bla.com', :password => 'secret'
72
- cookies["remember_me_token"].should be_nil
73
+ expect(cookies["remember_me_token"]).to be_nil
73
74
  end
74
75
 
75
76
  # --- login_user(user) ---
76
- specify { should respond_to(:auto_login) }
77
+ specify { expect(@controller).to respond_to :auto_login }
77
78
 
78
- it "auto_login(user) should login a user instance without remembering" do
79
- create_new_user
79
+ it "auto_login(user) logs in an user instance without remembering" do
80
80
  session[:user_id] = nil
81
- subject.auto_login(@user)
81
+ subject.auto_login(user)
82
82
  get :test_login_from_cookie
83
- assigns[:current_user].should == @user
84
- cookies["remember_me_token"].should be_nil
83
+
84
+ expect(assigns[:current_user]).to eq user
85
+ expect(cookies["remember_me_token"]).to be_nil
85
86
  end
86
87
 
87
- it "auto_login(user, true) should login a user instance with remembering" do
88
- create_new_user
88
+ it "auto_login(user, true) logs in an user instance with remembering" do
89
89
  session[:user_id] = nil
90
- subject.auto_login(@user, true)
90
+ subject.auto_login(user, true)
91
91
  get :test_login_from_cookie
92
- assigns[:current_user].should == @user
93
- cookies["remember_me_token"].should_not be_nil
92
+
93
+ expect(assigns[:current_user]).to eq user
94
+ expect(cookies["remember_me_token"]).not_to be_nil
94
95
  end
95
96
  end
96
97
  end
@@ -1,54 +1,69 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe SorceryController do
3
+ describe SorceryController, :active_record => true do
4
+
5
+ let!(:user) { create_new_user }
4
6
 
5
7
  # ----------------- SESSION TIMEOUT -----------------------
6
- describe SorceryController, "with session timeout features" do
8
+ context "with session timeout features" do
7
9
  before(:all) do
8
10
  sorcery_reload!([:session_timeout])
9
11
  sorcery_controller_property_set(:session_timeout,0.5)
10
- create_new_user
11
12
  end
12
13
 
13
14
  after(:each) do
14
15
  Timecop.return
15
16
  end
16
17
 
17
- it "should not reset session before session timeout" do
18
+ it "does not reset session before session timeout" do
18
19
  login_user
19
20
  get :test_should_be_logged_in
20
- session[:user_id].should_not be_nil
21
- response.should be_a_success
21
+
22
+ expect(session[:user_id]).not_to be_nil
23
+ expect(response).to be_a_success
22
24
  end
23
25
 
24
- it "should reset session after session timeout" do
26
+ it "resets session after session timeout" do
25
27
  login_user
26
28
  Timecop.travel(Time.now.in_time_zone+0.6)
27
29
  get :test_should_be_logged_in
28
- session[:user_id].should be_nil
29
- response.should be_a_redirect
30
+
31
+ expect(session[:user_id]).to be_nil
32
+ expect(response).to be_a_redirect
33
+ end
34
+
35
+ it "works if the session is stored as a string or a Time" do
36
+ session[:login_time] = Time.now.to_s
37
+ get :test_login, :email => 'bla@bla.com', :password => 'secret'
38
+
39
+ expect(session[:user_id]).not_to be_nil
40
+ expect(response).to be_a_success
30
41
  end
31
42
 
32
43
  context "with 'session_timeout_from_last_action'" do
33
- it "should not logout if there was activity" do
44
+ it "does not logout if there was activity" do
34
45
  sorcery_controller_property_set(:session_timeout_from_last_action, true)
35
46
  get :test_login, :email => 'bla@bla.com', :password => 'secret'
36
47
  Timecop.travel(Time.now.in_time_zone+0.3)
37
48
  get :test_should_be_logged_in
38
- session[:user_id].should_not be_nil
49
+
50
+ expect(session[:user_id]).not_to be_nil
51
+
39
52
  Timecop.travel(Time.now.in_time_zone+0.3)
40
53
  get :test_should_be_logged_in
41
- session[:user_id].should_not be_nil
42
- response.should be_a_success
54
+
55
+ expect(session[:user_id]).not_to be_nil
56
+ expect(response).to be_a_success
43
57
  end
44
58
 
45
- it "with 'session_timeout_from_last_action' should logout if there was no activity" do
59
+ it "with 'session_timeout_from_last_action' logs out if there was no activity" do
46
60
  sorcery_controller_property_set(:session_timeout_from_last_action, true)
47
61
  get :test_login, :email => 'bla@bla.com', :password => 'secret'
48
62
  Timecop.travel(Time.now.in_time_zone+0.6)
49
63
  get :test_should_be_logged_in
50
- session[:user_id].should be_nil
51
- response.should be_a_redirect
64
+
65
+ expect(session[:user_id]).to be_nil
66
+ expect(response).to be_a_redirect
52
67
  end
53
68
  end
54
69
  end
@@ -1,182 +1,8 @@
1
1
  require 'spec_helper'
2
+ require 'shared_examples/controller_shared_examples'
2
3
 
3
- describe SorceryController do
4
-
5
- # ----------------- PLUGIN CONFIGURATION -----------------------
6
- describe SorceryController, "plugin configuration" do
7
- before(:all) do
8
- sorcery_reload!
9
- end
10
-
11
- after(:each) do
12
- Sorcery::Controller::Config.reset!
13
- sorcery_reload!
14
- end
15
-
16
- it "should enable configuration option 'user_class'" do
17
- sorcery_controller_property_set(:user_class, "TestUser")
18
- Sorcery::Controller::Config.user_class.should == "TestUser"
19
- end
20
-
21
- it "should enable configuration option 'not_authenticated_action'" do
22
- sorcery_controller_property_set(:not_authenticated_action, :my_action)
23
- Sorcery::Controller::Config.not_authenticated_action.should equal(:my_action)
24
- end
25
-
26
- end
27
-
28
- # ----------------- PLUGIN ACTIVATED -----------------------
29
- describe SorceryController, "when activated with sorcery" do
30
- before(:all) do
31
- sorcery_reload!
32
- User.delete_all
33
- end
34
-
35
- before(:each) do
36
- create_new_user
37
- end
38
-
39
- after(:each) do
40
- Sorcery::Controller::Config.reset!
41
- sorcery_reload!
42
- User.delete_all
43
- sorcery_controller_property_set(:user_class, User)
44
- sorcery_model_property_set(:username_attribute_names, [:email])
45
- end
46
-
47
- specify { should respond_to(:login) }
48
-
49
- specify { should respond_to(:logout) }
50
-
51
- specify { should respond_to(:logged_in?) }
52
-
53
- specify { should respond_to(:current_user) }
54
-
55
- it "login(username,password) should return the user when success and set the session with user.id" do
56
- get :test_login, :email => 'bla@bla.com', :password => 'secret'
57
- assigns[:user].should == @user
58
- session[:user_id].should == @user.id
59
- end
60
-
61
- it "login(email,password) should return the user when success and set the session with user.id" do
62
- get :test_login, :email => 'bla@bla.com', :password => 'secret'
63
- assigns[:user].should == @user
64
- session[:user_id].should == @user.id
65
- end
66
-
67
- it "login(username,password) should return nil and not set the session when failure" do
68
- get :test_login, :email => 'bla@bla.com', :password => 'opensesame!'
69
- assigns[:user].should be_nil
70
- session[:user_id].should be_nil
71
- end
72
-
73
- it "login(email,password) should return the user when success and set the session with the _csrf_token" do
74
- get :test_login, :email => 'bla@bla.com', :password => 'secret'
75
- session[:_csrf_token].should_not be_nil
76
- end
77
-
78
- it "login(username,password) should return nil and not set the session when upper case username" do
79
- get :test_login, :email => 'BLA@BLA.COM', :password => 'secret'
80
- assigns[:user].should be_nil
81
- session[:user_id].should be_nil
82
- end
83
-
84
- it "login(username,password) should return the user and set the session with user.id when upper case username and config is downcase before authenticating" do
85
- sorcery_model_property_set(:downcase_username_before_authenticating, true)
86
- get :test_login, :email => 'BLA@BLA.COM', :password => 'secret'
87
- assigns[:user].should == @user
88
- session[:user_id].should == @user.id
89
- end
90
-
91
- it "login(username,password) should return nil and not set the session when user was created with upper case username, config is default, and log in username is lower case" do
92
- create_new_user({:username => "", :email => "BLA1@BLA.COM", :password => 'secret1'})
93
- get :test_login, :email => 'bla1@bla.com', :password => 'secret1'
94
- assigns[:user].should be_nil
95
- session[:user_id].should be_nil
96
- end
97
-
98
- it "login(username,password) should return the user and set the session with user.id when user was created with upper case username and config is downcase before authenticating" do
99
- sorcery_model_property_set(:downcase_username_before_authenticating, true)
100
- create_new_user({:username => "", :email => "BLA1@BLA.COM", :password => 'secret1'})
101
- get :test_login, :email => 'bla1@bla.com', :password => 'secret1'
102
- assigns[:user].should == @user
103
- session[:user_id].should == @user.id
104
- end
105
-
106
- it "logout should clear the session" do
107
- cookies[:remember_me_token] = nil
108
- session[:user_id] = @user.id
109
- get :test_logout
110
- session[:user_id].should be_nil
111
- end
112
-
113
- it "logged_in? should return true if logged in" do
114
- session[:user_id] = @user.id
115
- subject.logged_in?.should be_true
116
- end
117
-
118
- it "logged_in? should return false if not logged in" do
119
- session[:user_id] = nil
120
- subject.logged_in?.should be_false
121
- end
122
-
123
- it "current_user should return the user instance if logged in" do
124
- create_new_user
125
- session[:user_id] = @user.id
126
- 2.times { subject.current_user.should == @user } # memoized!
127
- end
128
-
129
- it "current_user should return false if not logged in" do
130
- session[:user_id] = nil
131
- 2.times { subject.current_user.should == false } # memoized!
132
- end
133
-
134
- specify { should respond_to(:require_login) }
135
-
136
- it "should call the configured 'not_authenticated_action' when authenticate before_filter fails" do
137
- session[:user_id] = nil
138
- sorcery_controller_property_set(:not_authenticated_action, :test_not_authenticated_action)
139
- get :test_logout
140
- response.body.should == "test_not_authenticated_action"
141
- end
142
-
143
- it "require_login before_filter should save the url that the user originally wanted" do
144
- get :some_action
145
- session[:return_to_url].should == "http://test.host/some_action"
146
- response.should redirect_to("http://test.host/")
147
- end
148
-
149
- it "require_login before_filter should not save the url that the user originally wanted upon all non-get http methods" do
150
- [:post, :put, :delete].each do |m|
151
- self.send(m, :some_action)
152
- session[:return_to_url].should be_nil
153
- end
154
- end
155
-
156
- it "on successful login the user should be redirected to the url he originally wanted" do
157
- session[:return_to_url] = "http://test.host/some_action"
158
- post :test_return_to, :email => 'bla@bla.com', :password => 'secret'
159
- response.should redirect_to("http://test.host/some_action")
160
- flash[:notice].should == "haha!"
161
- end
162
-
163
-
164
- # --- auto_login(user) ---
165
- specify { should respond_to(:auto_login) }
166
-
167
- it "auto_login(user) should login a user instance" do
168
- session[:user_id] = nil
169
- subject.auto_login(@user)
170
- subject.logged_in?.should be_true
171
- end
172
-
173
- it "auto_login(user) should work even if current_user was already set to false" do
174
- get :test_logout
175
- session[:user_id].should be_nil
176
- subject.current_user.should be_false
177
- get :test_auto_login
178
- assigns[:result].should == User.find(:first)
179
- end
180
- end
4
+ describe SorceryController, :active_record => true do
181
5
 
6
+ it_should_behave_like "sorcery_controller"
7
+
182
8
  end