sparkly-auth 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (243) hide show
  1. data/.document +5 -0
  2. data/.gitignore +25 -0
  3. data/HISTORY.txt +7 -0
  4. data/README.rdoc +127 -1
  5. data/Rakefile +45 -13
  6. data/TODO +4 -0
  7. data/VERSION +1 -1
  8. data/app/controllers/sparkly_accounts_controller.rb +20 -13
  9. data/app/controllers/sparkly_controller.rb +5 -4
  10. data/app/controllers/sparkly_sessions_controller.rb +16 -9
  11. data/app/helpers/sparkly_accounts_helper.rb +2 -0
  12. data/app/helpers/sparkly_helper.rb +2 -0
  13. data/app/helpers/sparkly_sessions_helper.rb +2 -0
  14. data/app/models/remembrance_token.rb +3 -1
  15. data/app/views/sparkly_accounts/_rails2_form.html.erb +24 -0
  16. data/app/views/sparkly_accounts/_rails3_form.html.erb +26 -0
  17. data/app/views/sparkly_accounts/edit.html.erb +5 -23
  18. data/app/views/sparkly_accounts/new.html.erb +4 -23
  19. data/app/views/sparkly_sessions/_rails2_form.html.erb +22 -0
  20. data/app/views/sparkly_sessions/_rails3_form.html.erb +22 -0
  21. data/app/views/sparkly_sessions/new.html.erb +4 -21
  22. data/features/create_sparkly_account.feature +12 -0
  23. data/features/delete_sparkly_account.feature +12 -0
  24. data/features/edit_sparkly_account.feature +54 -0
  25. data/features/lock_abused_sparkly_account.feature +7 -0
  26. data/features/login_sparkly_session.feature +43 -0
  27. data/features/logout_sparkly_session.feature +16 -0
  28. data/features/setup/sparkly.rb +4 -0
  29. data/features/show_sparkly_account.feature +13 -0
  30. data/features/sparkly_session_timeout.feature +6 -0
  31. data/features/step_definitions/account/account_steps.rb +16 -0
  32. data/features/step_definitions/account/brief_steps.rb +9 -0
  33. data/features/step_definitions/debug_steps.rb +3 -0
  34. data/features/step_definitions/email_steps.rb +182 -0
  35. data/features/step_definitions/session/brief_steps.rb +0 -0
  36. data/features/step_definitions/session/logged_in_steps.rb +42 -0
  37. data/features/step_definitions/session/login_steps.rb +37 -0
  38. data/features/step_definitions/session/logout_steps.rb +5 -0
  39. data/features/step_definitions/session_steps.rb +16 -0
  40. data/features/step_definitions/sparkly_auth_steps.rb +0 -0
  41. data/features/step_definitions/web_steps.rb +287 -0
  42. data/features/support/env.rb +10 -0
  43. data/features/support/paths.rb +35 -0
  44. data/features/support/sparkly_helpers.rb +42 -0
  45. data/generators/sparkly/rails2.rb +79 -0
  46. data/generators/sparkly/rails3.rb +89 -0
  47. data/generators/sparkly/sparkly_generator.rb +4 -75
  48. data/generators/sparkly/templates/accounts_controller.rb +14 -13
  49. data/generators/sparkly/templates/accounts_helper.rb +1 -1
  50. data/generators/sparkly/templates/sessions_controller.rb +26 -12
  51. data/generators/sparkly/templates/sessions_helper.rb +1 -1
  52. data/generators/sparkly/templates/views/sparkly_accounts/_rails2_form.html.erb +24 -0
  53. data/generators/sparkly/templates/views/sparkly_accounts/_rails3_form.html.erb +26 -0
  54. data/generators/sparkly/templates/views/sparkly_accounts/edit.html.erb +5 -23
  55. data/generators/sparkly/templates/views/sparkly_accounts/new.html.erb +4 -23
  56. data/generators/sparkly/templates/views/sparkly_sessions/_rails2_form.html.erb +22 -0
  57. data/generators/sparkly/templates/views/sparkly_sessions/_rails3_form.html.erb +22 -0
  58. data/generators/sparkly/templates/views/sparkly_sessions/new.html.erb +4 -21
  59. data/lib/auth.rb +22 -1
  60. data/lib/auth/behavior/base.rb +25 -18
  61. data/lib/auth/behavior/base/configuration.rb +37 -0
  62. data/lib/auth/behavior/core.rb +22 -14
  63. data/lib/auth/behavior/core/authenticated_model_methods.rb +9 -1
  64. data/lib/auth/behavior/core/controller_extensions.rb +2 -2
  65. data/lib/auth/behavior/core/controller_extensions/class_methods.rb +1 -1
  66. data/lib/auth/behavior/core/password_methods.rb +5 -5
  67. data/lib/auth/behavior/remember_me.rb +7 -3
  68. data/lib/auth/behavior/remember_me/configuration.rb +8 -11
  69. data/lib/auth/behavior/remember_me/controller_extensions.rb +1 -1
  70. data/lib/auth/behavior_lookup.rb +4 -4
  71. data/lib/auth/builtin_behaviors.rb +3 -0
  72. data/lib/auth/configuration.rb +71 -25
  73. data/lib/auth/configuration/keys.rb +50 -0
  74. data/lib/auth/engine.rb +33 -0
  75. data/lib/auth/generators/views_generator.rb +13 -4
  76. data/lib/auth/model.rb +52 -22
  77. data/{init.rb → rails/hacks/rails2.rb} +1 -14
  78. data/rails/hacks/rails3.rb +22 -0
  79. data/rails/init.rb +7 -17
  80. data/rails/init_rails2.rb +39 -0
  81. data/rails/init_rails3.rb +2 -0
  82. data/rails/routes_rails3.rb +37 -0
  83. data/rake3 +1 -0
  84. data/rerun.txt +1 -0
  85. data/run_all_tests +1 -0
  86. data/sparkly-auth.gemspec +481 -23
  87. data/spec/behaviors/core/controller_extensions_spec.rb +49 -0
  88. data/spec/{lib/auth/behavior → behaviors}/core_spec.rb +29 -12
  89. data/spec/behaviors/remember_me/configuration_spec.rb +16 -0
  90. data/spec/behaviors/remember_me_spec.rb +167 -0
  91. data/spec/generators/sanity_checks_spec.rb +58 -0
  92. data/spec/lib/auth/configuration_spec.rb +61 -0
  93. data/spec/lib/auth/model_spec.rb +6 -9
  94. data/spec/lib/auth_spec.rb +2 -2
  95. data/spec/lib/hacks/rename_attributes_spec.rb +49 -0
  96. data/spec/routes_spec.rb +8 -2
  97. data/spec/spec2_helper.rb +52 -0
  98. data/spec/spec3_helper.rb +72 -0
  99. data/spec/spec_helper.rb +109 -49
  100. data/spec_env/rails2/README.1ST +23 -0
  101. data/spec_env/rails2/Rakefile +10 -0
  102. data/spec_env/rails2/app/controllers/application_controller.rb +13 -0
  103. data/spec_env/rails2/app/helpers/application_helper.rb +3 -0
  104. data/spec_env/rails2/app/models/user.rb +2 -0
  105. data/spec_env/rails2/app/views/application/not_found.html.erb +9 -0
  106. data/spec_env/rails2/app/views/layouts/application.html.erb +9 -0
  107. data/spec_env/rails2/config/boot.rb +110 -0
  108. data/spec_env/rails2/config/cucumber.yml +7 -0
  109. data/spec_env/rails2/config/database.yml +25 -0
  110. data/spec_env/rails2/config/environment.rb +46 -0
  111. data/spec_env/rails2/config/environments/cucumber.rb +35 -0
  112. data/spec_env/rails2/config/environments/development.rb +17 -0
  113. data/spec_env/rails2/config/environments/production.rb +28 -0
  114. data/spec_env/rails2/config/environments/test.rb +31 -0
  115. data/spec_env/rails2/config/initializers/backtrace_silencers.rb +7 -0
  116. data/spec_env/rails2/config/initializers/inflections.rb +10 -0
  117. data/spec_env/rails2/config/initializers/mime_types.rb +5 -0
  118. data/spec_env/rails2/config/initializers/new_rails_defaults.rb +21 -0
  119. data/spec_env/rails2/config/initializers/session_store.rb +15 -0
  120. data/spec_env/rails2/config/initializers/sparkly_authentication.rb +30 -0
  121. data/spec_env/rails2/config/locales/en.yml +5 -0
  122. data/spec_env/rails2/config/routes.rb +46 -0
  123. data/spec_env/rails2/db/development.sqlite3 +0 -0
  124. data/spec_env/rails2/db/migrate/001_create_sparkly_passwords.rb +19 -0
  125. data/spec_env/rails2/db/migrate/002_create_sparkly_remembered_tokens.rb +15 -0
  126. data/spec_env/rails2/db/migrate/003_add_confirmed_to_sparkly_passwords.rb +9 -0
  127. data/spec_env/rails2/db/migrate/20100607103543_create_users.rb +12 -0
  128. data/spec_env/rails2/db/migrate/20100609152058_add_email_to_users.rb +9 -0
  129. data/spec_env/rails2/db/schema.rb +42 -0
  130. data/spec_env/rails2/db/seeds.rb +7 -0
  131. data/spec_env/rails2/db/test.sqlite3 +0 -0
  132. data/spec_env/rails2/doc/README_FOR_APP +2 -0
  133. data/spec_env/rails2/doc/sparkly_authentication.txt +56 -0
  134. data/spec_env/rails2/features/support/env.rb +58 -0
  135. data/spec_env/rails2/lib/tasks/cucumber.rake +47 -0
  136. data/spec_env/rails2/lib/tasks/rspec.rake +144 -0
  137. data/spec_env/rails2/lib/tasks/sparkly_migration.rb +1 -0
  138. data/spec_env/rails2/log/cucumber.log +8412 -0
  139. data/spec_env/rails2/log/development.log +317 -0
  140. data/spec_env/rails2/log/test.log +32053 -0
  141. data/spec_env/rails2/public/404.html +30 -0
  142. data/spec_env/rails2/public/422.html +30 -0
  143. data/spec_env/rails2/public/500.html +30 -0
  144. data/spec_env/rails2/public/favicon.ico +0 -0
  145. data/spec_env/rails2/public/images/rails.png +0 -0
  146. data/spec_env/rails2/public/javascripts/application.js +2 -0
  147. data/spec_env/rails2/public/javascripts/controls.js +963 -0
  148. data/spec_env/rails2/public/javascripts/dragdrop.js +973 -0
  149. data/spec_env/rails2/public/javascripts/effects.js +1128 -0
  150. data/spec_env/rails2/public/javascripts/prototype.js +4320 -0
  151. data/spec_env/rails2/public/robots.txt +5 -0
  152. data/spec_env/rails2/rerun.txt +1 -0
  153. data/spec_env/rails2/script/about +4 -0
  154. data/spec_env/rails2/script/autospec +6 -0
  155. data/spec_env/rails2/script/console +3 -0
  156. data/spec_env/rails2/script/cucumber +10 -0
  157. data/spec_env/rails2/script/dbconsole +3 -0
  158. data/spec_env/rails2/script/destroy +3 -0
  159. data/spec_env/rails2/script/generate +3 -0
  160. data/spec_env/rails2/script/performance/benchmarker +3 -0
  161. data/spec_env/rails2/script/performance/profiler +3 -0
  162. data/spec_env/rails2/script/plugin +3 -0
  163. data/spec_env/rails2/script/runner +3 -0
  164. data/spec_env/rails2/script/server +3 -0
  165. data/spec_env/rails2/script/spec +10 -0
  166. data/spec_env/rails2/spec/controllers/sparkly_user_sessions_controller_spec.rb +10 -0
  167. data/spec_env/rails2/spec/rcov.opts +2 -0
  168. data/spec_env/rails2/spec/spec.opts +4 -0
  169. data/spec_env/rails2/spec/spec_helper.rb +54 -0
  170. data/spec_env/rails2/test/fixtures/users.yml +7 -0
  171. data/spec_env/rails2/test/performance/browsing_test.rb +9 -0
  172. data/spec_env/rails2/test/test_helper.rb +38 -0
  173. data/spec_env/rails2/test/unit/user_test.rb +8 -0
  174. data/spec_env/rails2/vendor/gems/sparkly-auth-bootstrap-1.0.0/.specification +63 -0
  175. data/spec_env/rails2/vendor/gems/sparkly-auth-bootstrap-1.0.0/generators/sparkly/sparkly_generator.rb +1 -0
  176. data/spec_env/rails2/vendor/gems/sparkly-auth-bootstrap-1.0.0/lib/sparkly-auth-bootstrap.rb +6 -0
  177. data/spec_env/rails2/vendor/gems/sparkly-auth-bootstrap-1.0.0/rails/init.rb +1 -0
  178. data/spec_env/rails2/vendor/gems/sparkly-auth-bootstrap-1.0.0/sparkly-auth-bootstrap.gemspec +55 -0
  179. data/spec_env/rails3/.gitignore +4 -0
  180. data/spec_env/rails3/Gemfile +43 -0
  181. data/spec_env/rails3/Gemfile.lock +124 -0
  182. data/spec_env/rails3/README +256 -0
  183. data/spec_env/rails3/Rakefile +7 -0
  184. data/spec_env/rails3/app/controllers/application_controller.rb +6 -0
  185. data/spec_env/rails3/app/helpers/application_helper.rb +2 -0
  186. data/spec_env/rails3/app/models/user.rb +3 -0
  187. data/spec_env/rails3/app/views/application/not_found.html.erb +9 -0
  188. data/spec_env/rails3/app/views/layouts/application.html.erb +22 -0
  189. data/spec_env/rails3/config.ru +4 -0
  190. data/spec_env/rails3/config/application.rb +47 -0
  191. data/spec_env/rails3/config/boot.rb +13 -0
  192. data/spec_env/rails3/config/cucumber.yml +8 -0
  193. data/spec_env/rails3/config/database.yml +28 -0
  194. data/spec_env/rails3/config/environment.rb +5 -0
  195. data/spec_env/rails3/config/environments/development.rb +22 -0
  196. data/spec_env/rails3/config/environments/production.rb +49 -0
  197. data/spec_env/rails3/config/environments/spec.rb +35 -0
  198. data/spec_env/rails3/config/environments/test.rb +43 -0
  199. data/spec_env/rails3/config/initializers/backtrace_silencers.rb +7 -0
  200. data/spec_env/rails3/config/initializers/inflections.rb +10 -0
  201. data/spec_env/rails3/config/initializers/mime_types.rb +5 -0
  202. data/spec_env/rails3/config/initializers/secret_token.rb +7 -0
  203. data/spec_env/rails3/config/initializers/session_store.rb +8 -0
  204. data/spec_env/rails3/config/initializers/sparkly_authentication.rb +30 -0
  205. data/spec_env/rails3/config/locales/en.yml +5 -0
  206. data/spec_env/rails3/config/routes.rb +61 -0
  207. data/spec_env/rails3/db/migrate/001_create_sparkly_passwords.rb +19 -0
  208. data/spec_env/rails3/db/migrate/002_create_sparkly_remembered_tokens.rb +15 -0
  209. data/spec_env/rails3/db/migrate/20100810132843_create_users.rb +13 -0
  210. data/spec_env/rails3/db/schema.rb +42 -0
  211. data/spec_env/rails3/db/seeds.rb +7 -0
  212. data/spec_env/rails3/doc/README_FOR_APP +2 -0
  213. data/spec_env/rails3/doc/sparkly_authentication.txt +56 -0
  214. data/spec_env/rails3/features/support/env.rb +62 -0
  215. data/spec_env/rails3/lib/sparkly/bootstrap.rb +1 -0
  216. data/spec_env/rails3/lib/tasks/.gitkeep +0 -0
  217. data/spec_env/rails3/lib/tasks/cucumber.rake +53 -0
  218. data/spec_env/rails3/lib/tasks/sparkly_migration.rb +1 -0
  219. data/spec_env/rails3/public/404.html +26 -0
  220. data/spec_env/rails3/public/422.html +26 -0
  221. data/spec_env/rails3/public/500.html +26 -0
  222. data/spec_env/rails3/public/favicon.ico +0 -0
  223. data/spec_env/rails3/public/images/rails.png +0 -0
  224. data/spec_env/rails3/public/javascripts/application.js +2 -0
  225. data/spec_env/rails3/public/javascripts/controls.js +965 -0
  226. data/spec_env/rails3/public/javascripts/dragdrop.js +974 -0
  227. data/spec_env/rails3/public/javascripts/effects.js +1123 -0
  228. data/spec_env/rails3/public/javascripts/prototype.js +6001 -0
  229. data/spec_env/rails3/public/javascripts/rails.js +175 -0
  230. data/spec_env/rails3/public/robots.txt +5 -0
  231. data/spec_env/rails3/public/stylesheets/.gitkeep +0 -0
  232. data/spec_env/rails3/script/cucumber +10 -0
  233. data/spec_env/rails3/script/rails +6 -0
  234. data/spec_env/rails3/test/fixtures/users.yml +7 -0
  235. data/spec_env/rails3/test/performance/browsing_test.rb +9 -0
  236. data/spec_env/rails3/test/test_helper.rb +13 -0
  237. data/spec_env/rails3/test/unit/user_test.rb +8 -0
  238. data/spec_env/rails3/vendor/plugins/.gitkeep +0 -0
  239. data/spec_env/rails3/webrat.log +5 -0
  240. metadata +393 -15
  241. data/dependencies.rb +0 -1
  242. data/spec/lib/auth/behavior/remember_me_spec.rb +0 -127
  243. data/spec/lib/auth/extensions/controller_spec.rb +0 -32
@@ -24,9 +24,9 @@ describe Auth do
24
24
  config.authenticate :user, :with => :password
25
25
  end
26
26
 
27
- Auth.configuration.authenticated_models.find(:user).options.without(:behaviors).should == {
27
+ Auth.configuration.authenticated_models.find(:user).options.without(:behaviors).should include({
28
28
  :key => :login,
29
29
  :with => :password
30
- }
30
+ })
31
31
  end
32
32
  end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ # Extension to ActiveRecord::Errors to allow error attributes to be renamed. This is
4
+ # because otherwise we'll end up producing very confusing error messages complaining about :secret,
5
+ # :encrypted_secret, and so forth when all the user really cares about is :password.
6
+ #
7
+ # This is mainly for backward compatibility to Rails 2.3, where we had to hack such things into AR.
8
+ describe "Hacks: AR::Errors#rename_attributes" do
9
+ def errors_on(key)
10
+ if Rails::VERSION::MAJOR == 3
11
+ errors[key]
12
+ else
13
+ case e = errors.on(key)
14
+ when nil then []
15
+ when Array then e
16
+ else [e]
17
+ end
18
+ end
19
+ end
20
+
21
+ def errors; subject; end
22
+
23
+ subject do
24
+ errors =
25
+ if Rails::VERSION::MAJOR == 3
26
+ ActiveModel::Errors.new(Password.new)
27
+ else
28
+ ActiveRecord::Errors.new(Password.new)
29
+ end
30
+
31
+ errors.add(:unencrypted_secret, "must be unique")
32
+ errors.add(:password, "must be longer")
33
+ errors
34
+ end
35
+
36
+ it "should rename once" do
37
+ errors.rename_attribute(:unencrypted_secret, :secret)
38
+ errors_on(:secret).should == ["must be unique"]
39
+ end
40
+
41
+ it "should rename twice" do
42
+ errors.rename_attribute(:unencrypted_secret, :secret)
43
+ errors.rename_attribute(:secret, :password)
44
+
45
+ # order agnostic, else we'd use x.should == ["...","..."]
46
+ errors_on(:password).should include("must be unique")
47
+ errors_on(:password).should include("must be longer")
48
+ end
49
+ end
data/spec/routes_spec.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  describe 'routes' do
2
- include ActionController::UrlWriter
2
+ if Rails::VERSION::MAJOR == 2
3
+ include ActionController::UrlWriter
4
+ end
3
5
 
4
6
  before(:each) do
5
7
  Auth.configure do |config|
@@ -7,7 +9,11 @@ describe 'routes' do
7
9
  end
8
10
 
9
11
  Auth.kick!
10
- ActionController::Routing::Routes.install_helpers([self.class])
12
+ if Rails::VERSION::MAJOR == 2
13
+ ActionController::Routing::Routes.install_helpers([self.class])
14
+ elsif Rails::VERSION::MAJOR == 3
15
+ Rails.application.routes.install_helpers([self.class])
16
+ end
11
17
  end
12
18
 
13
19
  it "should map new_user_path to /user/new" do
@@ -0,0 +1,52 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+
3
+ def add_load_path(path)
4
+ path = File.expand_path(File.join("..", path), __FILE__)
5
+ $LOAD_PATH.unshift path
6
+ ActiveSupport::Dependencies.load_paths.unshift path
7
+ ActiveSupport::Dependencies.load_once_paths.delete path
8
+ end
9
+
10
+ # Add mock paths to load paths
11
+ add_load_path "mocks/models"
12
+ add_load_path "../app/models"
13
+ #add_load_path "../app/lib"
14
+ add_load_path "../app/controllers"
15
+
16
+ $LOAD_PATH.uniq!
17
+ ActiveSupport::Dependencies.load_paths.uniq!
18
+ ActiveSupport::Dependencies.load_once_paths.uniq!
19
+
20
+ undef add_load_path
21
+
22
+ def column(name)
23
+ ActiveRecord::ConnectionAdapters::Column.new(name, nil)
24
+ end
25
+
26
+ Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |fi| require fi }
27
+
28
+ Spec::Runner.configure do |config|
29
+ # Needed in order to reset configuration for each test. This should not happen in a real environment.
30
+ config.before(:each) do
31
+ Auth.reset_double_prevention!
32
+ apply_sparkly_configuration!
33
+
34
+ User.destroy_all
35
+ Password.destroy_all
36
+ RemembranceToken.destroy_all
37
+ end
38
+
39
+ config.extend(ClassHelpers)
40
+ config.include(InstanceHelpers)
41
+ config.include(EmailSpec::Helpers)
42
+ config.include(EmailSpec::Matchers)
43
+ end
44
+
45
+ def error_on(model, key, value = nil, options = {})
46
+ instance = model.new()
47
+ instance.send("#{key}=", value)
48
+ options.each { |k,v| instance.send("#{k}=", v) }
49
+
50
+ instance.valid?
51
+ instance.errors.on(key.to_s)
52
+ end
@@ -0,0 +1,72 @@
1
+ def add_load_path(path)
2
+ path = File.expand_path(File.join("..", path), __FILE__)
3
+ $LOAD_PATH.unshift path
4
+ ActiveSupport::Dependencies.autoload_paths.unshift path
5
+ ActiveSupport::Dependencies.autoload_once_paths.unshift path
6
+ end
7
+
8
+ # Add mock paths to load paths
9
+ add_load_path "mocks/models"
10
+ add_load_path "../app/models"
11
+ #add_load_path "../app/lib"
12
+ add_load_path "../app/controllers"
13
+
14
+ $LOAD_PATH.uniq!
15
+ ActiveSupport::Dependencies.autoload_paths.uniq!
16
+ ActiveSupport::Dependencies.autoload_once_paths.uniq!
17
+
18
+ undef add_load_path
19
+
20
+ def column(name)
21
+ ActiveRecord::ConnectionAdapters::Column.new(name, nil)
22
+ end
23
+
24
+ begin
25
+ require 'genspec'
26
+ rescue LoadError
27
+ puts " >> Missing gem: 'genspec' <<"
28
+ puts
29
+ puts "These specs rely on genspec, which tests the project's generators."
30
+ puts
31
+ raise
32
+ end
33
+
34
+ Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |fi| require fi }
35
+ #gem 'genspec', '0.2.0.prerails3.2'
36
+
37
+ RSpec.configure do |config|
38
+ # Needed in order to reset configuration for each test. This should not happen in a real environment.
39
+ config.before(:each) do
40
+ Auth.reset_double_prevention!
41
+ apply_sparkly_configuration!
42
+
43
+ User.destroy_all
44
+ Password.destroy_all
45
+ RemembranceToken.destroy_all
46
+ end
47
+
48
+ config.extend(ClassHelpers)
49
+ config.include(InstanceHelpers)
50
+ config.include(Rails.application.routes.url_helpers)
51
+ end
52
+
53
+ def error_on(model, key, value = nil, options = {})
54
+ instance = model.new()
55
+ instance.send("#{key}=", value)
56
+ options.each { |k,v| instance.send("#{k}=", v) }
57
+
58
+ instance.valid?
59
+ e = instance.errors[key.to_s]
60
+
61
+ # The specs were written to test for Rails 2 errors, and this more or less converts the Rails 3 ones for Rails 2
62
+ # compatibility.
63
+ #
64
+ # TODO: Update the specs for Rails 3, and let the R2 spec helper deal with compatibility.
65
+ if e.size < 2
66
+ return e.shift
67
+ else
68
+ e
69
+ end
70
+ end
71
+
72
+ require 'rspec/rails'
data/spec/spec_helper.rb CHANGED
@@ -1,61 +1,121 @@
1
- ENV['RAILS_ENV'] = 'test'
2
-
3
- require File.expand_path("../../../../../config/boot", __FILE__)
4
- require File.expand_path("../../../../../config/environment", __FILE__)
5
- require "email_spec"
6
-
7
- begin
8
- require 'genspec'
9
- rescue LoadError
10
- puts " >> Missing gem: 'genspec' <<"
11
- puts
12
- puts "These specs rely on gemspec, which tests the project's generators."
13
- puts
14
- raise
15
- end
1
+ ENV['DEFER_SPARKLY'] = 'true' # Sparkly will infer Auth.defer_kickstart from env
2
+ ENV['AUTH_BACKTRACE'] = 'true' # Sparkly will dump backtrace if NameError encountered during init
16
3
 
17
- def add_load_path(path)
18
- path = File.expand_path(File.join("..", path), __FILE__)
19
- $LOAD_PATH.unshift path
20
- ActiveSupport::Dependencies.load_paths.unshift path
21
- ActiveSupport::Dependencies.load_once_paths.delete path
4
+ if defined?(RSpec)
5
+ ENV['RAILS_ENV'] = 'spec'
6
+ RSPEC_VERSION = 2
7
+ require File.expand_path(File.join(File.dirname(__FILE__), "../spec_env/rails3/config/boot"))
8
+ require File.expand_path(File.join(File.dirname(__FILE__), "../spec_env/rails3/config/environment"))
9
+ else
10
+ ENV['RAILS_ENV'] = 'test'
11
+ RSPEC_VERSION = 1
12
+ #require File.expand_path(File.join(File.dirname(__FILE__), "../spec_env/rails2/config/boot"))
13
+ require File.expand_path(File.join(File.dirname(__FILE__), "../spec_env/rails2/config/environment"))
22
14
  end
23
15
 
24
- # Add mock paths to load paths
25
- add_load_path "mocks/models"
26
- add_load_path "../app/models"
27
- #add_load_path "../app/lib"
28
- add_load_path "../app/controllers"
29
-
30
- $LOAD_PATH.uniq!
31
- ActiveSupport::Dependencies.load_paths.uniq!
32
- ActiveSupport::Dependencies.load_once_paths.uniq!
33
-
34
- undef add_load_path
35
-
36
- def column(name)
37
- ActiveRecord::ConnectionAdapters::Column.new(name, nil)
16
+ ['email_spec', 'genspec'].each do |gem_name|
17
+ begin
18
+ require gem_name
19
+ rescue LoadError
20
+ puts " >> Missing gem: 'genspec' <<"
21
+ puts
22
+ puts "These specs rely on genspec, which tests the project's generators."
23
+ puts
24
+ raise
25
+ end
38
26
  end
39
27
 
40
- Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |fi| require fi }
41
-
42
- Spec::Runner.configure do |config|
43
- # Needed in order to reset configuration for each test. This should not happen in a real environment.
44
- config.before(:each) do
28
+ module InstanceHelpers
29
+ def reload!
30
+ if Rails.configuration.cache_classes
31
+ raise "Cannot reload: set Rails.configuration.cache_classes to false first"
32
+ end
33
+ if Rails::VERSION::MAJOR == 2
34
+ Dispatcher.cleanup_application
35
+ #Dispatcher.reload_application
36
+ else
37
+ ActiveSupport::Dependencies.clear
38
+ #ActionDispatch::Callbacks.new(Proc.new {}, false).call({})
39
+ end
40
+ end
41
+
42
+ def _sparkly_configurations
43
+ self.class.all_sparkly_configurations
44
+ end
45
+
46
+ def apply_sparkly_configuration!
47
+ reload!
45
48
  Auth.reset_configuration!
46
- Dispatcher.cleanup_application
47
- Dispatcher.reload_application
49
+ if !_sparkly_configurations.empty?
50
+ _sparkly_configurations.each do |config|
51
+ Auth.configure &config
52
+ end
53
+ Auth.kick! # because we deferred kicking during preparation
54
+ end
55
+ end
56
+ end
57
+
58
+ module ClassHelpers
59
+ def configure_auth(&block)
60
+ if block_given?
61
+ # we have to defer configuration until runtime otherwise all specs will end up
62
+ # using the last configuration defined, which is an error.
63
+ self.sparkly_configurations ||= [] # why ?
64
+ sparkly_configurations << block
65
+ end
66
+ sparkly_configurations
67
+ end
68
+
69
+ def all_sparkly_configurations
70
+ ret = []
71
+ ret.concat sparkly_configurations if sparkly_configurations
72
+ if RSPEC_VERSION == 2 && superclass.respond_to?(:sparkly_configurations)
73
+ ret = superclass.all_sparkly_configurations + ret # parents should run first.
74
+ end
75
+ ret
48
76
  end
49
77
 
50
- config.include(EmailSpec::Helpers)
51
- config.include(EmailSpec::Matchers)
78
+ def self.extended(base)
79
+ base.instance_eval do
80
+ class_inheritable_array :sparkly_configurations
81
+ read_inheritable_attribute(:sparkly_configurations) || write_inheritable_attribute(:sparkly_configurations, [])
82
+ end
83
+ end
52
84
  end
53
85
 
54
- def error_on(model, key, value = nil, options = {})
55
- instance = model.new()
56
- instance.send("#{key}=", value)
57
- options.each { |k,v| instance.send("#{k}=", v) }
86
+ # a safeguard against the 'old way'
87
+ Auth.class_eval do
88
+ class << self
89
+ def reset_double_prevention!
90
+ @reset_configuration_with_double_prevention = nil
91
+ @kick_with_double_prevention = nil
92
+ end
93
+
94
+ def reset_configuration_with_double_prevention!
95
+ if @reset_configuration_with_double_prevention
96
+ raise "Auth#reset_configuration! was already called"
97
+ end
98
+ @reset_configuration_with_double_prevention = 1
99
+ reset_configuration_without_double_prevention!
100
+ end
101
+
102
+ def kick_with_double_prevention!
103
+ if @kick_with_double_prevention
104
+ raise "Auth#kick! was already called"
105
+ end
106
+ @kicm_with_double_prevention = 1
107
+ kick_without_double_prevention!
108
+ end
109
+
110
+ alias_method_chain :reset_configuration!, :double_prevention
111
+ alias_method_chain :kick!, :double_prevention
112
+ end
113
+ end
58
114
 
59
- instance.valid?
60
- instance.errors.on(key.to_s)
115
+ if RSPEC_VERSION == 1
116
+ require 'spec/rails'
117
+ require File.join(File.dirname(__FILE__), 'spec2_helper')
118
+ else
119
+ require 'rspec/rails'
120
+ require File.join(File.dirname(__FILE__), 'spec3_helper')
61
121
  end
@@ -0,0 +1,23 @@
1
+ For a gem, this project has a strange file structure. It'll all make sense in about 30 seconds.
2
+
3
+ The gem is designed to work within a Rails project. Therefore, the best way to test it is within
4
+ a Rails project. I've been trying a lot of different approaches to this, and here's the best
5
+ I've found so far: build the gem within a TEST Rails project's vendor/gems directory, and then
6
+ use config.gem to activate it. This way, it's essentially treated by the Rails project as a
7
+ frozen gem (that is, unpacked into the Rails project tree) -- which, actually, it is.
8
+
9
+ This approach also circumvents the need to install the gem locally for integration testing,
10
+ but is also not so brittle as my RTML approach (create a 'bootstrap' gem that required the
11
+ files directly). In all, the result is a cleaner, faster test suite and, therefore, dev process.
12
+
13
+ I've added a nifty ./run_all_tests script to run both the Auth gem's RSpec specs and, assuming
14
+ they pass, it will then run the test project's Features (which comprise the integration portion
15
+ of the test suite).
16
+
17
+ Obviously, to do any real work on the gem you'll have to chdir into vendor/gems/sparkly-auth-0.
18
+ There's nothing special about the Rails test app, so it should be the same as running the gem
19
+ in any other default Rails app. It's safe to run i.e. Sparkly generators and such on it because
20
+ Sparkly's RSpec suite will override anything those generators produce (or at least, it SHOULD).
21
+
22
+ The Rakefile in the app's root directory is the same as any Rails rake file; to get to the
23
+ gem-specific Rake tasks, chdir to the frozen gem directory first.
@@ -0,0 +1,10 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require(File.join(File.dirname(__FILE__), 'config', 'boot'))
5
+
6
+ require 'rake'
7
+ require 'rake/testtask'
8
+ require 'rake/rdoctask'
9
+
10
+ require 'tasks/rails'
@@ -0,0 +1,13 @@
1
+ # Filters added to this controller apply to all controllers in the application.
2
+ # Likewise, all the methods added will be available for all controllers.
3
+
4
+ class ApplicationController < ActionController::Base
5
+ helper :all # include all helpers, all the time
6
+ protect_from_forgery # See ActionController::RequestForgeryProtection for details
7
+
8
+ # Scrub sensitive parameters from your log
9
+ # filter_parameter_logging :password
10
+
11
+ def not_found
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ # Methods added to this helper will be available to all templates in the application.
2
+ module ApplicationHelper
3
+ end
@@ -0,0 +1,2 @@
1
+ class User < ActiveRecord::Base
2
+ end
@@ -0,0 +1,9 @@
1
+ <h1>404'd!</h1>
2
+
3
+ <%if current_user%>
4
+ <%=link_to "Sign out", user_session_path, :method => :delete%>
5
+ <%=link_to "Edit Profile", edit_user_path%>
6
+ <%else%>
7
+ <%=link_to "Sign in", new_user_session_path%>
8
+ <%=link_to "Sign up", new_user_path%>
9
+ <%end%>