simple_auth 1.5.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -1
- data/.travis.yml +11 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +0 -2
- data/Gemfile.lock +102 -79
- data/README.md +243 -0
- data/Rakefile +15 -0
- data/gemfiles/rails_3_1.gemfile +5 -0
- data/gemfiles/rails_3_1.gemfile.lock +151 -0
- data/gemfiles/rails_3_2.gemfile +5 -0
- data/gemfiles/rails_3_2.gemfile.lock +149 -0
- data/gemfiles/rails_4_0.gemfile +4 -0
- data/gemfiles/rails_4_0.gemfile.lock +140 -0
- data/gemfiles/rails_4_1.gemfile +4 -0
- data/gemfiles/rails_4_1.gemfile.lock +146 -0
- data/lib/simple_auth.rb +1 -6
- data/lib/simple_auth/action_controller.rb +14 -10
- data/lib/simple_auth/active_record.rb +86 -0
- data/lib/simple_auth/compat.rb +2 -0
- data/lib/simple_auth/compat/active_record.rb +31 -0
- data/lib/simple_auth/compat/config.rb +17 -0
- data/lib/simple_auth/config.rb +0 -20
- data/lib/simple_auth/exceptions.rb +0 -1
- data/lib/simple_auth/railtie.rb +1 -1
- data/lib/simple_auth/rspec.rb +2 -2
- data/lib/simple_auth/session.rb +1 -1
- data/lib/simple_auth/version.rb +2 -2
- data/simple_auth.gemspec +4 -4
- data/spec/controllers/redirect_logged_user_spec.rb +16 -16
- data/spec/controllers/require_logged_user_spec.rb +34 -34
- data/spec/schema.rb +5 -1
- data/spec/simple_auth/active_record_spec.rb +104 -2
- data/spec/simple_auth/compat_spec.rb +31 -0
- data/spec/simple_auth/config_spec.rb +8 -27
- data/spec/simple_auth/helper_spec.rb +7 -7
- data/spec/simple_auth/session_spec.rb +76 -76
- data/spec/spec_helper.rb +2 -168
- data/spec/support/app/models/customer.rb +3 -0
- data/templates/initializer.rb +0 -8
- metadata +62 -33
- data/README.markdown +0 -202
- data/lib/simple_auth/orm/active_record.rb +0 -80
- data/lib/simple_auth/orm/base.rb +0 -89
- data/lib/simple_auth/orm/mongo_mapper.rb +0 -62
- data/spec/simple_auth/mongo_mapper_spec.rb +0 -10
- data/spec/support/app/models/account.rb +0 -6
@@ -8,17 +8,17 @@ describe SimpleAuth::Helper do
|
|
8
8
|
@helper.extend(ActionView::Helpers::CaptureHelper)
|
9
9
|
end
|
10
10
|
|
11
|
-
it "
|
11
|
+
it "includes module" do
|
12
12
|
ApplicationController.included_modules.include?(SimpleAuth::Helper)
|
13
13
|
end
|
14
14
|
|
15
|
-
it "
|
16
|
-
@helper.
|
17
|
-
@helper.when_logged { "logged" }.
|
15
|
+
it "renders block when user is logged" do
|
16
|
+
expect(@helper).to receive(:logged_in?).and_return(true)
|
17
|
+
expect(@helper.when_logged { "logged" }).to eq("logged")
|
18
18
|
end
|
19
19
|
|
20
|
-
it "
|
21
|
-
@helper.
|
22
|
-
@helper.when_logged { "logged" }.
|
20
|
+
it "doesn't render block when user is unlogged" do
|
21
|
+
expect(@helper).to receive(:logged_in?).and_return(false)
|
22
|
+
expect(@helper.when_logged { "logged" }).to be_nil
|
23
23
|
end
|
24
24
|
end
|
@@ -13,25 +13,25 @@ describe SimpleAuth::Session do
|
|
13
13
|
|
14
14
|
@session = Hash.new
|
15
15
|
@controller = ActionController::Base.new
|
16
|
-
@controller.
|
16
|
+
allow(@controller).to receive_messages :session => @session, :reset_session => nil
|
17
17
|
|
18
18
|
SimpleAuth::Config.controller = @controller
|
19
19
|
@user_session = SimpleAuth::Session.new(:credential => "johndoe", :password => "test")
|
20
20
|
end
|
21
21
|
|
22
|
-
it "
|
22
|
+
it "doesn't raise when trying to find a session without activating controller" do
|
23
23
|
SimpleAuth::Config.controller = nil
|
24
24
|
|
25
25
|
expect {
|
26
|
-
SimpleAuth::Session.find.
|
26
|
+
expect(SimpleAuth::Session.find).to be_nil
|
27
27
|
}.to_not raise_error
|
28
28
|
end
|
29
29
|
|
30
|
-
it "
|
30
|
+
it "returns session key" do
|
31
31
|
SimpleAuth::Session.session_key == :user_id
|
32
32
|
end
|
33
33
|
|
34
|
-
it "
|
34
|
+
it "returns record id" do
|
35
35
|
@session[:user_id] = 42
|
36
36
|
SimpleAuth::Session.record_id == 42
|
37
37
|
end
|
@@ -41,56 +41,56 @@ describe SimpleAuth::Session do
|
|
41
41
|
@user_session.save!
|
42
42
|
end
|
43
43
|
|
44
|
-
it "
|
44
|
+
it "returns existing session" do
|
45
45
|
@user_session = SimpleAuth::Session.find
|
46
|
-
@user_session.
|
47
|
-
@user_session.record.
|
46
|
+
expect(@user_session).to be_valid
|
47
|
+
expect(@user_session.record).to eq(@user)
|
48
48
|
end
|
49
49
|
|
50
|
-
it "
|
51
|
-
@user_session.
|
50
|
+
it "doesn't be new record" do
|
51
|
+
expect(@user_session).not_to be_new_record
|
52
52
|
end
|
53
53
|
|
54
|
-
it "
|
55
|
-
@
|
56
|
-
@user_session.
|
54
|
+
it "is invalid when record is not authorized" do
|
55
|
+
allow(@controller).to receive_messages :authorized? => false
|
56
|
+
expect(@user_session).not_to be_valid
|
57
57
|
end
|
58
58
|
|
59
|
-
it "
|
60
|
-
@user_session.record.
|
61
|
-
@user_session.
|
59
|
+
it "is valid when record is authorized" do
|
60
|
+
allow(@user_session.record).to receive_messages :authorized? => true
|
61
|
+
expect(@user_session).to be_valid
|
62
62
|
end
|
63
63
|
|
64
|
-
it "
|
65
|
-
@user_session.record.
|
64
|
+
it "finds record" do
|
65
|
+
expect(@user_session.record).to eq(@user)
|
66
66
|
end
|
67
67
|
|
68
|
-
it "
|
69
|
-
@user_session.save.
|
68
|
+
it "is saved" do
|
69
|
+
expect(@user_session.save).to be_truthy
|
70
70
|
end
|
71
71
|
|
72
|
-
it "
|
72
|
+
it "resets session before saving" do
|
73
73
|
@session[:session_id] = "xWA1"
|
74
74
|
@user_session.save
|
75
|
-
@session.
|
75
|
+
expect(@session).not_to have_key(:session_id)
|
76
76
|
end
|
77
77
|
|
78
|
-
it "
|
78
|
+
it "automatically saves session when calling create!" do
|
79
79
|
@user_session = SimpleAuth::Session.create!(:credential => "johndoe", :password => "test")
|
80
|
-
@user_session.
|
81
|
-
@user_session.record.
|
82
|
-
@session[:user_id].
|
80
|
+
expect(@user_session).to be_valid
|
81
|
+
expect(@user_session.record).to eq(@user)
|
82
|
+
expect(@session[:user_id]).to eq(@user.id)
|
83
83
|
end
|
84
84
|
|
85
|
-
it "
|
86
|
-
@user_session.destroy.
|
87
|
-
@user_session.record.
|
88
|
-
@session.
|
85
|
+
it "destroys session" do
|
86
|
+
expect(@user_session.destroy).to be_truthy
|
87
|
+
expect(@user_session.record).to be_nil
|
88
|
+
expect(@session).not_to have_key(:user)
|
89
89
|
end
|
90
90
|
|
91
|
-
it "
|
91
|
+
it "initializes record session" do
|
92
92
|
@user_session.save
|
93
|
-
@session[:user_id].
|
93
|
+
expect(@session[:user_id]).to eq(@user.id)
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
@@ -100,66 +100,66 @@ describe SimpleAuth::Session do
|
|
100
100
|
@user_session.save
|
101
101
|
end
|
102
102
|
|
103
|
-
it "
|
103
|
+
it "unsets previous record id when is not valid" do
|
104
104
|
@session[:user_id] = 1
|
105
|
-
@user_session.
|
106
|
-
@session.
|
105
|
+
expect(@user_session).not_to be_valid
|
106
|
+
expect(@session).not_to have_key(:user)
|
107
107
|
end
|
108
108
|
|
109
|
-
it "
|
109
|
+
it "unsets previous record id when is not saved" do
|
110
110
|
@session[:user_id] = 1
|
111
|
-
@user_session.save.
|
112
|
-
@session.
|
111
|
+
expect(@user_session.save).to be_falsey
|
112
|
+
expect(@session).not_to have_key(:user)
|
113
113
|
end
|
114
114
|
|
115
|
-
it "
|
116
|
-
SimpleAuth::Session.new.
|
117
|
-
@user_session.
|
115
|
+
it "is new record" do
|
116
|
+
expect(SimpleAuth::Session.new).to be_new_record
|
117
|
+
expect(@user_session).to be_new_record
|
118
118
|
end
|
119
119
|
|
120
|
-
it "
|
121
|
-
@user_session.errors.full_messages[0].
|
120
|
+
it "has error message" do
|
121
|
+
expect(@user_session.errors.full_messages[0]).to eq("Invalid username or password")
|
122
122
|
end
|
123
123
|
|
124
|
-
it "
|
125
|
-
@user_session.errors.on(:credential).
|
126
|
-
@user_session.errors.on(:password).
|
124
|
+
it "doesn't return error messages for attributes" do
|
125
|
+
expect(@user_session.errors.on(:credential)).to be_nil
|
126
|
+
expect(@user_session.errors.on(:password)).to be_nil
|
127
127
|
end
|
128
128
|
|
129
|
-
it "
|
130
|
-
@user_session.errors[:credential].
|
131
|
-
@user_session.errors[:password].
|
129
|
+
it "returns empty array when trying to get errors by using hash syntax" do
|
130
|
+
expect(@user_session.errors[:credential]).to be_empty
|
131
|
+
expect(@user_session.errors[:password]).to be_empty
|
132
132
|
end
|
133
133
|
|
134
|
-
it "
|
135
|
-
@user_session.errors.
|
134
|
+
it "has errors" do
|
135
|
+
expect(@user_session.errors).not_to be_empty
|
136
136
|
end
|
137
137
|
|
138
|
-
it "
|
139
|
-
SimpleAuth::Session.find.
|
138
|
+
it "doesn't find existing session" do
|
139
|
+
expect(SimpleAuth::Session.find).to be_nil
|
140
140
|
end
|
141
141
|
|
142
|
-
it "
|
143
|
-
@user_session.record.
|
142
|
+
it "doesn't find record" do
|
143
|
+
expect(@user_session.record).to be_nil
|
144
144
|
end
|
145
145
|
|
146
|
-
it "
|
147
|
-
@user_session.
|
146
|
+
it "doesn't be a valid session" do
|
147
|
+
expect(@user_session).not_to be_valid
|
148
148
|
end
|
149
149
|
|
150
|
-
it "
|
151
|
-
@session.
|
150
|
+
it "unsets record store from session" do
|
151
|
+
expect(@session).not_to have_key(:user)
|
152
152
|
end
|
153
153
|
|
154
|
-
it "
|
155
|
-
@user_session.save.
|
154
|
+
it "doesn't be saved" do
|
155
|
+
expect(@user_session.save).to be_falsey
|
156
156
|
end
|
157
157
|
|
158
|
-
it "
|
158
|
+
it "raises error with save!" do
|
159
159
|
expect { @user_session.save! }.to raise_error(SimpleAuth::NotAuthorized)
|
160
160
|
end
|
161
161
|
|
162
|
-
it "
|
162
|
+
it "raises error with create!" do
|
163
163
|
expect { SimpleAuth::Session.create!({}) }.to raise_error(SimpleAuth::NotAuthorized)
|
164
164
|
end
|
165
165
|
end
|
@@ -169,27 +169,27 @@ describe SimpleAuth::Session do
|
|
169
169
|
@user_session.save!
|
170
170
|
end
|
171
171
|
|
172
|
-
it "
|
172
|
+
it "keeps return to url" do
|
173
173
|
@session[:return_to] = "/some/path"
|
174
174
|
@user_session.destroy
|
175
|
-
@session[:return_to].
|
175
|
+
expect(@session[:return_to]).to eq("/some/path")
|
176
176
|
end
|
177
177
|
|
178
|
-
it "
|
178
|
+
it "removes record session" do
|
179
179
|
@user_session.destroy
|
180
|
-
@session.
|
180
|
+
expect(@session).not_to have_key(:user_id)
|
181
181
|
end
|
182
182
|
|
183
|
-
it "
|
183
|
+
it "keeps keys composed by user_*" do
|
184
184
|
SimpleAuth::Config.wipeout_session = false
|
185
185
|
|
186
186
|
@session[:user_friends_count] = 42
|
187
187
|
@user_session.destroy
|
188
188
|
|
189
|
-
@session[:user_friends_count].
|
189
|
+
expect(@session[:user_friends_count]).to eq(42)
|
190
190
|
end
|
191
191
|
|
192
|
-
it "
|
192
|
+
it "erases keys composed by user_*" do
|
193
193
|
SimpleAuth::Config.wipeout_session = true
|
194
194
|
|
195
195
|
@session[:user_friends_count] = 100
|
@@ -197,16 +197,16 @@ describe SimpleAuth::Session do
|
|
197
197
|
|
198
198
|
@user_session.destroy
|
199
199
|
|
200
|
-
@session.
|
201
|
-
@session.
|
200
|
+
expect(@session).not_to have_key(:user_friends_count)
|
201
|
+
expect(@session).not_to have_key(:user_preferred_number)
|
202
202
|
end
|
203
203
|
|
204
|
-
it "
|
204
|
+
it "unsets current_user instance variable" do
|
205
205
|
@user_session.destroy
|
206
206
|
|
207
|
-
SimpleAuth::Config.controller.send(:current_user).
|
208
|
-
SimpleAuth::Config.controller.instance_variable_get("@current_user").
|
209
|
-
SimpleAuth::Config.controller.instance_variable_get("@current_session").
|
207
|
+
expect(SimpleAuth::Config.controller.send(:current_user)).to be_nil
|
208
|
+
expect(SimpleAuth::Config.controller.instance_variable_get("@current_user")).to be_nil
|
209
|
+
expect(SimpleAuth::Config.controller.instance_variable_get("@current_session")).to be_nil
|
210
210
|
end
|
211
211
|
end
|
212
212
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,187 +1,21 @@
|
|
1
1
|
ENV["RAILS_ENV"] = "test"
|
2
|
-
require "bundler"
|
3
|
-
Bundler.setup(:default, :development, :test)
|
2
|
+
require "bundler/setup"
|
4
3
|
Bundler.require
|
5
4
|
|
6
5
|
I18n.load_path += Dir[File.expand_path("../../locales/*.yml", __FILE__)]
|
6
|
+
I18n.enforce_available_locales = false
|
7
7
|
|
8
8
|
require "rails"
|
9
9
|
require "simple_auth"
|
10
10
|
require File.dirname(__FILE__) + "/support/config/boot"
|
11
11
|
require "rspec/rails"
|
12
|
-
require "mongo_mapper"
|
13
12
|
|
14
13
|
# Load database schema
|
15
14
|
load File.dirname(__FILE__) + "/schema.rb"
|
16
15
|
|
17
|
-
# Set up MongoDB connection
|
18
|
-
MongoMapper.connection = Mongo::Connection.new("localhost")
|
19
|
-
MongoMapper.database = "simple_auth"
|
20
|
-
|
21
16
|
# Restore default configuration
|
22
17
|
RSpec.configure do |config|
|
23
18
|
config.before :each do
|
24
19
|
load File.dirname(__FILE__) + "/../lib/simple_auth/config.rb"
|
25
20
|
end
|
26
21
|
end
|
27
|
-
|
28
|
-
shared_examples_for "orm" do
|
29
|
-
before do
|
30
|
-
SimpleAuth::Config.model = model_name
|
31
|
-
end
|
32
|
-
|
33
|
-
context "configuration" do
|
34
|
-
it "should set credentials" do
|
35
|
-
model.authentication do |config|
|
36
|
-
config.credentials = ["uid"]
|
37
|
-
end
|
38
|
-
|
39
|
-
SimpleAuth::Config.credentials.should == ["uid"]
|
40
|
-
end
|
41
|
-
|
42
|
-
it "should automatically set model" do
|
43
|
-
model.authentication do |config|
|
44
|
-
config.model = nil
|
45
|
-
end
|
46
|
-
|
47
|
-
SimpleAuth::Config.model.should == model_name
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
context "new record" do
|
52
|
-
before do
|
53
|
-
subject.should_not be_valid
|
54
|
-
end
|
55
|
-
|
56
|
-
it "should require password" do
|
57
|
-
subject.errors[:password].should_not be_empty
|
58
|
-
end
|
59
|
-
|
60
|
-
it "should require password to be at least 4-chars long" do
|
61
|
-
subject.password = "123"
|
62
|
-
subject.should_not be_valid
|
63
|
-
subject.errors[:password].should_not be_empty
|
64
|
-
end
|
65
|
-
|
66
|
-
it "should require password confirmation not to be empty" do
|
67
|
-
subject.password_confirmation = ""
|
68
|
-
subject.errors[:password_confirmation].should_not be_empty
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should require password confirmation not to be nil" do
|
72
|
-
subject.password_confirmation = nil
|
73
|
-
subject.errors[:password_confirmation].should_not be_empty
|
74
|
-
end
|
75
|
-
|
76
|
-
it "should unset password after saving" do
|
77
|
-
subject = model.new(:password => "test", :password_confirmation => "test")
|
78
|
-
subject.save
|
79
|
-
subject.password.should be_nil
|
80
|
-
subject.password_confirmation.should be_nil
|
81
|
-
end
|
82
|
-
|
83
|
-
it "should mark password as changed" do
|
84
|
-
subject = model.new(:password => "test")
|
85
|
-
subject.password_changed?.should be_true
|
86
|
-
end
|
87
|
-
|
88
|
-
it "should not mark password as changed" do
|
89
|
-
subject = model.new
|
90
|
-
subject.password_changed?.should be_false
|
91
|
-
end
|
92
|
-
|
93
|
-
it "should mark password as unchanged after saving" do
|
94
|
-
subject = model.new(:password => "test", :password_confirmation => "test")
|
95
|
-
subject.save
|
96
|
-
subject.password_changed?.should be_false
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
context "existing record" do
|
101
|
-
before do
|
102
|
-
model.delete_all
|
103
|
-
model.create(
|
104
|
-
:email => "john@doe.com",
|
105
|
-
:login => "johndoe",
|
106
|
-
:password => "test",
|
107
|
-
:password_confirmation => "test",
|
108
|
-
:username => "john"
|
109
|
-
)
|
110
|
-
end
|
111
|
-
|
112
|
-
subject { model.first }
|
113
|
-
|
114
|
-
it "should not require password when it hasn't changed" do
|
115
|
-
subject.login = "john"
|
116
|
-
subject.should be_valid
|
117
|
-
end
|
118
|
-
|
119
|
-
it "should require password when explicitly said so" do
|
120
|
-
subject.require_password!
|
121
|
-
subject.should_not be_valid
|
122
|
-
subject.errors[:password].should_not be_empty
|
123
|
-
end
|
124
|
-
|
125
|
-
it "should require password" do
|
126
|
-
subject.require_password?.should be_false
|
127
|
-
subject.require_password!
|
128
|
-
subject.require_password?.should be_true
|
129
|
-
end
|
130
|
-
|
131
|
-
it "should not require password after saving" do
|
132
|
-
subject.require_password!
|
133
|
-
subject.password = "newpass"
|
134
|
-
subject.password_confirmation = "newpass"
|
135
|
-
subject.save.should be_true
|
136
|
-
subject.require_password?.should be_false
|
137
|
-
end
|
138
|
-
|
139
|
-
it "should require password confirmation when it has changed" do
|
140
|
-
subject.password = "newpass"
|
141
|
-
subject.should_not be_valid
|
142
|
-
subject.errors[:password_confirmation].should_not be_empty
|
143
|
-
end
|
144
|
-
|
145
|
-
it "should require password when it has changed to blank" do
|
146
|
-
subject.password = nil
|
147
|
-
subject.should_not be_valid
|
148
|
-
subject.errors[:password].should_not be_empty
|
149
|
-
end
|
150
|
-
|
151
|
-
it "should authenticate using email" do
|
152
|
-
model.authenticate("john@doe.com", "test").should == subject
|
153
|
-
end
|
154
|
-
|
155
|
-
it "should authenticate using login" do
|
156
|
-
model.authenticate("johndoe", "test").should == subject
|
157
|
-
end
|
158
|
-
|
159
|
-
it "should authenticate using custom attribute" do
|
160
|
-
SimpleAuth::Config.credentials = [:username]
|
161
|
-
model.authenticate("john", "test").should == subject
|
162
|
-
end
|
163
|
-
|
164
|
-
it "should not authenticate using invalid credential" do
|
165
|
-
model.authenticate("invalid", "test").should be_nil
|
166
|
-
end
|
167
|
-
|
168
|
-
it "should not authenticate using wrong password" do
|
169
|
-
model.authenticate("johndoe", "invalid").should be_nil
|
170
|
-
end
|
171
|
-
|
172
|
-
it "should return nil when no user has been found" do
|
173
|
-
model.find_by_credential("invalid").should be_nil
|
174
|
-
end
|
175
|
-
|
176
|
-
it "should raise error when no user has been found" do
|
177
|
-
expect {
|
178
|
-
model.find_by_credential!("invalid")
|
179
|
-
}.to raise_error(SimpleAuth::RecordNotFound)
|
180
|
-
end
|
181
|
-
|
182
|
-
it "should return user" do
|
183
|
-
model.find_by_credential(subject.email).should == subject
|
184
|
-
model.find_by_credential!(subject.email).should == subject
|
185
|
-
end
|
186
|
-
end
|
187
|
-
end
|