nagybence-clearance 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/LICENSE +21 -0
  2. data/README.textile +205 -0
  3. data/Rakefile +53 -0
  4. data/TODO.textile +8 -0
  5. data/generators/clearance/USAGE +1 -0
  6. data/generators/clearance/clearance_generator.rb +92 -0
  7. data/generators/clearance/lib/insert_commands.rb +103 -0
  8. data/generators/clearance/lib/rake_commands.rb +22 -0
  9. data/generators/clearance/templates/README +54 -0
  10. data/generators/clearance/templates/app/controllers/application.rb +5 -0
  11. data/generators/clearance/templates/app/controllers/confirmations_controller.rb +3 -0
  12. data/generators/clearance/templates/app/controllers/passwords_controller.rb +3 -0
  13. data/generators/clearance/templates/app/controllers/sessions_controller.rb +3 -0
  14. data/generators/clearance/templates/app/controllers/users_controller.rb +3 -0
  15. data/generators/clearance/templates/app/models/clearance_mailer.rb +5 -0
  16. data/generators/clearance/templates/app/models/user.rb +3 -0
  17. data/generators/clearance/templates/app/views/clearance_mailer/change_password.html.erb +10 -0
  18. data/generators/clearance/templates/app/views/clearance_mailer/confirmation.html.erb +1 -0
  19. data/generators/clearance/templates/app/views/passwords/edit.html.erb +25 -0
  20. data/generators/clearance/templates/app/views/passwords/new.html.erb +15 -0
  21. data/generators/clearance/templates/app/views/sessions/new.html.erb +28 -0
  22. data/generators/clearance/templates/app/views/users/_form.html.erb +13 -0
  23. data/generators/clearance/templates/app/views/users/edit.html.erb +6 -0
  24. data/generators/clearance/templates/app/views/users/new.html.erb +6 -0
  25. data/generators/clearance/templates/db/migrate/create_users_with_clearance_columns.rb +21 -0
  26. data/generators/clearance/templates/db/migrate/update_users_with_clearance_columns.rb +42 -0
  27. data/generators/clearance/templates/test/factories/clearance.rb +16 -0
  28. data/generators/clearance/templates/test/functional/confirmations_controller_test.rb +5 -0
  29. data/generators/clearance/templates/test/functional/passwords_controller_test.rb +5 -0
  30. data/generators/clearance/templates/test/functional/sessions_controller_test.rb +5 -0
  31. data/generators/clearance/templates/test/functional/users_controller_test.rb +5 -0
  32. data/generators/clearance/templates/test/unit/clearance_mailer_test.rb +6 -0
  33. data/generators/clearance/templates/test/unit/user_test.rb +5 -0
  34. data/lib/clearance/app/controllers/application_controller.rb +84 -0
  35. data/lib/clearance/app/controllers/confirmations_controller.rb +42 -0
  36. data/lib/clearance/app/controllers/passwords_controller.rb +67 -0
  37. data/lib/clearance/app/controllers/sessions_controller.rb +68 -0
  38. data/lib/clearance/app/controllers/users_controller.rb +40 -0
  39. data/lib/clearance/app/models/clearance_mailer.rb +29 -0
  40. data/lib/clearance/app/models/user.rb +89 -0
  41. data/lib/clearance/test/functional/confirmations_controller_test.rb +44 -0
  42. data/lib/clearance/test/functional/passwords_controller_test.rb +175 -0
  43. data/lib/clearance/test/functional/sessions_controller_test.rb +194 -0
  44. data/lib/clearance/test/functional/users_controller_test.rb +72 -0
  45. data/lib/clearance/test/test_helper.rb +28 -0
  46. data/lib/clearance/test/unit/clearance_mailer_test.rb +65 -0
  47. data/lib/clearance/test/unit/user_test.rb +167 -0
  48. data/lib/clearance.rb +14 -0
  49. data/rails/init.rb +1 -0
  50. data/shoulda_macros/clearance.rb +173 -0
  51. metadata +132 -0
@@ -0,0 +1,167 @@
1
+ module Clearance
2
+ module Test
3
+ module Unit
4
+ module UserTest
5
+
6
+ def self.included(unit_test)
7
+ unit_test.class_eval do
8
+
9
+ should_protect_attributes :email_confirmed,
10
+ :salt, :encrypted_password,
11
+ :remember_token, :remember_token_expires_at
12
+
13
+ # registering
14
+
15
+ context "When registering" do
16
+ should_require_attributes :email, :password
17
+ should_allow_values_for :email, "foo@example.com"
18
+ should_not_allow_values_for :email, "foo"
19
+ should_not_allow_values_for :email, "example.com"
20
+
21
+ should_validate_confirmation_of :password,
22
+ :factory => :registered_user
23
+
24
+ should "initialize salt" do
25
+ assert_not_nil Factory(:registered_user).salt
26
+ end
27
+
28
+ context "encrypt password" do
29
+ setup do
30
+ @salt = "salt"
31
+ User.any_instance.stubs(:initialize_salt)
32
+
33
+ @user = Factory(:registered_user, :salt => @salt)
34
+ @password = @user.password
35
+
36
+ @user.encrypt(@password)
37
+ @expected = Digest::SHA512.hexdigest("--#{@salt}--#{@password}--")
38
+ end
39
+
40
+ should "create an encrypted password using SHA512 encryption" do
41
+ assert_equal @expected, @user.encrypted_password
42
+ assert_not_equal @password, @user.encrypted_password
43
+ end
44
+ end
45
+
46
+ should "store email in lower case" do
47
+ user = Factory(:registered_user, :email => "John.Doe@example.com")
48
+ assert_equal "john.doe@example.com", user.email
49
+ end
50
+ end
51
+
52
+ context "When multiple users have registerd" do
53
+ setup { @user = Factory(:registered_user) }
54
+
55
+ should_require_unique_attributes :email
56
+ end
57
+
58
+ # confirming email
59
+
60
+ context "A registered user without email confirmation" do
61
+ setup do
62
+ @user = Factory(:registered_user)
63
+ assert ! @user.email_confirmed?
64
+ end
65
+
66
+ context "after #confirm_email!" do
67
+ setup do
68
+ assert @user.confirm_email!
69
+ @user.reload
70
+ end
71
+
72
+ should "have confirmed their email" do
73
+ assert @user.email_confirmed?
74
+ end
75
+ end
76
+ end
77
+
78
+ # authenticating
79
+
80
+ context "A user" do
81
+ setup do
82
+ @user = Factory(:registered_user)
83
+ @password = @user.password
84
+ end
85
+
86
+ should "authenticate with good credentials" do
87
+ assert User.authenticate(@user.email, @password)
88
+ assert @user.authenticated?(@password)
89
+ end
90
+
91
+ should "authenticate with good credentials, email in uppercase" do
92
+ assert User.authenticate(@user.email.upcase, @password)
93
+ assert @user.authenticated?(@password)
94
+ end
95
+
96
+ should "not authenticate with bad credentials" do
97
+ assert ! User.authenticate(@user.email, 'horribly_wrong_password')
98
+ assert ! @user.authenticated?('horribly_wrong_password')
99
+ end
100
+ end
101
+
102
+ # remember me
103
+
104
+ context "When registering with remember_me!" do
105
+ setup do
106
+ @user = Factory(:registered_user)
107
+ assert_nil @user.remember_token
108
+ assert_nil @user.remember_token_expires_at
109
+ @user.remember_me!
110
+ end
111
+
112
+ should "set the remember token and expiration date" do
113
+ assert_not_nil @user.remember_token
114
+ assert_not_nil @user.remember_token_expires_at
115
+ end
116
+
117
+ should "remember user when token expires in the future" do
118
+ @user.update_attribute :remember_token_expires_at,
119
+ 2.weeks.from_now.utc
120
+ assert @user.remember?
121
+ end
122
+
123
+ should "not remember user when token has already expired" do
124
+ @user.update_attribute :remember_token_expires_at,
125
+ 2.weeks.ago.utc
126
+ assert ! @user.remember?
127
+ end
128
+
129
+ # logging out
130
+
131
+ context "forget_me!" do
132
+ setup { @user.forget_me! }
133
+
134
+ should "unset the remember token and expiration date" do
135
+ assert_nil @user.remember_token
136
+ assert_nil @user.remember_token_expires_at
137
+ end
138
+
139
+ should "not remember user" do
140
+ assert ! @user.remember?
141
+ end
142
+ end
143
+ end
144
+
145
+ # updating password
146
+
147
+ context "An email confirmed user" do
148
+ setup { @user = Factory(:email_confirmed_user) }
149
+
150
+ context "who changes and confirms password" do
151
+ setup do
152
+ @user.password = "new_password"
153
+ @user.password_confirmation = "new_password"
154
+ @user.save
155
+ end
156
+
157
+ should_change "@user.encrypted_password"
158
+ end
159
+ end
160
+
161
+ end
162
+ end
163
+
164
+ end
165
+ end
166
+ end
167
+ end
data/lib/clearance.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'clearance/app/controllers/application_controller'
2
+ require 'clearance/app/controllers/confirmations_controller'
3
+ require 'clearance/app/controllers/passwords_controller'
4
+ require 'clearance/app/controllers/sessions_controller'
5
+ require 'clearance/app/controllers/users_controller'
6
+ require 'clearance/app/models/clearance_mailer'
7
+ require 'clearance/app/models/user'
8
+ require 'clearance/test/functional/confirmations_controller_test'
9
+ require 'clearance/test/functional/passwords_controller_test'
10
+ require 'clearance/test/functional/sessions_controller_test'
11
+ require 'clearance/test/functional/users_controller_test'
12
+ require 'clearance/test/test_helper'
13
+ require 'clearance/test/unit/clearance_mailer_test'
14
+ require 'clearance/test/unit/user_test'
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'clearance'
@@ -0,0 +1,173 @@
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
+ assert_equal user.salt, session[:salt],
14
+ "session[:salt] is not set to User's salt"
15
+ end
16
+ end
17
+
18
+ def should_be_signed_in_and_email_confirmed_as(&block)
19
+ should_be_signed_in_as &block
20
+
21
+ should "have confirmed email" do
22
+ user = block.bind(self).call
23
+
24
+ assert_not_nil user
25
+ assert_equal user, assigns(:user)
26
+ assert assigns(:user).email_confirmed?
27
+ end
28
+ end
29
+
30
+ def should_not_be_signed_in
31
+ should "not be signed in" do
32
+ assert_nil session[:user_id]
33
+ assert_nil session[:salt]
34
+ end
35
+ end
36
+
37
+ def should_deny_access_on(command, opts = {})
38
+ context "on #{command}" do
39
+ setup { eval command }
40
+ should_deny_access(opts)
41
+ end
42
+ end
43
+
44
+ def should_deny_access(opts = {})
45
+ if opts[:flash]
46
+ should_set_the_flash_to opts[:flash]
47
+ else
48
+ should_not_set_the_flash
49
+ end
50
+
51
+ should "respond with 401 Unauthorized and render sign_in template" do
52
+ assert_response :unauthorized,
53
+ "access was expected to be denied (401 unauthorized)"
54
+ assert_template "sessions/new",
55
+ "template was expected to be sign in (sessions/new)"
56
+ end
57
+ end
58
+
59
+ # CONTEXTS
60
+
61
+ def signed_in_user_context(&blk)
62
+ context "A signed in user" do
63
+ setup do
64
+ @user = Factory(:registered_user)
65
+ @user.confirm_email!
66
+ sign_in_as @user
67
+ end
68
+ merge_block(&blk)
69
+ end
70
+ end
71
+
72
+ def public_context(&blk)
73
+ context "The public" do
74
+ setup { sign_out }
75
+ merge_block(&blk)
76
+ end
77
+ end
78
+
79
+ # CREATING USERS
80
+
81
+ def should_create_user_successfully
82
+ should_assign_to :user
83
+ should_change 'User.count', :by => 1
84
+
85
+ should "send the confirmation email" do
86
+ assert_sent_email do |email|
87
+ email.subject =~ /account confirmation/i
88
+ end
89
+ end
90
+
91
+ should_set_the_flash_to /confirm/i
92
+ should_redirect_to_url_after_create
93
+ end
94
+
95
+ # RENDERING
96
+
97
+ def should_render_nothing
98
+ should "render nothing" do
99
+ assert @response.body.blank?
100
+ end
101
+ end
102
+
103
+ # REDIRECTS
104
+
105
+ def should_redirect_to_url_after_create
106
+ should_redirect_to "@controller.send(:url_after_create)"
107
+ end
108
+
109
+ def should_redirect_to_url_after_update
110
+ should_redirect_to "@controller.send(:url_after_update)"
111
+ end
112
+
113
+ def should_redirect_to_url_after_destroy
114
+ should_redirect_to "@controller.send(:url_after_destroy)"
115
+ end
116
+
117
+ # VALIDATIONS
118
+
119
+ def should_validate_confirmation_of(attribute, opts = {})
120
+ raise ArgumentError if opts[:factory].nil?
121
+
122
+ context "on save" do
123
+ should_validate_confirmation_is_not_blank opts[:factory], attribute
124
+ should_validate_confirmation_is_not_bad opts[:factory], attribute
125
+ end
126
+ end
127
+
128
+ def should_validate_confirmation_is_not_blank(factory, attribute, opts = {})
129
+ should "validate #{attribute}_confirmation is not blank" do
130
+ model = Factory.build(factory, blank_confirmation_options(attribute))
131
+ model.save
132
+ assert_confirmation_error(model, attribute,
133
+ "#{attribute}_confirmation cannot be blank")
134
+ end
135
+ end
136
+
137
+ def should_validate_confirmation_is_not_bad(factory, attribute, opts = {})
138
+ should "validate #{attribute}_confirmation is different than #{attribute}" do
139
+ model = Factory.build(factory, bad_confirmation_options(attribute))
140
+ model.save
141
+ assert_confirmation_error(model, attribute,
142
+ "#{attribute}_confirmation cannot be different than #{attribute}")
143
+ end
144
+ end
145
+
146
+ end
147
+ end
148
+
149
+ module Clearance
150
+ module Shoulda
151
+ module Helpers
152
+ def blank_confirmation_options(attribute)
153
+ opts = { attribute => attribute.to_s }
154
+ opts.merge("#{attribute}_confirmation".to_sym => "")
155
+ end
156
+
157
+ def bad_confirmation_options(attribute)
158
+ opts = { attribute => attribute.to_s }
159
+ opts.merge("#{attribute}_confirmation".to_sym => "not_#{attribute}")
160
+ end
161
+
162
+ def assert_confirmation_error(model, attribute, message = "confirmation error")
163
+ assert model.errors.on(attribute).include?("doesn't match confirmation"),
164
+ message
165
+ end
166
+ end
167
+ end
168
+ end
169
+
170
+ class Test::Unit::TestCase
171
+ include Clearance::Shoulda::Helpers
172
+ end
173
+ Test::Unit::TestCase.extend(Clearance::Shoulda)
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nagybence-clearance
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.1
5
+ platform: ruby
6
+ authors:
7
+ - thoughtbot, inc.
8
+ - Dan Croak
9
+ - Mike Burns
10
+ - Jason Morrison
11
+ - Eugene Bolshakov
12
+ - Josh Nichols
13
+ - Mike Breen
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2009-01-26 21:00:00 -08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Simple, complete Rails authentication scheme.
23
+ email: support@thoughtbot.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - Rakefile
32
+ - README.textile
33
+ - LICENSE
34
+ - TODO.textile
35
+ - generators/clearance
36
+ - generators/clearance/templates
37
+ - generators/clearance/templates/README
38
+ - generators/clearance/templates/test
39
+ - generators/clearance/templates/test/factories
40
+ - generators/clearance/templates/test/factories/clearance.rb
41
+ - generators/clearance/templates/test/functional
42
+ - generators/clearance/templates/test/functional/confirmations_controller_test.rb
43
+ - generators/clearance/templates/test/functional/passwords_controller_test.rb
44
+ - generators/clearance/templates/test/functional/sessions_controller_test.rb
45
+ - generators/clearance/templates/test/functional/users_controller_test.rb
46
+ - generators/clearance/templates/test/unit
47
+ - generators/clearance/templates/test/unit/clearance_mailer_test.rb
48
+ - generators/clearance/templates/test/unit/user_test.rb
49
+ - generators/clearance/templates/app
50
+ - generators/clearance/templates/app/views
51
+ - generators/clearance/templates/app/views/passwords
52
+ - generators/clearance/templates/app/views/passwords/edit.html.erb
53
+ - generators/clearance/templates/app/views/passwords/new.html.erb
54
+ - generators/clearance/templates/app/views/sessions
55
+ - generators/clearance/templates/app/views/sessions/new.html.erb
56
+ - generators/clearance/templates/app/views/users
57
+ - generators/clearance/templates/app/views/users/_form.html.erb
58
+ - generators/clearance/templates/app/views/users/edit.html.erb
59
+ - generators/clearance/templates/app/views/users/new.html.erb
60
+ - generators/clearance/templates/app/views/clearance_mailer
61
+ - generators/clearance/templates/app/views/clearance_mailer/change_password.html.erb
62
+ - generators/clearance/templates/app/views/clearance_mailer/confirmation.html.erb
63
+ - generators/clearance/templates/app/models
64
+ - generators/clearance/templates/app/models/user.rb
65
+ - generators/clearance/templates/app/models/clearance_mailer.rb
66
+ - generators/clearance/templates/app/controllers
67
+ - generators/clearance/templates/app/controllers/application.rb
68
+ - generators/clearance/templates/app/controllers/passwords_controller.rb
69
+ - generators/clearance/templates/app/controllers/users_controller.rb
70
+ - generators/clearance/templates/app/controllers/sessions_controller.rb
71
+ - generators/clearance/templates/app/controllers/confirmations_controller.rb
72
+ - generators/clearance/templates/db
73
+ - generators/clearance/templates/db/migrate
74
+ - generators/clearance/templates/db/migrate/create_users_with_clearance_columns.rb
75
+ - generators/clearance/templates/db/migrate/update_users_with_clearance_columns.rb
76
+ - generators/clearance/lib
77
+ - generators/clearance/lib/insert_commands.rb
78
+ - generators/clearance/lib/rake_commands.rb
79
+ - generators/clearance/USAGE
80
+ - generators/clearance/clearance_generator.rb
81
+ - lib/clearance
82
+ - lib/clearance/test
83
+ - lib/clearance/test/test_helper.rb
84
+ - lib/clearance/test/functional
85
+ - lib/clearance/test/functional/confirmations_controller_test.rb
86
+ - lib/clearance/test/functional/passwords_controller_test.rb
87
+ - lib/clearance/test/functional/sessions_controller_test.rb
88
+ - lib/clearance/test/functional/users_controller_test.rb
89
+ - lib/clearance/test/unit
90
+ - lib/clearance/test/unit/clearance_mailer_test.rb
91
+ - lib/clearance/test/unit/user_test.rb
92
+ - lib/clearance/app
93
+ - lib/clearance/app/models
94
+ - lib/clearance/app/models/user.rb
95
+ - lib/clearance/app/models/clearance_mailer.rb
96
+ - lib/clearance/app/controllers
97
+ - lib/clearance/app/controllers/application_controller.rb
98
+ - lib/clearance/app/controllers/passwords_controller.rb
99
+ - lib/clearance/app/controllers/users_controller.rb
100
+ - lib/clearance/app/controllers/sessions_controller.rb
101
+ - lib/clearance/app/controllers/confirmations_controller.rb
102
+ - lib/clearance.rb
103
+ - shoulda_macros/clearance.rb
104
+ - rails/init.rb
105
+ has_rdoc: false
106
+ homepage: http://github.com/thoughtbot/clearance
107
+ post_install_message:
108
+ rdoc_options: []
109
+
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: "0"
117
+ version:
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: "0"
123
+ version:
124
+ requirements: []
125
+
126
+ rubyforge_project:
127
+ rubygems_version: 1.2.0
128
+ signing_key:
129
+ specification_version: 2
130
+ summary: Rails authentication for developers who write tests.
131
+ test_files: []
132
+