sorcery 0.5.0 → 0.6.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 (98) hide show
  1. data/README.rdoc +9 -1
  2. data/VERSION +1 -1
  3. data/lib/sorcery/controller/adapters/sinatra.rb +35 -28
  4. data/lib/sorcery/controller/submodules/activity_logging.rb +11 -7
  5. data/lib/sorcery/controller/submodules/brute_force_protection.rb +8 -5
  6. data/lib/sorcery/controller/submodules/external.rb +11 -6
  7. data/lib/sorcery/controller/submodules/http_basic_auth.rb +12 -7
  8. data/lib/sorcery/controller/submodules/remember_me.rb +14 -5
  9. data/lib/sorcery/controller/submodules/session_timeout.rb +3 -1
  10. data/lib/sorcery/controller.rb +12 -9
  11. data/lib/sorcery/crypto_providers/aes256.rb +8 -5
  12. data/lib/sorcery/crypto_providers/bcrypt.rb +12 -6
  13. data/lib/sorcery/crypto_providers/sha256.rb +2 -1
  14. data/lib/sorcery/crypto_providers/sha512.rb +2 -1
  15. data/lib/sorcery/engine.rb +0 -7
  16. data/lib/sorcery/initializers/initializer.rb +127 -36
  17. data/lib/sorcery/model/adapters/active_record.rb +2 -2
  18. data/lib/sorcery/model/adapters/mongoid.rb +12 -7
  19. data/lib/sorcery/model/submodules/activity_logging.rb +7 -6
  20. data/lib/sorcery/model/submodules/brute_force_protection.rb +10 -6
  21. data/lib/sorcery/model/submodules/external.rb +4 -2
  22. data/lib/sorcery/model/submodules/remember_me.rb +4 -3
  23. data/lib/sorcery/model/submodules/reset_password.rb +21 -11
  24. data/lib/sorcery/model/submodules/user_activation.rb +23 -10
  25. data/lib/sorcery/model/temporary_token.rb +3 -2
  26. data/lib/sorcery/model.rb +60 -29
  27. data/lib/sorcery/test_helpers/internal/rails.rb +6 -1
  28. data/lib/sorcery/test_helpers/internal/sinatra.rb +3 -3
  29. data/lib/sorcery/test_helpers/internal/sinatra_modular.rb +74 -0
  30. data/lib/sorcery/test_helpers/internal.rb +2 -1
  31. data/lib/sorcery.rb +3 -5
  32. data/sorcery.gemspec +72 -2
  33. data/spec/Gemfile.lock +1 -4
  34. data/spec/rails3/Gemfile.lock +1 -4
  35. data/spec/rails3/spec/controller_oauth2_spec.rb +3 -24
  36. data/spec/rails3/spec/controller_oauth_spec.rb +3 -24
  37. data/spec/rails3/spec/controller_spec.rb +2 -2
  38. data/spec/rails3/spec/user_activation_spec.rb +2 -168
  39. data/spec/rails3/spec/user_activity_logging_spec.rb +2 -30
  40. data/spec/rails3/spec/user_brute_force_protection_spec.rb +2 -35
  41. data/spec/rails3/spec/user_oauth_spec.rb +2 -26
  42. data/spec/rails3/spec/user_remember_me_spec.rb +2 -45
  43. data/spec/rails3/spec/user_reset_password_spec.rb +3 -168
  44. data/spec/rails3/spec/user_spec.rb +3 -293
  45. data/spec/rails3_mongoid/Gemfile.lock +1 -4
  46. data/spec/rails3_mongoid/app/models/authentication.rb +3 -3
  47. data/spec/rails3_mongoid/spec/user_activation_spec.rb +2 -171
  48. data/spec/rails3_mongoid/spec/user_activity_logging_spec.rb +2 -25
  49. data/spec/rails3_mongoid/spec/user_brute_force_protection_spec.rb +2 -35
  50. data/spec/rails3_mongoid/spec/user_oauth_spec.rb +2 -28
  51. data/spec/rails3_mongoid/spec/user_remember_me_spec.rb +2 -45
  52. data/spec/rails3_mongoid/spec/user_reset_password_spec.rb +2 -168
  53. data/spec/rails3_mongoid/spec/user_spec.rb +3 -295
  54. data/spec/shared_examples/controller_oauth2_shared_examples.rb +37 -0
  55. data/spec/shared_examples/controller_oauth_shared_examples.rb +37 -0
  56. data/spec/shared_examples/user_activation_shared_examples.rb +173 -0
  57. data/spec/shared_examples/user_activity_logging_shared_examples.rb +27 -0
  58. data/spec/shared_examples/user_brute_force_protection_shared_examples.rb +37 -0
  59. data/spec/shared_examples/user_oauth_shared_examples.rb +31 -0
  60. data/spec/shared_examples/user_remember_me_shared_examples.rb +47 -0
  61. data/spec/shared_examples/user_reset_password_shared_examples.rb +177 -0
  62. data/spec/shared_examples/user_shared_examples.rb +292 -0
  63. data/spec/sinatra/Gemfile +3 -0
  64. data/spec/sinatra/Gemfile.lock +17 -34
  65. data/spec/sinatra/Rakefile +1 -1
  66. data/spec/sinatra/filters.rb +20 -14
  67. data/spec/sinatra/modular.rb +157 -0
  68. data/spec/sinatra/spec/controller_oauth2_spec.rb +3 -24
  69. data/spec/sinatra/spec/controller_oauth_spec.rb +3 -24
  70. data/spec/sinatra/spec/controller_spec.rb +2 -2
  71. data/spec/sinatra_modular/Gemfile +15 -0
  72. data/spec/sinatra_modular/Gemfile.lock +117 -0
  73. data/spec/sinatra_modular/Rakefile +11 -0
  74. data/spec/sinatra_modular/authentication.rb +3 -0
  75. data/spec/sinatra_modular/db/migrate/activation/20101224223622_add_activation_to_users.rb +17 -0
  76. data/spec/sinatra_modular/db/migrate/activity_logging/20101224223624_add_activity_logging_to_users.rb +17 -0
  77. data/spec/sinatra_modular/db/migrate/brute_force_protection/20101224223626_add_brute_force_protection_to_users.rb +11 -0
  78. data/spec/sinatra_modular/db/migrate/core/20101224223620_create_users.rb +16 -0
  79. data/spec/sinatra_modular/db/migrate/external/20101224223628_create_authentications.rb +14 -0
  80. data/spec/sinatra_modular/db/migrate/remember_me/20101224223623_add_remember_me_token_to_users.rb +15 -0
  81. data/spec/sinatra_modular/db/migrate/reset_password/20101224223622_add_reset_password_to_users.rb +13 -0
  82. data/spec/sinatra_modular/filters.rb +27 -0
  83. data/spec/sinatra_modular/modular.rb +157 -0
  84. data/spec/sinatra_modular/myapp.rb +133 -0
  85. data/spec/sinatra_modular/sorcery_mailer.rb +25 -0
  86. data/spec/sinatra_modular/spec_modular/controller_activity_logging_spec.rb +85 -0
  87. data/spec/sinatra_modular/spec_modular/controller_brute_force_protection_spec.rb +70 -0
  88. data/spec/sinatra_modular/spec_modular/controller_http_basic_auth_spec.rb +53 -0
  89. data/spec/sinatra_modular/spec_modular/controller_oauth2_spec.rb +99 -0
  90. data/spec/sinatra_modular/spec_modular/controller_oauth_spec.rb +100 -0
  91. data/spec/sinatra_modular/spec_modular/controller_remember_me_spec.rb +64 -0
  92. data/spec/sinatra_modular/spec_modular/controller_session_timeout_spec.rb +57 -0
  93. data/spec/sinatra_modular/spec_modular/controller_spec.rb +116 -0
  94. data/spec/sinatra_modular/spec_modular/spec.opts +2 -0
  95. data/spec/sinatra_modular/spec_modular/spec_helper.rb +51 -0
  96. data/spec/sinatra_modular/user.rb +6 -0
  97. data/spec/sinatra_modular/views/test_login.erb +4 -0
  98. metadata +72 -2
@@ -0,0 +1,292 @@
1
+ shared_examples_for "rails_3_core_model" do
2
+ describe User, "loaded plugin configuration" do
3
+ after(:each) do
4
+ User.sorcery_config.reset!
5
+ end
6
+
7
+ it "should enable configuration option 'username_attribute_name'" do
8
+ sorcery_model_property_set(:username_attribute_name, :email)
9
+ User.sorcery_config.username_attribute_name.should equal(:email)
10
+ end
11
+
12
+ it "should enable configuration option 'password_attribute_name'" do
13
+ sorcery_model_property_set(:password_attribute_name, :mypassword)
14
+ User.sorcery_config.password_attribute_name.should equal(:mypassword)
15
+ end
16
+
17
+ it "should enable configuration option 'email_attribute_name'" do
18
+ sorcery_model_property_set(:email_attribute_name, :my_email)
19
+ User.sorcery_config.email_attribute_name.should equal(:my_email)
20
+ end
21
+
22
+ it "should enable configuration option 'crypted_password_attribute_name'" do
23
+ sorcery_model_property_set(:crypted_password_attribute_name, :password)
24
+ User.sorcery_config.crypted_password_attribute_name.should equal(:password)
25
+ end
26
+
27
+ it "should enable configuration option 'salt_attribute_name'" do
28
+ sorcery_model_property_set(:salt_attribute_name, :my_salt)
29
+ User.sorcery_config.salt_attribute_name.should equal(:my_salt)
30
+ end
31
+
32
+ it "should enable configuration option 'encryption_algorithm'" do
33
+ sorcery_model_property_set(:encryption_algorithm, :none)
34
+ User.sorcery_config.encryption_algorithm.should equal(:none)
35
+ end
36
+
37
+ it "should enable configuration option 'encryption_key'" do
38
+ sorcery_model_property_set(:encryption_key, 'asdadas424234242')
39
+ User.sorcery_config.encryption_key.should == 'asdadas424234242'
40
+ end
41
+
42
+ it "should enable configuration option 'custom_encryption_provider'" do
43
+ sorcery_model_property_set(:encryption_algorithm, :custom)
44
+ sorcery_model_property_set(:custom_encryption_provider, Array)
45
+ User.sorcery_config.custom_encryption_provider.should equal(Array)
46
+ end
47
+
48
+ it "should enable configuration option 'salt_join_token'" do
49
+ salt_join_token = "--%%*&-"
50
+ sorcery_model_property_set(:salt_join_token, salt_join_token)
51
+ User.sorcery_config.salt_join_token.should equal(salt_join_token)
52
+ end
53
+
54
+ it "should enable configuration option 'stretches'" do
55
+ stretches = 15
56
+ sorcery_model_property_set(:stretches, stretches)
57
+ User.sorcery_config.stretches.should equal(stretches)
58
+ end
59
+
60
+ end
61
+
62
+ # ----------------- PLUGIN ACTIVATED -----------------------
63
+ describe User, "when activated with sorcery" do
64
+ before(:all) do
65
+ sorcery_reload!()
66
+ end
67
+
68
+ before(:each) do
69
+ User.delete_all
70
+ end
71
+
72
+ it "should respond to class method authenticate" do
73
+ ActiveRecord::Base.should_not respond_to(:authenticate) if defined?(ActiveRecord)
74
+ User.should respond_to(:authenticate)
75
+ end
76
+
77
+ it "authenticate should return true if credentials are good" do
78
+ create_new_user
79
+ User.authenticate(@user.send(User.sorcery_config.username_attribute_name), 'secret').should be_true
80
+ end
81
+
82
+ it "authenticate should return false if credentials are bad" do
83
+ create_new_user
84
+ User.authenticate(@user.send(User.sorcery_config.username_attribute_name), 'wrong!').should be_false
85
+ end
86
+
87
+ specify { User.should respond_to(:encrypt) }
88
+
89
+ it "subclass should inherit config if defined so" do
90
+ sorcery_reload!([],{:subclasses_inherit_config => true})
91
+ class Admin < User
92
+ end
93
+ Admin.sorcery_config.should_not be_nil
94
+ Admin.sorcery_config.should == User.sorcery_config
95
+ end
96
+
97
+ it "subclass should not inherit config if not defined so" do
98
+ sorcery_reload!([],{:subclasses_inherit_config => false})
99
+ class Admin2 < User
100
+ end
101
+ Admin2.sorcery_config.should be_nil
102
+ end
103
+ end
104
+
105
+ # ----------------- REGISTRATION -----------------------
106
+ describe User, "registration" do
107
+
108
+ before(:all) do
109
+ sorcery_reload!()
110
+ end
111
+
112
+ before(:each) do
113
+ User.delete_all
114
+ end
115
+
116
+ it "by default, encryption_provider should not be nil" do
117
+ User.sorcery_config.encryption_provider.should_not be_nil
118
+ end
119
+
120
+ it "should encrypt password when a new user is saved" do
121
+ create_new_user
122
+ User.sorcery_config.encryption_provider.matches?(@user.send(User.sorcery_config.crypted_password_attribute_name),'secret',@user.salt).should be_true
123
+ end
124
+
125
+ it "should clear the virtual password field if the encryption process worked" do
126
+ create_new_user
127
+ @user.password.should be_nil
128
+ end
129
+
130
+ it "should not clear the virtual password field if save failed due to validity" do
131
+ create_new_user
132
+ User.class_eval do
133
+ validates_format_of :email, :with => /^(.)+@(.)+$/, :if => Proc.new {|r| r.email}, :message => "is invalid"
134
+ end
135
+ @user.password = 'blupush'
136
+ @user.email = 'asd'
137
+ @user.save
138
+ @user.password.should_not be_nil
139
+ end
140
+
141
+ it "should not clear the virtual password field if save failed due to exception" do
142
+ create_new_user
143
+ @user.password = '4blupush'
144
+ @user.username = nil
145
+ User.class_eval do
146
+ validates_presence_of :username
147
+ end
148
+ begin
149
+ @user.save! # triggers validation exception since username field is required.
150
+ rescue
151
+ end
152
+ @user.password.should_not be_nil
153
+ end
154
+
155
+ it "should not encrypt the password twice when a user is updated" do
156
+ create_new_user
157
+ @user.email = "blup@bla.com"
158
+ @user.save!
159
+ User.sorcery_config.encryption_provider.matches?(@user.send(User.sorcery_config.crypted_password_attribute_name),'secret',@user.salt).should be_true
160
+ end
161
+
162
+ it "should replace the crypted_password in case a new password is set" do
163
+ create_new_user
164
+ @user.password = 'new_secret'
165
+ @user.save!
166
+ User.sorcery_config.encryption_provider.matches?(@user.send(User.sorcery_config.crypted_password_attribute_name),'secret',@user.salt).should be_false
167
+ end
168
+
169
+ end
170
+
171
+ # ----------------- PASSWORD ENCRYPTION -----------------------
172
+ describe User, "special encryption cases" do
173
+ before(:all) do
174
+ sorcery_reload!()
175
+ @text = "Some Text!"
176
+ end
177
+
178
+ before(:each) do
179
+ User.delete_all
180
+ end
181
+
182
+ after(:each) do
183
+ User.sorcery_config.reset!
184
+ end
185
+
186
+ it "should work with no password encryption" do
187
+ sorcery_model_property_set(:encryption_algorithm, :none)
188
+ create_new_user
189
+ User.authenticate(@user.send(User.sorcery_config.username_attribute_name), 'secret').should be_true
190
+ end
191
+
192
+ it "should work with custom password encryption" do
193
+ class MyCrypto
194
+ def self.encrypt(*tokens)
195
+ tokens.flatten.join('').gsub(/e/,'A')
196
+ end
197
+
198
+ def self.matches?(crypted,*tokens)
199
+ crypted == encrypt(*tokens)
200
+ end
201
+ end
202
+ sorcery_model_property_set(:encryption_algorithm, :custom)
203
+ sorcery_model_property_set(:custom_encryption_provider, MyCrypto)
204
+ create_new_user
205
+ User.authenticate(@user.send(User.sorcery_config.username_attribute_name), 'secret').should be_true
206
+ end
207
+
208
+ it "if encryption algo is aes256, it should set key to crypto provider" do
209
+ sorcery_model_property_set(:encryption_algorithm, :aes256)
210
+ sorcery_model_property_set(:encryption_key, nil)
211
+ expect{User.encrypt(@text)}.to raise_error(ArgumentError)
212
+ sorcery_model_property_set(:encryption_key, "asd234dfs423fddsmndsflktsdf32343")
213
+ expect{User.encrypt(@text)}.to_not raise_error(ArgumentError)
214
+ end
215
+
216
+ it "if encryption algo is aes256, it should set key to crypto provider, even if attributes are set in reverse" do
217
+ sorcery_model_property_set(:encryption_key, nil)
218
+ sorcery_model_property_set(:encryption_algorithm, :none)
219
+ sorcery_model_property_set(:encryption_key, "asd234dfs423fddsmndsflktsdf32343")
220
+ sorcery_model_property_set(:encryption_algorithm, :aes256)
221
+ expect{User.encrypt(@text)}.to_not raise_error(ArgumentError)
222
+ end
223
+
224
+ it "if encryption algo is md5 it should work" do
225
+ sorcery_model_property_set(:encryption_algorithm, :md5)
226
+ User.encrypt(@text).should == Sorcery::CryptoProviders::MD5.encrypt(@text)
227
+ end
228
+
229
+ it "if encryption algo is sha1 it should work" do
230
+ sorcery_model_property_set(:encryption_algorithm, :sha1)
231
+ User.encrypt(@text).should == Sorcery::CryptoProviders::SHA1.encrypt(@text)
232
+ end
233
+
234
+ it "if encryption algo is sha256 it should work" do
235
+ sorcery_model_property_set(:encryption_algorithm, :sha256)
236
+ User.encrypt(@text).should == Sorcery::CryptoProviders::SHA256.encrypt(@text)
237
+ end
238
+
239
+ it "if encryption algo is sha512 it should work" do
240
+ sorcery_model_property_set(:encryption_algorithm, :sha512)
241
+ User.encrypt(@text).should == Sorcery::CryptoProviders::SHA512.encrypt(@text)
242
+ end
243
+
244
+ it "salt should be random for each user and saved in db" do
245
+ sorcery_model_property_set(:salt_attribute_name, :salt)
246
+ create_new_user
247
+ @user.salt.should_not be_nil
248
+ end
249
+
250
+ it "if salt is set should use it to encrypt" do
251
+ sorcery_model_property_set(:salt_attribute_name, :salt)
252
+ sorcery_model_property_set(:encryption_algorithm, :sha512)
253
+ create_new_user
254
+ @user.crypted_password.should_not == Sorcery::CryptoProviders::SHA512.encrypt('secret')
255
+ @user.crypted_password.should == Sorcery::CryptoProviders::SHA512.encrypt('secret',@user.salt)
256
+ end
257
+
258
+ it "if salt_join_token is set should use it to encrypt" do
259
+ sorcery_model_property_set(:salt_attribute_name, :salt)
260
+ sorcery_model_property_set(:salt_join_token, "-@=>")
261
+ sorcery_model_property_set(:encryption_algorithm, :sha512)
262
+ create_new_user
263
+ @user.crypted_password.should_not == Sorcery::CryptoProviders::SHA512.encrypt('secret')
264
+ Sorcery::CryptoProviders::SHA512.join_token = ""
265
+ @user.crypted_password.should_not == Sorcery::CryptoProviders::SHA512.encrypt('secret',@user.salt)
266
+ Sorcery::CryptoProviders::SHA512.join_token = User.sorcery_config.salt_join_token
267
+ @user.crypted_password.should == Sorcery::CryptoProviders::SHA512.encrypt('secret',@user.salt)
268
+ end
269
+
270
+ end
271
+ end
272
+
273
+ shared_examples_for "external_user" do
274
+ before(:each) do
275
+ User.delete_all
276
+ end
277
+
278
+ it "should respond to 'external?'" do
279
+ create_new_user
280
+ @user.should respond_to(:external?)
281
+ end
282
+
283
+ it "external? should be false for regular users" do
284
+ create_new_user
285
+ @user.external?.should be_false
286
+ end
287
+
288
+ it "external? should be true for external users" do
289
+ create_new_external_user(:twitter)
290
+ @user.external?.should be_true
291
+ end
292
+ end
data/spec/sinatra/Gemfile CHANGED
@@ -2,9 +2,12 @@ source 'http://rubygems.org'
2
2
 
3
3
  gem 'sinatra', '>= 1.2.0'
4
4
  gem 'sqlite3-ruby', :require => 'sqlite3'
5
+ gem 'activerecord', '>= 3.0.3'
6
+ gem 'actionmailer', '>= 3.0.3'
5
7
  gem "sorcery", '>= 0.1.0', :path => '../../'
6
8
 
7
9
  group :development, :test do
10
+ gem "rake"
8
11
  gem "rspec", "~> 2.5.0"
9
12
  gem 'ruby-debug19'
10
13
  gem 'simplecov', '>= 0.3.8', :require => false # Will install simplecov-html as a dependency
@@ -1,45 +1,40 @@
1
1
  PATH
2
2
  remote: ../../
3
3
  specs:
4
- sorcery (0.5.0)
4
+ sorcery (0.5.30)
5
5
  bcrypt-ruby (~> 2.1.4)
6
- json (>= 1.5.1)
7
6
  oauth (>= 0.4.4)
8
7
  oauth (>= 0.4.4)
9
8
  oauth2 (>= 0.1.1)
10
9
  oauth2 (>= 0.1.1)
11
- rails (>= 3.0.0)
12
10
 
13
11
  GEM
14
12
  remote: http://rubygems.org/
15
13
  specs:
16
14
  abstract (1.0.0)
17
- actionmailer (3.0.3)
18
- actionpack (= 3.0.3)
19
- mail (~> 2.2.9)
20
- actionpack (3.0.3)
21
- activemodel (= 3.0.3)
22
- activesupport (= 3.0.3)
15
+ actionmailer (3.0.5)
16
+ actionpack (= 3.0.5)
17
+ mail (~> 2.2.15)
18
+ actionpack (3.0.5)
19
+ activemodel (= 3.0.5)
20
+ activesupport (= 3.0.5)
23
21
  builder (~> 2.1.2)
24
22
  erubis (~> 2.6.6)
25
23
  i18n (~> 0.4)
26
24
  rack (~> 1.2.1)
27
25
  rack-mount (~> 0.6.13)
28
- rack-test (~> 0.5.6)
26
+ rack-test (~> 0.5.7)
29
27
  tzinfo (~> 0.3.23)
30
- activemodel (3.0.3)
31
- activesupport (= 3.0.3)
28
+ activemodel (3.0.5)
29
+ activesupport (= 3.0.5)
32
30
  builder (~> 2.1.2)
33
31
  i18n (~> 0.4)
34
- activerecord (3.0.3)
35
- activemodel (= 3.0.3)
36
- activesupport (= 3.0.3)
32
+ activerecord (3.0.5)
33
+ activemodel (= 3.0.5)
34
+ activesupport (= 3.0.5)
37
35
  arel (~> 2.0.2)
38
36
  tzinfo (~> 0.3.23)
39
- activeresource (3.0.3)
40
- activemodel (= 3.0.3)
41
- activesupport (= 3.0.3)
42
- activesupport (3.0.3)
37
+ activesupport (3.0.5)
43
38
  addressable (2.2.5)
44
39
  archive-tar-minitar (0.5.2)
45
40
  arel (2.0.7)
@@ -54,7 +49,6 @@ GEM
54
49
  multipart-post (~> 1.1.0)
55
50
  rack (< 2, >= 1.1.0)
56
51
  i18n (0.5.0)
57
- json (1.5.1)
58
52
  linecache19 (0.5.11)
59
53
  ruby_core_source (>= 0.1.4)
60
54
  mail (2.2.15)
@@ -75,19 +69,6 @@ GEM
75
69
  rack (>= 1.0.0)
76
70
  rack-test (0.5.7)
77
71
  rack (>= 1.0)
78
- rails (3.0.3)
79
- actionmailer (= 3.0.3)
80
- actionpack (= 3.0.3)
81
- activerecord (= 3.0.3)
82
- activeresource (= 3.0.3)
83
- activesupport (= 3.0.3)
84
- bundler (~> 1.0)
85
- railties (= 3.0.3)
86
- railties (3.0.3)
87
- actionpack (= 3.0.3)
88
- activesupport (= 3.0.3)
89
- rake (>= 0.8.7)
90
- thor (~> 0.14.4)
91
72
  rake (0.8.7)
92
73
  rspec (2.5.0)
93
74
  rspec-core (~> 2.5.0)
@@ -114,7 +95,6 @@ GEM
114
95
  rack (~> 1.1)
115
96
  tilt (< 2.0, >= 1.2.2)
116
97
  sqlite3-ruby (1.3.2)
117
- thor (0.14.6)
118
98
  tilt (1.2.2)
119
99
  timecop (0.3.5)
120
100
  treetop (1.4.9)
@@ -125,6 +105,9 @@ PLATFORMS
125
105
  ruby
126
106
 
127
107
  DEPENDENCIES
108
+ actionmailer (>= 3.0.3)
109
+ activerecord (>= 3.0.3)
110
+ rake
128
111
  rspec (~> 2.5.0)
129
112
  ruby-debug19
130
113
  simplecov (>= 0.3.8)
@@ -6,6 +6,6 @@ task :default => :spec
6
6
 
7
7
  desc "Run all specs"
8
8
  RSpec::Core::RakeTask.new(:spec) do |t|
9
- t.pattern = '**/*_spec.rb'
9
+ t.pattern = 'spec/*_spec.rb'
10
10
  t.rspec_opts = ["--options #{File.dirname(__FILE__)}/spec/spec.opts"]
11
11
  end
@@ -1,21 +1,27 @@
1
1
  # --- before filters
2
+ module Filters
2
3
 
3
- ['/test_logout','/some_action','/test_should_be_logged_in'].each do |patt|
4
- before patt do
5
- require_login
6
- end
7
- end
4
+ def self.included(klass)
5
+ klass.class_eval do
6
+ ['/test_logout', '/some_action', '/test_should_be_logged_in'].each do |pattern|
7
+ before pattern do
8
+ require_login
9
+ end
10
+ end
8
11
 
9
- before '/test_http_basic_auth' do
10
- require_login_from_http_basic
11
- end
12
+ before '/test_http_basic_auth' do
13
+ require_login_from_http_basic
14
+ end
12
15
 
13
- # ----- test filters
16
+ # ----- test filters
14
17
 
15
- before do
16
- self.class.sorcery_vars = {}
17
- end
18
+ before do
19
+ self.class.sorcery_vars = {}
20
+ end
18
21
 
19
- after do
20
- save_instance_vars
22
+ after do
23
+ save_instance_vars
24
+ end
25
+ end
26
+ end
21
27
  end
@@ -0,0 +1,157 @@
1
+ require 'sinatra/base'
2
+ #enable :sessions
3
+
4
+ require 'sqlite3'
5
+ require 'active_record'
6
+
7
+ # establish connection
8
+ ActiveRecord::Base.establish_connection(
9
+ :adapter => "sqlite3",
10
+ :database => ":memory:",
11
+ :verbosity => "quiet"
12
+ )
13
+
14
+ require 'action_mailer'
15
+ ActionMailer::Base.delivery_method = :test
16
+ require File.join(File.dirname(__FILE__), 'sorcery_mailer')
17
+
18
+ # models
19
+ require File.join(File.dirname(__FILE__), 'user')
20
+ require 'sorcery'
21
+
22
+ APP_ROOT = File.dirname(__FILE__)
23
+
24
+ class Modular < Sinatra::Base
25
+ set :sessions, true
26
+
27
+ # ['/test_logout', '/some_action', '/test_should_be_logged_in'].each do |patt|
28
+ # before patt do
29
+ # require_login
30
+ # end
31
+ # end
32
+ #
33
+ # before '/test_http_basic_auth' do
34
+ # require_login_from_http_basic
35
+ # end
36
+ #
37
+ ## ----- test filters
38
+ #
39
+ # before do
40
+ # self.class.sorcery_vars = {}
41
+ # end
42
+ #
43
+ # after do
44
+ # save_instance_vars
45
+ # end
46
+
47
+ get '/' do
48
+
49
+ end
50
+
51
+ get '/test_login' do
52
+ @user = login(params[:username], params[:password])
53
+ @current_user = current_user
54
+ @logged_in = logged_in?
55
+ erb :test_login
56
+ end
57
+
58
+ get '/test_logout' do
59
+ session[:user_id] = User.first.id
60
+ logout
61
+ @current_user = current_user
62
+ @logged_in = logged_in?
63
+ end
64
+
65
+ get '/test_current_user' do
66
+ session[:user_id] = params[:id]
67
+ current_user
68
+ end
69
+
70
+ get '/some_action' do
71
+ erb ''
72
+ end
73
+
74
+ post '/test_return_to' do
75
+ session[:return_to_url] = params[:return_to_url] if params[:return_to_url]
76
+ @user = login(params[:username], params[:password])
77
+ redirect_back_or_to(:some_action)
78
+ end
79
+
80
+ get '/test_should_be_logged_in' do
81
+ erb ''
82
+ end
83
+
84
+ def test_not_authenticated_action
85
+ halt "test_not_authenticated_action"
86
+ end
87
+
88
+ def not_authenticated2
89
+ @session = session
90
+ save_instance_vars
91
+ redirect '/'
92
+ end
93
+
94
+ # remember me
95
+
96
+ post '/test_login_with_remember' do
97
+ @user = login(params[:username], params[:password])
98
+ remember_me!
99
+ erb ''
100
+ end
101
+
102
+ post '/test_login_with_remember_in_login' do
103
+ @user = login(params[:username], params[:password], params[:remember])
104
+ erb ''
105
+ end
106
+
107
+ get '/test_login_from_cookie' do
108
+ @user = current_user
109
+ erb ''
110
+ end
111
+
112
+ # http_basic
113
+
114
+ get '/test_http_basic_auth' do
115
+ erb "HTTP Basic Auth"
116
+ end
117
+
118
+ # oauth
119
+
120
+ get '/login_at_test' do
121
+ login_at(:twitter)
122
+ end
123
+
124
+ get '/test_login_from' do
125
+ if @user = login_from(:twitter)
126
+ erb "Success!"
127
+ else
128
+ erb "Failed!"
129
+ end
130
+ end
131
+
132
+ # oauth2
133
+
134
+ get '/login_at_test2' do
135
+ login_at(:facebook)
136
+ end
137
+
138
+ get '/test_login_from2' do
139
+ if @user = login_from(:facebook)
140
+ erb "Success!"
141
+ else
142
+ erb "Failed!"
143
+ end
144
+ end
145
+
146
+ get '/test_create_from_provider' do
147
+ provider = params[:provider]
148
+ login_from(provider)
149
+ if @user = create_from(provider)
150
+ erb "Success!"
151
+ else
152
+ erb "Failed!"
153
+ end
154
+ end
155
+
156
+ #run! if app_file == $2
157
+ end
@@ -1,4 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require File.expand_path(File.dirname(__FILE__) + '/../../shared_examples/controller_oauth2_shared_examples')
2
3
 
3
4
  def stub_all_oauth2_requests!
4
5
  @client = OAuth2::Client.new("key","secret", :site => "http://myapi.com")
@@ -58,30 +59,8 @@ describe 'MyApp' do
58
59
  end
59
60
  end
60
61
 
61
- describe Sinatra::Application, "'create_from'" do
62
- before(:each) do
63
- stub_all_oauth2_requests!
64
- User.delete_all
65
- Authentication.delete_all
66
- end
67
-
68
- it "should create a new user" do
69
- sorcery_model_property_set(:authentications_class, Authentication)
70
- sorcery_controller_external_property_set(:facebook, :user_info_mapping, {:username => "name"})
71
- lambda do
72
- get "/test_create_from_provider", :provider => "facebook"
73
- end.should change(User, :count).by(1)
74
- User.first.username.should == "Noam Ben Ari"
75
- end
76
-
77
- it "should support nested attributes" do
78
- sorcery_model_property_set(:authentications_class, Authentication)
79
- sorcery_controller_external_property_set(:facebook, :user_info_mapping, {:username => "hometown/name"})
80
- lambda do
81
- get "/test_create_from_provider", :provider => "facebook"
82
- end.should change(User, :count).by(1)
83
- User.first.username.should == "Haifa, Israel"
84
- end
62
+ describe Sinatra::Application do
63
+ it_behaves_like "oauth2_controller"
85
64
  end
86
65
 
87
66
  describe Sinatra::Application, "OAuth with User Activation features" do
@@ -1,4 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require File.expand_path(File.dirname(__FILE__) + '/../../shared_examples/controller_oauth_shared_examples')
2
3
  require 'ostruct'
3
4
 
4
5
  def stub_all_oauth_requests!
@@ -63,30 +64,8 @@ describe Sinatra::Application do
63
64
  end
64
65
  end
65
66
 
66
- describe Sinatra::Application, "'create_from'" do
67
- before(:each) do
68
- stub_all_oauth_requests!
69
- User.delete_all
70
- Authentication.delete_all
71
- end
72
-
73
- it "should create a new user" do
74
- sorcery_model_property_set(:authentications_class, Authentication)
75
- sorcery_controller_external_property_set(:twitter, :user_info_mapping, {:username => "screen_name"})
76
- lambda do
77
- get :test_create_from_provider, :provider => "twitter"
78
- end.should change(User, :count).by(1)
79
- User.first.username.should == "nbenari"
80
- end
81
-
82
- it "should support nested attributes" do
83
- sorcery_model_property_set(:authentications_class, Authentication)
84
- sorcery_controller_external_property_set(:twitter, :user_info_mapping, {:username => "status/text"})
85
- lambda do
86
- get :test_create_from_provider, :provider => "twitter"
87
- end.should change(User, :count).by(1)
88
- User.first.username.should == "coming soon to sorcery gem: twitter and facebook authentication support."
89
- end
67
+ describe Sinatra::Application do
68
+ it_behaves_like "oauth_controller"
90
69
  end
91
70
 
92
71
  describe Sinatra::Application, "OAuth with User Activation features" do