thoughtbot-clearance 0.3.8 → 0.3.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (27) hide show
  1. data/README.textile +17 -16
  2. data/Rakefile +8 -7
  3. data/TODO.textile +3 -3
  4. data/generators/clearance/clearance_generator.rb +1 -1
  5. data/generators/clearance/templates/app/views/clearance_mailer/change_password.html.erb +1 -1
  6. data/generators/clearance/templates/app/views/passwords/edit.html.erb +1 -1
  7. data/generators/clearance/templates/app/views/sessions/new.html.erb +2 -2
  8. data/generators/clearance/templates/db/migrate/create_users_with_clearance_columns.rb +3 -3
  9. data/generators/clearance/templates/db/migrate/update_users_with_clearance_columns.rb +14 -13
  10. data/generators/clearance/templates/test/factories/clearance.rb +16 -0
  11. data/lib/clearance/app/controllers/application_controller.rb +26 -14
  12. data/lib/clearance/app/controllers/confirmations_controller.rb +6 -3
  13. data/lib/clearance/app/controllers/passwords_controller.rb +15 -9
  14. data/lib/clearance/app/controllers/sessions_controller.rb +21 -17
  15. data/lib/clearance/app/controllers/users_controller.rb +4 -3
  16. data/lib/clearance/app/models/clearance_mailer.rb +1 -1
  17. data/lib/clearance/app/models/user.rb +15 -16
  18. data/lib/clearance/test/functional/confirmations_controller_test.rb +18 -30
  19. data/lib/clearance/test/functional/passwords_controller_test.rb +27 -45
  20. data/lib/clearance/test/functional/sessions_controller_test.rb +23 -27
  21. data/lib/clearance/test/functional/users_controller_test.rb +38 -28
  22. data/lib/clearance/test/test_helper.rb +7 -2
  23. data/lib/clearance/test/unit/clearance_mailer_test.rb +7 -5
  24. data/lib/clearance/test/unit/user_test.rb +107 -138
  25. data/shoulda_macros/clearance.rb +134 -6
  26. metadata +7 -3
  27. data/generators/clearance/templates/test/factories/clearance_user.rb +0 -9
@@ -1,6 +1,39 @@
1
1
  module Clearance
2
2
  module Shoulda
3
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
+
4
37
  def should_deny_access_on(command, opts = {})
5
38
  context "on #{command}" do
6
39
  setup { eval command }
@@ -15,19 +48,22 @@ module Clearance
15
48
  should_not_set_the_flash
16
49
  end
17
50
 
18
- should "respond with 401 Unauthorized and render login template" do
51
+ should "respond with 401 Unauthorized and render sign_in template" do
19
52
  assert_response :unauthorized,
20
53
  "access was expected to be denied (401 unauthorized)"
21
54
  assert_template "sessions/new",
22
- "template was expected to be login (sessions/new)"
55
+ "template was expected to be sign in (sessions/new)"
23
56
  end
24
57
  end
58
+
59
+ # CONTEXTS
25
60
 
26
- def logged_in_user_context(&blk)
27
- context "A logged in user" do
61
+ def signed_in_user_context(&blk)
62
+ context "A signed in user" do
28
63
  setup do
29
- @user = Factory :clearance_user
30
- login_as @user
64
+ @user = Factory(:registered_user)
65
+ @user.confirm_email!
66
+ sign_in_as @user
31
67
  end
32
68
  merge_block(&blk)
33
69
  end
@@ -39,7 +75,99 @@ module Clearance
39
75
  merge_block(&blk)
40
76
  end
41
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
+
42
146
  end
43
147
  end
44
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
45
173
  Test::Unit::TestCase.extend(Clearance::Shoulda)
metadata CHANGED
@@ -1,17 +1,21 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thoughtbot-clearance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.8
4
+ version: 0.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoughtbot, inc.
8
+ - Dan Croak
9
+ - Mike Burns
10
+ - Jason Morrison
11
+ - Eugene Bolshakov
8
12
  - Josh Nichols
9
13
  - Mike Breen
10
14
  autorequire:
11
15
  bindir: bin
12
16
  cert_chain: []
13
17
 
14
- date: 2009-01-16 21:00:00 -08:00
18
+ date: 2009-01-17 21:00:00 -08:00
15
19
  default_executable:
16
20
  dependencies: []
17
21
 
@@ -64,7 +68,7 @@ files:
64
68
  - generators/clearance/templates/README
65
69
  - generators/clearance/templates/test
66
70
  - generators/clearance/templates/test/factories
67
- - generators/clearance/templates/test/factories/clearance_user.rb
71
+ - generators/clearance/templates/test/factories/clearance.rb
68
72
  - generators/clearance/templates/test/functional
69
73
  - generators/clearance/templates/test/functional/confirmations_controller_test.rb
70
74
  - generators/clearance/templates/test/functional/passwords_controller_test.rb
@@ -1,9 +0,0 @@
1
- Factory.sequence :email do |n|
2
- "user#{n}@example.com"
3
- end
4
-
5
- Factory.define :clearance_user, :class => 'user' do |user|
6
- user.email { Factory.next :email }
7
- user.password "password"
8
- user.password_confirmation "password"
9
- end