sorcery 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sorcery might be problematic. Click here for more details.

Files changed (66) hide show
  1. data/README.rdoc +89 -59
  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/oauth.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.rb +8 -0
  12. data/lib/sorcery/controller/adapters/sinatra.rb +97 -0
  13. data/lib/sorcery/controller/submodules/http_basic_auth.rb +10 -6
  14. data/lib/sorcery/controller/submodules/oauth.rb +6 -3
  15. data/lib/sorcery/controller/submodules/oauth/oauth1.rb +11 -4
  16. data/lib/sorcery/controller/submodules/oauth/oauth2.rb +1 -1
  17. data/lib/sorcery/model/submodules/activity_logging.rb +1 -1
  18. data/lib/sorcery/model/submodules/brute_force_protection.rb +0 -4
  19. data/lib/sorcery/sinatra.rb +14 -0
  20. data/lib/sorcery/test_helpers.rb +8 -52
  21. data/lib/sorcery/test_helpers/rails.rb +57 -0
  22. data/lib/sorcery/test_helpers/sinatra.rb +131 -0
  23. data/sorcery.gemspec +77 -3
  24. data/spec/Gemfile +1 -1
  25. data/spec/Gemfile.lock +2 -2
  26. data/spec/rails3/app_root/Gemfile +2 -4
  27. data/spec/rails3/app_root/Gemfile.lock +2 -5
  28. data/spec/rails3/app_root/spec/controller_oauth2_spec.rb +2 -0
  29. data/spec/rails3/app_root/spec/controller_oauth_spec.rb +6 -1
  30. data/spec/rails3/app_root/spec/controller_session_timeout_spec.rb +2 -2
  31. data/spec/rails3/app_root/spec/spec_helper.rb +1 -0
  32. data/spec/sinatra/Gemfile +12 -0
  33. data/spec/sinatra/Gemfile.lock +134 -0
  34. data/spec/sinatra/Rakefile +10 -0
  35. data/spec/sinatra/authentication.rb +3 -0
  36. data/spec/sinatra/db/migrate/activation/20101224223622_add_activation_to_users.rb +17 -0
  37. data/spec/sinatra/db/migrate/activity_logging/20101224223624_add_activity_logging_to_users.rb +17 -0
  38. data/spec/sinatra/db/migrate/brute_force_protection/20101224223626_add_brute_force_protection_to_users.rb +11 -0
  39. data/spec/sinatra/db/migrate/core/20101224223620_create_users.rb +16 -0
  40. data/spec/sinatra/db/migrate/oauth/20101224223628_create_authentications.rb +14 -0
  41. data/spec/sinatra/db/migrate/remember_me/20101224223623_add_remember_me_token_to_users.rb +15 -0
  42. data/spec/sinatra/db/migrate/reset_password/20101224223622_add_reset_password_to_users.rb +13 -0
  43. data/spec/sinatra/filters.rb +21 -0
  44. data/spec/sinatra/myapp.rb +133 -0
  45. data/spec/sinatra/sorcery_mailer.rb +25 -0
  46. data/spec/sinatra/spec/controller_activity_logging_spec.rb +85 -0
  47. data/spec/sinatra/spec/controller_brute_force_protection_spec.rb +69 -0
  48. data/spec/sinatra/spec/controller_http_basic_auth_spec.rb +53 -0
  49. data/spec/sinatra/spec/controller_oauth2_spec.rb +119 -0
  50. data/spec/sinatra/spec/controller_oauth_spec.rb +121 -0
  51. data/spec/sinatra/spec/controller_remember_me_spec.rb +64 -0
  52. data/spec/sinatra/spec/controller_session_timeout_spec.rb +52 -0
  53. data/spec/sinatra/spec/controller_spec.rb +120 -0
  54. data/spec/sinatra/spec/spec.opts +4 -0
  55. data/spec/sinatra/spec/spec_helper.rb +44 -0
  56. data/spec/sinatra/spec/user_activation_spec.rb +188 -0
  57. data/spec/sinatra/spec/user_activity_logging_spec.rb +36 -0
  58. data/spec/sinatra/spec/user_brute_force_protection_spec.rb +76 -0
  59. data/spec/sinatra/spec/user_oauth_spec.rb +39 -0
  60. data/spec/sinatra/spec/user_remember_me_spec.rb +66 -0
  61. data/spec/sinatra/spec/user_reset_password_spec.rb +178 -0
  62. data/spec/sinatra/spec/user_spec.rb +317 -0
  63. data/spec/sinatra/user.rb +6 -0
  64. data/spec/sinatra/views/test_login.erb +4 -0
  65. data/spec/untitled folder +18 -0
  66. metadata +76 -2
@@ -0,0 +1,25 @@
1
+ class SorceryMailer < ActionMailer::Base
2
+
3
+ default :from => "notifications@example.com"
4
+
5
+ def activation_needed_email(user)
6
+ @user = user
7
+ @url = "http://example.com/login"
8
+ mail(:to => user.email,
9
+ :subject => "Welcome to My Awesome Site")
10
+ end
11
+
12
+ def activation_success_email(user)
13
+ @user = user
14
+ @url = "http://example.com/login"
15
+ mail(:to => user.email,
16
+ :subject => "Your account is now activated")
17
+ end
18
+
19
+ def reset_password_email(user)
20
+ @user = user
21
+ @url = "http://example.com/login"
22
+ mail(:to => user.email,
23
+ :subject => "Your password has been reset")
24
+ end
25
+ end
@@ -0,0 +1,85 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe 'MyApp' do
4
+ before(:all) do
5
+ ActiveRecord::Migrator.migrate("#{APP_ROOT}/db/migrate/activity_logging")
6
+ end
7
+
8
+ after(:all) do
9
+ ActiveRecord::Migrator.rollback("#{APP_ROOT}/db/migrate/activity_logging")
10
+ end
11
+
12
+ # ----------------- ACTIVITY LOGGING -----------------------
13
+ describe Sinatra::Application, "with activity logging features" do
14
+ before(:all) do
15
+ sorcery_reload!([:activity_logging])
16
+ clear_cookies
17
+ end
18
+
19
+ before(:each) do
20
+ create_new_user
21
+ end
22
+
23
+ after(:each) do
24
+ User.delete_all
25
+ end
26
+
27
+ it "should respond to 'current_users'" do
28
+ get_sinatra_app(subject).should respond_to(:current_users)
29
+ end
30
+
31
+ it "'current_users' should be empty when no users are logged in" do
32
+ get_sinatra_app(subject).current_users.size.should == 0
33
+ end
34
+
35
+ it "should log login time on login" do
36
+ now = Time.now.utc
37
+ get "/test_login", :username => 'gizmo', :password => 'secret'
38
+ User.first.last_login_at.should_not be_nil
39
+ User.first.last_login_at.to_s(:db).should >= now.to_s(:db)
40
+ User.first.last_login_at.to_s(:db).should <= (now+2).to_s(:db)
41
+ end
42
+
43
+ it "should log logout time on logout" do
44
+ get "/test_login", :username => 'gizmo', :password => 'secret'
45
+ now = Time.now.utc
46
+ get "/test_logout"
47
+ User.first.last_logout_at.should_not be_nil
48
+ User.first.last_logout_at.to_s(:db).should >= now.to_s(:db)
49
+ User.first.last_logout_at.to_s(:db).should <= (now+2).to_s(:db)
50
+ end
51
+
52
+ it "should log last activity time when logged in" do
53
+ get "/test_login", :username => 'gizmo', :password => 'secret'
54
+ now = Time.now.utc
55
+ get "/some_action"
56
+ User.first.last_activity_at.to_s.should >= now.to_s(:db)
57
+ User.first.last_activity_at.to_s.should <= (now+2).to_s(:db)
58
+ end
59
+
60
+ it "'current_users' should hold the user object when 1 user is logged in" do
61
+ get "/test_login", :username => 'gizmo', :password => 'secret'
62
+ get "/some_action"
63
+ get_sinatra_app(subject).current_users.size.should == 1
64
+ get_sinatra_app(subject).current_users[0].should == @user
65
+ end
66
+
67
+ it "'current_users' should show all current_users, whether they have logged out before or not." do
68
+ user1 = create_new_user({:username => 'gizmo1', :email => "bla1@bla.com", :password => 'secret1'})
69
+ get "/test_login", :username => 'gizmo1', :password => 'secret1'
70
+ get "/some_action"
71
+ clear_user_without_logout
72
+ user2 = create_new_user({:username => 'gizmo2', :email => "bla2@bla.com", :password => 'secret2'})
73
+ get "/test_login", :username => 'gizmo2', :password => 'secret2'
74
+ get "/some_action"
75
+ clear_user_without_logout
76
+ user3 = create_new_user({:username => 'gizmo3', :email => "bla3@bla.com", :password => 'secret3'})
77
+ get "/test_login", :username => 'gizmo3', :password => 'secret3'
78
+ get "/some_action"
79
+ get_sinatra_app(subject).current_users.size.should == 3
80
+ get_sinatra_app(subject).current_users[0].should == user1
81
+ get_sinatra_app(subject).current_users[1].should == user2
82
+ get_sinatra_app(subject).current_users[2].should == user3
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,69 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Sinatra::Application do
4
+ before(:all) do
5
+ ActiveRecord::Migrator.migrate("#{APP_ROOT}/db/migrate/brute_force_protection")
6
+ end
7
+
8
+ after(:all) do
9
+ ActiveRecord::Migrator.rollback("#{APP_ROOT}/db/migrate/brute_force_protection")
10
+ end
11
+
12
+ # ----------------- SESSION TIMEOUT -----------------------
13
+ describe Sinatra::Application, "with brute force protection features" do
14
+ before(:all) do
15
+ sorcery_reload!([:brute_force_protection])
16
+ end
17
+
18
+ before(:each) do
19
+ create_new_user
20
+ end
21
+
22
+ after(:each) do
23
+ Sorcery::Controller::Config.reset!
24
+ sorcery_controller_property_set(:user_class, User)
25
+ User.delete_all
26
+ end
27
+
28
+ it "should count login retries" do
29
+ 3.times {get "/test_login", :username => 'gizmo', :password => 'blabla'}
30
+ User.find_by_username('gizmo').failed_logins_count.should == 3
31
+ end
32
+
33
+ it "should reset the counter on a good login" do
34
+ sorcery_model_property_set(:consecutive_login_retries_amount_limit, 5)
35
+ 3.times {get "/test_login", :username => 'gizmo', :password => 'blabla'}
36
+ get "/test_login", :username => 'gizmo', :password => 'secret'
37
+ User.find_by_username('gizmo').failed_logins_count.should == 0
38
+ end
39
+
40
+ it "should lock user when number of retries reached the limit" do
41
+ User.find_by_username('gizmo').lock_expires_at.should be_nil
42
+ sorcery_model_property_set(:consecutive_login_retries_amount_limit, 1)
43
+ get "/test_login", :username => 'gizmo', :password => 'blabla'
44
+ User.find_by_username('gizmo').lock_expires_at.should_not be_nil
45
+ end
46
+
47
+ it "should unlock after lock time period passes" do
48
+ sorcery_model_property_set(:consecutive_login_retries_amount_limit, 2)
49
+ sorcery_model_property_set(:login_lock_time_period, 0.2)
50
+ get "/test_login", :username => 'gizmo', :password => 'blabla'
51
+ get "/test_login", :username => 'gizmo', :password => 'blabla'
52
+ User.find_by_username('gizmo').lock_expires_at.should_not be_nil
53
+ sleep 0.3
54
+ get "/test_login", :username => 'gizmo', :password => 'blabla'
55
+ User.find_by_username('gizmo').lock_expires_at.should be_nil
56
+ end
57
+
58
+ it "should not unlock if time period is 0 (permanent lock)" do
59
+ sorcery_model_property_set(:consecutive_login_retries_amount_limit, 2)
60
+ sorcery_model_property_set(:login_lock_time_period, 0)
61
+ get "/test_login", :username => 'gizmo', :password => 'blabla'
62
+ get "/test_login", :username => 'gizmo', :password => 'blabla'
63
+ unlock_date = User.find_by_username('gizmo').lock_expires_at
64
+ sleep 1
65
+ get "/test_login", :username => 'gizmo', :password => 'blabla'
66
+ User.find_by_username('gizmo').lock_expires_at.to_s.should == unlock_date.to_s
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,53 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'base64'
3
+
4
+ describe Sinatra::Application do
5
+
6
+ # ----------------- HTTP BASIC AUTH -----------------------
7
+ describe Sinatra::Application, "with http basic auth features" do
8
+ before(:all) do
9
+ sorcery_reload!([:http_basic_auth])
10
+ create_new_user
11
+ end
12
+
13
+ after(:each) do
14
+ get "/test_logout"
15
+ end
16
+
17
+ it "requests basic authentication when before_filter is used" do
18
+ session[:http_authentication_used] = nil
19
+ get "/test_http_basic_auth"
20
+ last_response.status.should == 401
21
+ session[:http_authentication_used].should == true
22
+ end
23
+
24
+ it "authenticates from http basic if credentials are sent" do
25
+ session[:http_authentication_used] = true
26
+ get "/test_http_basic_auth", {}, {"HTTP_AUTHORIZATION" => "Basic " + Base64::encode64("#{@user.username}:secret")}
27
+ last_response.should be_ok
28
+ end
29
+
30
+ it "fails authentication if credentials are wrong" do
31
+ session[:http_authentication_used] = true
32
+ get "/test_http_basic_auth", {}, {"HTTP_AUTHORIZATION" => "Basic " + Base64::encode64("#{@user.username}:wrong!")}
33
+ last_response.should redirect_to 'http://example.org/'
34
+ end
35
+
36
+ it "should allow configuration option 'controller_to_realm_map'" do
37
+ sorcery_controller_property_set(:controller_to_realm_map, {"1" => "2"})
38
+ Sorcery::Controller::Config.controller_to_realm_map.should == {"1" => "2"}
39
+ end
40
+
41
+ it "should display the correct realm name configured for the controller" do
42
+ sorcery_controller_property_set(:controller_to_realm_map, {"application" => "Salad"})
43
+ get "/test_http_basic_auth"
44
+ last_response.headers["WWW-Authenticate"].should == "Basic realm=\"Salad\""
45
+ end
46
+
47
+ it "should sign in the user's session on successful login" do
48
+ session[:http_authentication_used] = true
49
+ get "/test_http_basic_auth", {}, {"HTTP_AUTHORIZATION" => "Basic " + Base64::encode64("#{@user.username}:secret")}
50
+ session[:user_id].should == User.find_by_username(@user.username).id
51
+ end
52
+ end
53
+ end
@@ -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/oauth")
16
+ sorcery_reload!([:oauth])
17
+ sorcery_controller_property_set(:oauth_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/oauth")
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 "auth_at_provider redirects correctly" do
39
+ create_new_user
40
+ get "/auth_at_provider_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_access_token' logins if user exists" do
46
+ sorcery_model_property_set(:authentications_class, Authentication)
47
+ create_new_external_user(:facebook)
48
+ get "/test_login_from_access_token2"
49
+ last_response.body.should == "Success!"
50
+ end
51
+
52
+ it "'login_from_access_token' fails if user doesn't exist" do
53
+ sorcery_model_property_set(:authentications_class, Authentication)
54
+ create_new_user
55
+ get "/test_login_from_access_token2"
56
+ last_response.body.should == "Failed!"
57
+ end
58
+ end
59
+
60
+ describe Sinatra::Application, "'create_from_provider!'" 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,:oauth], :user_activation_mailer => ::SorceryMailer)
90
+ sorcery_controller_property_set(:oauth_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/oauth")
22
+ sorcery_reload!([:oauth])
23
+ sorcery_controller_property_set(:oauth_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/oauth")
31
+ end
32
+ # ----------------- OAuth -----------------------
33
+ describe Sinatra::Application, "'login_from_access_token'" 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 "auth_at_provider redirects correctly" do
45
+ create_new_user
46
+ get "/auth_at_provider_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_access_token', :oauth_verifier => "blablaRERASDFcxvSDFA"
55
+ last_response.body.should == "Success!"
56
+ end
57
+
58
+ it "'login_from_access_token' fails if user doesn't exist" do
59
+ sorcery_model_property_set(:authentications_class, Authentication)
60
+ create_new_user
61
+ get :test_login_from_access_token, :oauth_verifier => "blablaRERASDFcxvSDFA"
62
+ last_response.body.should == "Failed!"
63
+ end
64
+ end
65
+
66
+ describe Sinatra::Application, "'create_from_provider!'" 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,:oauth], :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