joshuaclayton-clearance 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/CHANGELOG.textile +135 -0
  2. data/LICENSE +21 -0
  3. data/README.textile +113 -0
  4. data/Rakefile +73 -0
  5. data/TODO.textile +6 -0
  6. data/app/controllers/clearance/confirmations_controller.rb +46 -0
  7. data/app/controllers/clearance/passwords_controller.rb +65 -0
  8. data/app/controllers/clearance/sessions_controller.rb +45 -0
  9. data/app/controllers/clearance/users_controller.rb +29 -0
  10. data/app/models/clearance_mailer.rb +19 -0
  11. data/app/views/clearance_mailer/change_password.html.erb +7 -0
  12. data/app/views/clearance_mailer/confirmation.html.erb +2 -0
  13. data/app/views/passwords/edit.html.erb +23 -0
  14. data/app/views/passwords/new.html.erb +15 -0
  15. data/app/views/sessions/new.html.erb +28 -0
  16. data/app/views/users/_form.html.erb +13 -0
  17. data/app/views/users/new.html.erb +6 -0
  18. data/config/routes.rb +19 -0
  19. data/generators/clearance/USAGE +1 -0
  20. data/generators/clearance/clearance_generator.rb +41 -0
  21. data/generators/clearance/lib/insert_commands.rb +103 -0
  22. data/generators/clearance/lib/rake_commands.rb +22 -0
  23. data/generators/clearance/templates/README +22 -0
  24. data/generators/clearance/templates/factories.rb +13 -0
  25. data/generators/clearance/templates/migrations/create_users.rb +20 -0
  26. data/generators/clearance/templates/migrations/update_users.rb +41 -0
  27. data/generators/clearance/templates/user.rb +3 -0
  28. data/generators/clearance_features/USAGE +1 -0
  29. data/generators/clearance_features/clearance_features_generator.rb +20 -0
  30. data/generators/clearance_features/templates/features/password_reset.feature +33 -0
  31. data/generators/clearance_features/templates/features/sign_in.feature +42 -0
  32. data/generators/clearance_features/templates/features/sign_out.feature +23 -0
  33. data/generators/clearance_features/templates/features/sign_up.feature +28 -0
  34. data/generators/clearance_features/templates/features/step_definitions/clearance_steps.rb +110 -0
  35. data/generators/clearance_features/templates/features/step_definitions/factory_girl_steps.rb +5 -0
  36. data/generators/clearance_features/templates/features/support/paths.rb +22 -0
  37. data/lib/clearance.rb +5 -0
  38. data/lib/clearance/authentication.rb +96 -0
  39. data/lib/clearance/extensions/errors.rb +4 -0
  40. data/lib/clearance/extensions/rescue.rb +1 -0
  41. data/lib/clearance/user.rb +115 -0
  42. data/rails/init.rb +1 -0
  43. data/shoulda_macros/clearance.rb +261 -0
  44. metadata +129 -0
@@ -0,0 +1,4 @@
1
+ module ActionController
2
+ class Forbidden < StandardError
3
+ end
4
+ end
@@ -0,0 +1 @@
1
+ ActionController::Base.rescue_responses.update('ActionController::Forbidden' => :forbidden)
@@ -0,0 +1,115 @@
1
+ require 'digest/sha1'
2
+
3
+ module Clearance
4
+ module User
5
+
6
+ def self.included(model)
7
+ model.extend ClassMethods
8
+ model.send(:include, InstanceMethods)
9
+
10
+ model.class_eval do
11
+ attr_accessible :email, :password, :password_confirmation
12
+ attr_accessor :password, :password_confirmation
13
+
14
+ validates_presence_of :email
15
+ validates_uniqueness_of :email, :case_sensitive => false
16
+ validates_format_of :email, :with => %r{.+@.+\..+}
17
+
18
+ validates_presence_of :password, :if => :password_required?
19
+ validates_confirmation_of :password, :if => :password_required?
20
+
21
+ before_save :initialize_salt, :encrypt_password, :initialize_token
22
+ end
23
+ end
24
+
25
+ module InstanceMethods
26
+ def authenticated?(password)
27
+ encrypted_password == encrypt(password)
28
+ end
29
+
30
+ def encrypt(string)
31
+ generate_hash("--#{salt}--#{string}--")
32
+ end
33
+
34
+ def remember?
35
+ token_expires_at && Time.now.utc < token_expires_at
36
+ end
37
+
38
+ def remember_me!
39
+ remember_me_until! 2.weeks.from_now.utc
40
+ end
41
+
42
+ def forget_me!
43
+ clear_token
44
+ save(false)
45
+ end
46
+
47
+ def confirm_email!
48
+ self.email_confirmed = true
49
+ self.token = nil
50
+ save(false)
51
+ end
52
+
53
+ def forgot_password!
54
+ generate_token
55
+ save(false)
56
+ end
57
+
58
+ def update_password(new_password, new_password_confirmation)
59
+ self.password = new_password
60
+ self.password_confirmation = new_password_confirmation
61
+ clear_token if valid?
62
+ save
63
+ end
64
+
65
+ protected
66
+
67
+ def generate_hash(string)
68
+ Digest::SHA1.hexdigest(string)
69
+ end
70
+
71
+ def initialize_salt
72
+ if new_record?
73
+ self.salt = generate_hash("--#{Time.now.utc.to_s}--#{password}--")
74
+ end
75
+ end
76
+
77
+ def encrypt_password
78
+ return if password.blank?
79
+ self.encrypted_password = encrypt(password)
80
+ end
81
+
82
+ def generate_token
83
+ self.token = encrypt("--#{Time.now.utc.to_s}--#{password}--")
84
+ self.token_expires_at = nil
85
+ end
86
+
87
+ def clear_token
88
+ self.token = nil
89
+ self.token_expires_at = nil
90
+ end
91
+
92
+ def initialize_token
93
+ generate_token if new_record?
94
+ end
95
+
96
+ def password_required?
97
+ encrypted_password.blank? || !password.blank?
98
+ end
99
+
100
+ def remember_me_until!(time)
101
+ self.token_expires_at = time
102
+ self.token = encrypt("--#{token_expires_at}--#{password}--")
103
+ save(false)
104
+ end
105
+ end
106
+
107
+ module ClassMethods
108
+ def authenticate(email, password)
109
+ return nil unless user = find_by_email(email)
110
+ return user if user.authenticated?(password)
111
+ end
112
+ end
113
+
114
+ end
115
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'clearance'
@@ -0,0 +1,261 @@
1
+ module Clearance
2
+ module Shoulda
3
+
4
+ # STATE OF AUTHENTICATION
5
+
6
+ def should_be_signed_in_as(&block)
7
+ should "be signed in as #{block.bind(self).call}" do
8
+ user = block.bind(self).call
9
+ assert_not_nil user,
10
+ "please pass a User. try: should_be_signed_in_as { @user }"
11
+ assert_equal user.id, session[:user_id],
12
+ "session[:user_id] is not set to User's id"
13
+ end
14
+ end
15
+
16
+ def should_be_signed_in_and_email_confirmed_as(&block)
17
+ warn "[DEPRECATION] questionable usefulness"
18
+ should_be_signed_in_as &block
19
+
20
+ should "have confirmed email" do
21
+ user = block.bind(self).call
22
+
23
+ assert_not_nil user
24
+ assert_equal user, assigns(:user)
25
+ assert assigns(:user).email_confirmed?
26
+ end
27
+ end
28
+
29
+ def should_not_be_signed_in
30
+ should "not be signed in" do
31
+ assert_nil session[:user_id]
32
+ end
33
+ end
34
+
35
+ def should_deny_access_on(http_method, action, opts = {})
36
+ warn "[DEPRECATION] use a setup & should_deny_access(:flash => ?)"
37
+ flash_message = opts.delete(:flash)
38
+ context "on #{http_method} to #{action}" do
39
+ setup do
40
+ send(http_method, action, opts)
41
+ end
42
+
43
+ should_deny_access(:flash => flash_message)
44
+ end
45
+ end
46
+
47
+ def should_deny_access(opts = {})
48
+ if opts[:flash]
49
+ should_set_the_flash_to opts[:flash]
50
+ else
51
+ should_not_set_the_flash
52
+ end
53
+
54
+ should_redirect_to('new_session_url') { new_session_url }
55
+ end
56
+
57
+ # HTTP FLUENCY
58
+
59
+ def should_forbid(description, &block)
60
+ should "forbid #{description}" do
61
+ assert_raises ActionController::Forbidden do
62
+ instance_eval(&block)
63
+ end
64
+ end
65
+ end
66
+
67
+ # CONTEXTS
68
+
69
+ def signed_in_user_context(&blk)
70
+ warn "[DEPRECATION] creates a Mystery Guest, causes Obscure Test"
71
+ context "A signed in user" do
72
+ setup do
73
+ @user = Factory(:user)
74
+ @user.confirm_email!
75
+ sign_in_as @user
76
+ end
77
+ merge_block(&blk)
78
+ end
79
+ end
80
+
81
+ def public_context(&blk)
82
+ warn "[DEPRECATION] common case is no-op. call sign_out otherwise"
83
+ context "The public" do
84
+ setup { sign_out }
85
+ merge_block(&blk)
86
+ end
87
+ end
88
+
89
+ # CREATING USERS
90
+
91
+ def should_create_user_successfully
92
+ warn "[DEPRECATION] not meant to be public, no longer used internally"
93
+ should_assign_to :user
94
+ should_change 'User.count', :by => 1
95
+
96
+ should "send the confirmation email" do
97
+ assert_sent_email do |email|
98
+ email.subject =~ /account confirmation/i
99
+ end
100
+ end
101
+
102
+ should_set_the_flash_to /confirm/i
103
+ should_redirect_to_url_after_create
104
+ end
105
+
106
+ # RENDERING
107
+
108
+ def should_render_nothing
109
+ should "render nothing" do
110
+ assert @response.body.blank?
111
+ end
112
+ end
113
+
114
+ # REDIRECTS
115
+
116
+ def should_redirect_to_url_after_create
117
+ should_redirect_to("the post-create url") do
118
+ @controller.send(:url_after_create)
119
+ end
120
+ end
121
+
122
+ def should_redirect_to_url_after_update
123
+ should_redirect_to("the post-update url") do
124
+ @controller.send(:url_after_update)
125
+ end
126
+ end
127
+
128
+ def should_redirect_to_url_after_destroy
129
+ should_redirect_to("the post-destroy url") do
130
+ @controller.send(:url_after_destroy)
131
+ end
132
+ end
133
+
134
+ # VALIDATIONS
135
+
136
+ def should_validate_confirmation_of(attribute, opts = {})
137
+ warn "[DEPRECATION] not meant to be public, no longer used internally"
138
+ raise ArgumentError if opts[:factory].nil?
139
+
140
+ context "on save" do
141
+ should_validate_confirmation_is_not_blank opts[:factory], attribute
142
+ should_validate_confirmation_is_not_bad opts[:factory], attribute
143
+ end
144
+ end
145
+
146
+ def should_validate_confirmation_is_not_blank(factory, attribute, opts = {})
147
+ warn "[DEPRECATION] not meant to be public, no longer used internally"
148
+ should "validate #{attribute}_confirmation is not blank" do
149
+ model = Factory.build(factory, blank_confirmation_options(attribute))
150
+ model.save
151
+ assert_confirmation_error(model, attribute,
152
+ "#{attribute}_confirmation cannot be blank")
153
+ end
154
+ end
155
+
156
+ def should_validate_confirmation_is_not_bad(factory, attribute, opts = {})
157
+ warn "[DEPRECATION] not meant to be public, no longer used internally"
158
+ should "validate #{attribute}_confirmation is different than #{attribute}" do
159
+ model = Factory.build(factory, bad_confirmation_options(attribute))
160
+ model.save
161
+ assert_confirmation_error(model, attribute,
162
+ "#{attribute}_confirmation cannot be different than #{attribute}")
163
+ end
164
+ end
165
+
166
+ # FORMS
167
+
168
+ def should_display_a_password_update_form
169
+ warn "[DEPRECATION] not meant to be public, no longer used internally"
170
+ should "have a form for the user's token, password, and password confirm" do
171
+ update_path = ERB::Util.h(
172
+ user_password_path(@user, :token => @user.token)
173
+ )
174
+
175
+ assert_select 'form[action=?]', update_path do
176
+ assert_select 'input[name=_method][value=?]', 'put'
177
+ assert_select 'input[name=?]', 'user[password]'
178
+ assert_select 'input[name=?]', 'user[password_confirmation]'
179
+ end
180
+ end
181
+ end
182
+
183
+ def should_display_a_sign_up_form
184
+ warn "[DEPRECATION] not meant to be public, no longer used internally"
185
+ should "display a form to sign up" do
186
+ assert_select "form[action=#{users_path}][method=post]",
187
+ true, "There must be a form to sign up" do
188
+ assert_select "input[type=text][name=?]",
189
+ "user[email]", true, "There must be an email field"
190
+ assert_select "input[type=password][name=?]",
191
+ "user[password]", true, "There must be a password field"
192
+ assert_select "input[type=password][name=?]",
193
+ "user[password_confirmation]", true, "There must be a password confirmation field"
194
+ assert_select "input[type=submit]", true,
195
+ "There must be a submit button"
196
+ end
197
+ end
198
+ end
199
+
200
+ def should_display_a_sign_in_form
201
+ warn "[DEPRECATION] not meant to be public, no longer used internally"
202
+ should 'display a "sign in" form' do
203
+ assert_select "form[action=#{session_path}][method=post]",
204
+ true, "There must be a form to sign in" do
205
+ assert_select "input[type=text][name=?]",
206
+ "session[email]", true, "There must be an email field"
207
+ assert_select "input[type=password][name=?]",
208
+ "session[password]", true, "There must be a password field"
209
+ assert_select "input[type=checkbox][name=?]",
210
+ "session[remember_me]", true, "There must be a 'remember me' check box"
211
+ assert_select "input[type=submit]", true,
212
+ "There must be a submit button"
213
+ end
214
+ end
215
+ end
216
+ end
217
+ end
218
+
219
+ module Clearance
220
+ module Shoulda
221
+ module Helpers
222
+ def sign_in_as(user)
223
+ @controller.class_eval { attr_accessor :current_user }
224
+ @controller.current_user = user
225
+ return user
226
+ end
227
+
228
+ def sign_in
229
+ sign_in_as Factory(:email_confirmed_user)
230
+ end
231
+
232
+ def sign_out
233
+ @controller.class_eval { attr_accessor :current_user }
234
+ @controller.current_user = nil
235
+ end
236
+
237
+ def blank_confirmation_options(attribute)
238
+ warn "[DEPRECATION] not meant to be public, no longer used internally"
239
+ opts = { attribute => attribute.to_s }
240
+ opts.merge("#{attribute}_confirmation".to_sym => "")
241
+ end
242
+
243
+ def bad_confirmation_options(attribute)
244
+ warn "[DEPRECATION] not meant to be public, no longer used internally"
245
+ opts = { attribute => attribute.to_s }
246
+ opts.merge("#{attribute}_confirmation".to_sym => "not_#{attribute}")
247
+ end
248
+
249
+ def assert_confirmation_error(model, attribute, message = "confirmation error")
250
+ warn "[DEPRECATION] not meant to be public, no longer used internally"
251
+ assert model.errors.on(attribute).include?("doesn't match confirmation"),
252
+ message
253
+ end
254
+ end
255
+ end
256
+ end
257
+
258
+ class Test::Unit::TestCase
259
+ include Clearance::Shoulda::Helpers
260
+ end
261
+ Test::Unit::TestCase.extend(Clearance::Shoulda)
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: joshuaclayton-clearance
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.4
5
+ platform: ruby
6
+ authors:
7
+ - Dan Croak
8
+ - Mike Burns
9
+ - Jason Morrison
10
+ - Joe Ferris
11
+ - Eugene Bolshakov
12
+ - Nick Quaranto
13
+ - Josh Nichols
14
+ - Mike Breen
15
+ - "Marcel G\xC3\xB6rner"
16
+ - Bence Nagy
17
+ - Ben Mabey
18
+ - Eloy Duran
19
+ - Tim Pope
20
+ - Mihai Anca
21
+ - Mark Cornick
22
+ - Shay Arnett
23
+ autorequire:
24
+ bindir: bin
25
+ cert_chain: []
26
+
27
+ date: 2009-05-11 21:00:00 -07:00
28
+ default_executable:
29
+ dependencies: []
30
+
31
+ description: Rails authentication with email & password.
32
+ email: support@thoughtbot.com
33
+ executables: []
34
+
35
+ extensions: []
36
+
37
+ extra_rdoc_files: []
38
+
39
+ files:
40
+ - CHANGELOG.textile
41
+ - LICENSE
42
+ - Rakefile
43
+ - README.textile
44
+ - TODO.textile
45
+ - app/controllers
46
+ - app/controllers/clearance
47
+ - app/controllers/clearance/confirmations_controller.rb
48
+ - app/controllers/clearance/passwords_controller.rb
49
+ - app/controllers/clearance/sessions_controller.rb
50
+ - app/controllers/clearance/users_controller.rb
51
+ - app/models
52
+ - app/models/clearance_mailer.rb
53
+ - app/views
54
+ - app/views/clearance_mailer
55
+ - app/views/clearance_mailer/change_password.html.erb
56
+ - app/views/clearance_mailer/confirmation.html.erb
57
+ - app/views/passwords
58
+ - app/views/passwords/edit.html.erb
59
+ - app/views/passwords/new.html.erb
60
+ - app/views/sessions
61
+ - app/views/sessions/new.html.erb
62
+ - app/views/users
63
+ - app/views/users/_form.html.erb
64
+ - app/views/users/new.html.erb
65
+ - config/routes.rb
66
+ - generators/clearance
67
+ - generators/clearance/clearance_generator.rb
68
+ - generators/clearance/lib
69
+ - generators/clearance/lib/insert_commands.rb
70
+ - generators/clearance/lib/rake_commands.rb
71
+ - generators/clearance/templates
72
+ - generators/clearance/templates/factories.rb
73
+ - generators/clearance/templates/migrations
74
+ - generators/clearance/templates/migrations/create_users.rb
75
+ - generators/clearance/templates/migrations/update_users.rb
76
+ - generators/clearance/templates/README
77
+ - generators/clearance/templates/user.rb
78
+ - generators/clearance/USAGE
79
+ - generators/clearance_features
80
+ - generators/clearance_features/clearance_features_generator.rb
81
+ - generators/clearance_features/templates
82
+ - generators/clearance_features/templates/features
83
+ - generators/clearance_features/templates/features/password_reset.feature
84
+ - generators/clearance_features/templates/features/sign_in.feature
85
+ - generators/clearance_features/templates/features/sign_out.feature
86
+ - generators/clearance_features/templates/features/sign_up.feature
87
+ - generators/clearance_features/templates/features/step_definitions
88
+ - generators/clearance_features/templates/features/step_definitions/clearance_steps.rb
89
+ - generators/clearance_features/templates/features/step_definitions/factory_girl_steps.rb
90
+ - generators/clearance_features/templates/features/support
91
+ - generators/clearance_features/templates/features/support/paths.rb
92
+ - generators/clearance_features/USAGE
93
+ - lib/clearance
94
+ - lib/clearance/authentication.rb
95
+ - lib/clearance/extensions
96
+ - lib/clearance/extensions/errors.rb
97
+ - lib/clearance/extensions/rescue.rb
98
+ - lib/clearance/user.rb
99
+ - lib/clearance.rb
100
+ - shoulda_macros/clearance.rb
101
+ - rails/init.rb
102
+ has_rdoc: false
103
+ homepage: http://github.com/thoughtbot/clearance
104
+ post_install_message:
105
+ rdoc_options: []
106
+
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: "0"
114
+ version:
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: "0"
120
+ version:
121
+ requirements: []
122
+
123
+ rubyforge_project:
124
+ rubygems_version: 1.2.0
125
+ signing_key:
126
+ specification_version: 2
127
+ summary: Rails authentication with email & password.
128
+ test_files: []
129
+