sorcery 0.2.1 → 0.3.1

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 (79) hide show
  1. data/README.rdoc +129 -61
  2. data/VERSION +1 -1
  3. data/lib/generators/sorcery_migration/sorcery_migration_generator.rb +24 -0
  4. data/lib/generators/sorcery_migration/templates/activity_logging.rb +17 -0
  5. data/lib/generators/sorcery_migration/templates/brute_force_protection.rb +11 -0
  6. data/lib/generators/sorcery_migration/templates/core.rb +16 -0
  7. data/lib/generators/sorcery_migration/templates/external.rb +14 -0
  8. data/lib/generators/sorcery_migration/templates/remember_me.rb +15 -0
  9. data/lib/generators/sorcery_migration/templates/reset_password.rb +13 -0
  10. data/lib/generators/sorcery_migration/templates/user_activation.rb +17 -0
  11. data/lib/sorcery/controller/adapters/sinatra.rb +97 -0
  12. data/lib/sorcery/controller/submodules/email.rb +44 -0
  13. data/lib/sorcery/controller/submodules/external/protocols/oauth1.rb +35 -0
  14. data/lib/sorcery/controller/submodules/external/protocols/oauth2.rb +26 -0
  15. data/lib/sorcery/controller/submodules/{oauth → external}/providers/facebook.rb +24 -6
  16. data/lib/sorcery/controller/submodules/{oauth → external}/providers/twitter.rb +31 -6
  17. data/lib/sorcery/controller/submodules/external.rb +83 -0
  18. data/lib/sorcery/controller/submodules/http_basic_auth.rb +10 -6
  19. data/lib/sorcery/controller/submodules/oauth.rb +6 -3
  20. data/lib/sorcery/controller.rb +5 -3
  21. data/lib/sorcery/model/submodules/activity_logging.rb +1 -1
  22. data/lib/sorcery/model/submodules/brute_force_protection.rb +0 -4
  23. data/lib/sorcery/model/submodules/{oauth.rb → external.rb} +3 -3
  24. data/lib/sorcery/model.rb +9 -6
  25. data/lib/sorcery/sinatra.rb +14 -0
  26. data/lib/sorcery/test_helpers/rails.rb +57 -0
  27. data/lib/sorcery/test_helpers/sinatra.rb +131 -0
  28. data/lib/sorcery/test_helpers.rb +8 -52
  29. data/lib/sorcery.rb +17 -7
  30. data/sorcery.gemspec +86 -10
  31. data/spec/Gemfile +1 -1
  32. data/spec/Gemfile.lock +2 -2
  33. data/spec/rails3/app_root/Gemfile +2 -4
  34. data/spec/rails3/app_root/Gemfile.lock +2 -5
  35. data/spec/rails3/app_root/app/controllers/application_controller.rb +11 -11
  36. data/spec/rails3/app_root/spec/controller_oauth2_spec.rb +15 -13
  37. data/spec/rails3/app_root/spec/controller_oauth_spec.rb +18 -13
  38. data/spec/rails3/app_root/spec/controller_session_timeout_spec.rb +2 -2
  39. data/spec/rails3/app_root/spec/spec_helper.rb +1 -0
  40. data/spec/rails3/app_root/spec/user_oauth_spec.rb +4 -4
  41. data/spec/rails3/app_root/spec/user_spec.rb +2 -2
  42. data/spec/sinatra/Gemfile +12 -0
  43. data/spec/sinatra/Gemfile.lock +134 -0
  44. data/spec/sinatra/Rakefile +10 -0
  45. data/spec/sinatra/authentication.rb +3 -0
  46. data/spec/sinatra/db/migrate/activation/20101224223622_add_activation_to_users.rb +17 -0
  47. data/spec/sinatra/db/migrate/activity_logging/20101224223624_add_activity_logging_to_users.rb +17 -0
  48. data/spec/sinatra/db/migrate/brute_force_protection/20101224223626_add_brute_force_protection_to_users.rb +11 -0
  49. data/spec/sinatra/db/migrate/core/20101224223620_create_users.rb +16 -0
  50. data/spec/sinatra/db/migrate/external/20101224223628_create_authentications.rb +14 -0
  51. data/spec/sinatra/db/migrate/remember_me/20101224223623_add_remember_me_token_to_users.rb +15 -0
  52. data/spec/sinatra/db/migrate/reset_password/20101224223622_add_reset_password_to_users.rb +13 -0
  53. data/spec/sinatra/filters.rb +21 -0
  54. data/spec/sinatra/myapp.rb +133 -0
  55. data/spec/sinatra/sorcery_mailer.rb +25 -0
  56. data/spec/sinatra/spec/controller_activity_logging_spec.rb +85 -0
  57. data/spec/sinatra/spec/controller_brute_force_protection_spec.rb +69 -0
  58. data/spec/sinatra/spec/controller_http_basic_auth_spec.rb +53 -0
  59. data/spec/sinatra/spec/controller_oauth2_spec.rb +119 -0
  60. data/spec/sinatra/spec/controller_oauth_spec.rb +121 -0
  61. data/spec/sinatra/spec/controller_remember_me_spec.rb +64 -0
  62. data/spec/sinatra/spec/controller_session_timeout_spec.rb +52 -0
  63. data/spec/sinatra/spec/controller_spec.rb +120 -0
  64. data/spec/sinatra/spec/spec.opts +4 -0
  65. data/spec/sinatra/spec/spec_helper.rb +44 -0
  66. data/spec/sinatra/spec/user_activation_spec.rb +188 -0
  67. data/spec/sinatra/spec/user_activity_logging_spec.rb +36 -0
  68. data/spec/sinatra/spec/user_brute_force_protection_spec.rb +76 -0
  69. data/spec/sinatra/spec/user_oauth_spec.rb +39 -0
  70. data/spec/sinatra/spec/user_remember_me_spec.rb +66 -0
  71. data/spec/sinatra/spec/user_reset_password_spec.rb +178 -0
  72. data/spec/sinatra/spec/user_spec.rb +317 -0
  73. data/spec/sinatra/user.rb +6 -0
  74. data/spec/sinatra/views/test_login.erb +4 -0
  75. data/spec/untitled folder +18 -0
  76. metadata +85 -9
  77. data/lib/sorcery/controller/submodules/oauth/oauth1.rb +0 -26
  78. data/lib/sorcery/controller/submodules/oauth/oauth2.rb +0 -24
  79. /data/spec/rails3/app_root/db/migrate/{oauth → external}/20101224223628_create_authentications.rb +0 -0
@@ -0,0 +1,119 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ def stub_all_oauth2_requests!
4
+ @client = OAuth2::Client.new("key","secret", :site => "http://myapi.com")
5
+ OAuth2::Client.stub!(:new).and_return(@client)
6
+ @acc_token = OAuth2::AccessToken.new(@client, "", "asd", nil, {})
7
+ @webby = @client.web_server
8
+ OAuth2::Strategy::WebServer.stub!(:new).and_return(@webby)
9
+ @webby.stub!(:get_access_token).and_return(@acc_token)
10
+ @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)
11
+ end
12
+
13
+ describe 'MyApp' do
14
+ before(:all) do
15
+ ActiveRecord::Migrator.migrate("#{APP_ROOT}/db/migrate/external")
16
+ sorcery_reload!([:external])
17
+ sorcery_controller_property_set(:external_providers, [:facebook])
18
+ sorcery_controller_oauth_property_set(:facebook, :key, "eYVNBjBDi33aa9GkA3w")
19
+ sorcery_controller_oauth_property_set(:facebook, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
20
+ sorcery_controller_oauth_property_set(:facebook, :callback_url, "http://blabla.com")
21
+ end
22
+
23
+ after(:all) do
24
+ ActiveRecord::Migrator.rollback("#{APP_ROOT}/db/migrate/external")
25
+ end
26
+ # ----------------- OAuth -----------------------
27
+ describe Sinatra::Application, "with OAuth features" do
28
+
29
+ before(:each) do
30
+ stub_all_oauth2_requests!
31
+ end
32
+
33
+ after(:each) do
34
+ User.delete_all
35
+ Authentication.delete_all
36
+ end
37
+
38
+ it "login_at redirects correctly" do
39
+ create_new_user
40
+ get "/login_at_test2"
41
+ last_response.should be_a_redirect
42
+ last_response.should redirect_to("http://myapi.com/oauth/authorize?client_id=key&redirect_uri=http%3A%2F%2Fblabla.com&scope=email%2Coffline_access&type=web_server")
43
+ end
44
+
45
+ it "'login_from' logins if user exists" do
46
+ sorcery_model_property_set(:authentications_class, Authentication)
47
+ create_new_external_user(:facebook)
48
+ get "/test_login_from2"
49
+ last_response.body.should == "Success!"
50
+ end
51
+
52
+ it "'login_from' fails if user doesn't exist" do
53
+ sorcery_model_property_set(:authentications_class, Authentication)
54
+ create_new_user
55
+ get "/test_login_from2"
56
+ last_response.body.should == "Failed!"
57
+ end
58
+ end
59
+
60
+ describe Sinatra::Application, "'create_from'" do
61
+ before(:each) do
62
+ stub_all_oauth2_requests!
63
+ User.delete_all
64
+ Authentication.delete_all
65
+ end
66
+
67
+ it "should create a new user" do
68
+ sorcery_model_property_set(:authentications_class, Authentication)
69
+ sorcery_controller_oauth_property_set(:facebook, :user_info_mapping, {:username => "name"})
70
+ lambda do
71
+ get "/test_create_from_provider", :provider => "facebook"
72
+ end.should change(User, :count).by(1)
73
+ User.first.username.should == "Noam Ben Ari"
74
+ end
75
+
76
+ it "should support nested attributes" do
77
+ sorcery_model_property_set(:authentications_class, Authentication)
78
+ sorcery_controller_oauth_property_set(:facebook, :user_info_mapping, {:username => "hometown/name"})
79
+ lambda do
80
+ get "/test_create_from_provider", :provider => "facebook"
81
+ end.should change(User, :count).by(1)
82
+ User.first.username.should == "Haifa, Israel"
83
+ end
84
+ end
85
+
86
+ describe Sinatra::Application, "OAuth with User Activation features" do
87
+ before(:all) do
88
+ ActiveRecord::Migrator.migrate("#{APP_ROOT}/db/migrate/activation")
89
+ sorcery_reload!([:user_activation,:external], :user_activation_mailer => ::SorceryMailer)
90
+ sorcery_controller_property_set(:external_providers, [:facebook])
91
+ sorcery_controller_oauth_property_set(:facebook, :key, "eYVNBjBDi33aa9GkA3w")
92
+ sorcery_controller_oauth_property_set(:facebook, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
93
+ sorcery_controller_oauth_property_set(:facebook, :callback_url, "http://blabla.com")
94
+ end
95
+
96
+ after(:all) do
97
+ ActiveRecord::Migrator.rollback("#{APP_ROOT}/db/migrate/activation")
98
+ end
99
+
100
+ after(:each) do
101
+ User.delete_all
102
+ end
103
+
104
+ it "should not send activation email to external users" do
105
+ old_size = ActionMailer::Base.deliveries.size
106
+ create_new_external_user(:facebook)
107
+ ActionMailer::Base.deliveries.size.should == old_size
108
+ end
109
+
110
+ it "should not send external users an activation success email" do
111
+ sorcery_model_property_set(:activation_success_email_method_name, nil)
112
+ create_new_external_user(:facebook)
113
+ old_size = ActionMailer::Base.deliveries.size
114
+ @user.activate!
115
+ ActionMailer::Base.deliveries.size.should == old_size
116
+ end
117
+ end
118
+
119
+ end
@@ -0,0 +1,121 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'ostruct'
3
+
4
+ def stub_all_oauth_requests!
5
+ @consumer = OAuth::Consumer.new("key","secret", :site => "http://myapi.com")
6
+ OAuth::Consumer.stub!(:new).and_return(@consumer)
7
+ @req_token = OAuth::RequestToken.new(@consumer) # OpenStruct.new()
8
+ @consumer.stub!(:get_request_token).and_return(@req_token)
9
+ @acc_token = OAuth::AccessToken.new(@consumer)
10
+ @req_token.stub!(:get_access_token).and_return(@acc_token)
11
+ session[:request_token] = @req_token.token
12
+ session[:request_token_secret] = @req_token.secret
13
+ OAuth::RequestToken.stub!(:new).and_return(@req_token)
14
+ response = OpenStruct.new()
15
+ response.body = {"following"=>false, "listed_count"=>0, "profile_link_color"=>"0084B4", "profile_image_url"=>"http://a1.twimg.com/profile_images/536178575/noamb_normal.jpg", "description"=>"Programmer/Heavy Metal Fan/New Father", "status"=>{"text"=>"coming soon to sorcery gem: twitter and facebook authentication support.", "truncated"=>false, "favorited"=>false, "source"=>"web", "geo"=>nil, "in_reply_to_screen_name"=>nil, "in_reply_to_user_id"=>nil, "in_reply_to_status_id_str"=>nil, "created_at"=>"Sun Mar 06 23:01:12 +0000 2011", "contributors"=>nil, "place"=>nil, "retweeted"=>false, "in_reply_to_status_id"=>nil, "in_reply_to_user_id_str"=>nil, "coordinates"=>nil, "retweet_count"=>0, "id"=>44533012284706816, "id_str"=>"44533012284706816"}, "show_all_inline_media"=>false, "geo_enabled"=>true, "profile_sidebar_border_color"=>"a8c7f7", "url"=>nil, "followers_count"=>10, "screen_name"=>"nbenari", "profile_use_background_image"=>true, "location"=>"Israel", "statuses_count"=>25, "profile_background_color"=>"022330", "lang"=>"en", "verified"=>false, "notifications"=>false, "profile_background_image_url"=>"http://a3.twimg.com/profile_background_images/104087198/04042010339.jpg", "favourites_count"=>5, "created_at"=>"Fri Nov 20 21:58:19 +0000 2009", "is_translator"=>false, "contributors_enabled"=>false, "protected"=>false, "follow_request_sent"=>false, "time_zone"=>"Greenland", "profile_text_color"=>"333333", "name"=>"Noam Ben Ari", "friends_count"=>10, "profile_sidebar_fill_color"=>"C0DFEC", "id"=>123, "id_str"=>"91434812", "profile_background_tile"=>false, "utc_offset"=>-10800}.to_json
16
+ @acc_token.stub!(:get).and_return(response)
17
+ end
18
+
19
+ describe Sinatra::Application do
20
+ before(:all) do
21
+ ActiveRecord::Migrator.migrate("#{APP_ROOT}/db/migrate/external")
22
+ sorcery_reload!([:external])
23
+ sorcery_controller_property_set(:external_providers, [:twitter])
24
+ sorcery_controller_oauth_property_set(:twitter, :key, "eYVNBjBDi33aa9GkA3w")
25
+ sorcery_controller_oauth_property_set(:twitter, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
26
+ sorcery_controller_oauth_property_set(:twitter, :callback_url, "http://blabla.com")
27
+ end
28
+
29
+ after(:all) do
30
+ ActiveRecord::Migrator.rollback("#{APP_ROOT}/db/migrate/external")
31
+ end
32
+ # ----------------- OAuth -----------------------
33
+ describe Sinatra::Application, "'login_from'" do
34
+
35
+ before(:each) do
36
+ stub_all_oauth_requests!
37
+ end
38
+
39
+ after(:each) do
40
+ User.delete_all
41
+ Authentication.delete_all
42
+ end
43
+
44
+ it "login_at_test redirects correctly" do
45
+ create_new_user
46
+ get "/login_at_test"
47
+ last_response.should be_a_redirect
48
+ last_response.should redirect_to("http://myapi.com/oauth/authorize?oauth_callback=http%3A%2F%2Fblabla.com&oauth_token=")
49
+ end
50
+
51
+ it "logins if user exists" do
52
+ sorcery_model_property_set(:authentications_class, Authentication)
53
+ create_new_external_user(:twitter)
54
+ get '/test_login_from', :oauth_verifier => "blablaRERASDFcxvSDFA"
55
+ last_response.body.should == "Success!"
56
+ end
57
+
58
+ it "'login_from' fails if user doesn't exist" do
59
+ sorcery_model_property_set(:authentications_class, Authentication)
60
+ create_new_user
61
+ get '/test_login_from', :oauth_verifier => "blablaRERASDFcxvSDFA"
62
+ last_response.body.should == "Failed!"
63
+ end
64
+ end
65
+
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_oauth_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_oauth_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
90
+ end
91
+
92
+ describe Sinatra::Application, "OAuth with User Activation features" do
93
+ before(:all) do
94
+ ActiveRecord::Migrator.migrate("#{APP_ROOT}/db/migrate/activation")
95
+ sorcery_reload!([:user_activation,:external], :user_activation_mailer => ::SorceryMailer)
96
+ end
97
+
98
+ after(:all) do
99
+ ActiveRecord::Migrator.rollback("#{APP_ROOT}/db/migrate/activation")
100
+ end
101
+
102
+ after(:each) do
103
+ User.delete_all
104
+ Authentication.delete_all
105
+ end
106
+
107
+ it "should not send activation email to external users" do
108
+ old_size = ActionMailer::Base.deliveries.size
109
+ create_new_external_user(:twitter)
110
+ ActionMailer::Base.deliveries.size.should == old_size
111
+ end
112
+
113
+ it "should not send external users an activation success email" do
114
+ sorcery_model_property_set(:activation_success_email_method_name, nil)
115
+ create_new_external_user(:twitter)
116
+ old_size = ActionMailer::Base.deliveries.size
117
+ @user.activate!
118
+ ActionMailer::Base.deliveries.size.should == old_size
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,64 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Sinatra::Application do
4
+
5
+ # ----------------- REMEMBER ME -----------------------
6
+ describe Sinatra::Application, "with remember me features" do
7
+ before(:all) do
8
+ ActiveRecord::Migrator.migrate("#{APP_ROOT}/db/migrate/remember_me")
9
+ sorcery_reload!([:remember_me])
10
+ end
11
+
12
+ before(:each) do
13
+ create_new_user
14
+ end
15
+
16
+ after(:all) do
17
+ ActiveRecord::Migrator.rollback("#{APP_ROOT}/db/migrate/remember_me")
18
+ end
19
+
20
+ after(:each) do
21
+ session = nil
22
+ clear_cookies
23
+ User.delete_all
24
+ end
25
+
26
+ it "should set cookie on remember_me!" do
27
+ post "/test_login_with_remember", :username => 'gizmo', :password => 'secret'
28
+ cookies["remember_me_token"].should == assigns[:user].remember_me_token
29
+ end
30
+
31
+ it "should clear cookie on forget_me!" do
32
+ cookies["remember_me_token"] == {:value => 'asd54234dsfsd43534', :expires => 3600}
33
+ get '/test_logout'
34
+ cookies["remember_me_token"].should == nil
35
+ end
36
+
37
+ it "login(username,password,remember_me) should login and remember" do
38
+ post '/test_login_with_remember_in_login', :username => 'gizmo', :password => 'secret', :remember => "1"
39
+ cookies["remember_me_token"].should_not be_nil
40
+ cookies["remember_me_token"].should == assigns[:user].remember_me_token
41
+ end
42
+
43
+ it "logout should also forget_me!" do
44
+ session[:user_id] = @user.id
45
+ get '/test_logout_with_remember'
46
+ cookies["remember_me_token"].should == nil
47
+ end
48
+
49
+ it "should login_from_cookie" do
50
+ post "/test_login_with_remember", :username => 'gizmo', :password => 'secret'
51
+ get_sinatra_app(subject).instance_eval do
52
+ @current_user = nil
53
+ end
54
+ session[:user_id] = nil
55
+ get '/test_login_from_cookie'
56
+ assigns[:current_user].should == @user
57
+ end
58
+
59
+ it "should not remember_me! when not asked to" do
60
+ post '/test_login', :username => 'gizmo', :password => 'secret'
61
+ cookies["remember_me_token"].should == nil
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,52 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Sinatra::Application do
4
+
5
+ # ----------------- SESSION TIMEOUT -----------------------
6
+ describe Sinatra::Application, "with session timeout features" do
7
+ before(:all) do
8
+ sorcery_reload!([:session_timeout])
9
+ sorcery_controller_property_set(:session_timeout,0.5)
10
+ create_new_user
11
+ end
12
+
13
+ it "should not reset session before session timeout" do
14
+ session[:user_id] = User.first.id
15
+ get "/test_should_be_logged_in"
16
+ last_response.should be_ok
17
+ end
18
+
19
+ it "should reset session after session timeout" do
20
+ get "/test_login", :username => 'gizmo', :password => 'secret'
21
+ session[:user_id].should_not be_nil
22
+ sleep 0.6
23
+ get "/test_should_be_logged_in"
24
+ last_response.should be_a_redirect
25
+ end
26
+
27
+ it "with 'session_timeout_from_last_action' should not logout if there was activity" do
28
+ session[:user_id] = nil
29
+ sorcery_controller_property_set(:session_timeout,2)
30
+ sorcery_controller_property_set(:session_timeout_from_last_action, true)
31
+ get "/test_login", :username => 'gizmo', :password => 'secret'
32
+ sleep 1
33
+ get "/test_should_be_logged_in"
34
+ session[:user_id].should_not be_nil
35
+ sleep 1
36
+ get "/test_should_be_logged_in"
37
+ session[:user_id].should_not be_nil
38
+ last_response.should be_ok
39
+ end
40
+
41
+ it "with 'session_timeout_from_last_action' should logout if there was no activity" do
42
+ sorcery_controller_property_set(:session_timeout,0.5)
43
+ sorcery_controller_property_set(:session_timeout_from_last_action, true)
44
+ get "/test_login", :username => 'gizmo', :password => 'secret'
45
+ sleep 0.6
46
+ get "/test_should_be_logged_in"
47
+ session[:user_id].should be_nil
48
+ last_response.should be_a_redirect
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,120 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Sinatra::Application do
4
+
5
+ # ----------------- PLUGIN CONFIGURATION -----------------------
6
+ describe "plugin configuration" do
7
+ before(:all) do
8
+ sorcery_reload!
9
+ end
10
+
11
+ after(:each) do
12
+ Sorcery::Controller::Config.reset!
13
+ sorcery_reload!
14
+ end
15
+
16
+ it "should enable configuration option 'user_class'" do
17
+ sorcery_controller_property_set(:user_class, TestUser)
18
+ Sorcery::Controller::Config.user_class.should equal(TestUser)
19
+ end
20
+
21
+ it "should enable configuration option 'not_authenticated_action'" do
22
+ sorcery_controller_property_set(:not_authenticated_action, :my_action)
23
+ Sorcery::Controller::Config.not_authenticated_action.should equal(:my_action)
24
+ end
25
+
26
+ end
27
+
28
+ # ----------------- PLUGIN ACTIVATED -----------------------
29
+ describe Sinatra::Application, "when activated with sorcery" do
30
+
31
+ before(:all) do
32
+ User.delete_all
33
+ create_new_user
34
+ end
35
+
36
+ after(:each) do
37
+ Sorcery::Controller::Config.reset!
38
+ sorcery_controller_property_set(:user_class, User)
39
+ end
40
+
41
+ it "should respond to the instance method login" do
42
+ get_sinatra_app(subject).should respond_to(:login)
43
+ end
44
+
45
+ it "should respond to the instance method logout" do
46
+ get_sinatra_app(subject).should respond_to(:logout)
47
+ end
48
+
49
+ it "should respond to the instance method logged_in?" do
50
+ get_sinatra_app(subject).should respond_to(:logged_in?)
51
+ end
52
+
53
+ it "should respond to the instance method current_user" do
54
+ get_sinatra_app(subject).should respond_to(:current_user)
55
+ end
56
+
57
+ it "login(username,password) should return the user when success and set the session with user.id" do
58
+ get "/test_login", :username => 'gizmo', :password => 'secret'
59
+ assigns[:user].should == @user
60
+ session[:user_id].should == @user.id
61
+ end
62
+
63
+ it "login(username,password) should return nil and not set the session when failure" do
64
+ get "/test_login", :username => 'gizmo', :password => 'opensesame!'
65
+ assigns[:user].should be_nil
66
+ session[:user_id].should be_nil
67
+ end
68
+
69
+ it "logout should clear the session" do
70
+ get "/test_logout"
71
+ session[:user_id].should be_nil
72
+ end
73
+
74
+ it "logged_in? should return true if logged in" do
75
+ get "/test_login", :username => 'gizmo', :password => 'secret'
76
+ assigns[:logged_in].should be_true
77
+ end
78
+
79
+ it "logged_in? should return false if not logged in" do
80
+ get "/test_login", :username => 'gizmo', :password => 'opensesame!'
81
+ assigns[:logged_in].should be_false
82
+ end
83
+
84
+ it "current_user should return the user instance if logged in" do
85
+ create_new_user
86
+ get "/test_current_user", :id => @user.id
87
+ assigns[:current_user].should == @user
88
+ end
89
+
90
+ it "current_user should return false if not logged in" do
91
+ get "/test_logout"
92
+ assigns[:current_user].should == false
93
+ end
94
+
95
+ it "should respond to 'require_login'" do
96
+ get_sinatra_app(subject).should respond_to(:require_login)
97
+ end
98
+
99
+ it "should call the configured 'not_authenticated_action' when authenticate before_filter fails" do
100
+ sorcery_controller_property_set(:not_authenticated_action, :test_not_authenticated_action)
101
+ get "/test_logout"
102
+ last_response.body.should == "test_not_authenticated_action"
103
+ end
104
+
105
+ it "require_login before_filter should save the url that the user originally wanted" do
106
+ sorcery_controller_property_set(:not_authenticated_action, :not_authenticated2)
107
+ get "/some_action"
108
+ assigns[:session][:return_to_url].should == "http://example.org/some_action"
109
+ last_response.status.should == 302
110
+ last_response.should redirect_to("http://example.org/")
111
+ end
112
+
113
+ it "on successful login the user should be redirected to the url he originally wanted" do
114
+ post "/test_return_to", :username => 'gizmo', :password => 'secret', :return_to_url => "http://example.org/blabla"
115
+ last_response.should redirect_to("http://example.org/blabla")
116
+ end
117
+
118
+ end
119
+
120
+ end
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format progress
3
+ --loadby mtime
4
+ --reverse
@@ -0,0 +1,44 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'myapp.rb')
2
+ $: << APP_ROOT # for model reloading
3
+
4
+ require 'rack/test'
5
+ require 'rspec'
6
+
7
+ # set test environment
8
+ set :environment, :test
9
+ set :run, false
10
+ set :raise_errors, true
11
+ set :logging, false
12
+
13
+ module RSpecMixinExample
14
+ include Rack::Test::Methods
15
+ def app
16
+ @app ||= Sinatra::Application
17
+ end
18
+ end
19
+
20
+ Rspec.configure do |config|
21
+ config.send(:include, RSpecMixinExample)
22
+ config.send(:include, ::Sorcery::TestHelpers)
23
+ config.send(:include, ::Sorcery::TestHelpers::Sinatra)
24
+ config.before(:suite) do
25
+ ActiveRecord::Migrator.migrate("#{APP_ROOT}/db/migrate/core")
26
+ end
27
+
28
+ config.after(:suite) do
29
+ ActiveRecord::Migrator.rollback("#{APP_ROOT}/db/migrate/core")
30
+ end
31
+
32
+ end
33
+
34
+ # needed when running individual specs
35
+ require File.join(File.dirname(__FILE__), '..','user')
36
+ require File.join(File.dirname(__FILE__), '..','authentication')
37
+
38
+ class TestUser < ActiveRecord::Base
39
+ activate_sorcery!
40
+ end
41
+
42
+ class TestMailer < ActionMailer::Base
43
+
44
+ end