authkit 0.2.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (23) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +144 -34
  3. data/Rakefile +8 -0
  4. data/lib/authkit/version.rb +1 -1
  5. data/lib/generators/authkit/install_generator.rb +0 -3
  6. data/lib/generators/authkit/templates/app/controllers/application_controller.rb +20 -13
  7. data/lib/generators/authkit/templates/app/controllers/email_confirmation_controller.rb +21 -3
  8. data/lib/generators/authkit/templates/app/controllers/password_change_controller.rb +37 -5
  9. data/lib/generators/authkit/templates/app/controllers/sessions_controller.rb +3 -1
  10. data/lib/generators/authkit/templates/app/controllers/signup_controller.rb +3 -1
  11. data/lib/generators/authkit/templates/app/models/user.rb +48 -52
  12. data/lib/generators/authkit/templates/app/views/password_change/show.html.erb +1 -0
  13. data/lib/generators/authkit/templates/app/views/sessions/new.html.erb +4 -0
  14. data/lib/generators/authkit/templates/app/views/signup/new.html.erb +5 -1
  15. data/lib/generators/authkit/templates/app/views/users/edit.html.erb +1 -1
  16. data/lib/generators/authkit/templates/spec/controllers/application_controller_spec.rb +26 -26
  17. data/lib/generators/authkit/templates/spec/controllers/email_confirmation_controller_spec.rb +28 -10
  18. data/lib/generators/authkit/templates/spec/controllers/password_change_controller_spec.rb +71 -21
  19. data/lib/generators/authkit/templates/spec/controllers/sessions_controller_spec.rb +14 -0
  20. data/lib/generators/authkit/templates/spec/controllers/signup_controller_spec.rb +14 -0
  21. data/lib/generators/authkit/templates/spec/forms/signup_spec.rb +3 -0
  22. data/lib/generators/authkit/templates/spec/models/user_spec.rb +63 -66
  23. metadata +2 -2
@@ -43,6 +43,20 @@ describe SessionsController do
43
43
  controller.send(:current_user).should == user
44
44
  end
45
45
 
46
+ it "remembers the user if remember me is chosen" do
47
+ User.any_instance.should_receive(:set_remember_token)
48
+ controller.should_receive(:set_remember_cookie)
49
+ post :create, {email: "test@example.com", password: "example", remember_me: "1"}
50
+ controller.send(:current_user).should == user
51
+ end
52
+
53
+ it "does not remember the user if remember me is not chosen" do
54
+ User.any_instance.should_not_receive(:set_remember_token)
55
+ controller.should_not_receive(:set_remember_cookie)
56
+ post :create, {email: "test@example.com", password: "example", remember_me: ""}
57
+ controller.send(:current_user).should == user
58
+ end
59
+
46
60
  describe "from json" do
47
61
  it "returns http success" do
48
62
  post :create, {email: "test@example.com", password: "example", format: "json"}
@@ -33,6 +33,20 @@ describe SignupController do
33
33
  controller.send(:current_user).should == assigns(:signup).user
34
34
  end
35
35
 
36
+ it "remembers the user if remember me is chosen" do
37
+ User.any_instance.should_receive(:set_remember_token)
38
+ controller.should_receive(:set_remember_cookie)
39
+ post :create, {signup: signup_params, remember_me: "1"}, {}
40
+ controller.send(:current_user).should == assigns(:signup).user
41
+ end
42
+
43
+ it "does not remember the user if remember me is not chosen" do
44
+ User.any_instance.should_not_receive(:set_remember_token)
45
+ controller.should_not_receive(:set_remember_cookie)
46
+ post :create, {signup: signup_params, remember_me: ""}, {}
47
+ controller.send(:current_user).should == assigns(:signup).user
48
+ end
49
+
36
50
  it "redirects to the root" do
37
51
  post :create, {signup: signup_params}
38
52
  response.should be_redirect
@@ -38,6 +38,7 @@ describe Signup do
38
38
  signup.user = User.new
39
39
  signup.should_receive(:valid?).and_return(true)
40
40
  signup.user.should_receive(:save!)
41
+ signup.user.should_receive(:send_confirmation)
41
42
  signup.save
42
43
  end
43
44
 
@@ -45,6 +46,7 @@ describe Signup do
45
46
  signup.user = User.new
46
47
  signup.should_receive(:valid?).and_return(true)
47
48
  signup.stub(:persist!)
49
+ signup.user.should_receive(:send_confirmation)
48
50
  signup.user.should_receive(:send_welcome)
49
51
  signup.save
50
52
  end
@@ -84,6 +86,7 @@ describe Signup do
84
86
  User.should_receive(:new).and_return(user)
85
87
  user.stub(:valid?).and_return(true)
86
88
  user.should_receive(:save!)
89
+ user.should_receive(:send_confirmation)
87
90
  signup.stub(:valid?).and_return(true)
88
91
  signup.save
89
92
  end
@@ -52,72 +52,51 @@ describe User do
52
52
  end
53
53
 
54
54
  describe "tokens" do
55
- it "finds a user from a token" do
56
- user = create(:user)
57
- verifier = ActiveSupport::MessageVerifier.new(Rails.application.config.secret_key_base)
58
- token = verifier.generate(user.id)
59
- User.user_from_token(token).id.should == user.id
60
- end
61
-
62
- it "does not find a user from an invalid token" do
63
- User.user_from_token("INVALID").should be_nil
64
- end
65
-
66
- describe "for fields" do
67
- before(:each) do
68
- User.should_receive(:user_from_token).with("TOKEN").and_return("USER")
69
- end
70
-
71
- it "finds a user from the remember token" do
72
- User.user_from_remember_token("TOKEN").should == "USER"
73
- end
74
-
75
- it "finds a user from the reset password token" do
76
- User.user_from_reset_password_token("TOKEN").should == "USER"
77
- end
78
-
79
- it "finds a user from the confirm token" do
80
- User.user_from_confirmation_token("TOKEN").should == "USER"
81
- end
82
-
83
- it "finds a user from the unlock token" do
84
- User.user_from_unlock_token("TOKEN").should == "USER"
85
- end
55
+ it "sets the remember token" do
56
+ user = User.new
57
+ user.should_receive(:save!).and_return(true)
58
+ user.set_remember_token
59
+ user.remember_token.should_not be_blank
60
+ user.remember_token_created_at.should_not be_blank
86
61
  end
87
62
 
88
- it "sets a token" do
63
+ it "clears the remember token" do
89
64
  user = User.new
90
- user.should_receive(:persisted?).and_return(true)
91
- user.should_receive(:id).and_return(1)
92
- user.should_receive(:save).and_return(true)
93
- user.set_token(:remember_token)
94
- user.remember_token.should_not be_nil
65
+ user.should_receive(:save!).and_return(true)
66
+ user.remember_token = "TOKEN"
67
+ user.remember_token_created_at = Time.now
68
+ user.clear_remember_token
69
+ user.remember_token.should be_nil
70
+ user.remember_token_created_at.should be_nil
95
71
  end
72
+ end
96
73
 
97
- it "does not set a token for a new record" do
74
+ describe "token expiry" do
75
+ it "should expire reset password tokens" do
98
76
  user = User.new
99
- user.set_token(:remember_token)
100
- user.remember_token.should be_nil
77
+ user.reset_password_token_expired?.should == true
78
+ user.reset_password_token_created_at = 10.minutes.ago
79
+ user.reset_password_token_expired?.should == false
80
+ user.reset_password_token_created_at = 1.day.ago
81
+ user.reset_password_token_expired?.should == true
101
82
  end
102
83
 
103
- it "sets the created at for the token" do
104
- Time.stub(:now).and_return(time = Time.now)
84
+ it "should expire confirmation tokens" do
105
85
  user = User.new
106
- user.should_receive(:persisted?).and_return(true)
107
- user.should_receive(:id).and_return(1)
108
- user.should_receive(:save).and_return(true)
109
- user.set_token(:remember_token)
110
- user.remember_token_created_at.should == time
86
+ user.confirmation_token_expired?.should == true
87
+ user.confirmation_token_created_at = 2.days.ago
88
+ user.confirmation_token_expired?.should == false
89
+ user.confirmation_token_created_at = 3.days.ago
90
+ user.confirmation_token_expired?.should == true
111
91
  end
112
92
 
113
- it "clears the remember token" do
93
+ it "should expire remember tokens" do
114
94
  user = User.new
115
- user.should_receive(:save).and_return(true)
116
- user.remember_token = "TOKEN"
117
- user.remember_token_created_at = Time.now
118
- user.clear_remember_token
119
- user.remember_token.should be_nil
120
- user.remember_token_created_at.should be_nil
95
+ user.remember_token_expired?.should == true
96
+ user.remember_token_created_at = 30.days.ago
97
+ user.remember_token_expired?.should == false
98
+ user.remember_token_created_at = 1.years.ago
99
+ user.remember_token_expired?.should == true
121
100
  end
122
101
  end
123
102
 
@@ -175,9 +154,7 @@ describe User do
175
154
  describe "with valid params" do
176
155
  it "confirms the email" do
177
156
  user = User.new
178
- user.should_receive(:persisted?).and_return(true)
179
- user.should_receive(:id).and_return(1)
180
- user.should_receive(:save).and_return(true)
157
+ user.should_receive(:save!).and_return(true)
181
158
  Time.stub(:now).and_return(time = Time.now)
182
159
 
183
160
  user.send_confirmation
@@ -185,11 +162,17 @@ describe User do
185
162
  user.confirmation_token.should_not be_blank
186
163
  end
187
164
 
165
+ it "generates a token before it sends confirmation email instructions" do
166
+ user = User.new
167
+ user.should_receive(:save!).and_return(true)
168
+ user.send_confirmation
169
+ user.confirmation_token.should_not be_blank
170
+ user.confirmation_token_created_at.should_not be_blank
171
+ end
172
+
188
173
  it "sends confirmation email instructions" do
189
174
  user = User.new
190
- user.should_receive(:persisted?).and_return(true)
191
- user.should_receive(:id).and_return(1)
192
- user.should_receive(:save).and_return(true)
175
+ user.should_receive(:save!).and_return(true)
193
176
  user.send_confirmation
194
177
  end
195
178
 
@@ -224,6 +207,16 @@ describe User do
224
207
  user.email_confirmed.should == false
225
208
  user.should have(1).errors_on(:email)
226
209
  end
210
+
211
+ it "is pending confirmation if there is a confirmation token" do
212
+ user = build(:user, confirmation_token: "TOKEN")
213
+ user.should be_pending_confirmation
214
+ end
215
+
216
+ it "there is no pending confirmation if there is not a confirmation token" do
217
+ user = build(:user, confirmation_token: nil)
218
+ user.should_not be_pending_confirmation
219
+ end
227
220
  end
228
221
 
229
222
  describe "passwords" do
@@ -246,9 +239,7 @@ describe User do
246
239
 
247
240
  it "resets the password" do
248
241
  user = User.new
249
- user.should_receive(:persisted?).and_return(true)
250
- user.should_receive(:id).and_return(1)
251
- user.should_receive(:save).and_return(true)
242
+ user.should_receive(:save!).and_return(true)
252
243
  Time.stub(:now).and_return(time = Time.now)
253
244
 
254
245
  user.send_reset_password
@@ -256,11 +247,17 @@ describe User do
256
247
  user.reset_password_token.should_not be_blank
257
248
  end
258
249
 
250
+ it "generates a token before it sends reset password instructions" do
251
+ user = User.new
252
+ user.should_receive(:save!).and_return(true)
253
+ user.send_reset_password
254
+ user.reset_password_token.should_not be_blank
255
+ user.reset_password_token_created_at.should_not be_blank
256
+ end
257
+
259
258
  it "sends reset password instructions" do
260
259
  user = User.new
261
- user.should_receive(:persisted?).and_return(true)
262
- user.should_receive(:id).and_return(1)
263
- user.should_receive(:save).and_return(true)
260
+ user.should_receive(:save!).and_return(true)
264
261
  user.send_reset_password
265
262
  end
266
263
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Rafter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-02 00:00:00.000000000 Z
11
+ date: 2014-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler