sorcery 0.7.0 → 0.8.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 (204) hide show
  1. data/.travis.yml +3 -0
  2. data/Gemfile +9 -6
  3. data/Gemfile.lock +106 -75
  4. data/README.rdoc +42 -55
  5. data/Rakefile +32 -7
  6. data/VERSION +1 -1
  7. data/lib/generators/sorcery/USAGE +22 -0
  8. data/lib/generators/sorcery/install_generator.rb +77 -0
  9. data/lib/generators/sorcery/templates/initializer.rb +417 -0
  10. data/lib/generators/sorcery/templates/migration/activity_logging.rb +17 -0
  11. data/lib/generators/sorcery/templates/migration/brute_force_protection.rb +13 -0
  12. data/lib/generators/{sorcery_migration/templates → sorcery/templates/migration}/core.rb +2 -2
  13. data/lib/generators/{sorcery_migration/templates → sorcery/templates/migration}/external.rb +1 -1
  14. data/lib/generators/sorcery/templates/migration/remember_me.rb +15 -0
  15. data/lib/generators/sorcery/templates/migration/reset_password.rb +17 -0
  16. data/lib/generators/sorcery/templates/migration/user_activation.rb +17 -0
  17. data/lib/sorcery/controller/submodules/activity_logging.rb +8 -11
  18. data/lib/sorcery/controller/submodules/brute_force_protection.rb +2 -3
  19. data/lib/sorcery/controller/submodules/external/protocols/oauth2.rb +25 -10
  20. data/lib/sorcery/controller/submodules/external/providers/facebook.rb +20 -5
  21. data/lib/sorcery/controller/submodules/external/providers/github.rb +15 -4
  22. data/lib/sorcery/controller/submodules/external/providers/google.rb +90 -0
  23. data/lib/sorcery/controller/submodules/external/providers/linkedin.rb +101 -0
  24. data/lib/sorcery/controller/submodules/external/providers/liveid.rb +91 -0
  25. data/lib/sorcery/controller/submodules/external/providers/twitter.rb +3 -2
  26. data/lib/sorcery/controller/submodules/external/providers/vkontakte.rb +94 -0
  27. data/lib/sorcery/controller/submodules/external.rb +89 -15
  28. data/lib/sorcery/controller/submodules/http_basic_auth.rb +1 -1
  29. data/lib/sorcery/controller/submodules/remember_me.rb +26 -15
  30. data/lib/sorcery/controller/submodules/session_timeout.rb +3 -3
  31. data/lib/sorcery/controller.rb +55 -46
  32. data/lib/sorcery/crypto_providers/bcrypt.rb +1 -0
  33. data/lib/sorcery/model/adapters/active_record.rb +21 -1
  34. data/lib/sorcery/model/adapters/mongo_mapper.rb +56 -0
  35. data/lib/sorcery/model/adapters/mongoid.rb +23 -4
  36. data/lib/sorcery/model/submodules/activity_logging.rb +4 -4
  37. data/lib/sorcery/model/submodules/brute_force_protection.rb +54 -15
  38. data/lib/sorcery/model/submodules/remember_me.rb +12 -7
  39. data/lib/sorcery/model/submodules/reset_password.rb +27 -11
  40. data/lib/sorcery/model/submodules/user_activation.rb +27 -8
  41. data/lib/sorcery/model/temporary_token.rb +4 -2
  42. data/lib/sorcery/model.rb +78 -47
  43. data/lib/sorcery/railties/tasks.rake +2 -0
  44. data/lib/sorcery.rb +10 -8
  45. data/sorcery.gemspec +107 -228
  46. data/spec/Gemfile +1 -1
  47. data/spec/Gemfile.lock +27 -23
  48. data/spec/README.md +12 -7
  49. data/spec/rails3/Gemfile +3 -3
  50. data/spec/rails3/Gemfile.lock +62 -55
  51. data/spec/rails3/app/controllers/application_controller.rb +85 -2
  52. data/spec/rails3/app/mailers/sorcery_mailer.rb +7 -0
  53. data/spec/rails3/app/views/sorcery_mailer/send_unlock_token_email.text.erb +1 -0
  54. data/spec/rails3/config/environments/in_memory.rb +35 -0
  55. data/spec/rails3/db/migrate/brute_force_protection/20101224223626_add_brute_force_protection_to_users.rb +2 -0
  56. data/spec/rails3/spec/controller_activity_logging_spec.rb +16 -6
  57. data/spec/rails3/spec/controller_brute_force_protection_spec.rb +25 -3
  58. data/spec/rails3/spec/controller_oauth2_spec.rb +260 -20
  59. data/spec/rails3/spec/controller_oauth_spec.rb +111 -6
  60. data/spec/rails3/spec/controller_session_timeout_spec.rb +4 -4
  61. data/spec/rails3/spec/controller_spec.rb +37 -2
  62. data/spec/rails3/spec/integration_spec.rb +15 -15
  63. data/spec/rails3/spec/spec_helper.rb +0 -2
  64. data/spec/rails3_mongo_mapper/.gitignore +4 -0
  65. data/spec/rails3_mongo_mapper/.rspec +1 -0
  66. data/spec/{sinatra_modular → rails3_mongo_mapper}/Gemfile +6 -5
  67. data/spec/rails3_mongo_mapper/Gemfile.lock +156 -0
  68. data/spec/{sinatra → rails3_mongo_mapper}/Rakefile +3 -3
  69. data/spec/rails3_mongo_mapper/app/controllers/application_controller.rb +122 -0
  70. data/spec/rails3_mongo_mapper/app/helpers/application_helper.rb +2 -0
  71. data/spec/rails3_mongo_mapper/app/models/authentication.rb +6 -0
  72. data/spec/rails3_mongo_mapper/app/models/user.rb +5 -0
  73. data/spec/rails3_mongo_mapper/app/views/layouts/application.html.erb +14 -0
  74. data/spec/rails3_mongo_mapper/app/views/sorcery_mailer/activation_email.html.erb +17 -0
  75. data/spec/rails3_mongo_mapper/app/views/sorcery_mailer/activation_email.text.erb +9 -0
  76. data/spec/rails3_mongo_mapper/app/views/sorcery_mailer/activation_success_email.html.erb +17 -0
  77. data/spec/rails3_mongo_mapper/app/views/sorcery_mailer/activation_success_email.text.erb +9 -0
  78. data/spec/rails3_mongo_mapper/app/views/sorcery_mailer/reset_password_email.html.erb +16 -0
  79. data/spec/rails3_mongo_mapper/app/views/sorcery_mailer/reset_password_email.text.erb +8 -0
  80. data/spec/rails3_mongo_mapper/config/application.rb +51 -0
  81. data/spec/rails3_mongo_mapper/config/boot.rb +13 -0
  82. data/spec/rails3_mongo_mapper/config/environment.rb +5 -0
  83. data/spec/rails3_mongo_mapper/config/environments/development.rb +30 -0
  84. data/spec/rails3_mongo_mapper/config/environments/in_memory.rb +0 -0
  85. data/spec/rails3_mongo_mapper/config/environments/production.rb +49 -0
  86. data/spec/rails3_mongo_mapper/config/environments/test.rb +35 -0
  87. data/spec/rails3_mongo_mapper/config/initializers/backtrace_silencers.rb +7 -0
  88. data/spec/rails3_mongo_mapper/config/initializers/inflections.rb +10 -0
  89. data/spec/rails3_mongo_mapper/config/initializers/mime_types.rb +5 -0
  90. data/spec/rails3_mongo_mapper/config/initializers/mongo.rb +2 -0
  91. data/spec/rails3_mongo_mapper/config/initializers/secret_token.rb +7 -0
  92. data/spec/rails3_mongo_mapper/config/initializers/session_store.rb +8 -0
  93. data/spec/rails3_mongo_mapper/config/locales/en.yml +5 -0
  94. data/spec/rails3_mongo_mapper/config/routes.rb +59 -0
  95. data/spec/rails3_mongo_mapper/config.ru +4 -0
  96. data/spec/rails3_mongo_mapper/db/schema.rb +23 -0
  97. data/spec/rails3_mongo_mapper/db/seeds.rb +7 -0
  98. data/spec/rails3_mongo_mapper/lib/tasks/.gitkeep +0 -0
  99. data/spec/rails3_mongo_mapper/public/404.html +26 -0
  100. data/spec/rails3_mongo_mapper/public/422.html +26 -0
  101. data/spec/rails3_mongo_mapper/public/500.html +26 -0
  102. data/spec/rails3_mongo_mapper/public/favicon.ico +0 -0
  103. data/spec/rails3_mongo_mapper/public/images/rails.png +0 -0
  104. data/spec/rails3_mongo_mapper/public/javascripts/application.js +2 -0
  105. data/spec/rails3_mongo_mapper/public/javascripts/controls.js +965 -0
  106. data/spec/rails3_mongo_mapper/public/javascripts/dragdrop.js +974 -0
  107. data/spec/rails3_mongo_mapper/public/javascripts/effects.js +1123 -0
  108. data/spec/rails3_mongo_mapper/public/javascripts/prototype.js +6001 -0
  109. data/spec/rails3_mongo_mapper/public/javascripts/rails.js +175 -0
  110. data/spec/rails3_mongo_mapper/public/robots.txt +5 -0
  111. data/spec/rails3_mongo_mapper/public/stylesheets/.gitkeep +0 -0
  112. data/spec/rails3_mongo_mapper/script/rails +6 -0
  113. data/spec/rails3_mongo_mapper/spec/controller_spec.rb +170 -0
  114. data/spec/rails3_mongo_mapper/spec/spec_helper.orig.rb +27 -0
  115. data/spec/rails3_mongo_mapper/spec/spec_helper.rb +55 -0
  116. data/spec/rails3_mongo_mapper/spec/user_activation_spec.rb +9 -0
  117. data/spec/rails3_mongo_mapper/spec/user_activity_logging_spec.rb +8 -0
  118. data/spec/rails3_mongo_mapper/spec/user_brute_force_protection_spec.rb +8 -0
  119. data/spec/rails3_mongo_mapper/spec/user_oauth_spec.rb +8 -0
  120. data/spec/rails3_mongo_mapper/spec/user_remember_me_spec.rb +8 -0
  121. data/spec/rails3_mongo_mapper/spec/user_reset_password_spec.rb +8 -0
  122. data/spec/rails3_mongo_mapper/spec/user_spec.rb +37 -0
  123. data/spec/rails3_mongo_mapper/vendor/plugins/.gitkeep +0 -0
  124. data/spec/rails3_mongoid/Gemfile +2 -1
  125. data/spec/rails3_mongoid/Gemfile.lock +47 -42
  126. data/spec/rails3_mongoid/app/controllers/application_controller.rb +19 -0
  127. data/spec/rails3_mongoid/config/mongoid.yml +1 -1
  128. data/spec/rails3_mongoid/script/rails +0 -0
  129. data/spec/rails3_mongoid/spec/controller_activity_logging_spec.rb +105 -0
  130. data/spec/rails3_mongoid/spec/controller_spec.rb +52 -1
  131. data/spec/rails3_mongoid/spec/user_spec.rb +1 -0
  132. data/spec/shared_examples/controller_oauth2_shared_examples.rb +20 -1
  133. data/spec/shared_examples/controller_oauth_shared_examples.rb +18 -0
  134. data/spec/shared_examples/user_activation_shared_examples.rb +72 -42
  135. data/spec/shared_examples/user_remember_me_shared_examples.rb +1 -1
  136. data/spec/shared_examples/user_reset_password_shared_examples.rb +81 -28
  137. data/spec/shared_examples/user_shared_examples.rb +29 -1
  138. data/spec/sorcery_crypto_providers_spec.rb +14 -1
  139. metadata +111 -229
  140. data/lib/generators/sorcery_migration/sorcery_migration_generator.rb +0 -24
  141. data/lib/generators/sorcery_migration/templates/activity_logging.rb +0 -17
  142. data/lib/generators/sorcery_migration/templates/brute_force_protection.rb +0 -11
  143. data/lib/generators/sorcery_migration/templates/remember_me.rb +0 -15
  144. data/lib/generators/sorcery_migration/templates/reset_password.rb +0 -17
  145. data/lib/generators/sorcery_migration/templates/user_activation.rb +0 -17
  146. data/lib/sorcery/controller/adapters/sinatra.rb +0 -115
  147. data/lib/sorcery/initializers/initializer.rb +0 -199
  148. data/lib/sorcery/sinatra.rb +0 -4
  149. data/lib/sorcery/test_helpers/internal/sinatra.rb +0 -74
  150. data/lib/sorcery/test_helpers/internal/sinatra_modular.rb +0 -74
  151. data/lib/sorcery/test_helpers/sinatra.rb +0 -88
  152. data/spec/rails3/Rakefile.unused +0 -7
  153. data/spec/sinatra/Gemfile +0 -15
  154. data/spec/sinatra/Gemfile.lock +0 -115
  155. data/spec/sinatra/authentication.rb +0 -3
  156. data/spec/sinatra/db/migrate/activation/20101224223622_add_activation_to_users.rb +0 -17
  157. data/spec/sinatra/db/migrate/activity_logging/20101224223624_add_activity_logging_to_users.rb +0 -17
  158. data/spec/sinatra/db/migrate/brute_force_protection/20101224223626_add_brute_force_protection_to_users.rb +0 -11
  159. data/spec/sinatra/db/migrate/core/20101224223620_create_users.rb +0 -16
  160. data/spec/sinatra/db/migrate/external/20101224223628_create_authentications.rb +0 -14
  161. data/spec/sinatra/db/migrate/remember_me/20101224223623_add_remember_me_token_to_users.rb +0 -15
  162. data/spec/sinatra/db/migrate/reset_password/20101224223622_add_reset_password_to_users.rb +0 -13
  163. data/spec/sinatra/filters.rb +0 -27
  164. data/spec/sinatra/modular.rb +0 -157
  165. data/spec/sinatra/myapp.rb +0 -133
  166. data/spec/sinatra/spec/controller_activity_logging_spec.rb +0 -85
  167. data/spec/sinatra/spec/controller_brute_force_protection_spec.rb +0 -70
  168. data/spec/sinatra/spec/controller_http_basic_auth_spec.rb +0 -53
  169. data/spec/sinatra/spec/controller_oauth2_spec.rb +0 -96
  170. data/spec/sinatra/spec/controller_oauth_spec.rb +0 -100
  171. data/spec/sinatra/spec/controller_remember_me_spec.rb +0 -64
  172. data/spec/sinatra/spec/controller_session_timeout_spec.rb +0 -57
  173. data/spec/sinatra/spec/controller_spec.rb +0 -127
  174. data/spec/sinatra/spec/spec_helper.rb +0 -45
  175. data/spec/sinatra/user.rb +0 -6
  176. data/spec/sinatra/views/test_login.erb +0 -4
  177. data/spec/sinatra_modular/Gemfile.lock +0 -115
  178. data/spec/sinatra_modular/Rakefile +0 -11
  179. data/spec/sinatra_modular/authentication.rb +0 -3
  180. data/spec/sinatra_modular/db/migrate/activation/20101224223622_add_activation_to_users.rb +0 -17
  181. data/spec/sinatra_modular/db/migrate/activity_logging/20101224223624_add_activity_logging_to_users.rb +0 -17
  182. data/spec/sinatra_modular/db/migrate/brute_force_protection/20101224223626_add_brute_force_protection_to_users.rb +0 -11
  183. data/spec/sinatra_modular/db/migrate/core/20101224223620_create_users.rb +0 -16
  184. data/spec/sinatra_modular/db/migrate/external/20101224223628_create_authentications.rb +0 -14
  185. data/spec/sinatra_modular/db/migrate/remember_me/20101224223623_add_remember_me_token_to_users.rb +0 -15
  186. data/spec/sinatra_modular/db/migrate/reset_password/20101224223622_add_reset_password_to_users.rb +0 -13
  187. data/spec/sinatra_modular/filters.rb +0 -27
  188. data/spec/sinatra_modular/modular.rb +0 -157
  189. data/spec/sinatra_modular/myapp.rb +0 -133
  190. data/spec/sinatra_modular/sorcery_mailer.rb +0 -25
  191. data/spec/sinatra_modular/spec_modular/controller_activity_logging_spec.rb +0 -85
  192. data/spec/sinatra_modular/spec_modular/controller_brute_force_protection_spec.rb +0 -70
  193. data/spec/sinatra_modular/spec_modular/controller_http_basic_auth_spec.rb +0 -53
  194. data/spec/sinatra_modular/spec_modular/controller_oauth2_spec.rb +0 -96
  195. data/spec/sinatra_modular/spec_modular/controller_oauth_spec.rb +0 -100
  196. data/spec/sinatra_modular/spec_modular/controller_remember_me_spec.rb +0 -64
  197. data/spec/sinatra_modular/spec_modular/controller_session_timeout_spec.rb +0 -57
  198. data/spec/sinatra_modular/spec_modular/controller_spec.rb +0 -116
  199. data/spec/sinatra_modular/spec_modular/spec.opts +0 -2
  200. data/spec/sinatra_modular/spec_modular/spec_helper.rb +0 -51
  201. data/spec/sinatra_modular/user.rb +0 -6
  202. data/spec/sinatra_modular/views/test_login.erb +0 -4
  203. /data/spec/{sinatra → rails3_mongo_mapper/app/mailers}/sorcery_mailer.rb +0 -0
  204. /data/spec/{sinatra → rails3_mongo_mapper}/spec/spec.opts +0 -0
@@ -2,24 +2,51 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
  require File.expand_path(File.dirname(__FILE__) + '/../../shared_examples/controller_oauth2_shared_examples')
3
3
 
4
4
  def stub_all_oauth2_requests!
5
- @client = OAuth2::Client.new("key","secret", :site => "http://myapi.com")
6
- OAuth2::Client.stub!(:new).and_return(@client)
7
- @acc_token = OAuth2::AccessToken.new(@client, "asd", {})
8
- @client.stub!(:get_token).and_return(@acc_token)
9
- @acc_token.stub!(:get).and_return({"id"=>"123", "name"=>"Noam Ben Ari", "first_name"=>"Noam", "last_name"=>"Ben Ari", "link"=>"http://www.facebook.com/nbenari1", "hometown"=>{"id"=>"110619208966868", "name"=>"Haifa, Israel"}, "location"=>{"id"=>"106906559341067", "name"=>"Pardes Hanah, Hefa, Israel"}, "bio"=>"I'm a new daddy, and enjoying it!", "gender"=>"male", "email"=>"nbenari@gmail.com", "timezone"=>2, "locale"=>"en_US", "languages"=>[{"id"=>"108405449189952", "name"=>"Hebrew"}, {"id"=>"106059522759137", "name"=>"English"}, {"id"=>"112624162082677", "name"=>"Russian"}], "verified"=>true, "updated_time"=>"2011-02-16T20:59:38+0000"}.to_json)
5
+ auth_code = OAuth2::Strategy::AuthCode.any_instance
6
+ access_token = mock(OAuth2::AccessToken)
7
+ access_token.stub(:token_param=)
8
+ response = mock(OAuth2::Response)
9
+ response.stub(:body).and_return({
10
+ "id"=>"123",
11
+ "name"=>"Noam Ben Ari",
12
+ "first_name"=>"Noam",
13
+ "last_name"=>"Ben Ari",
14
+ "link"=>"http://www.facebook.com/nbenari1",
15
+ "hometown"=>{"id"=>"110619208966868", "name"=>"Haifa, Israel"},
16
+ "location"=>{"id"=>"106906559341067", "name"=>"Pardes Hanah, Hefa, Israel"},
17
+ "bio"=>"I'm a new daddy, and enjoying it!",
18
+ "gender"=>"male",
19
+ "email"=>"nbenari@gmail.com",
20
+ "timezone"=>2,
21
+ "locale"=>"en_US",
22
+ "languages"=>[{"id"=>"108405449189952", "name"=>"Hebrew"}, {"id"=>"106059522759137", "name"=>"English"}, {"id"=>"112624162082677", "name"=>"Russian"}],
23
+ "verified"=>true,
24
+ "updated_time"=>"2011-02-16T20:59:38+0000"}.to_json)
25
+ access_token.stub(:get).and_return(response)
26
+ auth_code.stub(:get_token).and_return(access_token)
27
+ end
28
+
29
+ def set_external_property
30
+ sorcery_controller_property_set(:external_providers, [:facebook, :github, :google, :liveid])
31
+ sorcery_controller_external_property_set(:facebook, :key, "eYVNBjBDi33aa9GkA3w")
32
+ sorcery_controller_external_property_set(:facebook, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
33
+ sorcery_controller_external_property_set(:facebook, :callback_url, "http://blabla.com")
34
+ sorcery_controller_external_property_set(:github, :key, "eYVNBjBDi33aa9GkA3w")
35
+ sorcery_controller_external_property_set(:github, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
36
+ sorcery_controller_external_property_set(:github, :callback_url, "http://blabla.com")
37
+ sorcery_controller_external_property_set(:google, :key, "eYVNBjBDi33aa9GkA3w")
38
+ sorcery_controller_external_property_set(:google, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
39
+ sorcery_controller_external_property_set(:google, :callback_url, "http://blabla.com")
40
+ sorcery_controller_external_property_set(:liveid, :key, "eYVNBjBDi33aa9GkA3w")
41
+ sorcery_controller_external_property_set(:liveid, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
42
+ sorcery_controller_external_property_set(:liveid, :callback_url, "http://blabla.com")
10
43
  end
11
44
 
12
45
  describe ApplicationController do
13
46
  before(:all) do
14
47
  ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/external")
15
48
  sorcery_reload!([:external])
16
- sorcery_controller_property_set(:external_providers, [:facebook, :github])
17
- sorcery_controller_external_property_set(:facebook, :key, "eYVNBjBDi33aa9GkA3w")
18
- sorcery_controller_external_property_set(:facebook, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
19
- sorcery_controller_external_property_set(:facebook, :callback_url, "http://blabla.com")
20
- sorcery_controller_external_property_set(:github, :key, "eYVNBjBDi33aa9GkA3w")
21
- sorcery_controller_external_property_set(:github, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
22
- sorcery_controller_external_property_set(:github, :callback_url, "http://blabla.com")
49
+ set_external_property
23
50
  end
24
51
 
25
52
  after(:all) do
@@ -37,13 +64,30 @@ describe ApplicationController do
37
64
  Authentication.delete_all
38
65
  end
39
66
 
40
- it "login_at redirects correctly" do
41
- create_new_user
42
- get :login_at_test2
43
- response.should be_a_redirect
44
- response.should redirect_to("http://myapi.com/oauth/authorize?redirect_uri=http%3A%2F%2Fblabla.com&scope=email%2Coffline_access")
67
+ context "when callback_url begin with /" do
68
+ before do
69
+ sorcery_controller_external_property_set(:facebook, :callback_url, "/oauth/twitter/callback")
70
+ end
71
+ it "login_at redirects correctly" do
72
+ create_new_user
73
+ get :login_at_test2
74
+ response.should be_a_redirect
75
+ response.should redirect_to("https://graph.facebook.com/oauth/authorize?response_type=code&client_id=#{::Sorcery::Controller::Config.facebook.key}&redirect_uri=http%3A%2F%2Ftest.host%2Foauth%2Ftwitter%2Fcallback&scope=email%2Coffline_access&display=page")
76
+ end
77
+ after do
78
+ sorcery_controller_external_property_set(:facebook, :callback_url, "http://blabla.com")
79
+ end
45
80
  end
46
81
 
82
+ context "when callback_url begin with http://" do
83
+ it "login_at redirects correctly" do
84
+ create_new_user
85
+ get :login_at_test2
86
+ response.should be_a_redirect
87
+ response.should redirect_to("https://graph.facebook.com/oauth/authorize?response_type=code&client_id=#{::Sorcery::Controller::Config.facebook.key}&redirect_uri=http%3A%2F%2Fblabla.com&scope=email%2Coffline_access&display=page")
88
+ end
89
+ end
90
+
47
91
  it "'login_from' logins if user exists" do
48
92
  sorcery_model_property_set(:authentications_class, Authentication)
49
93
  create_new_external_user(:facebook)
@@ -58,12 +102,20 @@ describe ApplicationController do
58
102
  flash[:alert].should == "Failed!"
59
103
  end
60
104
 
105
+ it "on successful login_from the user should be redirected to the url he originally wanted" do
106
+ sorcery_model_property_set(:authentications_class, Authentication)
107
+ create_new_external_user(:facebook)
108
+ get :test_return_to_with_external2, {}, :return_to_url => "fuu"
109
+ response.should redirect_to("fuu")
110
+ flash[:notice].should == "Success!"
111
+ end
112
+
61
113
  # provider: github
62
114
  it "login_at redirects correctly (github)" do
63
115
  create_new_user
64
116
  get :login_at_test3
65
117
  response.should be_a_redirect
66
- response.should redirect_to("http://myapi.com/oauth/authorize?redirect_uri=http%3A%2F%2Fblabla.com&scope=")
118
+ response.should redirect_to("https://github.com/login/oauth/authorize?response_type=code&client_id=#{::Sorcery::Controller::Config.github.key}&redirect_uri=http%3A%2F%2Fblabla.com&scope&display")
67
119
  end
68
120
 
69
121
  it "'login_from' logins if user exists (github)" do
@@ -80,6 +132,74 @@ describe ApplicationController do
80
132
  flash[:alert].should == "Failed!"
81
133
  end
82
134
 
135
+ it "on successful login_from the user should be redirected to the url he originally wanted (github)" do
136
+ sorcery_model_property_set(:authentications_class, Authentication)
137
+ create_new_external_user(:github)
138
+ get :test_return_to_with_external3, {}, :return_to_url => "fuu"
139
+ response.should redirect_to("fuu")
140
+ flash[:notice].should == "Success!"
141
+ end
142
+
143
+ # provider: google
144
+ it "login_at redirects correctly (google)" do
145
+ create_new_user
146
+ get :login_at_test4
147
+ response.should be_a_redirect
148
+ response.should redirect_to("https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=#{::Sorcery::Controller::Config.google.key}&redirect_uri=http%3A%2F%2Fblabla.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&display")
149
+ end
150
+
151
+ it "'login_from' logins if user exists (google)" do
152
+ sorcery_model_property_set(:authentications_class, Authentication)
153
+ create_new_external_user(:google)
154
+ get :test_login_from4
155
+ flash[:notice].should == "Success!"
156
+ end
157
+
158
+ it "'login_from' fails if user doesn't exist (google)" do
159
+ sorcery_model_property_set(:authentications_class, Authentication)
160
+ create_new_user
161
+ get :test_login_from4
162
+ flash[:alert].should == "Failed!"
163
+ end
164
+
165
+ it "on successful login_from the user should be redirected to the url he originally wanted (google)" do
166
+ sorcery_model_property_set(:authentications_class, Authentication)
167
+ create_new_external_user(:google)
168
+ get :test_return_to_with_external4, {}, :return_to_url => "fuu"
169
+ response.should redirect_to("fuu")
170
+ flash[:notice].should == "Success!"
171
+ end
172
+
173
+ # provider: liveid
174
+ it "login_at redirects correctly (liveid)" do
175
+ create_new_user
176
+ get :login_at_test5
177
+ response.should be_a_redirect
178
+ response.should redirect_to("https://oauth.live.com/authorize?response_type=code&client_id=#{::Sorcery::Controller::Config.liveid.key}&redirect_uri=http%3A%2F%2Fblabla.com&scope=wl.basic+wl.emails+wl.offline_access&display")
179
+ end
180
+
181
+ it "'login_from' logins if user exists (liveid)" do
182
+ sorcery_model_property_set(:authentications_class, Authentication)
183
+ create_new_external_user(:liveid)
184
+ get :test_login_from5
185
+ flash[:notice].should == "Success!"
186
+ end
187
+
188
+ it "'login_from' fails if user doesn't exist (liveid)" do
189
+ sorcery_model_property_set(:authentications_class, Authentication)
190
+ create_new_user
191
+ get :test_login_from5
192
+ flash[:alert].should == "Failed!"
193
+ end
194
+
195
+ it "on successful login_from the user should be redirected to the url he originally wanted (liveid)" do
196
+ sorcery_model_property_set(:authentications_class, Authentication)
197
+ create_new_external_user(:liveid)
198
+ get :test_return_to_with_external5, {}, :return_to_url => "fuu"
199
+ response.should redirect_to("fuu")
200
+ flash[:notice].should == "Success!"
201
+ end
202
+
83
203
  end
84
204
 
85
205
 
@@ -91,13 +211,20 @@ describe ApplicationController do
91
211
  before(:all) do
92
212
  ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/activation")
93
213
  sorcery_reload!([:user_activation,:external], :user_activation_mailer => ::SorceryMailer)
94
- sorcery_controller_property_set(:external_providers, [:facebook, :github])
214
+ sorcery_controller_property_set(:external_providers, [:facebook, :github, :google, :liveid])
95
215
  sorcery_controller_external_property_set(:facebook, :key, "eYVNBjBDi33aa9GkA3w")
96
216
  sorcery_controller_external_property_set(:facebook, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
97
217
  sorcery_controller_external_property_set(:facebook, :callback_url, "http://blabla.com")
98
218
  sorcery_controller_external_property_set(:github, :key, "eYVNBjBDi33aa9GkA3w")
99
219
  sorcery_controller_external_property_set(:github, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
100
220
  sorcery_controller_external_property_set(:github, :callback_url, "http://blabla.com")
221
+ sorcery_controller_external_property_set(:google, :key, "eYVNBjBDi33aa9GkA3w")
222
+ sorcery_controller_external_property_set(:google, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
223
+ sorcery_controller_external_property_set(:google, :callback_url, "http://blabla.com")
224
+ sorcery_controller_external_property_set(:liveid, :key, "eYVNBjBDi33aa9GkA3w")
225
+ sorcery_controller_external_property_set(:liveid, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
226
+ sorcery_controller_external_property_set(:liveid, :callback_url, "http://blabla.com")
227
+
101
228
  end
102
229
 
103
230
  after(:all) do
@@ -136,5 +263,118 @@ describe ApplicationController do
136
263
  @user.activate!
137
264
  ActionMailer::Base.deliveries.size.should == old_size
138
265
  end
266
+
267
+ # provider: google
268
+ it "should not send activation email to external users (google)" do
269
+ old_size = ActionMailer::Base.deliveries.size
270
+ create_new_external_user(:google)
271
+ ActionMailer::Base.deliveries.size.should == old_size
272
+ end
273
+
274
+ it "should not send external users an activation success email (google)" do
275
+ sorcery_model_property_set(:activation_success_email_method_name, nil)
276
+ create_new_external_user(:google)
277
+ old_size = ActionMailer::Base.deliveries.size
278
+ @user.activate!
279
+ ActionMailer::Base.deliveries.size.should == old_size
280
+ end
281
+
282
+ # provider: liveid
283
+ it "should not send activation email to external users (liveid)" do
284
+ old_size = ActionMailer::Base.deliveries.size
285
+ create_new_external_user(:liveid)
286
+ ActionMailer::Base.deliveries.size.should == old_size
287
+ end
288
+
289
+ it "should not send external users an activation success email (liveid)" do
290
+ sorcery_model_property_set(:activation_success_email_method_name, nil)
291
+ create_new_external_user(:liveid)
292
+ old_size = ActionMailer::Base.deliveries.size
293
+ @user.activate!
294
+ ActionMailer::Base.deliveries.size.should == old_size
295
+ end
139
296
  end
140
- end
297
+
298
+ describe ApplicationController, "OAuth with user activation features" do
299
+ before(:all) do
300
+ ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/external")
301
+ ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/activity_logging")
302
+ sorcery_reload!([:activity_logging, :external])
303
+ end
304
+
305
+ after(:all) do
306
+ ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/external")
307
+ ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/activity_logging")
308
+ end
309
+
310
+ %w(facebook github google liveid).each.with_index(2) do |provider, index|
311
+ context "when #{provider}" do
312
+ before(:each) do
313
+ User.delete_all
314
+ Authentication.delete_all
315
+ sorcery_controller_property_set(:register_login_time, true)
316
+ stub_all_oauth2_requests!
317
+ sorcery_model_property_set(:authentications_class, Authentication)
318
+ create_new_external_user(provider.to_sym)
319
+ end
320
+
321
+ it "should register login time" do
322
+ now = Time.now.in_time_zone
323
+ get "test_login_from#{index}".to_sym
324
+ User.last.last_login_at.should_not be_nil
325
+ User.last.last_login_at.to_s(:db).should >= now.to_s(:db)
326
+ User.last.last_login_at.to_s(:db).should <= (now+2).to_s(:db)
327
+ end
328
+
329
+ it "should not register login time if configured so" do
330
+ sorcery_controller_property_set(:register_login_time, false)
331
+ now = Time.now.in_time_zone
332
+ get "test_login_from#{index}".to_sym
333
+ User.last.last_login_at.should be_nil
334
+ end
335
+ end
336
+ end
337
+ end
338
+
339
+ describe ApplicationController, "OAuth with session timeout features" do
340
+ before(:all) do
341
+ ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/external")
342
+ sorcery_reload!([:session_timeout, :external])
343
+ end
344
+
345
+ after(:all) do
346
+ ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/external")
347
+ end
348
+
349
+ %w(facebook github google liveid).each.with_index(2) do |provider, index|
350
+ context "when #{provider}" do
351
+ before(:each) do
352
+ User.delete_all
353
+ Authentication.delete_all
354
+ sorcery_model_property_set(:authentications_class, Authentication)
355
+ sorcery_controller_property_set(:session_timeout,0.5)
356
+ stub_all_oauth2_requests!
357
+ create_new_external_user(provider.to_sym)
358
+ end
359
+
360
+ after(:each) do
361
+ Timecop.return
362
+ end
363
+
364
+ it "should not reset session before session timeout" do
365
+ get "test_login_from#{index}".to_sym
366
+ session[:user_id].should_not be_nil
367
+ flash[:notice].should == "Success!"
368
+ end
369
+
370
+ it "should reset session after session timeout" do
371
+ get "test_login_from#{index}".to_sym
372
+ Timecop.travel(Time.now.in_time_zone+0.6)
373
+ get :test_should_be_logged_in
374
+ session[:user_id].should be_nil
375
+ response.should be_a_redirect
376
+ end
377
+ end
378
+ end
379
+ end
380
+ end
@@ -43,11 +43,28 @@ describe ApplicationController do
43
43
  Authentication.delete_all
44
44
  end
45
45
 
46
- it "login_at redirects correctly" do
47
- create_new_user
48
- get :login_at_test
49
- response.should be_a_redirect
50
- response.should redirect_to("http://myapi.com/oauth/authorize?oauth_callback=http%3A%2F%2Fblabla.com&oauth_token=")
46
+ context "when callback_url begin with /" do
47
+ before do
48
+ sorcery_controller_external_property_set(:twitter, :callback_url, "/oauth/twitter/callback")
49
+ end
50
+ it "login_at redirects correctly" do
51
+ create_new_user
52
+ get :login_at_test
53
+ response.should be_a_redirect
54
+ response.should redirect_to("http://myapi.com/oauth/authorize?oauth_callback=http%3A%2F%2Ftest.host%2Foauth%2Ftwitter%2Fcallback&oauth_token=")
55
+ end
56
+ after do
57
+ sorcery_controller_external_property_set(:twitter, :callback_url, "http://blabla.com")
58
+ end
59
+ end
60
+
61
+ context "when callback_url begin with http://" do
62
+ it "login_at redirects correctly" do
63
+ create_new_user
64
+ get :login_at_test
65
+ response.should be_a_redirect
66
+ response.should redirect_to("http://myapi.com/oauth/authorize?oauth_callback=http%3A%2F%2Fblabla.com&oauth_token=")
67
+ end
51
68
  end
52
69
 
53
70
  it "logins if user exists" do
@@ -63,6 +80,15 @@ describe ApplicationController do
63
80
  get :test_login_from, :oauth_verifier => "blablaRERASDFcxvSDFA"
64
81
  flash[:alert].should == "Failed!"
65
82
  end
83
+
84
+ it "on successful 'login_from' the user should be redirected to the url he originally wanted" do
85
+ sorcery_model_property_set(:authentications_class, Authentication)
86
+ create_new_external_user(:twitter)
87
+ get :test_return_to_with_external, {}, :return_to_url => "fuu"
88
+ response.should redirect_to("fuu")
89
+ flash[:notice].should == "Success!"
90
+ end
91
+
66
92
  end
67
93
 
68
94
  describe ApplicationController do
@@ -98,4 +124,83 @@ describe ApplicationController do
98
124
  ActionMailer::Base.deliveries.size.should == old_size
99
125
  end
100
126
  end
101
- end
127
+
128
+ describe ApplicationController, "OAuth with user activation features" do
129
+ before(:all) do
130
+ ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/external")
131
+ ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/activity_logging")
132
+ sorcery_reload!([:activity_logging, :external])
133
+ end
134
+
135
+ after(:all) do
136
+ ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/external")
137
+ ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/activity_logging")
138
+ end
139
+
140
+ context "when twitter" do
141
+ before(:each) do
142
+ User.delete_all
143
+ Authentication.delete_all
144
+ sorcery_controller_property_set(:register_login_time, true)
145
+ stub_all_oauth_requests!
146
+ sorcery_model_property_set(:authentications_class, Authentication)
147
+ create_new_external_user(:twitter)
148
+ end
149
+
150
+ it "should register login time" do
151
+ now = Time.now.in_time_zone
152
+ get :test_login_from
153
+ User.last.last_login_at.should_not be_nil
154
+ User.last.last_login_at.to_s(:db).should >= now.to_s(:db)
155
+ User.last.last_login_at.to_s(:db).should <= (now+2).to_s(:db)
156
+ end
157
+
158
+ it "should not register login time if configured so" do
159
+ sorcery_controller_property_set(:register_login_time, false)
160
+ now = Time.now.in_time_zone
161
+ get :test_login_from
162
+ User.last.last_login_at.should be_nil
163
+ end
164
+ end
165
+ end
166
+
167
+ describe ApplicationController, "OAuth with session timeout features" do
168
+ before(:all) do
169
+ ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/external")
170
+ sorcery_reload!([:session_timeout, :external])
171
+ end
172
+
173
+ after(:all) do
174
+ ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/external")
175
+ end
176
+
177
+ context "when twitter" do
178
+ before(:each) do
179
+ User.delete_all
180
+ Authentication.delete_all
181
+ sorcery_model_property_set(:authentications_class, Authentication)
182
+ sorcery_controller_property_set(:session_timeout,0.5)
183
+ stub_all_oauth_requests!
184
+ create_new_external_user(:twitter)
185
+ end
186
+
187
+ after(:each) do
188
+ Timecop.return
189
+ end
190
+
191
+ it "should not reset session before session timeout" do
192
+ get :test_login_from
193
+ session[:user_id].should_not be_nil
194
+ flash[:notice].should == "Success!"
195
+ end
196
+
197
+ it "should reset session after session timeout" do
198
+ get :test_login_from
199
+ Timecop.travel(Time.now.in_time_zone+0.6)
200
+ get :test_should_be_logged_in
201
+ session[:user_id].should be_nil
202
+ response.should be_a_redirect
203
+ end
204
+ end
205
+ end
206
+ end
@@ -23,7 +23,7 @@ describe ApplicationController do
23
23
 
24
24
  it "should reset session after session timeout" do
25
25
  login_user
26
- Timecop.travel(Time.now+0.6)
26
+ Timecop.travel(Time.now.in_time_zone+0.6)
27
27
  get :test_should_be_logged_in
28
28
  session[:user_id].should be_nil
29
29
  response.should be_a_redirect
@@ -33,10 +33,10 @@ describe ApplicationController do
33
33
  it "should not logout if there was activity" do
34
34
  sorcery_controller_property_set(:session_timeout_from_last_action, true)
35
35
  get :test_login, :username => 'gizmo', :password => 'secret'
36
- Timecop.travel(Time.now+0.3)
36
+ Timecop.travel(Time.now.in_time_zone+0.3)
37
37
  get :test_should_be_logged_in
38
38
  session[:user_id].should_not be_nil
39
- Timecop.travel(Time.now+0.3)
39
+ Timecop.travel(Time.now.in_time_zone+0.3)
40
40
  get :test_should_be_logged_in
41
41
  session[:user_id].should_not be_nil
42
42
  response.should be_a_success
@@ -45,7 +45,7 @@ describe ApplicationController do
45
45
  it "with 'session_timeout_from_last_action' should logout if there was no activity" do
46
46
  sorcery_controller_property_set(:session_timeout_from_last_action, true)
47
47
  get :test_login, :username => 'gizmo', :password => 'secret'
48
- Timecop.travel(Time.now+0.6)
48
+ Timecop.travel(Time.now.in_time_zone+0.6)
49
49
  get :test_should_be_logged_in
50
50
  session[:user_id].should be_nil
51
51
  response.should be_a_redirect
@@ -63,13 +63,41 @@ describe ApplicationController do
63
63
  assigns[:user].should == @user
64
64
  session[:user_id].should == @user.id
65
65
  end
66
-
66
+
67
67
  it "login(username,password) should return nil and not set the session when failure" do
68
68
  get :test_login, :username => 'gizmo', :password => 'opensesame!'
69
69
  assigns[:user].should be_nil
70
70
  session[:user_id].should be_nil
71
71
  end
72
+
73
+ it "login(username,password) should return nil and not set the session when upper case username" do
74
+ get :test_login, :username => 'GIZMO', :password => 'secret'
75
+ assigns[:user].should be_nil
76
+ session[:user_id].should be_nil
77
+ end
72
78
 
79
+ 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
80
+ sorcery_model_property_set(:downcase_username_before_authenticating, true)
81
+ get :test_login, :username => 'GIZMO', :password => 'secret'
82
+ assigns[:user].should == @user
83
+ session[:user_id].should == @user.id
84
+ end
85
+
86
+ 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
87
+ create_new_user({:username => 'GIZMO1', :email => "bla1@bla.com", :password => 'secret1'})
88
+ get :test_login, :username => 'gizmo1', :password => 'secret1'
89
+ assigns[:user].should be_nil
90
+ session[:user_id].should be_nil
91
+ end
92
+
93
+ 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
94
+ sorcery_model_property_set(:downcase_username_before_authenticating, true)
95
+ create_new_user({:username => 'GIZMO1', :email => "bla1@bla.com", :password => 'secret1'})
96
+ get :test_login, :username => 'gizmo1', :password => 'secret1'
97
+ assigns[:user].should == @user
98
+ session[:user_id].should == @user.id
99
+ end
100
+
73
101
  it "logout should clear the session" do
74
102
  cookies[:remember_me_token] = nil
75
103
  session[:user_id] = @user.id
@@ -113,6 +141,13 @@ describe ApplicationController do
113
141
  response.should redirect_to("http://test.host/")
114
142
  end
115
143
 
144
+ it "require_login before_filter should not save the url that the user originally wanted upon all non-get http methods" do
145
+ [:post, :put, :delete].each do |m|
146
+ self.send(m, :some_action)
147
+ session[:return_to_url].should be_nil
148
+ end
149
+ end
150
+
116
151
  it "on successful login the user should be redirected to the url he originally wanted" do
117
152
  session[:return_to_url] = "http://test.host/some_action"
118
153
  post :test_return_to, :username => 'gizmo', :password => 'secret'
@@ -139,4 +174,4 @@ describe ApplicationController do
139
174
  end
140
175
  end
141
176
 
142
- end
177
+ end
@@ -1,21 +1,21 @@
1
- # require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
- #
3
- # describe "the login process", :type => :request do
4
- # before(:all) do
5
- # sorcery_reload!
6
- # create_new_user
7
- # end
8
- #
9
- # after(:all) do
10
- # end
11
- #
12
- # it "handles unverified request" do
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "the login process", :type => :request do
4
+ before(:all) do
5
+ sorcery_reload!
6
+ create_new_user
7
+ end
8
+
9
+ after(:all) do
10
+ end
11
+ end
12
+ # it "handles unverified request", :js => true do
13
13
  # visit root_path
14
- # # save_and_open_page
15
- # fill_in 'Username', :with => 'gizmo'
14
+ # #save_and_open_page
15
+ # fill_in 'Username', :with => 'gizmo1'
16
16
  # fill_in 'Password', :with => 'secret'
17
17
  # # <input name="authenticity_token" type="hidden" value="+8M9lXnjnhAW/mAuzwI9Mmy6hM+00qZJa8VMQUg+NmM=">
18
- # #page.execute_script("$$('hidden').value='mezuza'")
18
+ # page.execute_script("$$('hidden').value='mezuza'")
19
19
  # #save_and_open_page
20
20
  # click_button 'Login'
21
21
  # save_and_open_page
@@ -24,8 +24,6 @@ require 'timecop'
24
24
  # in spec/support/ and its subdirectories.
25
25
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
26
26
 
27
-
28
-
29
27
  RSpec.configure do |config|
30
28
  config.include RSpec::Rails::ControllerExampleGroup, :example_group => { :file_path => /controller(.)*_spec.rb$/ }
31
29
  # == Mock Framework
@@ -0,0 +1,4 @@
1
+ .bundle
2
+ db/*.sqlite3
3
+ log/*.log
4
+ tmp/**/*
@@ -0,0 +1 @@
1
+ --colour
@@ -1,15 +1,16 @@
1
1
  source 'http://rubygems.org'
2
2
 
3
- gem 'sinatra', '>= 1.2.0'
4
- gem 'sqlite3-ruby', :require => 'sqlite3'
5
- gem 'activerecord', '>= 3.0.3'
6
- gem 'actionmailer', '>= 3.0.3'
3
+ gem 'bcrypt-ruby', '~> 3.0.0'
4
+ gem 'rails', '3.0.3'
7
5
  gem "sorcery", '>= 0.1.0', :path => '../../'
6
+ gem "mongo_mapper", '~> 0.11.0'
7
+ gem "bson_ext", "~> 1.3"
8
8
 
9
9
  group :development, :test do
10
- gem "rake"
11
10
  gem "rspec", "~> 2.5.0"
11
+ gem 'rspec-rails', "~> 2.5.0"
12
12
  gem 'ruby-debug19'
13
13
  gem 'simplecov', '>= 0.3.8', :require => false # Will install simplecov-html as a dependency
14
14
  gem 'timecop'
15
+ gem 'pry'
15
16
  end