authkit 0.0.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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/FEATURES.md +73 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +168 -0
- data/Rakefile +60 -0
- data/authkit.gemspec +27 -0
- data/config/database.yml.example +19 -0
- data/lib/authkit.rb +5 -0
- data/lib/authkit/engine.rb +7 -0
- data/lib/authkit/version.rb +3 -0
- data/lib/generators/authkit/USAGE +18 -0
- data/lib/generators/authkit/install_generator.rb +113 -0
- data/lib/generators/authkit/templates/app/controllers/application_controller.rb +94 -0
- data/lib/generators/authkit/templates/app/controllers/email_confirmation_controller.rb +25 -0
- data/lib/generators/authkit/templates/app/controllers/password_change_controller.rb +29 -0
- data/lib/generators/authkit/templates/app/controllers/password_reset_controller.rb +29 -0
- data/lib/generators/authkit/templates/app/controllers/sessions_controller.rb +35 -0
- data/lib/generators/authkit/templates/app/controllers/users_controller.rb +89 -0
- data/lib/generators/authkit/templates/app/models/user.rb +170 -0
- data/lib/generators/authkit/templates/app/views/password_change/show.html.erb +16 -0
- data/lib/generators/authkit/templates/app/views/password_reset/show.html.erb +12 -0
- data/lib/generators/authkit/templates/app/views/sessions/new.html.erb +13 -0
- data/lib/generators/authkit/templates/app/views/users/edit.html.erb +58 -0
- data/lib/generators/authkit/templates/app/views/users/new.html.erb +58 -0
- data/lib/generators/authkit/templates/db/migrate/add_authkit_fields_to_users.rb +110 -0
- data/lib/generators/authkit/templates/db/migrate/create_users.rb +17 -0
- data/lib/generators/authkit/templates/lib/email_format_validator.rb +11 -0
- data/lib/generators/authkit/templates/spec/controllers/application_controller_spec.rb +188 -0
- data/lib/generators/authkit/templates/spec/controllers/email_confirmation_controller_spec.rb +80 -0
- data/lib/generators/authkit/templates/spec/controllers/password_change_controller_spec.rb +98 -0
- data/lib/generators/authkit/templates/spec/controllers/password_reset_controller_spec.rb +87 -0
- data/lib/generators/authkit/templates/spec/controllers/sessions_controller_spec.rb +111 -0
- data/lib/generators/authkit/templates/spec/controllers/users_controller_spec.rb +195 -0
- data/lib/generators/authkit/templates/spec/models/user_spec.rb +268 -0
- data/spec/spec_helper.rb +16 -0
- metadata +165 -0
@@ -0,0 +1,268 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe User do
|
4
|
+
let(:user_params) { { email: "test@example.com", username: "test", password: "example", password_confirmation: "example" } }
|
5
|
+
|
6
|
+
it "has secure password support" do
|
7
|
+
User.new.should respond_to(:authenticate)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "has one time password support" do
|
11
|
+
User.new.should respond_to(:otp_secret_key)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "accepts a password confirmation" do
|
15
|
+
User.new.should respond_to(:password_confirmation=)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "downcases the email address" do
|
19
|
+
user = User.new
|
20
|
+
user.email = "SIR@CAPSALOCK.COM"
|
21
|
+
user.valid?
|
22
|
+
user.email.should == "sir@capsalock.com"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "knows if the password was set" do
|
26
|
+
user = User.new
|
27
|
+
user.send(:password_set?).should == false
|
28
|
+
user.password = "example"
|
29
|
+
user.send(:password_set?).should == true
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "validations" do
|
33
|
+
describe "unique" do
|
34
|
+
before(:each) do
|
35
|
+
User.create!(user_params)
|
36
|
+
end
|
37
|
+
it { should validate_uniqueness_of(:username) }
|
38
|
+
it { should validate_uniqueness_of(:email) }
|
39
|
+
it "validates the uniqueness of the the confirmation email" do
|
40
|
+
user = User.new(user_params.merge(email: "old@example.com", username: "old"))
|
41
|
+
user.confirmation_email = "new@example.com"
|
42
|
+
user.should be_valid
|
43
|
+
user.confirmation_email = "test@example.com"
|
44
|
+
user.should_not be_valid
|
45
|
+
end
|
46
|
+
end
|
47
|
+
it { should validate_presence_of(:confirmation_email) }
|
48
|
+
it { should validate_presence_of(:username) }
|
49
|
+
it { should validate_presence_of(:password) }
|
50
|
+
it { should validate_confirmation_of(:password) }
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "tokens" do
|
55
|
+
it "finds a user from a token" do
|
56
|
+
verifier = ActiveSupport::MessageVerifier.new("SECRET")
|
57
|
+
token = verifier.generate(1)
|
58
|
+
user = User.new
|
59
|
+
User.should_receive(:find_by_id).with(1).and_return(user)
|
60
|
+
User.user_from_token(token).should == user
|
61
|
+
end
|
62
|
+
|
63
|
+
it "does not find a user from an invalid token" do
|
64
|
+
User.user_from_token("INVALID").should be_nil
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "for fields" do
|
68
|
+
before(:each) do
|
69
|
+
User.should_receive(:user_from_token).with("TOKEN").and_return("USER")
|
70
|
+
end
|
71
|
+
|
72
|
+
it "finds a user from the remember token" do
|
73
|
+
User.user_from_remember_token("TOKEN").should == "USER"
|
74
|
+
end
|
75
|
+
|
76
|
+
it "finds a user from the reset password token" do
|
77
|
+
User.user_from_reset_password_token("TOKEN").should == "USER"
|
78
|
+
end
|
79
|
+
|
80
|
+
it "finds a user from the confirm token" do
|
81
|
+
User.user_from_confirmation_token("TOKEN").should == "USER"
|
82
|
+
end
|
83
|
+
|
84
|
+
it "finds a user from the unlock token" do
|
85
|
+
User.user_from_unlock_token("TOKEN").should == "USER"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
it "sets a token" do
|
90
|
+
user = User.new
|
91
|
+
user.should_receive(:persisted?).and_return(true)
|
92
|
+
user.should_receive(:id).and_return(1)
|
93
|
+
user.should_receive(:save).and_return(true)
|
94
|
+
user.set_token(:remember_token)
|
95
|
+
user.remember_token.should_not be_nil
|
96
|
+
end
|
97
|
+
|
98
|
+
it "does not set a token for a new record" do
|
99
|
+
user = User.new
|
100
|
+
user.set_token(:remember_token)
|
101
|
+
user.remember_token.should be_nil
|
102
|
+
end
|
103
|
+
|
104
|
+
it "sets the created at for the token" do
|
105
|
+
Time.stub(:now).and_return(time = Time.now)
|
106
|
+
user = User.new
|
107
|
+
user.should_receive(:persisted?).and_return(true)
|
108
|
+
user.should_receive(:id).and_return(1)
|
109
|
+
user.should_receive(:save).and_return(true)
|
110
|
+
user.set_token(:remember_token)
|
111
|
+
user.remember_token_created_at.should == time
|
112
|
+
end
|
113
|
+
|
114
|
+
it "clears the remember token" do
|
115
|
+
user = User.new
|
116
|
+
user.should_receive(:save).and_return(true)
|
117
|
+
user.remember_token = "TOKEN"
|
118
|
+
user.remember_token_created_at = Time.now
|
119
|
+
user.clear_remember_token
|
120
|
+
user.remember_token.should be_nil
|
121
|
+
user.remember_token_created_at.should be_nil
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe "display name" do
|
126
|
+
it "has a display name" do
|
127
|
+
user = User.new(first_name: "Boss", last_name: "Hogg")
|
128
|
+
user.display_name.should == "Boss Hogg"
|
129
|
+
user.first_name = nil
|
130
|
+
user.display_name.should == "Hogg"
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
describe "tracking" do
|
135
|
+
let(:user) do
|
136
|
+
user = User.new
|
137
|
+
user.should_receive(:save).and_return(true)
|
138
|
+
user
|
139
|
+
end
|
140
|
+
|
141
|
+
it "tracks sign in count" do
|
142
|
+
expect {
|
143
|
+
user.track_sign_in(nil)
|
144
|
+
}.to change(user, :sign_in_count).by(1)
|
145
|
+
end
|
146
|
+
|
147
|
+
it "tracks current sign in" do
|
148
|
+
Time.stub(:now).and_return(time = Time.now)
|
149
|
+
user.track_sign_in(nil)
|
150
|
+
user.current_sign_in_at.should == time
|
151
|
+
end
|
152
|
+
|
153
|
+
it "tracks last sign in" do
|
154
|
+
time = Time.now
|
155
|
+
user.current_sign_in_at = time
|
156
|
+
user.track_sign_in(nil)
|
157
|
+
user.last_sign_in_at.should == time
|
158
|
+
end
|
159
|
+
|
160
|
+
it "tracks current and last ip" do
|
161
|
+
user.track_sign_in(ip = "123.456.789.001")
|
162
|
+
user.current_sign_in_ip.should == ip
|
163
|
+
end
|
164
|
+
|
165
|
+
it "tracks current and last ip" do
|
166
|
+
ip = "123.456.789.001"
|
167
|
+
user.current_sign_in_ip = ip
|
168
|
+
user.track_sign_in(nil)
|
169
|
+
user.last_sign_in_ip.should == ip
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
describe "emails" do
|
174
|
+
let(:user) { User.new(user_params) }
|
175
|
+
|
176
|
+
describe "with valid params" do
|
177
|
+
it "confirms the email" do
|
178
|
+
user = User.new
|
179
|
+
user.should_receive(:persisted?).and_return(true)
|
180
|
+
user.should_receive(:id).and_return(1)
|
181
|
+
user.should_receive(:save).and_return(true)
|
182
|
+
Time.stub(:now).and_return(time = Time.now)
|
183
|
+
|
184
|
+
user.send_confirmation
|
185
|
+
user.confirmation_token_created_at.should == time
|
186
|
+
user.confirmation_token.should_not be_blank
|
187
|
+
end
|
188
|
+
|
189
|
+
it "sends confirmation email instructions" do
|
190
|
+
user = User.new
|
191
|
+
user.should_receive(:persisted?).and_return(true)
|
192
|
+
user.should_receive(:id).and_return(1)
|
193
|
+
user.should_receive(:save).and_return(true)
|
194
|
+
user.send_confirmation
|
195
|
+
end
|
196
|
+
|
197
|
+
it "handles confirmed emails" do
|
198
|
+
user.should_receive(:save).and_return(true)
|
199
|
+
user.confirmation_email = "new@example.com"
|
200
|
+
user.confirmation_token = "TOKEN"
|
201
|
+
user.email_confirmed.should == true
|
202
|
+
user.confirmation_email.should == user.email
|
203
|
+
user.confirmation_token.should be_nil
|
204
|
+
user.confirmation_token_created_at.should be_nil
|
205
|
+
user.email.should == "new@example.com"
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
it "does not confirm if there is no confirmation token" do
|
210
|
+
user.confirmation_email = "new@example.com"
|
211
|
+
user.confirmation_token = nil
|
212
|
+
user.email_confirmed.should == false
|
213
|
+
end
|
214
|
+
|
215
|
+
it "does not confirm if there is no confirmation email" do
|
216
|
+
user.confirmation_email = ""
|
217
|
+
user.confirmation_token = "TOKEN"
|
218
|
+
user.email_confirmed.should == false
|
219
|
+
end
|
220
|
+
|
221
|
+
it "does not confirm emails if they are already used" do
|
222
|
+
User.create(user_params.merge(email: "new@example.com", username: "newuser"))
|
223
|
+
user.confirmation_email = "new@example.com"
|
224
|
+
user.confirmation_token = "TOKEN"
|
225
|
+
user.email_confirmed.should == false
|
226
|
+
user.should have(1).errors_on(:email)
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
describe "passwords" do
|
231
|
+
it "changes the password if it matches" do
|
232
|
+
user = User.new(user_params)
|
233
|
+
user.should_receive(:save).and_return(true)
|
234
|
+
user.change_password("password", "password")
|
235
|
+
user.password_digest.should_not be_blank
|
236
|
+
user.remember_token.should be_nil
|
237
|
+
user.remember_token_created_at.should be_nil
|
238
|
+
end
|
239
|
+
|
240
|
+
it "doesn't change the password if it doesn't match" do
|
241
|
+
user = User.new
|
242
|
+
user.remember_token = "token"
|
243
|
+
user.change_password("password", "typotypo")
|
244
|
+
user.should_not be_valid
|
245
|
+
user.remember_token.should == "token"
|
246
|
+
end
|
247
|
+
|
248
|
+
it "resets the password" do
|
249
|
+
user = User.new
|
250
|
+
user.should_receive(:persisted?).and_return(true)
|
251
|
+
user.should_receive(:id).and_return(1)
|
252
|
+
user.should_receive(:save).and_return(true)
|
253
|
+
Time.stub(:now).and_return(time = Time.now)
|
254
|
+
|
255
|
+
user.send_reset_password
|
256
|
+
user.reset_password_token_created_at.should == time
|
257
|
+
user.reset_password_token.should_not be_blank
|
258
|
+
end
|
259
|
+
|
260
|
+
it "sends reset password instructions" do
|
261
|
+
user = User.new
|
262
|
+
user.should_receive(:persisted?).and_return(true)
|
263
|
+
user.should_receive(:id).and_return(1)
|
264
|
+
user.should_receive(:save).and_return(true)
|
265
|
+
user.send_reset_password
|
266
|
+
end
|
267
|
+
end
|
268
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
ENV["RAILS_ENV"] ||= 'test'
|
2
|
+
require File.expand_path('../tmp/sample/config/environment', __FILE__)
|
3
|
+
require 'rspec/rails'
|
4
|
+
require 'rspec/autorun'
|
5
|
+
|
6
|
+
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
|
7
|
+
|
8
|
+
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.use_transactional_fixtures = true
|
12
|
+
config.infer_base_class_for_anonymous_controllers = false
|
13
|
+
config.order = "random"
|
14
|
+
# Because we are not running things in Rails we need to stub some secrets
|
15
|
+
config.before(:each) { Rails.application.config.stub(:secret_token).and_return("SECRET") }
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: authkit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeff Rafter
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec-rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: factory_girl_rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: mocha
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: active_model_otp
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Auth for your Rails application
|
98
|
+
email:
|
99
|
+
- jeffrafter@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- .gitignore
|
105
|
+
- FEATURES.md
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- authkit.gemspec
|
111
|
+
- config/database.yml.example
|
112
|
+
- lib/authkit.rb
|
113
|
+
- lib/authkit/engine.rb
|
114
|
+
- lib/authkit/version.rb
|
115
|
+
- lib/generators/authkit/USAGE
|
116
|
+
- lib/generators/authkit/install_generator.rb
|
117
|
+
- lib/generators/authkit/templates/app/controllers/application_controller.rb
|
118
|
+
- lib/generators/authkit/templates/app/controllers/email_confirmation_controller.rb
|
119
|
+
- lib/generators/authkit/templates/app/controllers/password_change_controller.rb
|
120
|
+
- lib/generators/authkit/templates/app/controllers/password_reset_controller.rb
|
121
|
+
- lib/generators/authkit/templates/app/controllers/sessions_controller.rb
|
122
|
+
- lib/generators/authkit/templates/app/controllers/users_controller.rb
|
123
|
+
- lib/generators/authkit/templates/app/models/user.rb
|
124
|
+
- lib/generators/authkit/templates/app/views/password_change/show.html.erb
|
125
|
+
- lib/generators/authkit/templates/app/views/password_reset/show.html.erb
|
126
|
+
- lib/generators/authkit/templates/app/views/sessions/new.html.erb
|
127
|
+
- lib/generators/authkit/templates/app/views/users/edit.html.erb
|
128
|
+
- lib/generators/authkit/templates/app/views/users/new.html.erb
|
129
|
+
- lib/generators/authkit/templates/db/migrate/add_authkit_fields_to_users.rb
|
130
|
+
- lib/generators/authkit/templates/db/migrate/create_users.rb
|
131
|
+
- lib/generators/authkit/templates/lib/email_format_validator.rb
|
132
|
+
- lib/generators/authkit/templates/spec/controllers/application_controller_spec.rb
|
133
|
+
- lib/generators/authkit/templates/spec/controllers/email_confirmation_controller_spec.rb
|
134
|
+
- lib/generators/authkit/templates/spec/controllers/password_change_controller_spec.rb
|
135
|
+
- lib/generators/authkit/templates/spec/controllers/password_reset_controller_spec.rb
|
136
|
+
- lib/generators/authkit/templates/spec/controllers/sessions_controller_spec.rb
|
137
|
+
- lib/generators/authkit/templates/spec/controllers/users_controller_spec.rb
|
138
|
+
- lib/generators/authkit/templates/spec/models/user_spec.rb
|
139
|
+
- spec/spec_helper.rb
|
140
|
+
homepage: https://github.com/jeffrafter/authkit
|
141
|
+
licenses:
|
142
|
+
- MIT
|
143
|
+
metadata: {}
|
144
|
+
post_install_message:
|
145
|
+
rdoc_options: []
|
146
|
+
require_paths:
|
147
|
+
- lib
|
148
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - '>='
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
154
|
+
requirements:
|
155
|
+
- - '>='
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0'
|
158
|
+
requirements: []
|
159
|
+
rubyforge_project:
|
160
|
+
rubygems_version: 2.0.3
|
161
|
+
signing_key:
|
162
|
+
specification_version: 4
|
163
|
+
summary: Auth for your Rails application
|
164
|
+
test_files:
|
165
|
+
- spec/spec_helper.rb
|