sorcery 0.8.5 → 0.8.6
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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +60 -4
- data/CHANGELOG.md +15 -1
- data/Gemfile +9 -18
- data/Gemfile.rails4 +8 -10
- data/README.md +31 -11
- data/VERSION +1 -1
- data/gemfiles/active_record-rails41.gemfile +6 -0
- data/gemfiles/mongo_mapper-rails41.gemfile +8 -0
- data/gemfiles/mongoid-rails41.gemfile +11 -0
- data/lib/sorcery.rb +20 -28
- data/lib/sorcery/controller.rb +6 -11
- data/lib/sorcery/controller/submodules/external.rb +30 -15
- data/lib/sorcery/controller/submodules/session_timeout.rb +1 -1
- data/lib/sorcery/model.rb +102 -70
- data/lib/sorcery/model/adapters/active_record.rb +7 -2
- data/lib/sorcery/model/adapters/datamapper.rb +123 -0
- data/lib/sorcery/model/adapters/mongo_mapper.rb +8 -4
- data/lib/sorcery/model/adapters/mongoid.rb +6 -6
- data/lib/sorcery/model/submodules/activity_logging.rb +24 -0
- data/lib/sorcery/model/submodules/brute_force_protection.rb +16 -0
- data/lib/sorcery/model/submodules/remember_me.rb +19 -4
- data/lib/sorcery/model/submodules/reset_password.rb +30 -13
- data/lib/sorcery/model/submodules/user_activation.rb +53 -22
- data/lib/sorcery/{controller/submodules/external/protocols → protocols}/certs/ca-bundle.crt +0 -0
- data/lib/sorcery/protocols/oauth.rb +42 -0
- data/lib/sorcery/protocols/oauth2.rb +47 -0
- data/lib/sorcery/providers/base.rb +27 -0
- data/lib/sorcery/providers/facebook.rb +63 -0
- data/lib/sorcery/providers/github.rb +51 -0
- data/lib/sorcery/providers/google.rb +51 -0
- data/lib/sorcery/providers/linkedin.rb +66 -0
- data/lib/sorcery/providers/liveid.rb +53 -0
- data/lib/sorcery/providers/twitter.rb +59 -0
- data/lib/sorcery/providers/vk.rb +61 -0
- data/lib/sorcery/providers/xing.rb +64 -0
- data/lib/sorcery/test_helpers/internal.rb +3 -3
- data/lib/sorcery/test_helpers/internal/rails.rb +14 -3
- data/lib/sorcery/test_helpers/rails.rb +1 -10
- data/lib/sorcery/test_helpers/rails/controller.rb +17 -0
- data/lib/sorcery/test_helpers/rails/integration.rb +26 -0
- data/sorcery.gemspec +14 -18
- data/spec/active_record/controller_activity_logging_spec.rb +5 -116
- data/spec/active_record/controller_brute_force_protection_spec.rb +69 -47
- data/spec/active_record/controller_http_basic_auth_spec.rb +24 -18
- data/spec/active_record/controller_oauth2_spec.rb +112 -187
- data/spec/active_record/controller_oauth_spec.rb +41 -37
- data/spec/active_record/controller_remember_me_spec.rb +39 -38
- data/spec/active_record/controller_session_timeout_spec.rb +31 -16
- data/spec/active_record/controller_spec.rb +4 -178
- data/spec/active_record/integration_spec.rb +1 -1
- data/spec/active_record/user_activation_spec.rb +1 -1
- data/spec/active_record/user_activity_logging_spec.rb +1 -1
- data/spec/active_record/user_brute_force_protection_spec.rb +1 -1
- data/spec/active_record/user_oauth_spec.rb +1 -1
- data/spec/active_record/user_remember_me_spec.rb +1 -1
- data/spec/active_record/user_reset_password_spec.rb +1 -1
- data/spec/active_record/user_spec.rb +7 -8
- data/spec/datamapper/controller_activity_logging_spec.rb +17 -0
- data/spec/datamapper/controller_spec.rb +8 -0
- data/spec/datamapper/user_activation_spec.rb +10 -0
- data/spec/datamapper/user_activity_logging_spec.rb +9 -0
- data/spec/datamapper/user_brute_force_protection_spec.rb +9 -0
- data/spec/datamapper/user_oauth_spec.rb +9 -0
- data/spec/datamapper/user_remember_me_spec.rb +8 -0
- data/spec/datamapper/user_reset_password_spec.rb +8 -0
- data/spec/datamapper/user_spec.rb +27 -0
- data/spec/mongo_mapper/controller_spec.rb +4 -171
- data/spec/mongo_mapper/user_activation_spec.rb +1 -2
- data/spec/mongo_mapper/user_activity_logging_spec.rb +1 -1
- data/spec/mongo_mapper/user_brute_force_protection_spec.rb +1 -1
- data/spec/mongo_mapper/user_oauth_spec.rb +1 -1
- data/spec/mongo_mapper/user_remember_me_spec.rb +1 -1
- data/spec/mongo_mapper/user_reset_password_spec.rb +1 -1
- data/spec/mongo_mapper/user_spec.rb +7 -8
- data/spec/mongoid/controller_activity_logging_spec.rb +4 -99
- data/spec/mongoid/controller_spec.rb +4 -182
- data/spec/mongoid/user_activation_spec.rb +1 -2
- data/spec/mongoid/user_activity_logging_spec.rb +1 -2
- data/spec/mongoid/user_brute_force_protection_spec.rb +1 -2
- data/spec/mongoid/user_oauth_spec.rb +1 -2
- data/spec/mongoid/user_remember_me_spec.rb +1 -2
- data/spec/mongoid/user_reset_password_spec.rb +1 -2
- data/spec/mongoid/user_spec.rb +8 -9
- data/spec/orm/active_record.rb +2 -0
- data/spec/orm/datamapper.rb +34 -0
- data/spec/orm/mongo_mapper.rb +1 -0
- data/spec/orm/mongoid.rb +1 -0
- data/spec/rails_app/app/controllers/sorcery_controller.rb +64 -59
- data/spec/rails_app/app/datamapper/authentication.rb +8 -0
- data/spec/rails_app/app/datamapper/user.rb +7 -0
- data/spec/rails_app/config/routes.rb +18 -13
- data/spec/shared_examples/controller_activity_logging_shared_examples.rb +125 -0
- data/spec/shared_examples/controller_oauth2_shared_examples.rb +32 -36
- data/spec/shared_examples/controller_oauth_shared_examples.rb +19 -26
- data/spec/shared_examples/controller_shared_examples.rb +203 -0
- data/spec/shared_examples/user_activation_shared_examples.rb +107 -90
- data/spec/shared_examples/user_activity_logging_shared_examples.rb +10 -10
- data/spec/shared_examples/user_brute_force_protection_shared_examples.rb +14 -13
- data/spec/shared_examples/user_oauth_shared_examples.rb +23 -15
- data/spec/shared_examples/user_remember_me_shared_examples.rb +32 -23
- data/spec/shared_examples/user_reset_password_shared_examples.rb +136 -115
- data/spec/shared_examples/user_shared_examples.rb +206 -146
- data/spec/sorcery_crypto_providers_spec.rb +28 -28
- data/spec/spec_helper.rb +15 -6
- metadata +83 -127
- data/lib/sorcery/controller/submodules/external/protocols/oauth1.rb +0 -46
- data/lib/sorcery/controller/submodules/external/protocols/oauth2.rb +0 -50
- data/lib/sorcery/controller/submodules/external/providers/base.rb +0 -21
- data/lib/sorcery/controller/submodules/external/providers/facebook.rb +0 -99
- data/lib/sorcery/controller/submodules/external/providers/github.rb +0 -93
- data/lib/sorcery/controller/submodules/external/providers/google.rb +0 -92
- data/lib/sorcery/controller/submodules/external/providers/linkedin.rb +0 -103
- data/lib/sorcery/controller/submodules/external/providers/liveid.rb +0 -93
- data/lib/sorcery/controller/submodules/external/providers/twitter.rb +0 -94
- data/lib/sorcery/controller/submodules/external/providers/vk.rb +0 -101
- data/lib/sorcery/controller/submodules/external/providers/xing.rb +0 -98
- data/lib/sorcery/test_helpers.rb +0 -5
@@ -1,56 +1,52 @@
|
|
1
|
-
shared_examples_for
|
2
|
-
describe
|
1
|
+
shared_examples_for 'oauth2_controller' do
|
2
|
+
describe 'using create_from' do
|
3
3
|
before(:each) do
|
4
4
|
stub_all_oauth2_requests!
|
5
5
|
User.delete_all
|
6
6
|
Authentication.delete_all
|
7
7
|
end
|
8
8
|
|
9
|
-
it
|
9
|
+
it 'creates a new user' do
|
10
10
|
sorcery_model_property_set(:authentications_class, Authentication)
|
11
|
-
sorcery_controller_external_property_set(:facebook, :user_info_mapping, {:
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
User.first.username.should == "Noam Ben Ari"
|
11
|
+
sorcery_controller_external_property_set(:facebook, :user_info_mapping, { username: 'name' })
|
12
|
+
|
13
|
+
expect { get :test_create_from_provider, provider: 'facebook' }.to change { User.count }.by 1
|
14
|
+
expect(User.first.username).to eq 'Noam Ben Ari'
|
16
15
|
end
|
17
16
|
|
18
|
-
it
|
17
|
+
it 'supports nested attributes' do
|
19
18
|
sorcery_model_property_set(:authentications_class, Authentication)
|
20
|
-
sorcery_controller_external_property_set(:facebook, :user_info_mapping, {:
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
User.first.username.should == "Haifa, Israel"
|
19
|
+
sorcery_controller_external_property_set(:facebook, :user_info_mapping, { username: 'hometown/name' })
|
20
|
+
|
21
|
+
expect { get :test_create_from_provider, provider: 'facebook' }.to change { User.count }.by(1)
|
22
|
+
expect(User.first.username).to eq 'Haifa, Israel'
|
25
23
|
end
|
26
24
|
|
27
|
-
it
|
25
|
+
it 'does not crash on missing nested attributes' do
|
28
26
|
sorcery_model_property_set(:authentications_class, Authentication)
|
29
|
-
sorcery_controller_external_property_set(:facebook, :user_info_mapping, {:
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
User.first.
|
34
|
-
User.first.created_at.should_not be_nil
|
27
|
+
sorcery_controller_external_property_set(:facebook, :user_info_mapping, { username: 'name', created_at: 'does/not/exist' })
|
28
|
+
|
29
|
+
expect { get :test_create_from_provider, provider: 'facebook' }.to change { User.count }.by 1
|
30
|
+
expect(User.first.username).to eq 'Noam Ben Ari'
|
31
|
+
expect(User.first.created_at).not_to be_nil
|
35
32
|
end
|
36
|
-
|
37
|
-
describe
|
38
|
-
|
33
|
+
|
34
|
+
describe 'with a block' do
|
35
|
+
|
39
36
|
before(:each) do
|
40
|
-
user = User.new(:
|
41
|
-
user.save!(:
|
42
|
-
user.authentications.create(:
|
37
|
+
user = User.new(username: 'Noam Ben Ari')
|
38
|
+
user.save!(validate: false)
|
39
|
+
user.authentications.create(provider: 'twitter', uid: '456')
|
43
40
|
end
|
44
|
-
|
45
|
-
it
|
41
|
+
|
42
|
+
it 'does not create user' do
|
46
43
|
sorcery_model_property_set(:authentications_class, Authentication)
|
47
|
-
sorcery_controller_external_property_set(:facebook, :user_info_mapping, {:
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
end.should_not change(User, :count)
|
44
|
+
sorcery_controller_external_property_set(:facebook, :user_info_mapping, { username: 'name' })
|
45
|
+
|
46
|
+
# test_create_from_provider_with_block in controller will check for uniqueness of username
|
47
|
+
expect { get :test_create_from_provider_with_block, provider: 'facebook' }.not_to change { User.count }
|
52
48
|
end
|
53
|
-
|
49
|
+
|
54
50
|
end
|
55
51
|
end
|
56
|
-
end
|
52
|
+
end
|
@@ -6,32 +6,29 @@ shared_examples_for "oauth_controller" do
|
|
6
6
|
Authentication.delete_all
|
7
7
|
end
|
8
8
|
|
9
|
-
it "
|
9
|
+
it "creates a new user" do
|
10
10
|
sorcery_model_property_set(:authentications_class, Authentication)
|
11
11
|
sorcery_controller_external_property_set(:twitter, :user_info_mapping, {:username => "screen_name"})
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
User.first.username.should == "nbenari"
|
12
|
+
|
13
|
+
expect { get :test_create_from_provider, :provider => "twitter" }.to change { User.count }.by 1
|
14
|
+
expect(User.first.username).to eq "nbenari"
|
16
15
|
end
|
17
16
|
|
18
|
-
it "
|
17
|
+
it "supports nested attributes" do
|
19
18
|
sorcery_model_property_set(:authentications_class, Authentication)
|
20
19
|
sorcery_controller_external_property_set(:twitter, :user_info_mapping, {:username => "status/text"})
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
User.first.username.should == "coming soon to sorcery gem: twitter and facebook authentication support."
|
20
|
+
|
21
|
+
expect { get :test_create_from_provider, :provider => "twitter" }.to change { User.count }.by 1
|
22
|
+
expect(User.first.username).to eq "coming soon to sorcery gem: twitter and facebook authentication support."
|
25
23
|
end
|
26
24
|
|
27
|
-
it "
|
25
|
+
it "does not crash on missing nested attributes" do
|
28
26
|
sorcery_model_property_set(:authentications_class, Authentication)
|
29
27
|
sorcery_controller_external_property_set(:twitter, :user_info_mapping, {:username => "status/text", :created_at => "does/not/exist"})
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
User.first.
|
34
|
-
User.first.created_at.should_not be_nil
|
28
|
+
|
29
|
+
expect { get :test_create_from_provider, :provider => "twitter" }.to change { User.count }.by 1
|
30
|
+
expect(User.first.username).to eq "coming soon to sorcery gem: twitter and facebook authentication support."
|
31
|
+
expect(User.first.created_at).not_to be_nil
|
35
32
|
end
|
36
33
|
|
37
34
|
it "binds new provider" do
|
@@ -40,12 +37,9 @@ shared_examples_for "oauth_controller" do
|
|
40
37
|
current_user = custom_create_new_external_user(:facebook, UserProvider)
|
41
38
|
login_user(current_user)
|
42
39
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
UserProvider.where(:user_id => current_user.id).should have(2).items
|
48
|
-
User.should have(1).item
|
40
|
+
expect { get :test_add_second_provider, :provider => "twitter" }.to change { UserProvider.count }.by 1
|
41
|
+
expect(UserProvider.where(:user_id => current_user.id).size).to eq 2
|
42
|
+
expect(User.count).to eq 1
|
49
43
|
end
|
50
44
|
|
51
45
|
describe "with a block" do
|
@@ -56,12 +50,11 @@ shared_examples_for "oauth_controller" do
|
|
56
50
|
user.authentications.create(:provider => 'github', :uid => '456')
|
57
51
|
end
|
58
52
|
|
59
|
-
it "
|
53
|
+
it "does not create user" do
|
60
54
|
sorcery_model_property_set(:authentications_class, Authentication)
|
61
55
|
sorcery_controller_external_property_set(:twitter, :user_info_mapping, {:username => "screen_name"})
|
62
|
-
|
63
|
-
|
64
|
-
end.should_not change(User, :count)
|
56
|
+
|
57
|
+
expect { get :test_create_from_provider_with_block, :provider => "twitter" }.not_to change { User.count }
|
65
58
|
end
|
66
59
|
|
67
60
|
end
|
@@ -0,0 +1,203 @@
|
|
1
|
+
shared_examples_for "sorcery_controller" do
|
2
|
+
|
3
|
+
describe "plugin configuration" do
|
4
|
+
before(:all) do
|
5
|
+
sorcery_reload!
|
6
|
+
end
|
7
|
+
|
8
|
+
after(:each) do
|
9
|
+
Sorcery::Controller::Config.reset!
|
10
|
+
sorcery_reload!
|
11
|
+
end
|
12
|
+
|
13
|
+
it "enables configuration option 'user_class'" do
|
14
|
+
sorcery_controller_property_set(:user_class, "TestUser")
|
15
|
+
|
16
|
+
expect(Sorcery::Controller::Config.user_class).to eq "TestUser"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "enables configuration option 'not_authenticated_action'" do
|
20
|
+
sorcery_controller_property_set(:not_authenticated_action, :my_action)
|
21
|
+
|
22
|
+
expect(Sorcery::Controller::Config.not_authenticated_action).to eq :my_action
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
# ----------------- PLUGIN ACTIVATED -----------------------
|
28
|
+
context "when activated with sorcery" do
|
29
|
+
let!(:user) { create_new_user }
|
30
|
+
|
31
|
+
before(:all) do
|
32
|
+
sorcery_reload!
|
33
|
+
User.delete_all
|
34
|
+
end
|
35
|
+
|
36
|
+
after(:each) do
|
37
|
+
Sorcery::Controller::Config.reset!
|
38
|
+
sorcery_reload!
|
39
|
+
User.delete_all
|
40
|
+
sorcery_controller_property_set(:user_class, User)
|
41
|
+
sorcery_model_property_set(:username_attribute_names, [:email])
|
42
|
+
end
|
43
|
+
|
44
|
+
specify { should respond_to(:login) }
|
45
|
+
|
46
|
+
specify { should respond_to(:logout) }
|
47
|
+
|
48
|
+
specify { should respond_to(:logged_in?) }
|
49
|
+
|
50
|
+
specify { should respond_to(:current_user) }
|
51
|
+
|
52
|
+
it "login(username,password) returns the user when success and set the session with user.id" do
|
53
|
+
get :test_login, :email => 'bla@bla.com', :password => 'secret'
|
54
|
+
|
55
|
+
expect(assigns[:user]).to eq user
|
56
|
+
expect(session[:user_id]).to eq user.id
|
57
|
+
end
|
58
|
+
|
59
|
+
it "login(email,password) returns the user when success and set the session with user.id" do
|
60
|
+
get :test_login, :email => 'bla@bla.com', :password => 'secret'
|
61
|
+
|
62
|
+
expect(assigns[:user]).to eq user
|
63
|
+
expect(session[:user_id]).to eq user.id
|
64
|
+
end
|
65
|
+
|
66
|
+
it "login(username,password) returns nil and not set the session when failure" do
|
67
|
+
get :test_login, :email => 'bla@bla.com', :password => 'opensesame!'
|
68
|
+
|
69
|
+
expect(assigns[:user]).to be_nil
|
70
|
+
expect(session[:user_id]).to be_nil
|
71
|
+
end
|
72
|
+
|
73
|
+
it "login(email,password) returns the user when success and set the session with the _csrf_token" do
|
74
|
+
get :test_login, :email => 'bla@bla.com', :password => 'secret'
|
75
|
+
|
76
|
+
expect(session[:_csrf_token]).not_to be_nil
|
77
|
+
end
|
78
|
+
|
79
|
+
it "login(username,password) returns nil and not set the session when upper case username" do
|
80
|
+
skip('DM Adapter dependant') if SORCERY_ORM == :datamapper
|
81
|
+
get :test_login, :email => 'BLA@BLA.COM', :password => 'secret'
|
82
|
+
|
83
|
+
expect(assigns[:user]).to be_nil
|
84
|
+
expect(session[:user_id]).to be_nil
|
85
|
+
end
|
86
|
+
|
87
|
+
it "login(username,password) returns the user and set the session with user.id when upper case username and config is downcase before authenticating" do
|
88
|
+
sorcery_model_property_set(:downcase_username_before_authenticating, true)
|
89
|
+
get :test_login, :email => 'BLA@BLA.COM', :password => 'secret'
|
90
|
+
|
91
|
+
expect(assigns[:user]).to eq user
|
92
|
+
expect(session[:user_id]).to eq user.id
|
93
|
+
end
|
94
|
+
|
95
|
+
it "login(username,password) returns 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
|
96
|
+
skip('DM Adapter dependant') if SORCERY_ORM == :datamapper
|
97
|
+
create_new_user({:username => "", :email => "BLA1@BLA.COM", :password => 'secret1'})
|
98
|
+
get :test_login, :email => 'bla1@bla.com', :password => 'secret1'
|
99
|
+
|
100
|
+
expect(assigns[:user]).to be_nil
|
101
|
+
expect(session[:user_id]).to be_nil
|
102
|
+
end
|
103
|
+
|
104
|
+
it "login(username,password) returns the user and set the session with user.id when user was created with upper case username and config is downcase before authenticating" do
|
105
|
+
skip('DM Adapter dependant') if SORCERY_ORM == :datamapper
|
106
|
+
sorcery_model_property_set(:downcase_username_before_authenticating, true)
|
107
|
+
new_user = create_new_user({:username => "", :email => "BLA1@BLA.COM", :password => 'secret1'})
|
108
|
+
get :test_login, :email => 'bla1@bla.com', :password => 'secret1'
|
109
|
+
|
110
|
+
expect(assigns[:user]).to eq new_user
|
111
|
+
expect(session[:user_id]).to eq new_user.id
|
112
|
+
end
|
113
|
+
|
114
|
+
it "logout clears the session" do
|
115
|
+
cookies[:remember_me_token] = nil
|
116
|
+
session[:user_id] = user.id
|
117
|
+
get :test_logout
|
118
|
+
|
119
|
+
expect(session[:user_id]).to be_nil
|
120
|
+
end
|
121
|
+
|
122
|
+
it "logged_in? returns true if logged in" do
|
123
|
+
session[:user_id] = user.id
|
124
|
+
|
125
|
+
expect(subject.logged_in?).to be true
|
126
|
+
end
|
127
|
+
|
128
|
+
it "logged_in? returns false if not logged in" do
|
129
|
+
session[:user_id] = nil
|
130
|
+
|
131
|
+
expect(subject.logged_in?).to be false
|
132
|
+
end
|
133
|
+
|
134
|
+
it "current_user returns the user instance if logged in" do
|
135
|
+
create_new_user
|
136
|
+
session[:user_id] = user.id
|
137
|
+
|
138
|
+
2.times { expect(subject.current_user).to eq user } # memoized!
|
139
|
+
end
|
140
|
+
|
141
|
+
it "current_user returns false if not logged in" do
|
142
|
+
session[:user_id] = nil
|
143
|
+
|
144
|
+
2.times { expect(subject.current_user).to be_nil } # memoized!
|
145
|
+
end
|
146
|
+
|
147
|
+
specify { should respond_to(:require_login) }
|
148
|
+
|
149
|
+
it "calls the configured 'not_authenticated_action' when authenticate before_filter fails" do
|
150
|
+
session[:user_id] = nil
|
151
|
+
sorcery_controller_property_set(:not_authenticated_action, :test_not_authenticated_action)
|
152
|
+
get :test_logout
|
153
|
+
|
154
|
+
expect(response.body).to eq "test_not_authenticated_action"
|
155
|
+
end
|
156
|
+
|
157
|
+
it "require_login before_filter saves the url that the user originally wanted" do
|
158
|
+
get :some_action
|
159
|
+
|
160
|
+
expect(session[:return_to_url]).to eq "http://test.host/some_action"
|
161
|
+
expect(response).to redirect_to("http://test.host/")
|
162
|
+
end
|
163
|
+
|
164
|
+
it "require_login before_filter does not save the url that the user originally wanted upon all non-get http methods" do
|
165
|
+
[:post, :put, :delete].each do |m|
|
166
|
+
self.send(m, :some_action)
|
167
|
+
|
168
|
+
expect(session[:return_to_url]).to be_nil
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
it "on successful login the user is redirected to the url he originally wanted" do
|
173
|
+
session[:return_to_url] = "http://test.host/some_action"
|
174
|
+
post :test_return_to, :email => 'bla@bla.com', :password => 'secret'
|
175
|
+
|
176
|
+
expect(response).to redirect_to("http://test.host/some_action")
|
177
|
+
expect(flash[:notice]).to eq "haha!"
|
178
|
+
end
|
179
|
+
|
180
|
+
|
181
|
+
# --- auto_login(user) ---
|
182
|
+
specify { should respond_to(:auto_login) }
|
183
|
+
|
184
|
+
it "auto_login(user) los in a user instance" do
|
185
|
+
session[:user_id] = nil
|
186
|
+
subject.auto_login(user)
|
187
|
+
|
188
|
+
expect(subject.logged_in?).to be true
|
189
|
+
end
|
190
|
+
|
191
|
+
it "auto_login(user) works even if current_user was already set to false" do
|
192
|
+
get :test_logout
|
193
|
+
|
194
|
+
expect(session[:user_id]).to be_nil
|
195
|
+
expect(subject.current_user).to be_nil
|
196
|
+
|
197
|
+
get :test_auto_login
|
198
|
+
|
199
|
+
expect(assigns[:result]).to eq User.first
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
shared_examples_for "rails_3_activation_model" do
|
2
|
-
|
3
|
-
|
2
|
+
let(:user) { create_new_user }
|
3
|
+
let(:new_user) { build_new_user }
|
4
|
+
|
5
|
+
context "loaded plugin configuration" do
|
4
6
|
before(:all) do
|
5
7
|
sorcery_reload!([:user_activation], :user_activation_mailer => ::SorceryMailer)
|
6
8
|
end
|
@@ -10,34 +12,40 @@ shared_examples_for "rails_3_activation_model" do
|
|
10
12
|
sorcery_reload!([:user_activation], :user_activation_mailer => ::SorceryMailer)
|
11
13
|
end
|
12
14
|
|
13
|
-
it "
|
15
|
+
it "enables configuration option 'activation_state_attribute_name'" do
|
14
16
|
sorcery_model_property_set(:activation_state_attribute_name, :status)
|
15
|
-
|
17
|
+
|
18
|
+
expect(User.sorcery_config.activation_state_attribute_name).to eq :status
|
16
19
|
end
|
17
20
|
|
18
|
-
it "
|
21
|
+
it "enables configuration option 'activation_token_attribute_name'" do
|
19
22
|
sorcery_model_property_set(:activation_token_attribute_name, :code)
|
20
|
-
|
23
|
+
|
24
|
+
expect(User.sorcery_config.activation_token_attribute_name).to eql :code
|
21
25
|
end
|
22
26
|
|
23
|
-
it "
|
27
|
+
it "enables configuration option 'user_activation_mailer'" do
|
24
28
|
sorcery_model_property_set(:user_activation_mailer, TestMailer)
|
25
|
-
|
29
|
+
|
30
|
+
expect(User.sorcery_config.user_activation_mailer).to equal(TestMailer)
|
26
31
|
end
|
27
32
|
|
28
|
-
it "
|
33
|
+
it "enables configuration option 'activation_needed_email_method_name'" do
|
29
34
|
sorcery_model_property_set(:activation_needed_email_method_name, :my_activation_email)
|
30
|
-
|
35
|
+
|
36
|
+
expect(User.sorcery_config.activation_needed_email_method_name).to eq :my_activation_email
|
31
37
|
end
|
32
38
|
|
33
|
-
it "
|
39
|
+
it "enables configuration option 'activation_success_email_method_name'" do
|
34
40
|
sorcery_model_property_set(:activation_success_email_method_name, :my_activation_email)
|
35
|
-
|
41
|
+
|
42
|
+
expect(User.sorcery_config.activation_success_email_method_name).to eq :my_activation_email
|
36
43
|
end
|
37
44
|
|
38
|
-
it "
|
45
|
+
it "enables configuration option 'activation_mailer_disabled'" do
|
39
46
|
sorcery_model_property_set(:activation_mailer_disabled, :my_activation_mailer_disabled)
|
40
|
-
|
47
|
+
|
48
|
+
expect(User.sorcery_config.activation_mailer_disabled).to eq :my_activation_mailer_disabled
|
41
49
|
end
|
42
50
|
|
43
51
|
it "if mailer is nil and mailer is enabled, throw exception!" do
|
@@ -49,83 +57,88 @@ shared_examples_for "rails_3_activation_model" do
|
|
49
57
|
end
|
50
58
|
end
|
51
59
|
|
52
|
-
|
53
|
-
|
60
|
+
|
61
|
+
context "activation process" do
|
54
62
|
before(:all) do
|
55
63
|
sorcery_reload!([:user_activation], :user_activation_mailer => ::SorceryMailer)
|
56
64
|
end
|
57
65
|
|
58
|
-
|
59
|
-
|
66
|
+
it "initializes user state to 'pending'" do
|
67
|
+
expect(user.activation_state).to eq "pending"
|
60
68
|
end
|
61
69
|
|
62
|
-
|
63
|
-
@user.activation_state.should == "pending"
|
64
|
-
end
|
70
|
+
specify { expect(user).to respond_to :activate! }
|
65
71
|
|
66
|
-
|
72
|
+
it "clears activation code and change state to 'active' on activation" do
|
73
|
+
activation_token = user.activation_token
|
74
|
+
user.activate!
|
75
|
+
user2 = User.find(user.id) # go to db to make sure it was saved and not just in memory
|
67
76
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
@user2 = User.find(@user.id) # go to db to make sure it was saved and not just in memory
|
72
|
-
@user2.activation_token.should be_nil
|
73
|
-
@user2.activation_state.should == "active"
|
74
|
-
User.find_by_activation_token(activation_token).should be_nil
|
77
|
+
expect(user2.activation_token).to be_nil
|
78
|
+
expect(user2.activation_state).to eq "active"
|
79
|
+
expect(User.find_by_activation_token activation_token).to be_nil
|
75
80
|
end
|
76
81
|
|
77
82
|
|
78
83
|
context "mailer is enabled" do
|
79
|
-
it "
|
84
|
+
it "sends the user an activation email" do
|
80
85
|
old_size = ActionMailer::Base.deliveries.size
|
81
86
|
create_new_user
|
82
|
-
|
87
|
+
|
88
|
+
expect(ActionMailer::Base.deliveries.size).to eq old_size + 1
|
83
89
|
end
|
84
90
|
|
85
|
-
it "
|
86
|
-
|
87
|
-
|
88
|
-
|
91
|
+
it "calls send_activation_needed_email! method of user" do
|
92
|
+
expect(new_user).to receive(:send_activation_needed_email!).once
|
93
|
+
|
94
|
+
new_user.sorcery_save(:raise_on_failure => true)
|
89
95
|
end
|
90
96
|
|
91
97
|
it "subsequent saves do not send activation email" do
|
98
|
+
user
|
92
99
|
old_size = ActionMailer::Base.deliveries.size
|
93
|
-
|
94
|
-
|
95
|
-
|
100
|
+
user.email = "Shauli"
|
101
|
+
user.sorcery_save(:raise_on_failure => true)
|
102
|
+
|
103
|
+
expect(ActionMailer::Base.deliveries.size).to eq old_size
|
96
104
|
end
|
97
105
|
|
98
|
-
it "
|
106
|
+
it "sends the user an activation success email on successful activation" do
|
107
|
+
user
|
99
108
|
old_size = ActionMailer::Base.deliveries.size
|
100
|
-
|
101
|
-
|
109
|
+
user.activate!
|
110
|
+
|
111
|
+
expect(ActionMailer::Base.deliveries.size).to eq old_size + 1
|
102
112
|
end
|
103
113
|
|
104
|
-
it "
|
105
|
-
|
106
|
-
|
114
|
+
it "calls send_activation_success_email! method of user on activation" do
|
115
|
+
expect(user).to receive(:send_activation_success_email!).once
|
116
|
+
|
117
|
+
user.activate!
|
107
118
|
end
|
108
119
|
|
109
120
|
it "subsequent saves do not send activation success email" do
|
110
|
-
|
121
|
+
user.activate!
|
111
122
|
old_size = ActionMailer::Base.deliveries.size
|
112
|
-
|
113
|
-
|
114
|
-
|
123
|
+
user.email = "Shauli"
|
124
|
+
user.sorcery_save(:raise_on_failure => true)
|
125
|
+
|
126
|
+
expect(ActionMailer::Base.deliveries.size).to eq old_size
|
115
127
|
end
|
116
128
|
|
117
129
|
it "activation needed email is optional" do
|
118
130
|
sorcery_model_property_set(:activation_needed_email_method_name, nil)
|
119
131
|
old_size = ActionMailer::Base.deliveries.size
|
120
|
-
|
121
|
-
ActionMailer::Base.deliveries.size.
|
132
|
+
|
133
|
+
expect(ActionMailer::Base.deliveries.size).to eq old_size
|
122
134
|
end
|
123
135
|
|
124
136
|
it "activation success email is optional" do
|
125
137
|
sorcery_model_property_set(:activation_success_email_method_name, nil)
|
126
138
|
old_size = ActionMailer::Base.deliveries.size
|
127
|
-
|
128
|
-
|
139
|
+
user.activate!
|
140
|
+
|
141
|
+
expect(ActionMailer::Base.deliveries.size).to eq old_size
|
129
142
|
end
|
130
143
|
end
|
131
144
|
|
@@ -134,52 +147,56 @@ shared_examples_for "rails_3_activation_model" do
|
|
134
147
|
sorcery_reload!([:user_activation], :activation_mailer_disabled => true, :user_activation_mailer => ::SorceryMailer)
|
135
148
|
end
|
136
149
|
|
137
|
-
it "
|
150
|
+
it "does not send the user an activation email" do
|
138
151
|
old_size = ActionMailer::Base.deliveries.size
|
139
|
-
|
140
|
-
ActionMailer::Base.deliveries.size.
|
152
|
+
|
153
|
+
expect(ActionMailer::Base.deliveries.size).to eq old_size
|
141
154
|
end
|
142
155
|
|
143
|
-
it "
|
156
|
+
it "does not call send_activation_needed_email! method of user" do
|
144
157
|
user = build_new_user
|
145
|
-
|
146
|
-
user.
|
158
|
+
|
159
|
+
expect(user).to receive(:send_activation_needed_email!).never
|
160
|
+
|
161
|
+
user.sorcery_save(:raise_on_failure => true)
|
147
162
|
end
|
148
163
|
|
149
|
-
it "
|
164
|
+
it "does not send the user an activation success email on successful activation" do
|
150
165
|
old_size = ActionMailer::Base.deliveries.size
|
151
|
-
|
152
|
-
|
166
|
+
user.activate!
|
167
|
+
|
168
|
+
expect(ActionMailer::Base.deliveries.size).to eq old_size
|
153
169
|
end
|
154
170
|
|
155
|
-
it "
|
156
|
-
|
157
|
-
|
171
|
+
it "calls send_activation_success_email! method of user on activation" do
|
172
|
+
expect(user).to receive(:send_activation_success_email!).never
|
173
|
+
|
174
|
+
user.activate!
|
158
175
|
end
|
159
176
|
end
|
160
177
|
end
|
161
178
|
|
162
|
-
describe
|
179
|
+
describe "prevent non-active login feature" do
|
163
180
|
before(:all) do
|
164
181
|
sorcery_reload!([:user_activation], :user_activation_mailer => ::SorceryMailer)
|
165
182
|
end
|
166
183
|
|
167
184
|
before(:each) do
|
168
185
|
User.delete_all
|
169
|
-
create_new_user
|
170
186
|
end
|
171
187
|
|
172
|
-
it "
|
173
|
-
User.authenticate
|
188
|
+
it "does not allow a non-active user to authenticate" do
|
189
|
+
expect(User.authenticate user.email, 'secret').to be_falsy
|
174
190
|
end
|
175
191
|
|
176
|
-
it "
|
192
|
+
it "allows a non-active user to authenticate if configured so" do
|
177
193
|
sorcery_model_property_set(:prevent_non_active_users_to_login, false)
|
178
|
-
|
194
|
+
|
195
|
+
expect(User.authenticate user.email, 'secret').to be_truthy
|
179
196
|
end
|
180
197
|
end
|
181
198
|
|
182
|
-
describe
|
199
|
+
describe "load_from_activation_token" do
|
183
200
|
before(:all) do
|
184
201
|
sorcery_reload!([:user_activation], :user_activation_mailer => ::SorceryMailer)
|
185
202
|
end
|
@@ -188,38 +205,38 @@ shared_examples_for "rails_3_activation_model" do
|
|
188
205
|
Timecop.return
|
189
206
|
end
|
190
207
|
|
191
|
-
it "load_from_activation_token
|
192
|
-
|
193
|
-
User.load_from_activation_token(@user.activation_token).should == @user
|
208
|
+
it "load_from_activation_token returns user when token is found" do
|
209
|
+
expect(User.load_from_activation_token user.activation_token).to eq user
|
194
210
|
end
|
195
211
|
|
196
|
-
it "load_from_activation_token
|
197
|
-
|
198
|
-
User.load_from_activation_token("a").should == nil
|
212
|
+
it "load_from_activation_token does NOT return user when token is NOT found" do
|
213
|
+
expect(User.load_from_activation_token "a").to be_nil
|
199
214
|
end
|
200
215
|
|
201
|
-
it "load_from_activation_token
|
216
|
+
it "load_from_activation_token returas user when token is found and not expired" do
|
202
217
|
sorcery_model_property_set(:activation_token_expiration_period, 500)
|
203
|
-
|
204
|
-
User.load_from_activation_token
|
218
|
+
|
219
|
+
expect(User.load_from_activation_token user.activation_token).to eq user
|
205
220
|
end
|
206
221
|
|
207
|
-
it "load_from_activation_token
|
222
|
+
it "load_from_activation_token does NOT return user when token is found and expired" do
|
208
223
|
sorcery_model_property_set(:activation_token_expiration_period, 0.1)
|
209
|
-
|
224
|
+
user
|
225
|
+
|
210
226
|
Timecop.travel(Time.now.in_time_zone+0.5)
|
211
|
-
|
227
|
+
|
228
|
+
expect(User.load_from_activation_token user.activation_token).to be_nil
|
212
229
|
end
|
213
230
|
|
214
|
-
it "load_from_activation_token
|
215
|
-
User.load_from_activation_token
|
216
|
-
User.load_from_activation_token
|
231
|
+
it "load_from_activation_token returns nil if token is blank" do
|
232
|
+
expect(User.load_from_activation_token nil).to be_nil
|
233
|
+
expect(User.load_from_activation_token "").to be_nil
|
217
234
|
end
|
218
235
|
|
219
|
-
it "load_from_activation_token
|
236
|
+
it "load_from_activation_token is always valid if expiration period is nil" do
|
220
237
|
sorcery_model_property_set(:activation_token_expiration_period, nil)
|
221
|
-
|
222
|
-
User.load_from_activation_token
|
238
|
+
|
239
|
+
expect(User.load_from_activation_token user.activation_token).to eq user
|
223
240
|
end
|
224
241
|
end
|
225
|
-
end
|
242
|
+
end
|