sorcery 0.8.6 → 0.9.0

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 (126) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/.travis.yml +75 -14
  4. data/CHANGELOG.md +23 -1
  5. data/Gemfile +1 -0
  6. data/README.md +137 -86
  7. data/gemfiles/active_record-rails40.gemfile +7 -0
  8. data/gemfiles/active_record-rails41.gemfile +3 -2
  9. data/gemfiles/mongo_mapper-rails40.gemfile +9 -0
  10. data/gemfiles/mongo_mapper-rails41.gemfile +2 -1
  11. data/gemfiles/mongoid-rails40.gemfile +9 -0
  12. data/gemfiles/mongoid-rails41.gemfile +3 -5
  13. data/gemfiles/mongoid3-rails32.gemfile +9 -0
  14. data/lib/generators/sorcery/USAGE +1 -1
  15. data/lib/generators/sorcery/install_generator.rb +19 -5
  16. data/lib/generators/sorcery/templates/initializer.rb +34 -9
  17. data/lib/generators/sorcery/templates/migration/brute_force_protection.rb +3 -1
  18. data/lib/generators/sorcery/templates/migration/core.rb +2 -2
  19. data/lib/generators/sorcery/templates/migration/external.rb +3 -1
  20. data/lib/sorcery.rb +75 -43
  21. data/lib/sorcery/adapters/active_record_adapter.rb +120 -0
  22. data/lib/sorcery/adapters/base_adapter.rb +30 -0
  23. data/lib/sorcery/adapters/data_mapper_adapter.rb +176 -0
  24. data/lib/sorcery/adapters/mongo_mapper_adapter.rb +110 -0
  25. data/lib/sorcery/adapters/mongoid_adapter.rb +97 -0
  26. data/lib/sorcery/controller.rb +5 -64
  27. data/lib/sorcery/controller/config.rb +65 -0
  28. data/lib/sorcery/controller/submodules/activity_logging.rb +16 -21
  29. data/lib/sorcery/controller/submodules/brute_force_protection.rb +6 -6
  30. data/lib/sorcery/controller/submodules/external.rb +8 -28
  31. data/lib/sorcery/controller/submodules/remember_me.rb +4 -4
  32. data/lib/sorcery/controller/submodules/session_timeout.rb +10 -6
  33. data/lib/sorcery/model.rb +43 -175
  34. data/lib/sorcery/model/config.rb +96 -0
  35. data/lib/sorcery/model/submodules/activity_logging.rb +29 -36
  36. data/lib/sorcery/model/submodules/brute_force_protection.rb +21 -37
  37. data/lib/sorcery/model/submodules/external.rb +53 -9
  38. data/lib/sorcery/model/submodules/remember_me.rb +12 -31
  39. data/lib/sorcery/model/submodules/reset_password.rb +21 -39
  40. data/lib/sorcery/model/submodules/user_activation.rb +21 -63
  41. data/lib/sorcery/model/temporary_token.rb +4 -4
  42. data/lib/sorcery/providers/base.rb +11 -0
  43. data/lib/sorcery/providers/facebook.rb +1 -1
  44. data/lib/sorcery/providers/github.rb +1 -1
  45. data/lib/sorcery/providers/google.rb +1 -1
  46. data/lib/sorcery/providers/heroku.rb +57 -0
  47. data/lib/sorcery/providers/jira.rb +77 -0
  48. data/lib/sorcery/providers/linkedin.rb +1 -1
  49. data/lib/sorcery/providers/liveid.rb +1 -1
  50. data/lib/sorcery/providers/salesforce.rb +50 -0
  51. data/lib/sorcery/providers/twitter.rb +1 -1
  52. data/lib/sorcery/providers/vk.rb +6 -4
  53. data/lib/sorcery/providers/xing.rb +1 -1
  54. data/lib/sorcery/test_helpers/internal.rb +7 -3
  55. data/lib/sorcery/test_helpers/rails/controller.rb +5 -1
  56. data/lib/sorcery/version.rb +3 -0
  57. data/sorcery.gemspec +6 -2
  58. data/spec/active_record/user_activity_logging_spec.rb +9 -0
  59. data/spec/controllers/controller_activity_logging_spec.rb +124 -0
  60. data/spec/controllers/controller_brute_force_protection_spec.rb +43 -0
  61. data/spec/{active_record → controllers}/controller_http_basic_auth_spec.rb +14 -11
  62. data/spec/{active_record → controllers}/controller_oauth2_spec.rb +128 -56
  63. data/spec/{active_record → controllers}/controller_oauth_spec.rb +94 -70
  64. data/spec/{active_record → controllers}/controller_remember_me_spec.rb +32 -12
  65. data/spec/{active_record → controllers}/controller_session_timeout_spec.rb +15 -5
  66. data/spec/{shared_examples/controller_shared_examples.rb → controllers/controller_spec.rb} +34 -19
  67. data/spec/{datamapper → data_mapper}/user_activation_spec.rb +1 -1
  68. data/spec/data_mapper/user_activity_logging_spec.rb +14 -0
  69. data/spec/{datamapper → data_mapper}/user_brute_force_protection_spec.rb +1 -1
  70. data/spec/{datamapper → data_mapper}/user_oauth_spec.rb +1 -1
  71. data/spec/{datamapper → data_mapper}/user_remember_me_spec.rb +1 -1
  72. data/spec/{datamapper → data_mapper}/user_reset_password_spec.rb +1 -1
  73. data/spec/{datamapper → data_mapper}/user_spec.rb +1 -1
  74. data/spec/mongoid/user_spec.rb +13 -0
  75. data/spec/orm/active_record.rb +12 -0
  76. data/spec/orm/{datamapper.rb → data_mapper.rb} +16 -2
  77. data/spec/orm/mongo_mapper.rb +0 -1
  78. data/spec/orm/mongoid.rb +4 -0
  79. data/spec/rails_app/app/controllers/sorcery_controller.rb +62 -1
  80. data/spec/rails_app/app/{datamapper → data_mapper}/authentication.rb +0 -0
  81. data/spec/rails_app/app/{datamapper → data_mapper}/user.rb +0 -0
  82. data/spec/rails_app/app/mongo_mapper/user.rb +2 -0
  83. data/spec/rails_app/config/routes.rb +9 -0
  84. data/spec/rails_app/db/migrate/core/20101224223620_create_users.rb +2 -2
  85. data/spec/shared_examples/user_activation_shared_examples.rb +7 -7
  86. data/spec/shared_examples/user_activity_logging_shared_examples.rb +73 -5
  87. data/spec/shared_examples/user_brute_force_protection_shared_examples.rb +127 -9
  88. data/spec/shared_examples/user_oauth_shared_examples.rb +3 -6
  89. data/spec/shared_examples/user_remember_me_shared_examples.rb +6 -3
  90. data/spec/shared_examples/user_reset_password_shared_examples.rb +10 -10
  91. data/spec/shared_examples/user_shared_examples.rb +117 -30
  92. data/spec/spec_helper.rb +7 -22
  93. metadata +36 -58
  94. data/Gemfile.rails4 +0 -22
  95. data/VERSION +0 -1
  96. data/lib/sorcery/model/adapters/active_record.rb +0 -54
  97. data/lib/sorcery/model/adapters/datamapper.rb +0 -123
  98. data/lib/sorcery/model/adapters/mongo_mapper.rb +0 -60
  99. data/lib/sorcery/model/adapters/mongoid.rb +0 -88
  100. data/lib/sorcery/test_helpers/rails.rb +0 -7
  101. data/spec/active_record/controller_activity_logging_spec.rb +0 -29
  102. data/spec/active_record/controller_brute_force_protection_spec.rb +0 -158
  103. data/spec/active_record/controller_spec.rb +0 -8
  104. data/spec/active_record/integration_spec.rb +0 -23
  105. data/spec/datamapper/controller_activity_logging_spec.rb +0 -17
  106. data/spec/datamapper/controller_spec.rb +0 -8
  107. data/spec/datamapper/user_activity_logging_spec.rb +0 -9
  108. data/spec/mongo_mapper/controller_spec.rb +0 -8
  109. data/spec/mongoid/controller_activity_logging_spec.rb +0 -16
  110. data/spec/mongoid/controller_spec.rb +0 -8
  111. data/spec/rails_app/public/404.html +0 -26
  112. data/spec/rails_app/public/422.html +0 -26
  113. data/spec/rails_app/public/500.html +0 -26
  114. data/spec/rails_app/public/favicon.ico +0 -0
  115. data/spec/rails_app/public/images/rails.png +0 -0
  116. data/spec/rails_app/public/javascripts/application.js +0 -2
  117. data/spec/rails_app/public/javascripts/controls.js +0 -965
  118. data/spec/rails_app/public/javascripts/dragdrop.js +0 -974
  119. data/spec/rails_app/public/javascripts/effects.js +0 -1123
  120. data/spec/rails_app/public/javascripts/prototype.js +0 -6001
  121. data/spec/rails_app/public/javascripts/rails.js +0 -175
  122. data/spec/rails_app/public/robots.txt +0 -5
  123. data/spec/rails_app/public/stylesheets/.gitkeep +0 -0
  124. data/spec/shared_examples/controller_activity_logging_shared_examples.rb +0 -125
  125. data/spec/shared_examples/controller_oauth2_shared_examples.rb +0 -52
  126. data/spec/shared_examples/controller_oauth_shared_examples.rb +0 -62
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- require 'shared_examples/controller_oauth_shared_examples'
3
+ # require 'shared_examples/controller_oauth_shared_examples'
4
4
  require 'ostruct'
5
5
 
6
6
  def stub_all_oauth_requests!
@@ -21,21 +21,25 @@ def stub_all_oauth_requests!
21
21
  allow(acc_token).to receive(:get) { response }
22
22
  end
23
23
 
24
- describe SorceryController, :active_record => true do
25
- before(:all) do
26
- ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/external")
27
- User.reset_column_information
24
+ describe SorceryController do
25
+
26
+ let(:user) { double('user', id: 42) }
28
27
 
28
+ before(:all) do
29
29
  sorcery_reload!([:external])
30
- sorcery_controller_property_set(:external_providers, [:twitter])
30
+ sorcery_controller_property_set(:external_providers, [:twitter, :jira])
31
31
  sorcery_controller_external_property_set(:twitter, :key, "eYVNBjBDi33aa9GkA3w")
32
32
  sorcery_controller_external_property_set(:twitter, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
33
33
  sorcery_controller_external_property_set(:twitter, :callback_url, "http://blabla.com")
34
- end
35
34
 
36
- after(:all) do
37
- ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/external")
35
+ sorcery_controller_external_property_set(:jira, :key, "7810b8e317ebdc81601c72f8daecc0f1")
36
+ sorcery_controller_external_property_set(:jira, :secret, "MyAppUsingJira")
37
+ sorcery_controller_external_property_set(:jira, :site, "http://jira.mycompany.com/plugins/servlet/oauth")
38
+ sorcery_controller_external_property_set(:jira, :signature_method, "RSA-SHA1")
39
+ sorcery_controller_external_property_set(:jira, :private_key_file, "myrsakey.pem")
40
+ sorcery_controller_external_property_set(:jira, :callback_url, "http://myappusingjira.com/home")
38
41
  end
42
+
39
43
  # ----------------- OAuth -----------------------
40
44
  describe SorceryController, "'using external API to login'" do
41
45
 
@@ -43,17 +47,11 @@ describe SorceryController, :active_record => true do
43
47
  stub_all_oauth_requests!
44
48
  end
45
49
 
46
- after(:each) do
47
- User.delete_all
48
- Authentication.delete_all
49
- end
50
-
51
50
  context "when callback_url begin with /" do
52
51
  before do
53
52
  sorcery_controller_external_property_set(:twitter, :callback_url, "/oauth/twitter/callback")
54
53
  end
55
54
  it "login_at redirects correctly" do
56
- create_new_user
57
55
  get :login_at_test
58
56
  expect(response).to be_a_redirect
59
57
  expect(response).to redirect_to("http://myapi.com/oauth/authorize?oauth_callback=http%3A%2F%2Ftest.host%2Foauth%2Ftwitter%2Fcallback&oauth_token=")
@@ -65,7 +63,6 @@ describe SorceryController, :active_record => true do
65
63
 
66
64
  context "when callback_url begin with http://" do
67
65
  it "login_at redirects correctly", pending: true do
68
- create_new_user
69
66
  get :login_at_test
70
67
  expect(response).to be_a_redirect
71
68
  expect(response).to redirect_to("http://myapi.com/oauth/authorize?oauth_callback=http%3A%2F%2Fblabla.com&oauth_token=")
@@ -73,123 +70,149 @@ describe SorceryController, :active_record => true do
73
70
  end
74
71
 
75
72
  it "logins if user exists" do
76
- sorcery_model_property_set(:authentications_class, Authentication)
77
- create_new_external_user(:twitter)
73
+ expect(User).to receive(:load_from_provider).with(:twitter, '123').and_return(user)
74
+
78
75
  get :test_login_from, :oauth_verifier => "blablaRERASDFcxvSDFA"
79
76
  expect(flash[:notice]).to eq "Success!"
80
77
  end
81
78
 
82
79
  it "'login_from' fails if user doesn't exist" do
83
- sorcery_model_property_set(:authentications_class, Authentication)
84
- create_new_user
80
+ expect(User).to receive(:load_from_provider).with(:twitter, '123').and_return(nil)
81
+
85
82
  get :test_login_from, :oauth_verifier => "blablaRERASDFcxvSDFA"
86
83
  expect(flash[:alert]).to eq "Failed!"
87
84
  end
88
85
 
89
86
  it "on successful 'login_from' the user is redirected to the url he originally wanted" do
90
- sorcery_model_property_set(:authentications_class, Authentication)
91
- create_new_external_user(:twitter)
87
+ expect(User).to receive(:load_from_provider).with(:twitter, '123').and_return(user)
92
88
  get :test_return_to_with_external, {}, :return_to_url => "fuu"
93
89
  expect(response).to redirect_to("fuu")
94
90
  expect(flash[:notice]).to eq "Success!"
95
91
  end
96
92
 
93
+ context "when jira" do
94
+ it "user logins successfully" do
95
+ get :login_at_test_jira
96
+ expect(session[:request_token]).not_to be_nil
97
+ expect(response).to be_a_redirect
98
+ end
99
+ end
100
+
97
101
  end
98
102
 
99
103
  describe SorceryController do
100
- it_behaves_like "oauth_controller"
101
- end
104
+ describe "using 'create_from'" do
105
+ before(:each) do
106
+ stub_all_oauth_requests!
107
+ end
102
108
 
103
- describe SorceryController, "using OAuth with User Activation features" do
104
- before(:all) do
105
- ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/activation")
106
- sorcery_reload!([:user_activation,:external], :user_activation_mailer => ::SorceryMailer)
107
- end
109
+ it "creates a new user" do
110
+ sorcery_controller_external_property_set(:twitter, :user_info_mapping, {:username => "screen_name"})
111
+ expect(User).to receive(:load_from_provider).with('twitter', '123').and_return(nil)
112
+ expect(User).to receive(:create_from_provider).with('twitter', '123', {username: 'nbenari'}).and_return(user)
108
113
 
109
- after(:all) do
110
- ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/activation")
111
- end
114
+ get :test_create_from_provider, :provider => "twitter"
115
+ end
112
116
 
113
- after(:each) do
114
- User.delete_all
115
- Authentication.delete_all
116
- end
117
+ it "supports nested attributes" do
118
+ sorcery_controller_external_property_set(:twitter, :user_info_mapping, {:username => "status/text"})
119
+ expect(User).to receive(:load_from_provider).with('twitter', '123').and_return(nil)
120
+ expect(User).to receive(:create_from_provider).with('twitter', '123', {username: 'coming soon to sorcery gem: twitter and facebook authentication support.'}).and_return(user)
117
121
 
118
- it "does not send activation email to external users" do
119
- old_size = ActionMailer::Base.deliveries.size
120
- create_new_external_user(:twitter)
121
- expect(ActionMailer::Base.deliveries.size).to eq old_size
122
- end
122
+ get :test_create_from_provider, :provider => "twitter"
123
+ end
124
+
125
+ it "does not crash on missing nested attributes" do
126
+ sorcery_controller_external_property_set(:twitter, :user_info_mapping, {:username => "status/text", :created_at => "does/not/exist"})
127
+ expect(User).to receive(:load_from_provider).with('twitter', '123').and_return(nil)
128
+ expect(User).to receive(:create_from_provider).with('twitter', '123', {username: 'coming soon to sorcery gem: twitter and facebook authentication support.'}).and_return(user)
129
+
130
+ get :test_create_from_provider, :provider => "twitter"
131
+ end
132
+
133
+ it "binds new provider" do
134
+ sorcery_model_property_set(:authentications_class, UserProvider)
135
+
136
+ allow(user).to receive_message_chain(:sorcery_config, :username_attribute_names, :first) { :username }
137
+ allow(user).to receive(:username).and_return('bla@bla.com')
138
+ login_user(user)
139
+
140
+ expect(user).to receive(:add_provider_to_user).with('twitter', '123')
141
+ get :test_add_second_provider, :provider => "twitter"
142
+ end
143
+
144
+ describe "with a block" do
145
+ it "does not create user" do
146
+ sorcery_model_property_set(:authentications_class, Authentication)
147
+ sorcery_controller_external_property_set(:twitter, :user_info_mapping, {:username => "screen_name"})
148
+
149
+ u = double('user')
150
+ expect(User).to receive(:load_from_provider).with('twitter', '123').and_return(nil)
151
+ expect(User).to receive(:create_from_provider).with('twitter', '123', {username: 'nbenari'}).and_return(u).and_yield(u)
123
152
 
124
- it "does not send external users an activation success email" do
125
- sorcery_model_property_set(:activation_success_email_method_name, nil)
126
- create_new_external_user(:twitter)
127
- old_size = ActionMailer::Base.deliveries.size
128
- @user.activate!
129
- expect(ActionMailer::Base.deliveries.size).to eq old_size
153
+ get :test_create_from_provider_with_block, :provider => "twitter"
154
+ end
155
+
156
+ end
130
157
  end
131
158
  end
132
159
 
133
160
  describe SorceryController, "OAuth with user activation features" do
134
161
  before(:all) do
135
- ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/external")
136
- ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/activity_logging")
137
- User.reset_column_information
138
162
  sorcery_reload!([:activity_logging, :external])
139
163
  end
140
164
 
141
- after(:all) do
142
- ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/external")
143
- ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/activity_logging")
144
- User.reset_column_information
145
- end
146
-
147
165
  context "when twitter" do
148
166
  before(:each) do
149
- User.delete_all
150
- Authentication.delete_all
151
167
  sorcery_controller_property_set(:register_login_time, true)
168
+ sorcery_controller_property_set(:register_logout_time, false)
169
+ sorcery_controller_property_set(:register_last_activity_time, false)
170
+ sorcery_controller_property_set(:register_last_ip_address, false)
152
171
  stub_all_oauth_requests!
153
- sorcery_model_property_set(:authentications_class, Authentication)
154
- create_new_external_user(:twitter)
155
172
  end
156
173
 
157
174
  it "registers login time" do
158
175
  now = Time.now.in_time_zone
176
+ Timecop.freeze(now)
177
+ expect(User).to receive(:load_from_provider).and_return(user)
178
+ expect(user).to receive(:set_last_login_at).with(be_within(0.1).of(now))
159
179
  get :test_login_from
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)
180
+ Timecop.return
163
181
  end
164
182
 
165
183
  it "does not register login time if configured so" do
166
184
  sorcery_controller_property_set(:register_login_time, false)
167
185
  now = Time.now.in_time_zone
186
+ Timecop.freeze(now)
187
+ expect(User).to receive(:load_from_provider).and_return(user)
188
+ expect(user).to receive(:set_last_login_at).never
168
189
  get :test_login_from
169
- expect(User.last.last_login_at).to be_nil
190
+ Timecop.return
170
191
  end
171
192
  end
172
193
  end
173
194
 
174
195
  describe SorceryController, "OAuth with session timeout features" do
175
196
  before(:all) do
176
- ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/external")
177
- User.reset_column_information
197
+ if SORCERY_ORM == :active_record
198
+ ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/external")
199
+ User.reset_column_information
200
+ end
201
+
178
202
  sorcery_reload!([:session_timeout, :external])
179
203
  end
180
204
 
181
205
  after(:all) do
182
- ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/external")
206
+ if SORCERY_ORM == :active_record
207
+ ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/external")
208
+ end
183
209
  end
184
210
 
185
211
  context "when twitter" do
186
212
  before(:each) do
187
- User.delete_all
188
- Authentication.delete_all
189
213
  sorcery_model_property_set(:authentications_class, Authentication)
190
214
  sorcery_controller_property_set(:session_timeout,0.5)
191
215
  stub_all_oauth_requests!
192
- create_new_external_user(:twitter)
193
216
  end
194
217
 
195
218
  after(:each) do
@@ -197,6 +220,7 @@ describe SorceryController, :active_record => true do
197
220
  end
198
221
 
199
222
  it "does not reset session before session timeout" do
223
+ expect(User).to receive(:load_from_provider).with(:twitter, '123').and_return(user)
200
224
  get :test_login_from
201
225
 
202
226
  expect(session[:user_id]).not_to be_nil
@@ -1,29 +1,32 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe SorceryController, :active_record => true do
3
+ describe SorceryController do
4
4
 
5
- let!(:user) { create_new_user }
5
+ let!(:user) { double('user', id: 42) }
6
6
 
7
7
  # ----------------- REMEMBER ME -----------------------
8
8
  context "with remember me features" do
9
9
 
10
10
  before(:all) do
11
- ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/remember_me")
12
- User.reset_column_information
13
11
  sorcery_reload!([:remember_me])
14
12
  end
15
13
 
16
- after(:all) do
17
- ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/remember_me")
18
- end
19
-
20
14
  after(:each) do
21
15
  session = nil
22
16
  cookies = nil
23
- User.delete_all
17
+ end
18
+
19
+ before(:each) do
20
+ allow(user).to receive(:remember_me_token)
21
+ allow(user).to receive(:remember_me_token_expires_at)
22
+ allow(user).to receive_message_chain(:sorcery_config, :remember_me_token_attribute_name).and_return(:remember_me_token)
23
+ allow(user).to receive_message_chain(:sorcery_config, :remember_me_token_expires_at_attribute_name).and_return(:remember_me_token_expires_at)
24
24
  end
25
25
 
26
26
  it "sets cookie on remember_me!" do
27
+ expect(User).to receive(:authenticate).with('bla@bla.com', 'secret').and_return(user)
28
+ expect(user).to receive(:remember_me!)
29
+
27
30
  post :test_login_with_remember, :email => 'bla@bla.com', :password => 'secret'
28
31
 
29
32
  expect(cookies.signed["remember_me_token"]).to eq assigns[:current_user].remember_me_token
@@ -37,6 +40,10 @@ describe SorceryController, :active_record => true do
37
40
  end
38
41
 
39
42
  it "login(email,password,remember_me) logs user in and remembers" do
43
+ expect(User).to receive(:authenticate).with('bla@bla.com', 'secret', '1').and_return(user)
44
+ expect(user).to receive(:remember_me!)
45
+ expect(user).to receive(:remember_me_token).and_return('abracadabra').twice
46
+
40
47
  post :test_login_with_remember_in_login, :email => 'bla@bla.com', :password => 'secret', :remember => "1"
41
48
 
42
49
  expect(cookies.signed["remember_me_token"]).not_to be_nil
@@ -44,19 +51,30 @@ describe SorceryController, :active_record => true do
44
51
  end
45
52
 
46
53
  it "logout also calls forget_me!" do
47
- session[:user_id] = user.id
54
+ session[:user_id] = user.id.to_s
55
+ expect(User.sorcery_adapter).to receive(:find_by_id).with(user.id.to_s).and_return(user)
56
+ expect(user).to receive(:remember_me!)
57
+ expect(user).to receive(:forget_me!)
48
58
  get :test_logout_with_remember
49
59
 
50
60
  expect(cookies["remember_me_token"]).to be_nil
51
61
  end
52
62
 
53
63
  it "logs user in from cookie" do
54
- session[:user_id] = user.id
64
+ session[:user_id] = user.id.to_s
65
+ expect(User.sorcery_adapter).to receive(:find_by_id).with(user.id.to_s).and_return(user)
66
+ expect(user).to receive(:remember_me!)
67
+ expect(user).to receive(:remember_me_token).and_return('token').twice
68
+ expect(user).to receive(:has_remember_me_token?) { true }
69
+
55
70
  subject.remember_me!
56
71
  subject.instance_eval do
57
72
  remove_instance_variable :@current_user
58
73
  end
59
74
  session[:user_id] = nil
75
+
76
+ expect(User.sorcery_adapter).to receive(:find_by_remember_me_token).with('token').and_return(user)
77
+
60
78
  get :test_login_from_cookie
61
79
 
62
80
  expect(assigns[:current_user]).to eq user
@@ -87,11 +105,13 @@ describe SorceryController, :active_record => true do
87
105
 
88
106
  it "auto_login(user, true) logs in an user instance with remembering" do
89
107
  session[:user_id] = nil
108
+ expect(user).to receive(:remember_me!)
90
109
  subject.auto_login(user, true)
110
+
91
111
  get :test_login_from_cookie
92
112
 
93
113
  expect(assigns[:current_user]).to eq user
94
114
  expect(cookies["remember_me_token"]).not_to be_nil
95
115
  end
96
116
  end
97
- end
117
+ end
@@ -1,8 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe SorceryController, :active_record => true do
3
+ describe SorceryController do
4
4
 
5
- let!(:user) { create_new_user }
5
+ let!(:user) { double('user', id: 42) }
6
6
 
7
7
  # ----------------- SESSION TIMEOUT -----------------------
8
8
  context "with session timeout features" do
@@ -15,8 +15,13 @@ describe SorceryController, :active_record => true do
15
15
  Timecop.return
16
16
  end
17
17
 
18
+ before(:each) do
19
+ allow(user).to receive(:username)
20
+ allow(user).to receive_message_chain(:sorcery_config, :username_attribute_names, :first) { :username }
21
+ end
22
+
18
23
  it "does not reset session before session timeout" do
19
- login_user
24
+ login_user user
20
25
  get :test_should_be_logged_in
21
26
 
22
27
  expect(session[:user_id]).not_to be_nil
@@ -24,7 +29,7 @@ describe SorceryController, :active_record => true do
24
29
  end
25
30
 
26
31
  it "resets session after session timeout" do
27
- login_user
32
+ login_user user
28
33
  Timecop.travel(Time.now.in_time_zone+0.6)
29
34
  get :test_should_be_logged_in
30
35
 
@@ -34,6 +39,9 @@ describe SorceryController, :active_record => true do
34
39
 
35
40
  it "works if the session is stored as a string or a Time" do
36
41
  session[:login_time] = Time.now.to_s
42
+ # TODO: ???
43
+ expect(User).to receive(:authenticate).with('bla@bla.com', 'secret').and_return(user)
44
+
37
45
  get :test_login, :email => 'bla@bla.com', :password => 'secret'
38
46
 
39
47
  expect(session[:user_id]).not_to be_nil
@@ -43,6 +51,8 @@ describe SorceryController, :active_record => true do
43
51
  context "with 'session_timeout_from_last_action'" do
44
52
  it "does not logout if there was activity" do
45
53
  sorcery_controller_property_set(:session_timeout_from_last_action, true)
54
+ expect(User).to receive(:authenticate).with('bla@bla.com', 'secret').and_return(user)
55
+
46
56
  get :test_login, :email => 'bla@bla.com', :password => 'secret'
47
57
  Timecop.travel(Time.now.in_time_zone+0.3)
48
58
  get :test_should_be_logged_in
@@ -67,4 +77,4 @@ describe SorceryController, :active_record => true do
67
77
  end
68
78
  end
69
79
  end
70
- end
80
+ end
@@ -1,5 +1,6 @@
1
- shared_examples_for "sorcery_controller" do
1
+ require 'spec_helper'
2
2
 
3
+ describe SorceryController do
3
4
  describe "plugin configuration" do
4
5
  before(:all) do
5
6
  sorcery_reload!
@@ -26,17 +27,15 @@ shared_examples_for "sorcery_controller" do
26
27
 
27
28
  # ----------------- PLUGIN ACTIVATED -----------------------
28
29
  context "when activated with sorcery" do
29
- let!(:user) { create_new_user }
30
+ let(:user) { double('user', id: 42) }
30
31
 
31
32
  before(:all) do
32
33
  sorcery_reload!
33
- User.delete_all
34
34
  end
35
35
 
36
36
  after(:each) do
37
37
  Sorcery::Controller::Config.reset!
38
38
  sorcery_reload!
39
- User.delete_all
40
39
  sorcery_controller_property_set(:user_class, User)
41
40
  sorcery_model_property_set(:username_attribute_names, [:email])
42
41
  end
@@ -50,20 +49,26 @@ shared_examples_for "sorcery_controller" do
50
49
  specify { should respond_to(:current_user) }
51
50
 
52
51
  it "login(username,password) returns the user when success and set the session with user.id" do
52
+ expect(User).to receive(:authenticate).with('bla@bla.com', 'secret').and_return(user)
53
+
53
54
  get :test_login, :email => 'bla@bla.com', :password => 'secret'
54
55
 
55
56
  expect(assigns[:user]).to eq user
56
- expect(session[:user_id]).to eq user.id
57
+ expect(session[:user_id]).to eq "42"
57
58
  end
58
59
 
59
60
  it "login(email,password) returns the user when success and set the session with user.id" do
61
+ expect(User).to receive(:authenticate).with('bla@bla.com', 'secret').and_return(user)
62
+
60
63
  get :test_login, :email => 'bla@bla.com', :password => 'secret'
61
64
 
62
65
  expect(assigns[:user]).to eq user
63
- expect(session[:user_id]).to eq user.id
66
+ expect(session[:user_id]).to eq user.id.to_s
64
67
  end
65
68
 
66
69
  it "login(username,password) returns nil and not set the session when failure" do
70
+ expect(User).to receive(:authenticate).with('bla@bla.com', 'opensesame!').and_return(nil)
71
+
67
72
  get :test_login, :email => 'bla@bla.com', :password => 'opensesame!'
68
73
 
69
74
  expect(assigns[:user]).to be_nil
@@ -71,56 +76,63 @@ shared_examples_for "sorcery_controller" do
71
76
  end
72
77
 
73
78
  it "login(email,password) returns the user when success and set the session with the _csrf_token" do
79
+ expect(User).to receive(:authenticate).with('bla@bla.com', 'secret').and_return(user)
74
80
  get :test_login, :email => 'bla@bla.com', :password => 'secret'
75
81
 
76
82
  expect(session[:_csrf_token]).not_to be_nil
77
83
  end
78
84
 
79
85
  it "login(username,password) returns nil and not set the session when upper case username" do
80
- skip('DM Adapter dependant') if SORCERY_ORM == :datamapper
86
+ skip('DM Adapter dependant') if SORCERY_ORM == :data_mapper
81
87
  get :test_login, :email => 'BLA@BLA.COM', :password => 'secret'
82
88
 
83
89
  expect(assigns[:user]).to be_nil
84
90
  expect(session[:user_id]).to be_nil
85
91
  end
86
92
 
93
+ # TODO: move test to model
87
94
  it "login(username,password) returns the user and set the session with user.id when upper case username and config is downcase before authenticating" do
88
95
  sorcery_model_property_set(:downcase_username_before_authenticating, true)
96
+ expect(User).to receive(:authenticate).with('BLA@BLA.COM', 'secret').and_return(user)
89
97
  get :test_login, :email => 'BLA@BLA.COM', :password => 'secret'
90
98
 
91
99
  expect(assigns[:user]).to eq user
92
- expect(session[:user_id]).to eq user.id
100
+ expect(session[:user_id]).to eq user.id.to_s
93
101
  end
94
102
 
103
+ # TODO: move test to model
95
104
  it "login(username,password) returns 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
96
- skip('DM Adapter dependant') if SORCERY_ORM == :datamapper
97
- create_new_user({:username => "", :email => "BLA1@BLA.COM", :password => 'secret1'})
105
+ skip('DM Adapter dependant') if SORCERY_ORM == :data_mapper
106
+ expect(User).to receive(:authenticate).with('bla1@bla.com', 'secret1').and_return(nil)
98
107
  get :test_login, :email => 'bla1@bla.com', :password => 'secret1'
99
108
 
100
109
  expect(assigns[:user]).to be_nil
101
110
  expect(session[:user_id]).to be_nil
102
111
  end
103
112
 
113
+ # TODO: move test to model
104
114
  it "login(username,password) returns the user and set the session with user.id when user was created with upper case username and config is downcase before authenticating" do
105
- skip('DM Adapter dependant') if SORCERY_ORM == :datamapper
115
+ skip('DM Adapter dependant') if SORCERY_ORM == :data_mapper
106
116
  sorcery_model_property_set(:downcase_username_before_authenticating, true)
107
- new_user = create_new_user({:username => "", :email => "BLA1@BLA.COM", :password => 'secret1'})
117
+ expect(User).to receive(:authenticate).with('bla1@bla.com', 'secret1').and_return(user)
108
118
  get :test_login, :email => 'bla1@bla.com', :password => 'secret1'
109
119
 
110
- expect(assigns[:user]).to eq new_user
111
- expect(session[:user_id]).to eq new_user.id
120
+ expect(assigns[:user]).to eq user
121
+ expect(session[:user_id]).to eq user.id.to_s
112
122
  end
113
123
 
114
124
  it "logout clears the session" do
115
125
  cookies[:remember_me_token] = nil
116
- session[:user_id] = user.id
126
+ session[:user_id] = user.id.to_s
127
+ expect(User.sorcery_adapter).to receive(:find_by_id).with("42") { user }
117
128
  get :test_logout
118
129
 
119
130
  expect(session[:user_id]).to be_nil
120
131
  end
121
132
 
122
133
  it "logged_in? returns true if logged in" do
123
- session[:user_id] = user.id
134
+ session[:user_id] = user.id.to_s
135
+ expect(User.sorcery_adapter).to receive(:find_by_id).with("42") { user }
124
136
 
125
137
  expect(subject.logged_in?).to be true
126
138
  end
@@ -132,14 +144,15 @@ shared_examples_for "sorcery_controller" do
132
144
  end
133
145
 
134
146
  it "current_user returns the user instance if logged in" do
135
- create_new_user
136
- session[:user_id] = user.id
147
+ session[:user_id] = user.id.to_s
148
+ expect(User.sorcery_adapter).to receive(:find_by_id).with("42") { user }
137
149
 
138
150
  2.times { expect(subject.current_user).to eq user } # memoized!
139
151
  end
140
152
 
141
153
  it "current_user returns false if not logged in" do
142
154
  session[:user_id] = nil
155
+ expect(User.sorcery_adapter).to_not receive(:find_by_id)
143
156
 
144
157
  2.times { expect(subject.current_user).to be_nil } # memoized!
145
158
  end
@@ -194,9 +207,11 @@ shared_examples_for "sorcery_controller" do
194
207
  expect(session[:user_id]).to be_nil
195
208
  expect(subject.current_user).to be_nil
196
209
 
210
+ expect(User).to receive(:first) { user }
211
+
197
212
  get :test_auto_login
198
213
 
199
- expect(assigns[:result]).to eq User.first
214
+ expect(assigns[:result]).to eq user
200
215
  end
201
216
  end
202
217