login_sugar_generator 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,254 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+ require '<%= singular_name %>_controller'
3
+ require '<%= singular_name %>_notify'
4
+
5
+ # Raise errors beyond the default web-based presentation
6
+ class <%= class_name %>Controller; def rescue_action(e) raise e end; end
7
+
8
+ class <%= class_name %>ControllerTest < Test::Unit::TestCase
9
+ self.use_transactional_fixtures = false
10
+ fixtures :<%= plural_name %>
11
+
12
+ def setup
13
+ @controller = <%= class_name %>Controller.new
14
+ @request = ActionController::TestRequest.new
15
+ @response = ActionController::TestResponse.new
16
+ @request.host = "localhost"
17
+ ActionMailer::Base.inject_one_error = false
18
+ ActionMailer::Base.deliveries = []
19
+ end
20
+
21
+ def test_login__valid_login__redirects_as_specified
22
+ @request.session[:return_to] = "/bogus/location"
23
+ post :login, :<%= singular_name %> => { :login => "tesla", :password => "atest" }
24
+ assert_logged_in <%= plural_name %>(:tesla)
25
+ assert_response :redirect
26
+ assert_equal "http://#{@request.host}/bogus/location", @response.redirect_url
27
+ end
28
+
29
+ def test_login__valid_login__shows_welcome_as_default
30
+ post :login, :<%= singular_name %> => { :login => "tesla", :password => "atest" }
31
+ assert_logged_in <%= plural_name %>(:tesla)
32
+ assert_response :redirect
33
+ assert_equal @controller.url_for(:action => 'welcome'), @response.redirect_url
34
+ end
35
+
36
+ def test_login__wrong_password
37
+ post :login, :<%= singular_name %> => { :login => "tesla", :password => "wrong password" }
38
+ assert_not_logged_in
39
+ assert_template 'login'
40
+ assert_contains "Login failed", flash['message']
41
+ end
42
+
43
+ def test_login__wrong_login
44
+ post :login, :<%= singular_name %> => { :login => "wrong login", :password => "atest" }
45
+ assert_not_logged_in
46
+ assert_template 'login'
47
+ assert_contains "Login failed", flash['message']
48
+ end
49
+
50
+ def test_login__deleted_<%= singular_name %>_cant_login
51
+ post :login, :<%= singular_name %> => { :login => "deleted_tesla", :password => "atest" }
52
+ assert_not_logged_in
53
+ assert_template 'login'
54
+ assert_contains "Login failed", flash['message']
55
+ end
56
+
57
+ def test_signup
58
+ post_signup :login => "new<%= singular_name %>",
59
+ :password => "password", :password_confirmation => "password",
60
+ :email => "newemail@example.com"
61
+ assert_not_logged_in
62
+ assert_redirected_to_login
63
+ assert_equal 1, ActionMailer::Base.deliveries.size
64
+
65
+ mail = ActionMailer::Base.deliveries[0]
66
+ assert_equal "newemail@example.com", mail.to_addrs[0].to_s
67
+ assert_match /login:\s+\w+\n/, mail.encoded
68
+ assert_match /password:\s+\w+\n/, mail.encoded
69
+ <%= singular_name %> = <%= class_name %>.find_by_email("newemail@example.com")
70
+ assert_match /<%= file_name %>\[id\]=#{<%= singular_name %>.id}/, mail.encoded
71
+ assert_match /key=#{<%= singular_name %>.security_token}/, mail.encoded
72
+ assert !<%= singular_name %>.verified
73
+ end
74
+
75
+ def test_signup__validates_password_min_length
76
+ post_signup :login => "tesla_rhea", :password => "bad", :password_confirmation => "bad", :email => "someone@example.com"
77
+ assert_password_validation_fails
78
+ end
79
+
80
+ def test_signup__raises_delivery_errors
81
+ ActionMailer::Base.inject_one_error = true
82
+ post_signup :login => "newtesla",
83
+ :password => "newpassword", :password_confirmation => "newpassword",
84
+ :email => "newtesla@example.com"
85
+ assert_not_logged_in
86
+ assert_equal 0, ActionMailer::Base.deliveries.size
87
+ assert_contains "confirmation email not sent", flash['message']
88
+ end
89
+
90
+ def test_signup__mismatched_passwords
91
+ post :signup, :<%= singular_name %> => { :login => "newtesla", :password => "newpassword", :password_confirmation => "wrong" }
92
+ <%= singular_name %> = assigns(:<%= singular_name %>)
93
+ assert_equal 1, <%= singular_name %>.errors.size
94
+ assert_not_nil <%= singular_name %>.errors['password']
95
+ end
96
+
97
+ def test_signup__bad_login
98
+ post_signup :login => "yo", :password => "newpassword", :password_confirmation => "newpassword"
99
+ <%= singular_name %> = assigns(:<%= singular_name %>)
100
+ assert_equal 1, <%= singular_name %>.errors.size
101
+ assert_not_nil <%= singular_name %>.errors['login']
102
+ end
103
+
104
+ def test_welcome
105
+ <%= singular_name %> = <%= plural_name %>(:unverified_<%= singular_name %>)
106
+ get :welcome, :<%= singular_name %>=> { :id => <%= singular_name %>.id }, :key => <%= singular_name %>.security_token
107
+ <%= singular_name %>.reload
108
+ assert <%= singular_name %>.verified
109
+ assert_logged_in( <%= singular_name %> )
110
+ end
111
+
112
+ def test_welcome__fails_if_expired_token
113
+ <%= singular_name %> = <%= plural_name %>(:unverified_<%= singular_name %>)
114
+ Clock.advance_by_days 2 # now past verification deadline
115
+ get :welcome, :<%= singular_name %>=> { :id => <%= singular_name %>.id }, :key => <%= singular_name %>.security_token
116
+ <%= singular_name %>.reload
117
+ assert !<%= singular_name %>.verified
118
+ assert_not_logged_in
119
+ end
120
+
121
+ def test_welcome__fails_if_bad_token
122
+ <%= singular_name %> = <%= plural_name %>(:unverified_<%= singular_name %>)
123
+ Clock.time = Time.now # now before deadline, but with bad token
124
+ get :welcome, :<%= singular_name %>=> { :id => <%= singular_name %>.id }, :key => "boguskey"
125
+ <%= singular_name %>.reload
126
+ assert !<%= singular_name %>.verified
127
+ assert_not_logged_in
128
+ end
129
+
130
+ def test_edit
131
+ tesla = <%= plural_name %>(:tesla)
132
+ set_logged_in tesla
133
+ post :edit, :<%= singular_name %> => { :first_name => "Bob", :form => "edit" }
134
+ tesla.reload
135
+ assert_equal tesla.first_name, "Bob"
136
+ end
137
+
138
+ def test_delete
139
+ <%= singular_name %> = <%= plural_name %>(:deletable_<%= singular_name %>)
140
+ set_logged_in <%= singular_name %>
141
+ post :edit, "<%= singular_name %>" => { "form" => "delete" }
142
+ <%= singular_name %>.reload
143
+ assert <%= singular_name %>.deleted
144
+ assert_not_logged_in
145
+ end
146
+
147
+ def test_change_password
148
+ <%= singular_name %> = <%= plural_name %>(:tesla)
149
+ set_logged_in <%= singular_name %>
150
+ post :change_password, :<%= singular_name %> => { :password => "changed_password", :password_onfirmation => "changed_password" }
151
+ assert_equal 1, ActionMailer::Base.deliveries.size
152
+ mail = ActionMailer::Base.deliveries[0]
153
+ assert_equal "tesla@example.com", mail.to_addrs[0].to_s
154
+ assert_match /login:\s+\w+\n/, mail.encoded
155
+ assert_match /password:\s+\w+\n/, mail.encoded
156
+ assert_equal <%= singular_name %>, <%= class_name %>.authenticate(<%= singular_name %>.login, 'changed_password')
157
+ end
158
+
159
+ def test_change_password__confirms_password
160
+ set_logged_in <%= plural_name %>(:tesla)
161
+ post :change_password, :<%= singular_name %> => { :password => "bad", :password_confirmation => "bad" }
162
+ <%= singular_name %> = assigns(:<%= singular_name %>)
163
+ assert_equal 1, <%= singular_name %>.errors.size
164
+ assert_not_nil <%= singular_name %>.errors['password']
165
+ assert_response :success
166
+ assert_equal 0, ActionMailer::Base.deliveries.size
167
+ end
168
+
169
+ def test_change_password__succeeds_despite_delivery_errors
170
+ set_logged_in <%= plural_name %>(:tesla)
171
+ ActionMailer::Base.inject_one_error = true
172
+ post :change_password, :<%= singular_name %> => { :password => "changed_password", :password_confirmation => "changed_password" }
173
+ assert_equal 0, ActionMailer::Base.deliveries.size
174
+ assert_equal <%= plural_name %>(:tesla), <%= class_name %>.authenticate(<%= plural_name %>(:tesla).login, 'changed_password')
175
+ end
176
+
177
+ def test_forgot_password__when_logged_in_redirects_to_change_password
178
+ <%= singular_name %> = <%= plural_name %>(:tesla)
179
+ set_logged_in <%= singular_name %>
180
+ post :forgot_password, :<%= singular_name %> => { :email => <%= singular_name %>.email }
181
+ assert_equal 0, ActionMailer::Base.deliveries.size
182
+ assert_response :redirect
183
+ assert_equal @controller.url_for(:action => "change_password"), @response.redirect_url
184
+ end
185
+
186
+ def test_forgot_password__requires_valid_email_address
187
+ post :forgot_password, :<%= singular_name %> => { :email => "" }
188
+ assert_equal 0, ActionMailer::Base.deliveries.size
189
+ assert_match /Please enter a valid email address./, @response.body
190
+ end
191
+
192
+ def test_forgot_password__ignores_unknown_email_address
193
+ post :forgot_password, :<%= singular_name %> => { :email => "unknown_email@example.com" }
194
+ assert_equal 0, ActionMailer::Base.deliveries.size
195
+ end
196
+
197
+ def test_forgot_password__reports_delivery_error
198
+ ActionMailer::Base.inject_one_error = true
199
+ post :forgot_password, :<%= singular_name %> => { :email => <%= plural_name %>(:tesla).email }
200
+ assert_equal 0, ActionMailer::Base.deliveries.size
201
+ assert_match /Your password could not be emailed/, @response.body
202
+ end
203
+
204
+ def test_invalid_login
205
+ post :login, :<%= singular_name %> => { :login => "tesla", :password => "not_correct" }
206
+ assert_not_logged_in
207
+ assert_response :success
208
+ assert_template 'login'
209
+ end
210
+
211
+ def test_logout
212
+ set_logged_in <%= plural_name %>(:tesla)
213
+ get :logout
214
+ assert_not_logged_in
215
+ end
216
+
217
+ private
218
+
219
+ def set_logged_in( <%= singular_name %> )
220
+ @request.session[:<%= singular_name %>_id] = <%= singular_name %>.id
221
+ end
222
+
223
+ def assert_logged_in( <%= singular_name %> )
224
+ assert_equal <%= singular_name %>.id, @request.session[:<%= singular_name %>_id]
225
+ assert_equal <%= singular_name %>, assigns(:current_<%= singular_name %>)
226
+ end
227
+
228
+ def assert_not_logged_in
229
+ assert_nil @request.session[:<%= singular_name %>_id]
230
+ assert_nil assigns(:current_<%= singular_name %>)
231
+ end
232
+
233
+ def assert_redirected_to_login
234
+ assert_equal @controller.url_for(:action => "login"), @response.redirect_url
235
+ end
236
+
237
+ def post_signup( <%= singular_name %>_params )
238
+ post :signup, "<%= singular_name %>" => <%= singular_name %>_params
239
+ end
240
+
241
+ def assert_password_validation_fails
242
+ <%= singular_name %> = assigns(:<%= singular_name %>)
243
+ assert_equal 1, <%= singular_name %>.errors.size
244
+ assert_not_nil <%= singular_name %>.errors['password']
245
+ assert_response :success
246
+ assert_equal 0, ActionMailer::Base.deliveries.size
247
+ end
248
+
249
+ def assert_contains( target, container )
250
+ assert !container.nil?, %Q( Failed to find "#{target}" in nil String )
251
+ assert container.include?(target)
252
+ end
253
+
254
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.dirname(__FILE__) + '/../config/environment'
4
+ require 'rubygems'
5
+ require_gem 'db_structure'
6
+
7
+ DBStructure::db_structure
Binary file
@@ -0,0 +1,41 @@
1
+ module <%= class_name %>Helper
2
+
3
+ DEFAULT_HEAD_OPTIONS = {
4
+ :notice => true,
5
+ :message => true,
6
+ :error => false
7
+ }.freeze unless defined? DEFAULT_HEAD_OPTIONS
8
+
9
+ def title_helper
10
+ "#{@controller.controller_class_name} #{@controller.action_name}"
11
+ end
12
+
13
+ def head_helper(label, options = {})
14
+ notice = message = error = nil
15
+ opts = DEFAULT_HEAD_OPTIONS.dup
16
+ opts.update(options.symbolize_keys)
17
+ s = "<h3>#{label}</h3>"
18
+ if @flash['notice'] and not opts[:notice].nil? and opts[:notice]
19
+ notice = "<div><p>#{@flash['notice']}</p></div>"
20
+ s = s + notice
21
+ end
22
+ if @flash['message'] and not opts[:message].nil? and opts[:message]
23
+ message = "<div id=\"ErrorExplanation\"><p>#{@flash['message']}</p></div>"
24
+ s = s + message
25
+ end
26
+ if not opts[:error].nil? and opts[:error]
27
+ error = error_messages_for('<%= singular_name %>')
28
+ if not error.nil?
29
+ error = error + "<br/>"
30
+ s = s + error
31
+ end
32
+ end
33
+ return s
34
+ end
35
+
36
+ def start_form_tag_helper(options = {})
37
+ url = url_for(:action => "#{@controller.action_name}")
38
+ "#{self.send(:start_form_tag, url, options)}"
39
+ end
40
+
41
+ end
@@ -0,0 +1,95 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+ require '<%= singular_name %>_controller'
3
+ require '<%= singular_name %>_notify'
4
+
5
+ class <%= class_name %>SystemTest < ActionController::IntegrationTest
6
+ self.use_transactional_fixtures = false
7
+ fixtures :<%= plural_name %>
8
+
9
+ def setup
10
+ ActionMailer::Base.inject_one_error = false
11
+ ActionMailer::Base.deliveries = []
12
+ end
13
+
14
+ def test_signup_and_verify
15
+ Clock.time = Time.now
16
+ post url_for( :controller => '<%= singular_name %>', :action => 'signup'),
17
+ :<%= singular_name %> => { :login => "new<%= singular_name %>",
18
+ :password => "password", :password_confirmation => "password",
19
+ :email => "newemail@example.com" }
20
+
21
+ assert_not_logged_in
22
+ assert_redirected_to_login
23
+ assert_equal 1, ActionMailer::Base.deliveries.size
24
+
25
+ mail = ActionMailer::Base.deliveries[0]
26
+ assert_equal "newemail@example.com", mail.to_addrs[0].to_s
27
+ assert_match /login:\s+\w+\n/, mail.encoded
28
+ assert_match /password:\s+\w+\n/, mail.encoded
29
+ mail.encoded =~ /<%= file_name %>\[id\]=(\d+)&key=(.*?)"/
30
+ id = $1
31
+ key = $2
32
+
33
+ Clock.advance_by_days 2 # now past verification deadline
34
+
35
+ get url_for( :controller => '<%= singular_name %>', :action => 'welcome'),
36
+ :<%= singular_name %>=> { :id => id }, :key => key
37
+ assert_redirected_to_login
38
+ <%= singular_name %> = <%= class_name %>.find_by_id id
39
+ assert !<%= singular_name %>.verified
40
+ assert_not_logged_in
41
+
42
+ Clock.time = Time.now # now before deadline
43
+ get url_for( :controller => '<%= singular_name %>', :action => 'welcome'),
44
+ :<%= singular_name %>=> { :id => "#{id}" }, :key => "boguskey"
45
+ assert_redirected_to_login
46
+ assert_not_logged_in
47
+ <%= singular_name %>.reload
48
+ assert !<%= singular_name %>.verified
49
+
50
+ get url_for( :controller => '<%= singular_name %>', :action => 'welcome'),
51
+ :<%= singular_name %>=> { :id => "#{<%= singular_name %>.id}" }, :key => "#{key}"
52
+ assert_response :success
53
+ <%= singular_name %>.reload
54
+ assert <%= singular_name %>.verified
55
+ assert_logged_in( <%= singular_name %> )
56
+ end
57
+
58
+ def test_forgot_password__allows_change_password_after_mailing_key
59
+ <%= singular_name %> = <%= plural_name %>(:tesla)
60
+ post url_for( :controller => '<%= singular_name %>', :action => 'forgot_password'), :<%= singular_name %> => { :email => <%= singular_name %>.email }
61
+ assert_equal 1, ActionMailer::Base.deliveries.size
62
+ mail = ActionMailer::Base.deliveries[0]
63
+ assert_equal <%= plural_name %>(:tesla).email, mail.to_addrs[0].to_s
64
+ mail.encoded =~ /<%= file_name %>\[id\]=(.*?)&key=(.*?)"/
65
+ id = $1
66
+ key = $2
67
+ post url_for( :controller => '<%= singular_name %>', :action => 'change_password'),
68
+ :<%= singular_name %> => { :password => "newpassword",
69
+ :password_confirmation => "newpassword",
70
+ :id => id },
71
+ :key => key
72
+ <%= singular_name %>.reload
73
+ assert_logged_in <%= singular_name %>
74
+ assert_equal <%= singular_name %>, <%= class_name %>.authenticate(<%= singular_name %>.login, 'newpassword')
75
+ end
76
+
77
+
78
+
79
+ private
80
+ def assert_logged_in( <%= singular_name %> )
81
+ assert_equal <%= singular_name %>.id, request.session[:<%= singular_name %>_id]
82
+ assert_equal <%= singular_name %>, assigns(:current_<%= singular_name %>)
83
+ end
84
+
85
+ def assert_not_logged_in
86
+ assert_nil request.session[:<%= singular_name %>_id]
87
+ assert_nil assigns(:current_<%= singular_name %>)
88
+ end
89
+
90
+ def assert_redirected_to_login
91
+ assert_response :redirect
92
+ assert_equal controller.url_for(:action => "login"), response.redirect_url
93
+ end
94
+
95
+ end
@@ -0,0 +1,13 @@
1
+ <html>
2
+ <head>
3
+ <title>: <%%= controller.action_name %></title>
4
+ <%%= stylesheet_link_tag '<%= singular_name %>' %>
5
+ </head>
6
+ <body>
7
+
8
+ <p style="color: green"><%%= flash[:notice] %></p>
9
+
10
+ <%%= yield %>
11
+
12
+ </body>
13
+ </html>
@@ -0,0 +1,21 @@
1
+ module <%= class_name %>System
2
+ CONFIG = {
3
+ # Source address for <%= singular_name %> emails
4
+ :email_from => 'foo@example.com',
5
+
6
+ # Destination email for system errors
7
+ :admin_email => 'foo@example.com',
8
+
9
+ # Sent in emails to <%= plural_name %>
10
+ :app_url => 'http://example.com/',
11
+
12
+ # Sent in emails to <%= plural_name %>
13
+ :app_name => 'Login Sugar',
14
+
15
+ # Email charset
16
+ :mail_charset => 'utf-8',
17
+
18
+ # Security token lifetime in hours
19
+ :security_token_life_hours => 24,
20
+ }
21
+ end
@@ -0,0 +1,54 @@
1
+ module <%= class_name %>System
2
+
3
+ protected
4
+
5
+ # authenticate_<%= singular_name %> filter. add
6
+ #
7
+ # before_filter :authenticate_<%= singular_name %>
8
+ #
9
+ def authenticate_<%= singular_name %>
10
+ return true if authenticated_<%= singular_name %>?
11
+ session[:return_to] = request.request_uri
12
+ access_denied
13
+ return false
14
+ end
15
+
16
+ # overwrite if you want to have special behavior in case the <%= singular_name %> is not authorized
17
+ # to access the current operation.
18
+ # the default action is to redirect to the login screen
19
+ # example use :
20
+ # a popup window might just close itself for instance
21
+ def access_denied
22
+ redirect_to :controller => "/<%= file_name %>", :action => "login"
23
+ end
24
+
25
+ def redirect_back_or_default(default)
26
+ if session[:return_to].nil?
27
+ redirect_to default
28
+ else
29
+ redirect_to_url session[:return_to]
30
+ session[:return_to] = nil
31
+ end
32
+ end
33
+
34
+ def authenticated_<%= singular_name %>?
35
+ if session[:<%= singular_name %>_id]
36
+ @current_<%= singular_name %> = <%= class_name %>.find_by_id(session[:<%= singular_name %>_id])
37
+ return false if @current_<%= singular_name %>.nil?
38
+ return true
39
+ end
40
+
41
+ # If not, is the <%= singular_name %> being authenticated by a token (created by signup/forgot password actions)?
42
+ return false if not params['<%= singular_name %>']
43
+ id = params['<%= singular_name %>']['id']
44
+ key = params['key']
45
+ if id and key
46
+ @current_<%= singular_name %> = <%= class_name %>.authenticate_by_token(id, key)
47
+ session[:<%= singular_name %>_id] = @current_<%= singular_name %> ? @current_<%= singular_name %>.id : nil
48
+ return true if not @current_<%= singular_name %>.nil?
49
+ end
50
+
51
+ # Everything failed
52
+ return false
53
+ end
54
+ end