cybertrain 0.3.1 → 0.4.1
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.
- checksums.yaml +4 -4
- data/README.md +82 -2049
- data/cybertrain/cli/attachment.rb +246 -164
- data/cybertrain/cli/authentication.rb +2405 -898
- data/cybertrain/cli/authentication_tests.rb +3175 -0
- data/cybertrain/cli/build.rb +93 -7
- data/cybertrain/cli/component.rb +118 -0
- data/cybertrain/cli/helper.rb +143 -0
- data/cybertrain/cli/job.rb +159 -0
- data/cybertrain/cli/line_diff.rb +216 -0
- data/cybertrain/cli/new_app.rb +17 -7
- data/cybertrain/cli/scaffold.rb +29 -1
- data/cybertrain/cli/templates.rb +405 -29
- data/cybertrain/cli/test_command.rb +301 -0
- data/cybertrain/cli/toolchain.rb +12 -12
- data/cybertrain/cli.rb +145 -24
- data/cybertrain/crypto.rb +136 -0
- data/cybertrain/ident.rb +9 -1
- data/cybertrain/shell.rb +13 -0
- data/cybertrain/test/args.rb +286 -0
- data/cybertrain/test_files.rb +48 -0
- data/cybertrain/version.rb +2 -2
- metadata +11 -2
|
@@ -0,0 +1,3175 @@
|
|
|
1
|
+
# The tests `cybertrain generate authentication` writes (spec section 11,
|
|
2
|
+
# third bullet): Rails-shaped classes that `cybertrain test` runs in one
|
|
3
|
+
# program, on the fixtures of test/fixtures/users.yml, signing in with
|
|
4
|
+
# SessionTestHelper#sign_in_as (test/support/session_test_helper.rb, which
|
|
5
|
+
# test/test_helper.rb includes into ActionDispatch::IntegrationTest).
|
|
6
|
+
#
|
|
7
|
+
# test/fixtures/users.yml one (an admin) and two
|
|
8
|
+
# test/support/session_test_helper.rb sign_in_as, sign_out, open_session, authenticity_token_in
|
|
9
|
+
# test/models/user_test.rb the address format
|
|
10
|
+
# test/controllers/sessions_controller_test.rb sign-in and sign-out
|
|
11
|
+
# test/controllers/registrations_controller_test.rb sign-up (not --invite-only)
|
|
12
|
+
# test/integration/first_run_test.rb /setup and the user command (--invite-only)
|
|
13
|
+
# test/controllers/passwords_controller_test.rb the reset, its mail and link
|
|
14
|
+
# test/controllers/accounts_controller_test.rb the account page
|
|
15
|
+
# test/controllers/confirmations_controller_test.rb --confirmable
|
|
16
|
+
# test/integration/remember_me_test.rb day 15 and day 30, the idle limit
|
|
17
|
+
# test/integration/lockout_test.rb the lock and its hour
|
|
18
|
+
# test/integration/admin_test.rb require_admin, user admin
|
|
19
|
+
#
|
|
20
|
+
# Time moves with travel_to and travel (no Session row is moved back in
|
|
21
|
+
# time), and mail is read from Cybertrain::Mailer.deliveries (the test
|
|
22
|
+
# environment stores it) with assert_emails and perform_enqueued_jobs.
|
|
23
|
+
# Phase 4b's top-level test files these replace (TEST_FILES_REPLACED) are
|
|
24
|
+
# named on a re-run, and deleted with --force (plan decision D15).
|
|
25
|
+
require "digest"
|
|
26
|
+
require "base64"
|
|
27
|
+
|
|
28
|
+
module Cybertrain
|
|
29
|
+
module CLI
|
|
30
|
+
module Authentication
|
|
31
|
+
# The test files an earlier generator wrote (phase 4b, and 0.4's
|
|
32
|
+
# test/authentication.rb and test/support/app_test.rb), each with what
|
|
33
|
+
# replaces it: a re-run names them (`manual <path>: replaced by ...;
|
|
34
|
+
# delete it`) and --force deletes them. Pairs of [old, new].
|
|
35
|
+
TEST_FILES_REPLACED = [
|
|
36
|
+
["test/authentication.rb", "test/controllers/sessions_controller_test.rb"],
|
|
37
|
+
["test/admin.rb", "test/integration/admin_test.rb"],
|
|
38
|
+
["test/remember_me.rb", "test/integration/remember_me_test.rb"],
|
|
39
|
+
["test/lockout.rb", "test/integration/lockout_test.rb"],
|
|
40
|
+
["test/passwords.rb", "test/controllers/passwords_controller_test.rb"],
|
|
41
|
+
["test/accounts.rb", "test/controllers/accounts_controller_test.rb"],
|
|
42
|
+
["test/confirmations.rb", "test/controllers/confirmations_controller_test.rb"],
|
|
43
|
+
["test/support/app_test.rb", "test/test_helper.rb"],
|
|
44
|
+
["test/support/authentication_helpers.rb", Templates::SESSION_TEST_HELPER]
|
|
45
|
+
].freeze
|
|
46
|
+
|
|
47
|
+
# The password of every user in test/fixtures/users.yml.
|
|
48
|
+
FIXTURE_PASSWORD = "password"
|
|
49
|
+
|
|
50
|
+
# What test/test_helper.rb gets after the class that holds
|
|
51
|
+
# Templates::TEST_HELPER_MARKER: SessionTestHelper in every
|
|
52
|
+
# integration and controller test.
|
|
53
|
+
TEST_HELPER_INCLUDE = "class ActionDispatch::IntegrationTest\n include SessionTestHelper\nend\n"
|
|
54
|
+
|
|
55
|
+
# The scaffold's controller test setup line (Templates.controller_test):
|
|
56
|
+
# ` @product = products(:one)`, after which an app with
|
|
57
|
+
# authentication signs in.
|
|
58
|
+
SIGN_IN_LINE = " sign_in_as users(:one)\n"
|
|
59
|
+
|
|
60
|
+
# Cybertrain::Password's test-environment cost (Password::TEST_ITERATIONS),
|
|
61
|
+
# salt length and algorithm name. The gem's CLI does not load the
|
|
62
|
+
# framework's password.rb (it needs the configuration), so the digest
|
|
63
|
+
# is made here, in the same format; test/cli_authentication.rb checks
|
|
64
|
+
# it with Password.verify?.
|
|
65
|
+
FIXTURE_ITERATIONS = 1000
|
|
66
|
+
FIXTURE_SALT_BYTES = 16
|
|
67
|
+
|
|
68
|
+
# The password_digest of a fixture user: Cybertrain::Password's
|
|
69
|
+
# format (`$pbkdf2-sha256$i=N$salt$key`) for FIXTURE_PASSWORD at the
|
|
70
|
+
# test environment's cost (so no sign-in rehashes it), with a salt
|
|
71
|
+
# taken from the label so that a re-run writes the same file (fixtures
|
|
72
|
+
# are not ERB: the digest is written out).
|
|
73
|
+
def self.fixture_digest(label)
|
|
74
|
+
salt = Digest::SHA256.digest("cybertrain fixture users #{label}")[0, FIXTURE_SALT_BYTES].to_s
|
|
75
|
+
key = fixture_pbkdf2(FIXTURE_PASSWORD, salt, FIXTURE_ITERATIONS)
|
|
76
|
+
"$pbkdf2-sha256$i=#{FIXTURE_ITERATIONS}$#{Base64.urlsafe_encode64(salt, padding: false)}$#{Base64.urlsafe_encode64(key, padding: false)}"
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# PBKDF2-HMAC-SHA256, one 32-byte block, for a password of at most 64
|
|
80
|
+
# bytes (Cybertrain::Password.pbkdf2_sha256's loop).
|
|
81
|
+
def self.fixture_pbkdf2(password, salt, iterations)
|
|
82
|
+
ipad = ("6" * 64).b
|
|
83
|
+
opad = ("\\" * 64).b
|
|
84
|
+
n = password.bytesize
|
|
85
|
+
i = 0
|
|
86
|
+
while i < n
|
|
87
|
+
b = password.getbyte(i)
|
|
88
|
+
ipad.setbyte(i, b ^ 0x36)
|
|
89
|
+
opad.setbyte(i, b ^ 0x5c)
|
|
90
|
+
i += 1
|
|
91
|
+
end
|
|
92
|
+
u = Digest::SHA256.digest(opad + Digest::SHA256.digest(ipad + salt.b + "\x00\x00\x00\x01".b))
|
|
93
|
+
out = u.dup
|
|
94
|
+
c = 1
|
|
95
|
+
while c < iterations
|
|
96
|
+
u = Digest::SHA256.digest(opad + Digest::SHA256.digest(ipad + u))
|
|
97
|
+
j = 0
|
|
98
|
+
while j < 32
|
|
99
|
+
out.setbyte(j, out.getbyte(j) ^ u.getbyte(j))
|
|
100
|
+
j += 1
|
|
101
|
+
end
|
|
102
|
+
c += 1
|
|
103
|
+
end
|
|
104
|
+
out
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# test/fixtures/users.yml: `one` (an admin) and `two`, both with the
|
|
108
|
+
# password FIXTURE_PASSWORD; with `confirmable`, both confirmed.
|
|
109
|
+
def self.users_fixture(confirmable = false)
|
|
110
|
+
confirmed = confirmable ? " confirmed_at: 2026-11-11 12:00:00\n" : ""
|
|
111
|
+
out = +""
|
|
112
|
+
out << "# The users every test starts with (cybertrain generate authentication).\n"
|
|
113
|
+
out << "# Their password is \"#{FIXTURE_PASSWORD}\": each digest is Cybertrain::Password's,\n"
|
|
114
|
+
out << "# made when the generator ran (a fixture is not ERB, so it is written\n"
|
|
115
|
+
out << "# out). Sign one in with `sign_in_as users(:one)`.\n"
|
|
116
|
+
out << "one:\n"
|
|
117
|
+
out << " email_address: one@example.com\n"
|
|
118
|
+
out << " password_digest: #{fixture_digest("one")}\n"
|
|
119
|
+
out << " admin: true\n"
|
|
120
|
+
out << " failed_attempts: 0\n"
|
|
121
|
+
out << confirmed
|
|
122
|
+
out << "\n"
|
|
123
|
+
out << "two:\n"
|
|
124
|
+
out << " email_address: two@example.com\n"
|
|
125
|
+
out << " password_digest: #{fixture_digest("two")}\n"
|
|
126
|
+
out << " admin: false\n"
|
|
127
|
+
out << " failed_attempts: 0\n"
|
|
128
|
+
out << confirmed
|
|
129
|
+
out
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# test/support/session_test_helper.rb, as Rails 8's generator writes
|
|
133
|
+
# test/test_helpers/session_test_helper.rb (here it signs in through
|
|
134
|
+
# the form).
|
|
135
|
+
def self.session_test_helper
|
|
136
|
+
<<~'RUBY'
|
|
137
|
+
# Signing in and out in tests (cybertrain generate authentication), as
|
|
138
|
+
# Rails 8's generator writes it: test/test_helper.rb includes it into
|
|
139
|
+
# ActionDispatch::IntegrationTest, so every controller and integration
|
|
140
|
+
# test can sign in a fixture user (test/fixtures/users.yml).
|
|
141
|
+
#
|
|
142
|
+
# Written by the generator; it is yours to change.
|
|
143
|
+
module SessionTestHelper
|
|
144
|
+
# Signs user in through the sign-in form, as a browser would: every
|
|
145
|
+
# fixture user's password is "password". `remember: true` checks the
|
|
146
|
+
# form's "Keep me signed in" box.
|
|
147
|
+
def sign_in_as(user, remember: false)
|
|
148
|
+
fields = { email_address: user.email_address, password: "password" }
|
|
149
|
+
# A browser sends an unchecked box not at all.
|
|
150
|
+
fields[:remember_me] = "1" if remember
|
|
151
|
+
post session_path, params: fields
|
|
152
|
+
nil
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# Signs this browser out.
|
|
156
|
+
def sign_out = delete(session_path)
|
|
157
|
+
|
|
158
|
+
# The authenticity_token of the first form in a page (body), for a
|
|
159
|
+
# request sent inside with_forgery_protection.
|
|
160
|
+
def authenticity_token_in(body)
|
|
161
|
+
marker = "name=\"authenticity_token\" value=\""
|
|
162
|
+
at = body.index(marker)
|
|
163
|
+
raise "no authenticity_token in the page" if at.nil?
|
|
164
|
+
|
|
165
|
+
start = at + marker.size
|
|
166
|
+
stop = body.index("\"", start).to_i
|
|
167
|
+
body[start, stop - start].to_s
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Another browser, with cookies of its own, as Rails' open_session
|
|
171
|
+
# gives: `phone = open_session`, then `phone.sign_in_as users(:one)`,
|
|
172
|
+
# `phone.get ...` and `phone.assert_response :success`.
|
|
173
|
+
def open_session = ActionDispatch::IntegrationTest.new
|
|
174
|
+
end
|
|
175
|
+
RUBY
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# test/test_helper.rb with SessionTestHelper included into
|
|
179
|
+
# ActionDispatch::IntegrationTest, after the end of the class that
|
|
180
|
+
# holds Templates::TEST_HELPER_MARKER, together with the require of
|
|
181
|
+
# its file; "" when the marker or that end is not there.
|
|
182
|
+
def self.helper_with_session(source)
|
|
183
|
+
at = source.index("#{Templates::TEST_HELPER_MARKER}\n")
|
|
184
|
+
return "" if at.nil?
|
|
185
|
+
|
|
186
|
+
stop = source.index("\nend\n", at)
|
|
187
|
+
return "" if stop.nil?
|
|
188
|
+
|
|
189
|
+
cut = stop + 5
|
|
190
|
+
out = +""
|
|
191
|
+
out << source[0, cut].to_s
|
|
192
|
+
out << "\nrequire_relative \"support/session_test_helper\"\n\n"
|
|
193
|
+
out << TEST_HELPER_INCLUDE
|
|
194
|
+
out << source[cut, source.size - cut].to_s
|
|
195
|
+
out
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# The helper every test file requires (`require "test_helper"`).
|
|
199
|
+
TEST_HELPER = "test/test_helper.rb"
|
|
200
|
+
|
|
201
|
+
# test/test_helper.rb (Templates.test_helper, written when missing, as
|
|
202
|
+
# in an application 0.4 generated) gets SessionTestHelper included into
|
|
203
|
+
# ActionDispatch::IntegrationTest, once; a helper without the marker
|
|
204
|
+
# is left alone with the lines to add.
|
|
205
|
+
def self.add_session_helper(result, root)
|
|
206
|
+
path = File.join(root, TEST_HELPER)
|
|
207
|
+
unless File.exist?(path)
|
|
208
|
+
Templates.write(root, TEST_HELPER, Templates.test_helper)
|
|
209
|
+
result.created << TEST_HELPER
|
|
210
|
+
end
|
|
211
|
+
source = File.read(path)
|
|
212
|
+
unless source.index("include SessionTestHelper").nil?
|
|
213
|
+
puts "identical #{TEST_HELPER} (includes SessionTestHelper)"
|
|
214
|
+
return nil
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
changed = helper_with_session(source)
|
|
218
|
+
if changed.empty?
|
|
219
|
+
puts "skip #{TEST_HELPER} (no `#{Templates::TEST_HELPER_MARKER.strip}` line): add require_relative \"support/session_test_helper\""
|
|
220
|
+
puts " and #{TEST_HELPER_INCLUDE.gsub("\n", "; ").delete_suffix("; ")} to it"
|
|
221
|
+
return nil
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
File.write(path, changed)
|
|
225
|
+
puts "insert #{TEST_HELPER} (include SessionTestHelper)"
|
|
226
|
+
nil
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# Phase 4b's (and 0.4's) test files that the class-form ones replace
|
|
230
|
+
# (TEST_FILES_REPLACED), where they exist: named in result.manual, or
|
|
231
|
+
# with `force` deleted (result.removed). They would otherwise run in
|
|
232
|
+
# `cybertrain test` beside the new ones, each defining its own
|
|
233
|
+
# top-level new_client, which the bundle refuses.
|
|
234
|
+
def self.record_replaced_tests(result, root, force)
|
|
235
|
+
if File.exist?(File.join(root, "test/support/app_test.rb"))
|
|
236
|
+
app_test_users(root).each do |user|
|
|
237
|
+
puts "note #{user} uses test/support/app_test.rb (AppTest), which test/test_helper.rb replaces: " \
|
|
238
|
+
"move it to the class form (require \"test_helper\", sign_in_as users(:one)) before deleting it"
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
TEST_FILES_REPLACED.each do |pair|
|
|
242
|
+
old = pair[0]
|
|
243
|
+
path = File.join(root, old)
|
|
244
|
+
next unless File.exist?(path)
|
|
245
|
+
|
|
246
|
+
if force
|
|
247
|
+
File.delete(path)
|
|
248
|
+
puts "remove #{old} (replaced by #{pair[1]})"
|
|
249
|
+
result.removed << old
|
|
250
|
+
else
|
|
251
|
+
puts "manual #{old} (replaced by #{pair[1]})"
|
|
252
|
+
result.manual << "manual #{old}: replaced by #{pair[1]}; delete it\n"
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
nil
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# The test files (other than TEST_FILES_REPLACED) that require
|
|
259
|
+
# test/support/app_test.rb or call AppTest: an application's own tests
|
|
260
|
+
# written in phase 4b's shape, which lose their helper with it.
|
|
261
|
+
def self.app_test_users(root)
|
|
262
|
+
replaced = Array.new(0) { "" }
|
|
263
|
+
TEST_FILES_REPLACED.each { |pair| replaced << pair[0] }
|
|
264
|
+
out = Array.new(0) { "" }
|
|
265
|
+
Dir.glob(File.join(root, "test/**/*.rb")).sort.each do |path|
|
|
266
|
+
rel = path.delete_prefix("#{root}/")
|
|
267
|
+
next if replaced.include?(rel)
|
|
268
|
+
|
|
269
|
+
text = File.read(path)
|
|
270
|
+
out << rel unless text.index("support/app_test").nil? && text.index("AppTest").nil?
|
|
271
|
+
end
|
|
272
|
+
out
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
# The earlier generator's top-level-form attachment tests
|
|
276
|
+
# (test/attachment_<model>_<field>.rb) that sign nobody in: named by
|
|
277
|
+
# add_sign_in_to_attachment_tests, never rewritten.
|
|
278
|
+
def self.unsigned_attachment_tests(root)
|
|
279
|
+
out = Array.new(0) { "" }
|
|
280
|
+
Dir.glob(File.join(root, "test/attachment_*.rb")).sort.each do |path|
|
|
281
|
+
out << "test/#{File.basename(path)}" if File.read(path).index("/session").nil?
|
|
282
|
+
end
|
|
283
|
+
out
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
# A scaffold's controller test written before this generator ran
|
|
287
|
+
# (Templates.controller_test: ` setup do` then ` @x = xs(:one)`)
|
|
288
|
+
# meets the sign-in page at every action now: it signs in first,
|
|
289
|
+
# once. Any other controller test is left as it is.
|
|
290
|
+
def self.add_sign_in_to_controller_tests(root)
|
|
291
|
+
Dir.glob(File.join(root, "test/controllers/*_controller_test.rb")).sort.each do |path|
|
|
292
|
+
source = File.read(path)
|
|
293
|
+
next unless source.index(SIGN_IN_LINE).nil?
|
|
294
|
+
|
|
295
|
+
at = source.index("\n#{TEST_SETUP_LINE}\n")
|
|
296
|
+
next if at.nil?
|
|
297
|
+
|
|
298
|
+
line_at = at + TEST_SETUP_LINE.size + 2
|
|
299
|
+
stop = source.index("\n", line_at)
|
|
300
|
+
next if stop.nil?
|
|
301
|
+
next unless Templates.scaffold_setup_line?(source[line_at, stop - line_at].to_s)
|
|
302
|
+
|
|
303
|
+
File.write(path, insert_after_line(source, "#{TEST_SETUP_LINE}\n", SIGN_IN_LINE))
|
|
304
|
+
puts "insert test/controllers/#{File.basename(path)} (sign_in_as users(:one))"
|
|
305
|
+
end
|
|
306
|
+
nil
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
# ---- the test files ---------------------------------------------
|
|
310
|
+
|
|
311
|
+
# test/models/user_test.rb: the address User accepts.
|
|
312
|
+
def self.user_model_test
|
|
313
|
+
<<~'RUBY'
|
|
314
|
+
# The User model's address check (cybertrain generate authentication).
|
|
315
|
+
require "test_helper"
|
|
316
|
+
|
|
317
|
+
class UserTest < ActiveSupport::TestCase
|
|
318
|
+
# User.email_address_format?: one address, whatever the mail
|
|
319
|
+
# standards allow beyond that.
|
|
320
|
+
test "an address is one address: one @, nothing around it that splits it" do
|
|
321
|
+
["ann@example.com", "ann+news@example.com", "ann.lee+news@mail.example.co.jp", "a@b", "o'brien@example.com"].each do |address|
|
|
322
|
+
assert User.email_address_format?(address), address
|
|
323
|
+
end
|
|
324
|
+
["", "ann", "@example.com", "ann@", "a@b@example.com", "ann@example.com,eve@evil.example",
|
|
325
|
+
"ann@example.com eve@evil.example", "ann @example.com", "<ann@example.com>", "Ann <ann@example.com>",
|
|
326
|
+
"ann@example.com\n", "ann@exa\tmple.com", "\"ann lee\"@example.com",
|
|
327
|
+
"ann@example.com\u00A0", "ann\u3000@example.com", "ann\u0085@example.com", "ann@exa\u2028mple.com",
|
|
328
|
+
"ann;x@example.com", "ann(x)@example.com", "ann@[example].com", "ann:x@example.com"].each do |address|
|
|
329
|
+
refute User.email_address_format?(address), address
|
|
330
|
+
end
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
# An address saved before the check existed (an older release's) is
|
|
334
|
+
# not checked again: its owner can still reset the password.
|
|
335
|
+
test "an address saved before the format check still saves" do
|
|
336
|
+
user = users(:one)
|
|
337
|
+
user.update_columns(email_address: "one@example.com,old@example.com")
|
|
338
|
+
kept = User.find(user.id)
|
|
339
|
+
kept.password = "an0ther-s3cr3t"
|
|
340
|
+
assert kept.save
|
|
341
|
+
kept.email_address = "one@example.com,new@example.com"
|
|
342
|
+
refute kept.save
|
|
343
|
+
assert_includes kept.errors.full_messages, "Email address is invalid"
|
|
344
|
+
end
|
|
345
|
+
end
|
|
346
|
+
RUBY
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
# test/controllers/sessions_controller_test.rb; with --invite-only the
|
|
350
|
+
# sign-in page links to no sign-up page.
|
|
351
|
+
def self.sessions_controller_test(invite_only = false)
|
|
352
|
+
link = if invite_only
|
|
353
|
+
"refute response.body.include?(\"/sign_up\")\n"
|
|
354
|
+
else
|
|
355
|
+
"assert_includes response.body, \"<a href=\\\"/sign_up\\\">Sign up</a>\"\n"
|
|
356
|
+
end
|
|
357
|
+
open_actions = invite_only ? "sign-in and setup" : "sign-in and sign-up"
|
|
358
|
+
text = sessions_controller_test_body.sub("@OPEN_ACTIONS@", open_actions)
|
|
359
|
+
splice(text, "@SIGN_UP_LINK@", margin(link, 4))
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
def self.sessions_controller_test_body
|
|
363
|
+
<<~'RUBY'
|
|
364
|
+
# Signing in and out (cybertrain generate authentication) through the
|
|
365
|
+
# full stack: the session cookie, the router, the Authentication
|
|
366
|
+
# concern, the CSRF check.
|
|
367
|
+
require "test_helper"
|
|
368
|
+
|
|
369
|
+
# after_authentication_url is private (the Authentication concern's): a
|
|
370
|
+
# controller made for these tests reads it.
|
|
371
|
+
class ReturnPathProbeController < ApplicationController
|
|
372
|
+
def probe_after_authentication_url
|
|
373
|
+
after_authentication_url
|
|
374
|
+
end
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
class SessionsControllerTest < ActionDispatch::IntegrationTest
|
|
378
|
+
# Every action but @OPEN_ACTIONS@ needs a user: DELETE /session is
|
|
379
|
+
# the one such action this test can rely on before the app has pages
|
|
380
|
+
# of its own (a `root` route, a scaffold).
|
|
381
|
+
test "a visitor is sent to the sign-in page" do
|
|
382
|
+
delete "/session"
|
|
383
|
+
assert_redirected_to "/session/new"
|
|
384
|
+
assert_response :see_other
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
test "GET /session/new renders the sign-in form" do
|
|
388
|
+
get "/session/new"
|
|
389
|
+
assert_response :ok
|
|
390
|
+
assert_includes response.body, "<h1>Sign in</h1>"
|
|
391
|
+
assert_includes response.body, "<form action=\"/session\" method=\"post\">"
|
|
392
|
+
assert_includes response.body, "name=\"email_address\""
|
|
393
|
+
assert_includes response.body, "type=\"password\" name=\"password\""
|
|
394
|
+
@SIGN_UP_LINK@
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
# Every form that signs in, out or resets carries the page's token
|
|
398
|
+
# (Cybertrain's CSRF check, off in tests until with_forgery_protection
|
|
399
|
+
# turns it on): without it, a 403 and nothing done.
|
|
400
|
+
test "POST /session, PUT /passwords/:token and DELETE /session without the form's token are refused" do
|
|
401
|
+
user = users(:one)
|
|
402
|
+
token = user.password_reset_token
|
|
403
|
+
with_forgery_protection do
|
|
404
|
+
post "/session", params: { email_address: "one@example.com", password: "password" }
|
|
405
|
+
assert_response :forbidden
|
|
406
|
+
assert_equal 0, Session.count
|
|
407
|
+
put "/passwords/#{token}", params: { password: "an0ther-s3cr3t", password_confirmation: "an0ther-s3cr3t" }
|
|
408
|
+
assert_response :forbidden
|
|
409
|
+
refute User.find_by_password_reset_token(token).nil?
|
|
410
|
+
end
|
|
411
|
+
# The sign-in form's own token is accepted.
|
|
412
|
+
with_forgery_protection do
|
|
413
|
+
get "/session/new"
|
|
414
|
+
post "/session", params: { authenticity_token: authenticity_token_in(response.body), email_address: "one@example.com", password: "password" }
|
|
415
|
+
assert_redirected_to "/"
|
|
416
|
+
end
|
|
417
|
+
assert_equal 1, Session.count
|
|
418
|
+
with_forgery_protection do
|
|
419
|
+
delete "/session"
|
|
420
|
+
assert_response :forbidden
|
|
421
|
+
assert_equal 1, Session.count
|
|
422
|
+
end
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
test "POST /session signs in with the right password" do
|
|
426
|
+
sign_in_as users(:one)
|
|
427
|
+
assert_redirected_to "/"
|
|
428
|
+
assert_response :see_other
|
|
429
|
+
assert_equal 1, Session.count
|
|
430
|
+
assert_equal "127.0.0.1", Session.first.ip_address
|
|
431
|
+
end
|
|
432
|
+
|
|
433
|
+
test "authentication returns to a local path once" do
|
|
434
|
+
ctx = Cybertrain::Context.new(Cybertrain::Request.new("GET", "/", {}, ""))
|
|
435
|
+
ctx.session = Cybertrain::Session.new
|
|
436
|
+
controller = ReturnPathProbeController.new(ctx)
|
|
437
|
+
assert_equal "/", controller.probe_after_authentication_url
|
|
438
|
+
["/", "/products", "/products/1?tab=details#title", "/products//1", "/caf%C3%A9"].each do |path|
|
|
439
|
+
controller.session[:return_to_after_authenticating] = path
|
|
440
|
+
assert_equal path, controller.probe_after_authentication_url
|
|
441
|
+
refute controller.session.key?(:return_to_after_authenticating)
|
|
442
|
+
assert_equal "/", controller.probe_after_authentication_url
|
|
443
|
+
end
|
|
444
|
+
end
|
|
445
|
+
|
|
446
|
+
test "authentication rejects unsafe return destinations and consumes them" do
|
|
447
|
+
ctx = Cybertrain::Context.new(Cybertrain::Request.new("GET", "/", {}, ""))
|
|
448
|
+
ctx.session = Cybertrain::Session.new
|
|
449
|
+
controller = ReturnPathProbeController.new(ctx)
|
|
450
|
+
unsafe = ["", "products", "https://evil.example/", "//evil.example/", "///evil.example/",
|
|
451
|
+
"/\\evil.example/", "/products\\evil", " //evil.example/", "/記事", "/" + "a" * 2048]
|
|
452
|
+
(0..32).each { |byte| unsafe << "/#{Cybertrain::Test.binary([byte])}/evil.example/" }
|
|
453
|
+
unsafe << "/#{Cybertrain::Test.binary([127])}/evil.example/"
|
|
454
|
+
unsafe.each do |path|
|
|
455
|
+
controller.session[:return_to_after_authenticating] = path
|
|
456
|
+
assert_equal "/", controller.probe_after_authentication_url
|
|
457
|
+
refute controller.session.key?(:return_to_after_authenticating)
|
|
458
|
+
end
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
test "POST /session with the wrong password shows an alert" do
|
|
462
|
+
post "/session", params: { email_address: "one@example.com", password: "wrong-password" }
|
|
463
|
+
assert_redirected_to "/session/new"
|
|
464
|
+
follow_redirect!
|
|
465
|
+
assert_includes response.body, "<p class=\"alert\">Try another email address or password.</p>"
|
|
466
|
+
assert_equal 0, Session.count
|
|
467
|
+
post "/session", params: { email_address: "nobody@example.com", password: "password" }
|
|
468
|
+
assert_redirected_to "/session/new"
|
|
469
|
+
end
|
|
470
|
+
|
|
471
|
+
# SessionsController's rate_limit: ten attempts per address, then even
|
|
472
|
+
# the right password goes back to the form until the window closes.
|
|
473
|
+
# The counts live in Cybertrain::Cache, in windows aligned to the
|
|
474
|
+
# epoch: travel_to holds the clock still, so the attempts share one
|
|
475
|
+
# window, then travel moves it a whole window on. The ten go to an
|
|
476
|
+
# address no user has: the limit counts per client, whatever the
|
|
477
|
+
# address, and there is no account for lockout
|
|
478
|
+
# (test/integration/lockout_test.rb) to lock.
|
|
479
|
+
test "POST /session past ten attempts is refused for a while" do
|
|
480
|
+
travel_to(Time.at(2_000_000_000)) do
|
|
481
|
+
wrong_password = { email_address: "nobody@example.com", password: "wrong-password" }
|
|
482
|
+
right_password = { email_address: "one@example.com", password: "password" }
|
|
483
|
+
10.times { post "/session", params: wrong_password }
|
|
484
|
+
post "/session", params: right_password
|
|
485
|
+
assert_redirected_to "/session/new"
|
|
486
|
+
assert_equal 0, Session.count
|
|
487
|
+
follow_redirect!
|
|
488
|
+
assert_includes response.body, "<p class=\"alert\">Try again later.</p>"
|
|
489
|
+
travel 3.minutes
|
|
490
|
+
post "/session", params: right_password
|
|
491
|
+
assert_redirected_to "/"
|
|
492
|
+
assert_equal 1, Session.count
|
|
493
|
+
end
|
|
494
|
+
end
|
|
495
|
+
|
|
496
|
+
test "DELETE /session signs out and drops the Session row" do
|
|
497
|
+
sign_in_as users(:one)
|
|
498
|
+
sign_out
|
|
499
|
+
assert_redirected_to "/session/new"
|
|
500
|
+
assert_equal 0, Session.count
|
|
501
|
+
follow_redirect!
|
|
502
|
+
assert_includes response.body, "<p class=\"notice\">You have been signed out.</p>"
|
|
503
|
+
assert_includes response.body, "<a href=\"/session/new\">Sign in</a>"
|
|
504
|
+
end
|
|
505
|
+
|
|
506
|
+
test "a session the database no longer has signs the browser out" do
|
|
507
|
+
sign_in_as users(:one)
|
|
508
|
+
Session.all.delete_all
|
|
509
|
+
delete "/session"
|
|
510
|
+
assert_redirected_to "/session/new"
|
|
511
|
+
end
|
|
512
|
+
|
|
513
|
+
test "changing the password signs out the sessions started before" do
|
|
514
|
+
user = users(:one)
|
|
515
|
+
sign_in_as user
|
|
516
|
+
get "/session/new"
|
|
517
|
+
assert_includes response.body, "<span>one@example.com</span>"
|
|
518
|
+
user.password = "another-s3cr3t"
|
|
519
|
+
user.save!
|
|
520
|
+
get "/session/new"
|
|
521
|
+
refute response.body.include?("<span>one@example.com</span>")
|
|
522
|
+
assert_equal 0, Session.count
|
|
523
|
+
post "/session", params: { email_address: "one@example.com", password: "another-s3cr3t" }
|
|
524
|
+
get "/session/new"
|
|
525
|
+
assert_includes response.body, "<span>one@example.com</span>"
|
|
526
|
+
end
|
|
527
|
+
|
|
528
|
+
# The Session row's own limit, apart from the cookie's: signed in for
|
|
529
|
+
# two minutes, then the limit lowered to one, so that a minute later
|
|
530
|
+
# the cookie is still within its own two (SessionStore keeps it) and
|
|
531
|
+
# only the row's check can end the sign-in. The row is saved at that
|
|
532
|
+
# moment, which moves its updated_at: neither that nor a rewritten
|
|
533
|
+
# cookie extends authentication (the limit counts from created_at).
|
|
534
|
+
test "an expired cookie cannot restore its user or reach a protected action" do
|
|
535
|
+
max_age = Cybertrain.config.session_absolute_max_age
|
|
536
|
+
begin
|
|
537
|
+
freeze_time
|
|
538
|
+
Cybertrain.config.session_absolute_max_age = 120
|
|
539
|
+
sign_in_as users(:one)
|
|
540
|
+
name = Cybertrain.config.session_cookie_name
|
|
541
|
+
cookie = "#{name}=#{cookies[name]}"
|
|
542
|
+
get "/session/new", headers: { "cookie" => cookie }
|
|
543
|
+
assert_includes response.body, "<span>one@example.com</span>"
|
|
544
|
+
|
|
545
|
+
Cybertrain.config.session_absolute_max_age = 60
|
|
546
|
+
travel 1.minute
|
|
547
|
+
row = Session.first
|
|
548
|
+
row.user_agent = "saved at the limit"
|
|
549
|
+
row.save!
|
|
550
|
+
assert_equal Time.current.to_i, Session.first.updated_at.to_i
|
|
551
|
+
get "/session/new", headers: { "cookie" => cookie }
|
|
552
|
+
refute response.body.include?("<span>one@example.com</span>")
|
|
553
|
+
assert_equal 0, Session.count
|
|
554
|
+
delete "/session", headers: { "cookie" => cookie }
|
|
555
|
+
assert_redirected_to "/session/new"
|
|
556
|
+
get "/session/new"
|
|
557
|
+
refute response.body.include?("<span>one@example.com</span>")
|
|
558
|
+
assert_includes response.body, "<a href=\"/session/new\">Sign in</a>"
|
|
559
|
+
|
|
560
|
+
# Replaying the original credential again still fails, even
|
|
561
|
+
# after the server removed its id from the browser's cookie.
|
|
562
|
+
get "/session/new", headers: { "cookie" => cookie }
|
|
563
|
+
refute response.body.include?("<span>one@example.com</span>")
|
|
564
|
+
ensure
|
|
565
|
+
Cybertrain.config.session_absolute_max_age = max_age
|
|
566
|
+
end
|
|
567
|
+
end
|
|
568
|
+
|
|
569
|
+
end
|
|
570
|
+
RUBY
|
|
571
|
+
end
|
|
572
|
+
|
|
573
|
+
# test/controllers/registrations_controller_test.rb (the sign-up
|
|
574
|
+
# page; not --invite-only). With `confirmable` the first test's
|
|
575
|
+
# sign-up signs nobody in (the mail it sends is
|
|
576
|
+
# confirmations_controller_test.rb's).
|
|
577
|
+
def self.registrations_controller_test(confirmable = false)
|
|
578
|
+
text = registrations_controller_test_body
|
|
579
|
+
splice(text, "@SIGN_UP_TEST@", margin(confirmable ? sign_up_confirm_test : sign_up_sign_in_test, 2))
|
|
580
|
+
end
|
|
581
|
+
|
|
582
|
+
def self.registrations_controller_test_body
|
|
583
|
+
<<~'RUBY'
|
|
584
|
+
# Signing up (cybertrain generate authentication): the form, the
|
|
585
|
+
# address and password checks, and the rate limit.
|
|
586
|
+
require "test_helper"
|
|
587
|
+
|
|
588
|
+
class RegistrationsControllerTest < ActionDispatch::IntegrationTest
|
|
589
|
+
@SIGN_UP_TEST@
|
|
590
|
+
|
|
591
|
+
test "POST /sign_up with a short or mismatched password re-renders the form" do
|
|
592
|
+
assert_no_difference(-> { User.count }) do
|
|
593
|
+
post "/sign_up", params: { user: { email_address: "ann@example.com", password: "short", password_confirmation: "other" } }
|
|
594
|
+
end
|
|
595
|
+
assert_response :unprocessable_entity
|
|
596
|
+
assert_includes response.body, "<li>Password is too short (minimum is 8 characters)</li>"
|
|
597
|
+
assert_includes response.body, "<li>Password confirmation doesn't match Password</li>"
|
|
598
|
+
post "/sign_up", params: { user: { email_address: "ann@example.com", password: "", password_confirmation: "" } }
|
|
599
|
+
assert_includes response.body, "<li>Password can't be blank</li>"
|
|
600
|
+
end
|
|
601
|
+
|
|
602
|
+
# One address only: Cybertrain::Mailer reads a comma as a second
|
|
603
|
+
# recipient, so the account would have two mailboxes. Nor any address
|
|
604
|
+
# the mailer would refuse (Cybertrain::Mailer.address_problem: a
|
|
605
|
+
# Unicode space or control character, ";", "(" and the like), which
|
|
606
|
+
# could never be sent its mails.
|
|
607
|
+
test "POST /sign_up refuses an address list or a malformed address" do
|
|
608
|
+
assert_no_difference(-> { User.count }) do
|
|
609
|
+
["ann@example.com,eve@evil.example", "ann@example.com eve@evil.example", "<ann@example.com>", "ann", "@example.com", "ann@", "a@b@example.com",
|
|
610
|
+
"ann@example.com\u00A0", "ann\u3000@example.com", "ann\u0085@example.com", "ann@exa\u2028mple.com", "ann;x@example.com", "ann(x)@example.com"].each do |address|
|
|
611
|
+
Cybertrain::RateLimit.reset_all!
|
|
612
|
+
post "/sign_up", params: { user: { email_address: address, password: "s3cr3t-s3cr3t", password_confirmation: "s3cr3t-s3cr3t" } }
|
|
613
|
+
assert_response :unprocessable_entity
|
|
614
|
+
assert_includes response.body, "<li>Email address is invalid</li>"
|
|
615
|
+
end
|
|
616
|
+
end
|
|
617
|
+
assert_equal 0, Session.count
|
|
618
|
+
# A plus-address is one address the mailer sends to.
|
|
619
|
+
Cybertrain::RateLimit.reset_all!
|
|
620
|
+
assert_difference(-> { User.count }, 1) do
|
|
621
|
+
post "/sign_up", params: { user: { email_address: "ann+news@example.com", password: "s3cr3t-s3cr3t", password_confirmation: "s3cr3t-s3cr3t" } }
|
|
622
|
+
end
|
|
623
|
+
assert_equal "ann+news@example.com", User.last.email_address
|
|
624
|
+
end
|
|
625
|
+
|
|
626
|
+
# RegistrationsController's rate_limit: ten sign-ups per client
|
|
627
|
+
# address, then the form again until the window closes (the clock
|
|
628
|
+
# held still by travel_to, as in POST /session's test). The ten send no
|
|
629
|
+
# password, so none of them costs a hash.
|
|
630
|
+
test "POST /sign_up past ten requests is refused for a while" do
|
|
631
|
+
travel_to(Time.at(2_000_000_000)) do
|
|
632
|
+
empty = { user: { email_address: "nobody@example.com", password: "", password_confirmation: "" } }
|
|
633
|
+
right = { user: { email_address: "ann@example.com", password: "s3cr3t-s3cr3t", password_confirmation: "s3cr3t-s3cr3t" } }
|
|
634
|
+
10.times { post "/sign_up", params: empty }
|
|
635
|
+
assert_no_difference(-> { User.count }) do
|
|
636
|
+
post "/sign_up", params: right
|
|
637
|
+
end
|
|
638
|
+
assert_redirected_to "/sign_up"
|
|
639
|
+
follow_redirect!
|
|
640
|
+
assert_includes response.body, "<p class=\"alert\">Try again later.</p>"
|
|
641
|
+
travel 3.minutes
|
|
642
|
+
assert_difference(-> { User.count }, 1) do
|
|
643
|
+
post "/sign_up", params: right
|
|
644
|
+
end
|
|
645
|
+
end
|
|
646
|
+
end
|
|
647
|
+
|
|
648
|
+
test "an email address can be registered once" do
|
|
649
|
+
assert_no_difference(-> { User.count }) do
|
|
650
|
+
post "/sign_up", params: { user: { email_address: "ONE@example.com", password: "another-one", password_confirmation: "another-one" } }
|
|
651
|
+
end
|
|
652
|
+
assert_response :unprocessable_entity
|
|
653
|
+
assert_includes response.body, "<li>Email address has already been taken</li>"
|
|
654
|
+
end
|
|
655
|
+
end
|
|
656
|
+
RUBY
|
|
657
|
+
end
|
|
658
|
+
|
|
659
|
+
def self.sign_up_sign_in_test
|
|
660
|
+
<<~'RUBY'
|
|
661
|
+
test "POST /sign_up creates the user and signs them in" do
|
|
662
|
+
assert_difference(-> { User.count }, 1) do
|
|
663
|
+
post "/sign_up", params: { user: { email_address: " Ann@Example.com ", password: "s3cr3t-s3cr3t", password_confirmation: "s3cr3t-s3cr3t" } }
|
|
664
|
+
end
|
|
665
|
+
assert_redirected_to "/"
|
|
666
|
+
user = User.last
|
|
667
|
+
assert_equal "ann@example.com", user.email_address
|
|
668
|
+
assert user.password_digest.start_with?("$pbkdf2-sha256$")
|
|
669
|
+
assert_equal 1, Session.count
|
|
670
|
+
get "/session/new"
|
|
671
|
+
assert_includes response.body, "<span>ann@example.com</span>"
|
|
672
|
+
assert_includes response.body, "Sign out"
|
|
673
|
+
end
|
|
674
|
+
RUBY
|
|
675
|
+
end
|
|
676
|
+
|
|
677
|
+
# --confirmable's: the user, unconfirmed, and no session.
|
|
678
|
+
def self.sign_up_confirm_test
|
|
679
|
+
<<~'RUBY'
|
|
680
|
+
# --confirmable: nobody is signed in until the address is confirmed
|
|
681
|
+
# (confirmations_controller_test.rb follows the mail's link).
|
|
682
|
+
test "POST /sign_up creates the user and asks them to confirm the address" do
|
|
683
|
+
assert_difference(-> { User.count }, 1) do
|
|
684
|
+
post "/sign_up", params: { user: { email_address: " Ann@Example.com ", password: "s3cr3t-s3cr3t", password_confirmation: "s3cr3t-s3cr3t" } }
|
|
685
|
+
end
|
|
686
|
+
assert_redirected_to "/session/new"
|
|
687
|
+
user = User.last
|
|
688
|
+
assert_equal "ann@example.com", user.email_address
|
|
689
|
+
assert user.password_digest.start_with?("$pbkdf2-sha256$")
|
|
690
|
+
assert_equal false, user.confirmed?
|
|
691
|
+
assert_equal 0, Session.count
|
|
692
|
+
follow_redirect!
|
|
693
|
+
refute response.body.include?("<span>ann@example.com</span>")
|
|
694
|
+
assert_includes response.body, "<a href=\"/session/new\">Sign in</a>"
|
|
695
|
+
end
|
|
696
|
+
RUBY
|
|
697
|
+
end
|
|
698
|
+
|
|
699
|
+
# test/integration/first_run_test.rb (--invite-only): /setup, the
|
|
700
|
+
# boot warning and `./<app> user`.
|
|
701
|
+
def self.first_run_test(app)
|
|
702
|
+
first_run_test_body.gsub("@APP@", app).gsub("@SETUP_ENV@", setup_env(app))
|
|
703
|
+
end
|
|
704
|
+
|
|
705
|
+
def self.first_run_test_body
|
|
706
|
+
<<~'RUBY'
|
|
707
|
+
# The first run of an invitation-only application (cybertrain generate
|
|
708
|
+
# authentication --invite-only): /setup, the first user's page, only from
|
|
709
|
+
# this machine and with the token the boot warning prints, while no user
|
|
710
|
+
# exists; then `./@APP@ user`.
|
|
711
|
+
require "test_helper"
|
|
712
|
+
require_relative "../../app/commands/user_command"
|
|
713
|
+
|
|
714
|
+
class FirstRunTest < ActionDispatch::IntegrationTest
|
|
715
|
+
# /setup answers only while no user exists: these tests start without
|
|
716
|
+
# the fixtures' users.
|
|
717
|
+
setup do
|
|
718
|
+
User.all.delete_all
|
|
719
|
+
end
|
|
720
|
+
|
|
721
|
+
test "GET /setup with the token shows the form to this machine while no user exists" do
|
|
722
|
+
get setup_link
|
|
723
|
+
assert_response :ok
|
|
724
|
+
assert_includes response.body, "<h1>Set up</h1>"
|
|
725
|
+
assert_includes response.body, "<form action=\"/setup\" method=\"post\">"
|
|
726
|
+
assert_includes response.body, "<input type=\"hidden\" name=\"token\" value=\"#{SetupController.setup_token}\">"
|
|
727
|
+
assert_includes response.body, "name=\"user[password_confirmation]\""
|
|
728
|
+
end
|
|
729
|
+
|
|
730
|
+
test "/setup without the token, or with a wrong one, is a 404" do
|
|
731
|
+
assert_equal 32, SetupController.setup_token.size
|
|
732
|
+
get "/setup"
|
|
733
|
+
assert_response :not_found
|
|
734
|
+
get "/setup?token="
|
|
735
|
+
assert_response :not_found
|
|
736
|
+
get "/setup?token=0123456789abcdef0123456789abcdef"
|
|
737
|
+
assert_response :not_found
|
|
738
|
+
get "/setup?token=#{SetupController.setup_token}x"
|
|
739
|
+
assert_response :not_found
|
|
740
|
+
refute response.body.include?(SetupController.setup_token)
|
|
741
|
+
end
|
|
742
|
+
|
|
743
|
+
test "/setup is a 404 to another machine, through a proxy, and once a user exists" do
|
|
744
|
+
res = browser_at("203.0.113.7").get(setup_link)
|
|
745
|
+
assert_equal 404, res.status
|
|
746
|
+
get setup_link, headers: { "x-forwarded-for" => "203.0.113.7" }
|
|
747
|
+
assert_response :not_found
|
|
748
|
+
get setup_link
|
|
749
|
+
assert_response :ok
|
|
750
|
+
create_user
|
|
751
|
+
get setup_link
|
|
752
|
+
assert_response :not_found
|
|
753
|
+
end
|
|
754
|
+
|
|
755
|
+
test "POST /setup from another machine, or without the token, creates nothing" do
|
|
756
|
+
res = browser_at("203.0.113.7").post("/setup", Cybertrain::Test.flatten_params(setup_form("s3cr3t-s3cr3t", "s3cr3t-s3cr3t")))
|
|
757
|
+
assert_equal 404, res.status
|
|
758
|
+
user = { email_address: " Ann@Example.com ", password: "s3cr3t-s3cr3t", password_confirmation: "s3cr3t-s3cr3t" }
|
|
759
|
+
post "/setup", params: { token: "0123456789abcdef0123456789abcdef", user: user }
|
|
760
|
+
assert_response :not_found
|
|
761
|
+
post "/setup", params: { user: user }
|
|
762
|
+
assert_response :not_found
|
|
763
|
+
assert_equal 0, User.count
|
|
764
|
+
end
|
|
765
|
+
|
|
766
|
+
test "@SETUP_ENV@ opens /setup to any address with the token until a user exists" do
|
|
767
|
+
browser = browser_at("203.0.113.7")
|
|
768
|
+
Cybertrain.config.set_bool_setting(:setup_open, true)
|
|
769
|
+
res = browser.get(setup_link)
|
|
770
|
+
without = browser.get("/setup")
|
|
771
|
+
Cybertrain.config.set_bool_setting(:setup_open, false)
|
|
772
|
+
assert_equal 200, res.status
|
|
773
|
+
assert_equal 404, without.status
|
|
774
|
+
create_user
|
|
775
|
+
Cybertrain.config.set_bool_setting(:setup_open, true)
|
|
776
|
+
res = browser.get(setup_link)
|
|
777
|
+
Cybertrain.config.set_bool_setting(:setup_open, false)
|
|
778
|
+
assert_equal 404, res.status
|
|
779
|
+
end
|
|
780
|
+
|
|
781
|
+
# With forgery protection on, the page's own token is accepted.
|
|
782
|
+
test "POST /setup with the token creates the first user and signs them in" do
|
|
783
|
+
with_forgery_protection do
|
|
784
|
+
get setup_link
|
|
785
|
+
post "/setup", params: { authenticity_token: authenticity_token_in(response.body), token: SetupController.setup_token,
|
|
786
|
+
user: { email_address: " Ann@Example.com ", password: "s3cr3t-s3cr3t", password_confirmation: "s3cr3t-s3cr3t" } }
|
|
787
|
+
assert_redirected_to "/"
|
|
788
|
+
end
|
|
789
|
+
assert_equal 1, User.count
|
|
790
|
+
assert_equal "ann@example.com", User.first.email_address
|
|
791
|
+
assert_equal 1, Session.count
|
|
792
|
+
get "/session/new"
|
|
793
|
+
assert_includes response.body, "<span>ann@example.com</span>"
|
|
794
|
+
get setup_link
|
|
795
|
+
assert_response :not_found
|
|
796
|
+
end
|
|
797
|
+
|
|
798
|
+
test "POST /setup with a short or mismatched password re-renders the form" do
|
|
799
|
+
post "/setup", params: setup_form("short", "other")
|
|
800
|
+
assert_response :unprocessable_entity
|
|
801
|
+
assert_includes response.body, "<li>Password is too short (minimum is 8 characters)</li>"
|
|
802
|
+
assert_includes response.body, "<li>Password confirmation doesn't match Password</li>"
|
|
803
|
+
assert_includes response.body, "<input type=\"hidden\" name=\"token\" value=\"#{SetupController.setup_token}\">"
|
|
804
|
+
assert_equal 0, User.count
|
|
805
|
+
end
|
|
806
|
+
|
|
807
|
+
test "./@APP@ user creates a user, then sets its password" do
|
|
808
|
+
assert_equal 0, run_command(["user", "Bob@Example.com", "s3cr3t-s3cr3t"])
|
|
809
|
+
assert_equal 1, User.count
|
|
810
|
+
assert_equal "bob@example.com", User.first.email_address
|
|
811
|
+
assert User.first.authenticate("s3cr3t-s3cr3t")
|
|
812
|
+
assert_equal 0, run_command(["user", "bob@example.com", "another-s3cr3t"])
|
|
813
|
+
assert_equal 1, User.count
|
|
814
|
+
assert User.first.authenticate("another-s3cr3t")
|
|
815
|
+
refute User.first.authenticate("s3cr3t-s3cr3t")
|
|
816
|
+
end
|
|
817
|
+
|
|
818
|
+
test "POST /setup and ./@APP@ user refuse an address list or a malformed address" do
|
|
819
|
+
post "/setup", params: { token: SetupController.setup_token,
|
|
820
|
+
user: { email_address: "ann@example.com,eve@evil.example", password: "s3cr3t-s3cr3t", password_confirmation: "s3cr3t-s3cr3t" } }
|
|
821
|
+
assert_response :unprocessable_entity
|
|
822
|
+
assert_includes response.body, "<li>Email address is invalid</li>"
|
|
823
|
+
assert_equal 0, User.count
|
|
824
|
+
assert_equal 1, run_command(["user", "ann@example.com,eve@evil.example", "s3cr3t-s3cr3t"])
|
|
825
|
+
assert_equal 1, run_command(["user", "ann", "s3cr3t-s3cr3t"])
|
|
826
|
+
assert_equal 0, User.count
|
|
827
|
+
end
|
|
828
|
+
|
|
829
|
+
test "./@APP@ user refuses bad arguments and an empty or short password" do
|
|
830
|
+
assert_equal 1, run_command(["user"])
|
|
831
|
+
assert_equal 1, run_command(["user", "ann@example.com", "s3cr3t-s3cr3t", "extra"])
|
|
832
|
+
assert_equal 1, run_command(["user", "--help"])
|
|
833
|
+
assert_equal 1, run_command(["user", "ann@example.com", ""])
|
|
834
|
+
assert_equal 1, run_command(["user", "ann@example.com", "short"])
|
|
835
|
+
assert_equal 0, User.count
|
|
836
|
+
end
|
|
837
|
+
|
|
838
|
+
test "a password read from standard input loses one trailing newline" do
|
|
839
|
+
assert_equal "s3cr3t s3cr3t", UserCommand.password_from("s3cr3t s3cr3t\n")
|
|
840
|
+
assert_equal "s3cr3t", UserCommand.password_from("s3cr3t\r\n")
|
|
841
|
+
assert_equal "s3cr3t\n", UserCommand.password_from("s3cr3t\n\n")
|
|
842
|
+
assert_equal "", UserCommand.password_from("")
|
|
843
|
+
end
|
|
844
|
+
|
|
845
|
+
test "the boot warning is due while no user exists" do
|
|
846
|
+
assert UserCommand.no_user?
|
|
847
|
+
reconnect
|
|
848
|
+
create_user
|
|
849
|
+
refute UserCommand.no_user?
|
|
850
|
+
reconnect
|
|
851
|
+
end
|
|
852
|
+
|
|
853
|
+
private
|
|
854
|
+
|
|
855
|
+
# The form /setup posts, filled in, with the page's one-time token in
|
|
856
|
+
# its hidden field.
|
|
857
|
+
def setup_form(password, confirmation)
|
|
858
|
+
{ token: SetupController.setup_token,
|
|
859
|
+
user: { email_address: " Ann@Example.com ", password: password, password_confirmation: confirmation } }
|
|
860
|
+
end
|
|
861
|
+
|
|
862
|
+
# /setup with this process's token, the link the boot warning prints.
|
|
863
|
+
def setup_link
|
|
864
|
+
"/setup?token=#{SetupController.setup_token}"
|
|
865
|
+
end
|
|
866
|
+
|
|
867
|
+
# A browser on another machine: the test's own requests come from
|
|
868
|
+
# 127.0.0.1, and an integration test cannot change that, so this one
|
|
869
|
+
# is a Cybertrain::Test::Client (its responses are checked by status).
|
|
870
|
+
def browser_at(address)
|
|
871
|
+
browser = Cybertrain::Test::Client.new(Cybertrain::Test.app)
|
|
872
|
+
browser.peer_address = address
|
|
873
|
+
browser
|
|
874
|
+
end
|
|
875
|
+
|
|
876
|
+
# The first user, made as the user command makes one.
|
|
877
|
+
def create_user
|
|
878
|
+
user = User.new(email_address: "ann@example.com")
|
|
879
|
+
user.password = "s3cr3t-s3cr3t"
|
|
880
|
+
user.save!
|
|
881
|
+
nil
|
|
882
|
+
end
|
|
883
|
+
|
|
884
|
+
# `./@APP@ ARGS...` in-process. Main.run connects a pool of one for
|
|
885
|
+
# the command and disconnects it, so the tests' pool is connected
|
|
886
|
+
# again afterwards (the database file is the same).
|
|
887
|
+
def run_command(argv)
|
|
888
|
+
status = Cybertrain::Main.run("@APP@", argv, router: Gen::Routes.build(Cybertrain::Router.new),
|
|
889
|
+
url_resolver: Gen::Routes.url_resolver,
|
|
890
|
+
views: Cybertrain::Application.no_views)
|
|
891
|
+
reconnect
|
|
892
|
+
status
|
|
893
|
+
end
|
|
894
|
+
|
|
895
|
+
def reconnect
|
|
896
|
+
Cybertrain::DB.connect(Cybertrain.config.database_path, size: Cybertrain.config.pool_size)
|
|
897
|
+
nil
|
|
898
|
+
end
|
|
899
|
+
end
|
|
900
|
+
RUBY
|
|
901
|
+
end
|
|
902
|
+
|
|
903
|
+
# test/integration/admin_test.rb: the admin flag, require_admin
|
|
904
|
+
# (through the jobs page config/routes.rb draws behind it) and the
|
|
905
|
+
# user command's admin and unadmin.
|
|
906
|
+
def self.admin_integration_test(app)
|
|
907
|
+
<<~'RUBY'.gsub("@APP@", app)
|
|
908
|
+
# The admin flag (cybertrain generate authentication): User#admin?,
|
|
909
|
+
# require_admin (the Authentication concern's before_action, which the
|
|
910
|
+
# jobs page in config/routes.rb names) and `./@APP@ user admin EMAIL`.
|
|
911
|
+
require "test_helper"
|
|
912
|
+
require_relative "../../app/commands/user_command"
|
|
913
|
+
|
|
914
|
+
# require_admin is private (the Authentication concern's): a controller
|
|
915
|
+
# made for this test calls it, as a before_action would.
|
|
916
|
+
class AdminProbeController < ApplicationController
|
|
917
|
+
def probe_require_admin
|
|
918
|
+
require_admin
|
|
919
|
+
end
|
|
920
|
+
end
|
|
921
|
+
|
|
922
|
+
class AdminTest < ActionDispatch::IntegrationTest
|
|
923
|
+
test "user.admin? is false for a new user" do
|
|
924
|
+
user = User.new(email_address: "new@example.com")
|
|
925
|
+
user.password = "s3cr3t-s3cr3t"
|
|
926
|
+
user.save!
|
|
927
|
+
assert_equal false, user.admin?
|
|
928
|
+
assert_equal false, User.find(user.id).admin?
|
|
929
|
+
user.admin = true
|
|
930
|
+
user.save!
|
|
931
|
+
assert_equal true, User.find(user.id).admin?
|
|
932
|
+
end
|
|
933
|
+
|
|
934
|
+
test "require_admin answers 404 to a visitor and a non-admin and 200 to an admin" do
|
|
935
|
+
# A visitor (no @current_user): require_admin itself answers 404.
|
|
936
|
+
ctx = Cybertrain::Context.new(Cybertrain::Request.new("GET", "/admin/jobs", {}, ""))
|
|
937
|
+
ctx.session = Cybertrain::Session.new
|
|
938
|
+
probe = AdminProbeController.new(ctx)
|
|
939
|
+
probe.probe_require_admin
|
|
940
|
+
assert probe.performed?
|
|
941
|
+
assert_equal 404, ctx.response.status
|
|
942
|
+
assert_equal "Not Found", ctx.response.body
|
|
943
|
+
# Through the full stack the same: the jobs page runs require_admin
|
|
944
|
+
# alone (no redirect to sign in, which would show the page exists).
|
|
945
|
+
get "/admin/jobs"
|
|
946
|
+
assert_response :not_found
|
|
947
|
+
assert_equal "Not Found", response.body
|
|
948
|
+
# Signed in, not an admin: 404, as if there were no such page; the
|
|
949
|
+
# same body and type as a path no route has.
|
|
950
|
+
user = users(:two)
|
|
951
|
+
sign_in_as user
|
|
952
|
+
get "/admin/jobs"
|
|
953
|
+
assert_response :not_found
|
|
954
|
+
assert_equal "Not Found", response.body
|
|
955
|
+
refute response.body.include?("<h1>Jobs</h1>")
|
|
956
|
+
jobs = response
|
|
957
|
+
get "/admin/no-such-page"
|
|
958
|
+
assert_response :not_found
|
|
959
|
+
assert_equal response.body, jobs.body
|
|
960
|
+
assert_equal response.header("content-type"), jobs.header("content-type")
|
|
961
|
+
# Made an admin, the same browser sees the page.
|
|
962
|
+
user.admin = true
|
|
963
|
+
user.save!
|
|
964
|
+
get "/admin/jobs"
|
|
965
|
+
assert_response :ok
|
|
966
|
+
assert_includes response.body, "<h1>Jobs</h1>"
|
|
967
|
+
# And no more once that is taken back.
|
|
968
|
+
user.admin = false
|
|
969
|
+
user.save!
|
|
970
|
+
get "/admin/jobs"
|
|
971
|
+
assert_response :not_found
|
|
972
|
+
end
|
|
973
|
+
|
|
974
|
+
# users(:two) is not an admin; users(:one) is, and stays so.
|
|
975
|
+
test "./@APP@ user admin and unadmin print what they did" do
|
|
976
|
+
two = users(:two)
|
|
977
|
+
assert_equal "admin two@example.com", UserCommand.admin_change("Two@Example.com", true)
|
|
978
|
+
assert_equal true, User.find(two.id).admin?
|
|
979
|
+
assert_equal "unadmin two@example.com", UserCommand.admin_change("two@example.com", false)
|
|
980
|
+
assert_equal false, User.find(two.id).admin?
|
|
981
|
+
assert_equal "error: no user bob@example.com", UserCommand.admin_change("bob@example.com", true)
|
|
982
|
+
assert_equal 2, User.count
|
|
983
|
+
end
|
|
984
|
+
|
|
985
|
+
test "./@APP@ user admin and unadmin set the flag of an existing user" do
|
|
986
|
+
two = users(:two)
|
|
987
|
+
assert_equal 0, UserCommand.run(["admin", "Two@Example.com"])
|
|
988
|
+
assert_equal true, User.find(two.id).admin?
|
|
989
|
+
assert_equal 0, UserCommand.run(["unadmin", "two@example.com"])
|
|
990
|
+
assert_equal false, User.find(two.id).admin?
|
|
991
|
+
assert_equal 1, UserCommand.run(["admin", "bob@example.com"])
|
|
992
|
+
assert_equal 1, UserCommand.run(["admin"])
|
|
993
|
+
assert_equal 1, UserCommand.run(["unadmin", "two@example.com", "extra"])
|
|
994
|
+
assert_equal 2, User.count
|
|
995
|
+
assert_equal false, User.find(two.id).admin?
|
|
996
|
+
end
|
|
997
|
+
end
|
|
998
|
+
RUBY
|
|
999
|
+
end
|
|
1000
|
+
|
|
1001
|
+
# test/integration/remember_me_test.rb: how long a sign-in lasts,
|
|
1002
|
+
# remembered or not, and the idle limit, with the clock moved by
|
|
1003
|
+
# travel_to (the cookie's own limit is the framework's test/session.rb).
|
|
1004
|
+
def self.remember_me_integration_test
|
|
1005
|
+
<<~'RUBY'
|
|
1006
|
+
# "Keep me signed in" and the idle limit (cybertrain generate
|
|
1007
|
+
# authentication): a sign-in lasts session_absolute_max_age (two weeks)
|
|
1008
|
+
# unless the box was checked (auth_remember_for, 30 days), and ends
|
|
1009
|
+
# either way after auth_idle_timeout (7 days) without a request. The
|
|
1010
|
+
# tests move the clock with travel_to rather than wait; it is frozen
|
|
1011
|
+
# first, so that `15.days.from_now` is fifteen days after the sign-in.
|
|
1012
|
+
# A session meant to live is used every six days, inside the idle limit.
|
|
1013
|
+
require "test_helper"
|
|
1014
|
+
|
|
1015
|
+
# start_new_session_for is private (the Authentication concern's): a
|
|
1016
|
+
# controller made for these tests calls it, as an action would.
|
|
1017
|
+
class LifetimeProbeController < ApplicationController
|
|
1018
|
+
def probe_start(user, remember)
|
|
1019
|
+
start_new_session_for(user, remember: remember)
|
|
1020
|
+
end
|
|
1021
|
+
end
|
|
1022
|
+
|
|
1023
|
+
class RememberMeTest < ActionDispatch::IntegrationTest
|
|
1024
|
+
setup do
|
|
1025
|
+
freeze_time
|
|
1026
|
+
end
|
|
1027
|
+
|
|
1028
|
+
# A cookie that already carries a start (a browser that signed in
|
|
1029
|
+
# before, or the account page's restart after a new password, on day
|
|
1030
|
+
# 13 of its sign-in) starts again: SessionStore stamps this request's
|
|
1031
|
+
# time, so the new sign-in gets its whole limit.
|
|
1032
|
+
test "a sign-in restarts the lifetime of a cookie that already has one" do
|
|
1033
|
+
ctx = Cybertrain::Context.new(Cybertrain::Request.new("POST", "/session", {}, ""))
|
|
1034
|
+
ctx.session = Cybertrain::Session.new
|
|
1035
|
+
ctx.session["_issued_at"] = Time.current.to_i - 13.days
|
|
1036
|
+
assert ctx.session.issued_at > 0
|
|
1037
|
+
LifetimeProbeController.new(ctx).probe_start(users(:one), false)
|
|
1038
|
+
assert_equal 0, ctx.session.issued_at
|
|
1039
|
+
end
|
|
1040
|
+
|
|
1041
|
+
# Signed in while auth_remember_for is 31 days, then set back to its
|
|
1042
|
+
# 30: the cookie holds 31, so on day 30 it is the Session row's own
|
|
1043
|
+
# limit that ends the sign-in, and the row is gone.
|
|
1044
|
+
test "a remembered session survives day 15 and ends after day 30" do
|
|
1045
|
+
remember_for = Cybertrain.config.auth_remember_for
|
|
1046
|
+
begin
|
|
1047
|
+
Cybertrain.config.auth_remember_for = 31.days
|
|
1048
|
+
sign_in_as users(:one), remember: true
|
|
1049
|
+
Cybertrain.config.auth_remember_for = remember_for
|
|
1050
|
+
assert_redirected_to "/"
|
|
1051
|
+
assert_equal true, Session.first.remember
|
|
1052
|
+
assert session_cookie_of(response).end_with?("; Max-Age=2678400"), session_cookie_of(response)
|
|
1053
|
+
travel_to 6.days.from_now do
|
|
1054
|
+
assert signed_in?
|
|
1055
|
+
end
|
|
1056
|
+
travel_to 12.days.from_now do
|
|
1057
|
+
assert signed_in?
|
|
1058
|
+
end
|
|
1059
|
+
travel_to 15.days.from_now do
|
|
1060
|
+
assert signed_in?
|
|
1061
|
+
end
|
|
1062
|
+
travel_to 21.days.from_now do
|
|
1063
|
+
assert signed_in?
|
|
1064
|
+
end
|
|
1065
|
+
travel_to 27.days.from_now do
|
|
1066
|
+
assert signed_in?
|
|
1067
|
+
end
|
|
1068
|
+
cookie = cookie_header
|
|
1069
|
+
first = Session.last
|
|
1070
|
+
travel_to 30.days.from_now - 1.minute do
|
|
1071
|
+
assert signed_in?
|
|
1072
|
+
end
|
|
1073
|
+
travel_to 30.days.from_now do
|
|
1074
|
+
refute signed_in?
|
|
1075
|
+
# Ended: the row is gone.
|
|
1076
|
+
assert_nil Session.find_by(id: first.id)
|
|
1077
|
+
end
|
|
1078
|
+
travel_to 31.days.from_now do
|
|
1079
|
+
refute replayed_signed_in?(cookie)
|
|
1080
|
+
end
|
|
1081
|
+
# A sign-in whose limit was lowered to 15 days since (the cookie
|
|
1082
|
+
# still holds its 30) ends on day 15, its row with it, and a longer
|
|
1083
|
+
# limit set afterwards does not bring back a copy of the cookie.
|
|
1084
|
+
sign_in_as users(:one), remember: true
|
|
1085
|
+
started = Session.last
|
|
1086
|
+
cookie = cookie_header
|
|
1087
|
+
Cybertrain.config.auth_remember_for = 15.days
|
|
1088
|
+
travel_to 6.days.from_now do
|
|
1089
|
+
assert signed_in?
|
|
1090
|
+
end
|
|
1091
|
+
travel_to 12.days.from_now do
|
|
1092
|
+
assert signed_in?
|
|
1093
|
+
end
|
|
1094
|
+
travel_to 15.days.from_now do
|
|
1095
|
+
refute signed_in?
|
|
1096
|
+
assert_nil Session.find_by(id: started.id)
|
|
1097
|
+
Cybertrain.config.auth_remember_for = 60.days
|
|
1098
|
+
refute replayed_signed_in?(cookie)
|
|
1099
|
+
end
|
|
1100
|
+
ensure
|
|
1101
|
+
Cybertrain.config.auth_remember_for = remember_for
|
|
1102
|
+
end
|
|
1103
|
+
end
|
|
1104
|
+
|
|
1105
|
+
test "an unremembered session ends after day 14" do
|
|
1106
|
+
sign_in_as users(:one)
|
|
1107
|
+
assert_redirected_to "/"
|
|
1108
|
+
assert_equal false, Session.first.remember
|
|
1109
|
+
travel_to 6.days.from_now do
|
|
1110
|
+
assert signed_in?
|
|
1111
|
+
end
|
|
1112
|
+
travel_to 12.days.from_now do
|
|
1113
|
+
assert signed_in?
|
|
1114
|
+
end
|
|
1115
|
+
travel_to 14.days.from_now - 1.minute do
|
|
1116
|
+
assert signed_in?
|
|
1117
|
+
end
|
|
1118
|
+
travel_to 14.days.from_now do
|
|
1119
|
+
refute signed_in?
|
|
1120
|
+
end
|
|
1121
|
+
# Day 15, where a remembered one is alive.
|
|
1122
|
+
sign_in_as users(:one)
|
|
1123
|
+
travel_to 6.days.from_now do
|
|
1124
|
+
assert signed_in?
|
|
1125
|
+
end
|
|
1126
|
+
travel_to 12.days.from_now do
|
|
1127
|
+
assert signed_in?
|
|
1128
|
+
end
|
|
1129
|
+
travel_to 15.days.from_now do
|
|
1130
|
+
refute signed_in?
|
|
1131
|
+
end
|
|
1132
|
+
end
|
|
1133
|
+
|
|
1134
|
+
test "an unremembered session still ends with session_absolute_max_age off" do
|
|
1135
|
+
absolute = Cybertrain.config.session_absolute_max_age
|
|
1136
|
+
max_age = Cybertrain.config.session_max_age
|
|
1137
|
+
remember_for = Cybertrain.config.auth_remember_for
|
|
1138
|
+
begin
|
|
1139
|
+
# Off (0): the limit is session_max_age (two weeks), stamped on
|
|
1140
|
+
# the cookie and checked on the row. Signed in while it is 15
|
|
1141
|
+
# days (the cookie's stamp), then lowered to 13, so that the
|
|
1142
|
+
# cookie still holds its 15 on day 13 and the row's check alone
|
|
1143
|
+
# ends the sign-in (13 days: not the two-week constant).
|
|
1144
|
+
Cybertrain.config.session_absolute_max_age = 0
|
|
1145
|
+
Cybertrain.config.session_max_age = 15.days
|
|
1146
|
+
user = users(:one)
|
|
1147
|
+
assert_equal 1_296_000, stamped_session(user, false).lifetime_limit
|
|
1148
|
+
sign_in_as user
|
|
1149
|
+
assert_redirected_to "/"
|
|
1150
|
+
started = Session.last
|
|
1151
|
+
Cybertrain.config.session_max_age = 13.days
|
|
1152
|
+
refute session_cookie_of(response).empty?
|
|
1153
|
+
refute session_cookie_of(response).include?("Max-Age")
|
|
1154
|
+
travel_to 6.days.from_now do
|
|
1155
|
+
assert signed_in?
|
|
1156
|
+
end
|
|
1157
|
+
travel_to 12.days.from_now do
|
|
1158
|
+
assert signed_in?
|
|
1159
|
+
end
|
|
1160
|
+
travel_to 13.days.from_now - 1.minute do
|
|
1161
|
+
assert signed_in?
|
|
1162
|
+
end
|
|
1163
|
+
travel_to 13.days.from_now do
|
|
1164
|
+
refute signed_in?
|
|
1165
|
+
assert_nil Session.find_by(id: started.id)
|
|
1166
|
+
end
|
|
1167
|
+
# The cookie holds its 15 days: signed in again while it is 15,
|
|
1168
|
+
# then raised to 60 for the row, the cookie's own stamp ends the
|
|
1169
|
+
# sign-in on day 15 (a browser-session cookie, sent all along).
|
|
1170
|
+
Cybertrain.config.session_max_age = 15.days
|
|
1171
|
+
sign_in_as user
|
|
1172
|
+
started = Session.last
|
|
1173
|
+
Cybertrain.config.session_max_age = 60.days
|
|
1174
|
+
travel_to 6.days.from_now do
|
|
1175
|
+
assert signed_in?
|
|
1176
|
+
end
|
|
1177
|
+
travel_to 12.days.from_now do
|
|
1178
|
+
assert signed_in?
|
|
1179
|
+
end
|
|
1180
|
+
travel_to 15.days.from_now - 1.minute do
|
|
1181
|
+
assert signed_in?
|
|
1182
|
+
end
|
|
1183
|
+
travel_to 15.days.from_now do
|
|
1184
|
+
refute signed_in?
|
|
1185
|
+
# The row is still there: the cookie alone ended it.
|
|
1186
|
+
refute Session.find_by(id: started.id).nil?
|
|
1187
|
+
end
|
|
1188
|
+
# The stamp itself: session_max_age as it is (13 days), not the
|
|
1189
|
+
# two-week constant.
|
|
1190
|
+
Cybertrain.config.session_max_age = 13.days
|
|
1191
|
+
stamped = stamped_session(user, false)
|
|
1192
|
+
assert_equal 1_123_200, stamped.lifetime_limit
|
|
1193
|
+
assert stamped.browser_session?
|
|
1194
|
+
# session_max_age not positive either: two weeks all the same.
|
|
1195
|
+
Cybertrain.config.session_max_age = 0
|
|
1196
|
+
assert_equal 1_209_600, stamped_session(user, false).lifetime_limit
|
|
1197
|
+
# A limit of its own is stamped as it is; a remembered sign-in
|
|
1198
|
+
# takes auth_remember_for and a Max-Age (not a browser session).
|
|
1199
|
+
Cybertrain.config.session_absolute_max_age = 3600
|
|
1200
|
+
assert_equal 3600, stamped_session(user, false).lifetime_limit
|
|
1201
|
+
remembered = stamped_session(user, true)
|
|
1202
|
+
assert_equal 2_592_000, remembered.lifetime_limit
|
|
1203
|
+
refute remembered.browser_session?
|
|
1204
|
+
# auth_remember_for not positive (set at run time, past boot's
|
|
1205
|
+
# check): the same chain as an unremembered sign-in, still
|
|
1206
|
+
# persistent.
|
|
1207
|
+
Cybertrain.config.auth_remember_for = 0
|
|
1208
|
+
remembered = stamped_session(user, true)
|
|
1209
|
+
assert_equal 3600, remembered.lifetime_limit
|
|
1210
|
+
refute remembered.browser_session?
|
|
1211
|
+
Cybertrain.config.session_absolute_max_age = 0
|
|
1212
|
+
Cybertrain.config.session_max_age = 0
|
|
1213
|
+
assert_equal 1_209_600, stamped_session(user, true).lifetime_limit
|
|
1214
|
+
ensure
|
|
1215
|
+
Cybertrain.config.session_absolute_max_age = absolute
|
|
1216
|
+
Cybertrain.config.session_max_age = max_age
|
|
1217
|
+
Cybertrain.config.auth_remember_for = remember_for
|
|
1218
|
+
end
|
|
1219
|
+
end
|
|
1220
|
+
|
|
1221
|
+
# Used a minute short of seven days after the sign-in, then left
|
|
1222
|
+
# alone for seven days: ended (well inside the two weeks of an
|
|
1223
|
+
# unremembered sign-in).
|
|
1224
|
+
test "seven idle days end a session" do
|
|
1225
|
+
[true, false].each do |remember|
|
|
1226
|
+
sign_in_as users(:one), remember: remember
|
|
1227
|
+
started = Session.last
|
|
1228
|
+
travel_to 7.days.from_now - 1.minute do
|
|
1229
|
+
assert signed_in?
|
|
1230
|
+
end
|
|
1231
|
+
cookie = cookie_header
|
|
1232
|
+
travel_to 14.days.from_now - 1.minute do
|
|
1233
|
+
refute signed_in?
|
|
1234
|
+
# Ended: the row is gone, so turning the idle limit off
|
|
1235
|
+
# afterwards does not bring back a copy of the cookie.
|
|
1236
|
+
assert_nil Session.find_by(id: started.id)
|
|
1237
|
+
idle = Cybertrain.config.auth_idle_timeout
|
|
1238
|
+
begin
|
|
1239
|
+
Cybertrain.config.auth_idle_timeout = 0
|
|
1240
|
+
refute replayed_signed_in?(cookie)
|
|
1241
|
+
ensure
|
|
1242
|
+
Cybertrain.config.auth_idle_timeout = idle
|
|
1243
|
+
end
|
|
1244
|
+
end
|
|
1245
|
+
end
|
|
1246
|
+
# A row with no last_active_at is idle since created_at.
|
|
1247
|
+
sign_in_as users(:one), remember: true
|
|
1248
|
+
started = Session.last
|
|
1249
|
+
started.update_columns(last_active_at: nil)
|
|
1250
|
+
travel_to 7.days.from_now - 1.minute do
|
|
1251
|
+
assert signed_in?
|
|
1252
|
+
refute Session.find(started.id).last_active_at.nil?
|
|
1253
|
+
end
|
|
1254
|
+
Session.find(started.id).update_columns(last_active_at: nil)
|
|
1255
|
+
travel_to 7.days.from_now do
|
|
1256
|
+
refute signed_in?
|
|
1257
|
+
assert_nil Session.find_by(id: started.id)
|
|
1258
|
+
end
|
|
1259
|
+
# auth_idle_timeout 0: no idle limit, the absolute one alone (on
|
|
1260
|
+
# the row: lowered to 29 days after the sign-in, while the cookie
|
|
1261
|
+
# holds its 30).
|
|
1262
|
+
idle = Cybertrain.config.auth_idle_timeout
|
|
1263
|
+
remember_for = Cybertrain.config.auth_remember_for
|
|
1264
|
+
begin
|
|
1265
|
+
Cybertrain.config.auth_idle_timeout = 0
|
|
1266
|
+
sign_in_as users(:one), remember: true
|
|
1267
|
+
started = Session.last
|
|
1268
|
+
Cybertrain.config.auth_remember_for = 29.days
|
|
1269
|
+
travel_to 29.days.from_now - 1.minute do
|
|
1270
|
+
assert signed_in?
|
|
1271
|
+
end
|
|
1272
|
+
travel_to 29.days.from_now do
|
|
1273
|
+
refute signed_in?
|
|
1274
|
+
assert_nil Session.find_by(id: started.id)
|
|
1275
|
+
end
|
|
1276
|
+
ensure
|
|
1277
|
+
Cybertrain.config.auth_idle_timeout = idle
|
|
1278
|
+
Cybertrain.config.auth_remember_for = remember_for
|
|
1279
|
+
end
|
|
1280
|
+
end
|
|
1281
|
+
|
|
1282
|
+
test "last_active_at is touched at most once a minute" do
|
|
1283
|
+
before = Time.current.to_i
|
|
1284
|
+
sign_in_as users(:one)
|
|
1285
|
+
started = Session.first.last_active_at
|
|
1286
|
+
refute started.nil?
|
|
1287
|
+
assert started.to_i >= before
|
|
1288
|
+
# Used 30 seconds later: a request does not write the row.
|
|
1289
|
+
travel 30.seconds
|
|
1290
|
+
updated_at = Session.first.updated_at.to_i
|
|
1291
|
+
assert signed_in?
|
|
1292
|
+
assert_equal before, Session.first.last_active_at.to_i
|
|
1293
|
+
# 61 seconds after the last write: the request moves it to now, and
|
|
1294
|
+
# leaves updated_at alone.
|
|
1295
|
+
travel 31.seconds
|
|
1296
|
+
assert signed_in?
|
|
1297
|
+
assert_equal before + 61, Session.first.last_active_at.to_i
|
|
1298
|
+
assert_equal updated_at, Session.first.updated_at.to_i
|
|
1299
|
+
end
|
|
1300
|
+
|
|
1301
|
+
test "an unremembered sign-in sets a cookie without Max-Age" do
|
|
1302
|
+
sign_in_as users(:one)
|
|
1303
|
+
assert_redirected_to "/"
|
|
1304
|
+
cookie = session_cookie_of(response)
|
|
1305
|
+
refute cookie.empty?
|
|
1306
|
+
refute cookie.include?("Max-Age")
|
|
1307
|
+
refute cookie.include?("Expires")
|
|
1308
|
+
# The box checked, the same cookie lasts auth_remember_for.
|
|
1309
|
+
sign_in_as users(:one), remember: true
|
|
1310
|
+
cookie = session_cookie_of(response)
|
|
1311
|
+
assert cookie.end_with?("; Max-Age=2592000"), cookie
|
|
1312
|
+
refute cookie.include?("Expires")
|
|
1313
|
+
end
|
|
1314
|
+
|
|
1315
|
+
private
|
|
1316
|
+
|
|
1317
|
+
# Whether this browser is signed in: the nav names the user.
|
|
1318
|
+
def signed_in?
|
|
1319
|
+
get "/session/new"
|
|
1320
|
+
response.body.include?("<span>one@example.com</span>")
|
|
1321
|
+
end
|
|
1322
|
+
|
|
1323
|
+
# The session cookie this browser holds now, as a request header.
|
|
1324
|
+
def cookie_header
|
|
1325
|
+
name = Cybertrain.config.session_cookie_name
|
|
1326
|
+
"#{name}=#{cookies[name]}"
|
|
1327
|
+
end
|
|
1328
|
+
|
|
1329
|
+
# Whether a copy of an old cookie (kept by whoever had it) signs in.
|
|
1330
|
+
def replayed_signed_in?(cookie)
|
|
1331
|
+
get "/session/new", headers: { "cookie" => cookie }
|
|
1332
|
+
response.body.include?("<span>one@example.com</span>")
|
|
1333
|
+
end
|
|
1334
|
+
|
|
1335
|
+
# The Set-Cookie line of the session cookie in res ("" when none).
|
|
1336
|
+
def session_cookie_of(res)
|
|
1337
|
+
prefix = "#{Cybertrain.config.session_cookie_name}="
|
|
1338
|
+
found = ""
|
|
1339
|
+
res.cookies.each { |line| found = line if line.start_with?(prefix) }
|
|
1340
|
+
found
|
|
1341
|
+
end
|
|
1342
|
+
|
|
1343
|
+
# The cookie session start_new_session_for leaves for user.
|
|
1344
|
+
def stamped_session(user, remember)
|
|
1345
|
+
ctx = Cybertrain::Context.new(Cybertrain::Request.new("POST", "/session", {}, ""))
|
|
1346
|
+
ctx.session = Cybertrain::Session.new
|
|
1347
|
+
LifetimeProbeController.new(ctx).probe_start(user, remember)
|
|
1348
|
+
ctx.session
|
|
1349
|
+
end
|
|
1350
|
+
end
|
|
1351
|
+
RUBY
|
|
1352
|
+
end
|
|
1353
|
+
|
|
1354
|
+
# test/integration/lockout_test.rb: auth_lockout_attempts wrong
|
|
1355
|
+
# passwords lock the account for an hour (travel moves past it), the
|
|
1356
|
+
# lock answers as a wrong password does, and a successful sign-in
|
|
1357
|
+
# clears the count.
|
|
1358
|
+
def self.lockout_integration_test
|
|
1359
|
+
<<~'RUBY'
|
|
1360
|
+
# Account lockout (cybertrain generate authentication):
|
|
1361
|
+
# auth_lockout_attempts (10) wrong passwords in a row lock the account
|
|
1362
|
+
# for auth_lockout_period (an hour); while it is locked even the right
|
|
1363
|
+
# password gets the wrong-password message; a successful sign-in clears
|
|
1364
|
+
# the count. SessionsController's rate_limit (ten attempts per address
|
|
1365
|
+
# in three minutes) would answer the eleventh attempt first, so every
|
|
1366
|
+
# attempt here forgets its counts (Cybertrain::RateLimit.reset_all!).
|
|
1367
|
+
require "test_helper"
|
|
1368
|
+
require_relative "../../app/commands/user_command"
|
|
1369
|
+
|
|
1370
|
+
class LockoutTest < ActionDispatch::IntegrationTest
|
|
1371
|
+
WRONG = "<p class=\"alert\">Try another email address or password.</p>"
|
|
1372
|
+
|
|
1373
|
+
setup do
|
|
1374
|
+
@user = users(:one)
|
|
1375
|
+
end
|
|
1376
|
+
|
|
1377
|
+
test "10 failures lock the account for an hour with the wrong-password message" do
|
|
1378
|
+
assert_equal 0, @user.failed_attempts
|
|
1379
|
+
9.times do |i|
|
|
1380
|
+
attempt_sign_in("wrong-password")
|
|
1381
|
+
assert_includes response.body, WRONG
|
|
1382
|
+
assert_equal i + 1, User.find(@user.id).failed_attempts
|
|
1383
|
+
assert_nil User.find(@user.id).locked_until
|
|
1384
|
+
refute User.find(@user.id).locked?
|
|
1385
|
+
end
|
|
1386
|
+
before = Time.current.to_i
|
|
1387
|
+
attempt_sign_in("wrong-password")
|
|
1388
|
+
after = Time.current.to_i
|
|
1389
|
+
assert_includes response.body, WRONG
|
|
1390
|
+
locked = User.find(@user.id)
|
|
1391
|
+
assert locked.locked?
|
|
1392
|
+
refute locked.locked_until.nil?
|
|
1393
|
+
assert locked.locked_until.to_i >= before + 3600
|
|
1394
|
+
assert locked.locked_until.to_i <= after + 3600
|
|
1395
|
+
assert_equal 0, locked.failed_attempts
|
|
1396
|
+
assert_equal 0, Session.count
|
|
1397
|
+
end
|
|
1398
|
+
|
|
1399
|
+
test "ten failures lock the account until the hour passes" do
|
|
1400
|
+
freeze_time
|
|
1401
|
+
lock_out("one@example.com")
|
|
1402
|
+
ends = User.find(@user.id).locked_until.to_i
|
|
1403
|
+
# The eleventh attempt, the right password: the rate limit's counts
|
|
1404
|
+
# are forgotten, so it is the lock that answers, with the
|
|
1405
|
+
# wrong-password message.
|
|
1406
|
+
Cybertrain::RateLimit.reset_all!
|
|
1407
|
+
sign_in_as @user
|
|
1408
|
+
assert_redirected_to "/session/new"
|
|
1409
|
+
follow_redirect!
|
|
1410
|
+
assert_includes response.body, WRONG
|
|
1411
|
+
refute response.body.include?("Try again later.")
|
|
1412
|
+
assert_equal 0, Session.count
|
|
1413
|
+
# Refused while locked, neither counted nor extended.
|
|
1414
|
+
assert_equal 0, User.find(@user.id).failed_attempts
|
|
1415
|
+
assert_equal ends, User.find(@user.id).locked_until.to_i
|
|
1416
|
+
# A minute before the hour is over: still refused.
|
|
1417
|
+
travel 59.minutes do
|
|
1418
|
+
attempt_sign_in("password")
|
|
1419
|
+
assert_includes response.body, WRONG
|
|
1420
|
+
assert_equal 0, Session.count
|
|
1421
|
+
end
|
|
1422
|
+
# Once it has passed, the right password signs in and the lock is gone.
|
|
1423
|
+
travel 1.hour do
|
|
1424
|
+
Cybertrain::RateLimit.reset_all!
|
|
1425
|
+
sign_in_as @user
|
|
1426
|
+
assert_redirected_to "/"
|
|
1427
|
+
assert_equal 1, Session.count
|
|
1428
|
+
assert_nil User.find(@user.id).locked_until
|
|
1429
|
+
assert_equal 0, User.find(@user.id).failed_attempts
|
|
1430
|
+
end
|
|
1431
|
+
end
|
|
1432
|
+
|
|
1433
|
+
test "a successful sign-in clears the count" do
|
|
1434
|
+
3.times { attempt_sign_in("wrong-password") }
|
|
1435
|
+
assert_equal 3, User.find(@user.id).failed_attempts
|
|
1436
|
+
Cybertrain::RateLimit.reset_all!
|
|
1437
|
+
sign_in_as @user
|
|
1438
|
+
assert_redirected_to "/"
|
|
1439
|
+
assert_equal 0, User.find(@user.id).failed_attempts
|
|
1440
|
+
assert_nil User.find(@user.id).locked_until
|
|
1441
|
+
# The count starts again: nine more wrong passwords do not lock.
|
|
1442
|
+
9.times { attempt_sign_in("wrong-password") }
|
|
1443
|
+
assert_equal 9, User.find(@user.id).failed_attempts
|
|
1444
|
+
assert_nil User.find(@user.id).locked_until
|
|
1445
|
+
refute User.find(@user.id).locked?
|
|
1446
|
+
end
|
|
1447
|
+
|
|
1448
|
+
test "./APP user EMAIL PASSWORD clears a lock" do
|
|
1449
|
+
lock_out("one@example.com")
|
|
1450
|
+
assert User.find(@user.id).locked?
|
|
1451
|
+
assert_equal 0, UserCommand.run(["one@example.com", "an0ther-s3cr3t"])
|
|
1452
|
+
assert_nil User.find(@user.id).locked_until
|
|
1453
|
+
assert_equal 0, User.find(@user.id).failed_attempts
|
|
1454
|
+
Cybertrain::RateLimit.reset_all!
|
|
1455
|
+
post "/session", params: { email_address: "one@example.com", password: "an0ther-s3cr3t" }
|
|
1456
|
+
assert_redirected_to "/"
|
|
1457
|
+
assert_equal 1, Session.count
|
|
1458
|
+
# Some failures, no lock: the count is cleared too.
|
|
1459
|
+
two = users(:two)
|
|
1460
|
+
3.times { attempt_sign_in("wrong-password", "two@example.com") }
|
|
1461
|
+
assert_equal 0, UserCommand.run(["two@example.com", "an0ther-s3cr3t"])
|
|
1462
|
+
assert_equal 0, User.find(two.id).failed_attempts
|
|
1463
|
+
end
|
|
1464
|
+
|
|
1465
|
+
test "with auth_lockout_attempts 0 an existing lock is not enforced" do
|
|
1466
|
+
lock_out("one@example.com")
|
|
1467
|
+
attempts = Cybertrain.config.auth_lockout_attempts
|
|
1468
|
+
begin
|
|
1469
|
+
Cybertrain.config.auth_lockout_attempts = 0
|
|
1470
|
+
# Lockout off: the lock no longer refuses the right password, and
|
|
1471
|
+
# the sign-in clears it.
|
|
1472
|
+
refute User.find(@user.id).locked_until.nil?
|
|
1473
|
+
Cybertrain::RateLimit.reset_all!
|
|
1474
|
+
sign_in_as @user
|
|
1475
|
+
assert_redirected_to "/"
|
|
1476
|
+
assert_equal 1, Session.count
|
|
1477
|
+
assert_nil User.find(@user.id).locked_until
|
|
1478
|
+
ensure
|
|
1479
|
+
Cybertrain.config.auth_lockout_attempts = attempts
|
|
1480
|
+
end
|
|
1481
|
+
# Lockout on again, a lock is enforced (made afresh, on two).
|
|
1482
|
+
two = users(:two)
|
|
1483
|
+
lock_out("two@example.com")
|
|
1484
|
+
Cybertrain::RateLimit.reset_all!
|
|
1485
|
+
sign_in_as two
|
|
1486
|
+
assert_redirected_to "/session/new"
|
|
1487
|
+
assert_equal 0, Session.where(user_id: two.id).count
|
|
1488
|
+
end
|
|
1489
|
+
|
|
1490
|
+
test "auth_lockout_attempts 0 never locks" do
|
|
1491
|
+
attempts = Cybertrain.config.auth_lockout_attempts
|
|
1492
|
+
begin
|
|
1493
|
+
Cybertrain.config.auth_lockout_attempts = 0
|
|
1494
|
+
11.times { attempt_sign_in("wrong-password") }
|
|
1495
|
+
assert_equal 0, User.find(@user.id).failed_attempts
|
|
1496
|
+
assert_nil User.find(@user.id).locked_until
|
|
1497
|
+
Cybertrain::RateLimit.reset_all!
|
|
1498
|
+
sign_in_as @user
|
|
1499
|
+
assert_redirected_to "/"
|
|
1500
|
+
assert_equal 1, Session.count
|
|
1501
|
+
ensure
|
|
1502
|
+
Cybertrain.config.auth_lockout_attempts = attempts
|
|
1503
|
+
end
|
|
1504
|
+
end
|
|
1505
|
+
|
|
1506
|
+
private
|
|
1507
|
+
|
|
1508
|
+
# One sign-in attempt through the form, the rate limit's counts
|
|
1509
|
+
# forgotten first; the page the redirect leads to is the response.
|
|
1510
|
+
def attempt_sign_in(password, email_address = "one@example.com")
|
|
1511
|
+
Cybertrain::RateLimit.reset_all!
|
|
1512
|
+
post "/session", params: { email_address: email_address, password: password }
|
|
1513
|
+
assert_response :see_other
|
|
1514
|
+
follow_redirect!
|
|
1515
|
+
nil
|
|
1516
|
+
end
|
|
1517
|
+
|
|
1518
|
+
# Ten wrong passwords: the account is locked.
|
|
1519
|
+
def lock_out(email_address)
|
|
1520
|
+
10.times { attempt_sign_in("wrong-password", email_address) }
|
|
1521
|
+
nil
|
|
1522
|
+
end
|
|
1523
|
+
end
|
|
1524
|
+
RUBY
|
|
1525
|
+
end
|
|
1526
|
+
|
|
1527
|
+
# Stands for the app's base_url variable in the tests' texts.
|
|
1528
|
+
BASE_URL_ENV_MARK = "@BASE_URL_ENV@"
|
|
1529
|
+
|
|
1530
|
+
# The private with_config helper (and NO_HOSTS) of the tests that
|
|
1531
|
+
# change the environment, the hosts and the base_url setting, each
|
|
1532
|
+
# line indented for a class body.
|
|
1533
|
+
def self.with_config_helper
|
|
1534
|
+
margin(<<~'RUBY', 2)
|
|
1535
|
+
# Runs the block with Config#env, hosts_from_env and the base_url
|
|
1536
|
+
# setting given (and Cybertrain.logger writing errors to log, unless
|
|
1537
|
+
# it is nil), then puts them back, also when the block fails.
|
|
1538
|
+
def with_config(env, hosts, base_url, log = nil)
|
|
1539
|
+
c = Cybertrain.config
|
|
1540
|
+
saved_env = c.env
|
|
1541
|
+
saved_hosts = c.hosts_from_env
|
|
1542
|
+
saved_base_url = c.string_setting(:base_url)
|
|
1543
|
+
saved_logger = Cybertrain.logger
|
|
1544
|
+
begin
|
|
1545
|
+
c.env = env
|
|
1546
|
+
c.hosts_from_env = hosts
|
|
1547
|
+
c.set_string_setting(:base_url, base_url)
|
|
1548
|
+
Cybertrain.logger = Cybertrain::Logger.new(log, :error) unless log.nil?
|
|
1549
|
+
yield
|
|
1550
|
+
ensure
|
|
1551
|
+
c.env = saved_env
|
|
1552
|
+
c.hosts_from_env = saved_hosts
|
|
1553
|
+
c.set_string_setting(:base_url, saved_base_url)
|
|
1554
|
+
Cybertrain.logger = saved_logger
|
|
1555
|
+
end
|
|
1556
|
+
nil
|
|
1557
|
+
end
|
|
1558
|
+
|
|
1559
|
+
# The first http:// or https:// link in the text of the last mail
|
|
1560
|
+
# sent (Cybertrain::Mailer.deliveries, where the test environment
|
|
1561
|
+
# keeps it); "" when none was.
|
|
1562
|
+
def last_mail_link
|
|
1563
|
+
mail = Cybertrain::Mailer.deliveries.last
|
|
1564
|
+
text = mail.nil? ? "" : mail.mail_body
|
|
1565
|
+
start = text.index("https://")
|
|
1566
|
+
plain = text.index("http://")
|
|
1567
|
+
start = plain if start.nil? || (!plain.nil? && plain < start)
|
|
1568
|
+
found = ""
|
|
1569
|
+
unless start.nil?
|
|
1570
|
+
stop = start
|
|
1571
|
+
while stop < text.size && text[stop] != " " && text[stop] != "\r" && text[stop] != "\n"
|
|
1572
|
+
stop += 1
|
|
1573
|
+
end
|
|
1574
|
+
found = text[start, stop - start].to_s
|
|
1575
|
+
end
|
|
1576
|
+
found
|
|
1577
|
+
end
|
|
1578
|
+
|
|
1579
|
+
# Whether another browser (open_session) is signed in as
|
|
1580
|
+
# email_address: the nav names them.
|
|
1581
|
+
def browser_signed_in?(browser, email_address = "one@example.com")
|
|
1582
|
+
browser.get "/session/new"
|
|
1583
|
+
browser.response.body.include?("<span>#{email_address}</span>")
|
|
1584
|
+
end
|
|
1585
|
+
RUBY
|
|
1586
|
+
end
|
|
1587
|
+
|
|
1588
|
+
# test/controllers/passwords_controller_test.rb: the reset through the
|
|
1589
|
+
# stack, its mail from Cybertrain::Mailer.deliveries.
|
|
1590
|
+
def self.passwords_controller_test(app)
|
|
1591
|
+
text = passwords_controller_test_body.gsub(BASE_URL_ENV_MARK, base_url_env(app))
|
|
1592
|
+
splice(text, "@HELPERS@", with_config_helper)
|
|
1593
|
+
end
|
|
1594
|
+
|
|
1595
|
+
def self.passwords_controller_test_body
|
|
1596
|
+
<<~'RUBY'
|
|
1597
|
+
# Password reset (cybertrain generate authentication): the address form,
|
|
1598
|
+
# the mail (kept in Cybertrain::Mailer.deliveries, as the test
|
|
1599
|
+
# environment does), the link's token (15 minutes, bound to the password
|
|
1600
|
+
# and the address) and what a reset ends.
|
|
1601
|
+
require "stringio"
|
|
1602
|
+
require "test_helper"
|
|
1603
|
+
|
|
1604
|
+
class PasswordsControllerTest < ActionDispatch::IntegrationTest
|
|
1605
|
+
SENT = "<p class=\"notice\">If that address is registered, a link to reset the password was sent to it.</p>"
|
|
1606
|
+
INVALID = "<p class=\"alert\">That password reset link is invalid or has expired.</p>"
|
|
1607
|
+
UPDATED = "<p class=\"notice\">Your password has been reset. Sign in with the new one.</p>"
|
|
1608
|
+
NEW_PASSWORD = "n3w-s3cr3t-n3w"
|
|
1609
|
+
# with_config's `hosts` for an app that lists none (CYBERTRAIN_HOSTS unset).
|
|
1610
|
+
NO_HOSTS = Array.new(0) { "" }
|
|
1611
|
+
|
|
1612
|
+
test "a reset mail is sent once and its link expires after 15 minutes" do
|
|
1613
|
+
freeze_time
|
|
1614
|
+
assert_emails(1) do
|
|
1615
|
+
request_reset("one@example.com")
|
|
1616
|
+
end
|
|
1617
|
+
assert_redirected_to "/session/new"
|
|
1618
|
+
assert_equal "one@example.com", Cybertrain::Mailer.deliveries.last.mail_to
|
|
1619
|
+
token = token_in(last_mail_link)
|
|
1620
|
+
travel 14.minutes do
|
|
1621
|
+
get "/passwords/#{token}/edit"
|
|
1622
|
+
assert_response :ok
|
|
1623
|
+
end
|
|
1624
|
+
travel 16.minutes do
|
|
1625
|
+
get "/passwords/#{token}/edit"
|
|
1626
|
+
assert_redirected_to "/passwords/new"
|
|
1627
|
+
reset_with(token, NEW_PASSWORD)
|
|
1628
|
+
assert_redirected_to "/passwords/new"
|
|
1629
|
+
end
|
|
1630
|
+
assert Cybertrain::Password.verify?("password", User.find(users(:one).id).password_digest)
|
|
1631
|
+
end
|
|
1632
|
+
|
|
1633
|
+
test "reset answers the same for unknown addresses and links to base_url" do
|
|
1634
|
+
with_config("test", NO_HOSTS, "https://shop.example") do
|
|
1635
|
+
# A forged Host on both: the link takes base_url, never the header.
|
|
1636
|
+
request_reset("One@Example.com", "evil.example")
|
|
1637
|
+
known = response
|
|
1638
|
+
follow_redirect!
|
|
1639
|
+
known_page = response.body
|
|
1640
|
+
assert_enqueued_jobs 1, "cybertrain mail"
|
|
1641
|
+
request_reset("nobody@example.com", "evil.example")
|
|
1642
|
+
unknown = response
|
|
1643
|
+
assert_equal 303, known.status
|
|
1644
|
+
assert_equal known.status, unknown.status
|
|
1645
|
+
assert_equal "/session/new", known.header("Location")
|
|
1646
|
+
assert_equal known.header("Location"), unknown.header("Location")
|
|
1647
|
+
assert_equal known.body, unknown.body
|
|
1648
|
+
follow_redirect!
|
|
1649
|
+
assert_includes known_page, SENT
|
|
1650
|
+
assert_includes response.body, SENT
|
|
1651
|
+
# One mail, for the registered address only.
|
|
1652
|
+
assert_enqueued_jobs 1, "cybertrain mail"
|
|
1653
|
+
end
|
|
1654
|
+
assert_equal 1, perform_enqueued_jobs
|
|
1655
|
+
link = last_mail_link
|
|
1656
|
+
assert link.start_with?("https://shop.example/passwords/"), link
|
|
1657
|
+
assert link.end_with?("/edit"), link
|
|
1658
|
+
refute link.include?("evil.example")
|
|
1659
|
+
# The link works once.
|
|
1660
|
+
token = token_in(link)
|
|
1661
|
+
get "/passwords/#{token}/edit"
|
|
1662
|
+
assert_response :ok
|
|
1663
|
+
reset_with(token, NEW_PASSWORD)
|
|
1664
|
+
assert_redirected_to "/session/new"
|
|
1665
|
+
get "/passwords/#{token}/edit"
|
|
1666
|
+
assert_redirected_to "/passwords/new"
|
|
1667
|
+
end
|
|
1668
|
+
|
|
1669
|
+
test "without base_url a link takes the request's address only where that is safe" do
|
|
1670
|
+
# Tests: the address the request came to.
|
|
1671
|
+
with_config("test", NO_HOSTS, "") do
|
|
1672
|
+
request_reset("one@example.com", "www.example.com")
|
|
1673
|
+
end
|
|
1674
|
+
assert_equal 1, perform_enqueued_jobs
|
|
1675
|
+
link = last_mail_link
|
|
1676
|
+
assert link.start_with?("http://www.example.com/passwords/"), link
|
|
1677
|
+
# Development checks the Host header: the request's address.
|
|
1678
|
+
with_config("development", NO_HOSTS, "") do
|
|
1679
|
+
assert Cybertrain.config.host_check?
|
|
1680
|
+
request_reset("one@example.com", "localhost:3000")
|
|
1681
|
+
assert_redirected_to "/session/new"
|
|
1682
|
+
end
|
|
1683
|
+
assert_equal 1, perform_enqueued_jobs
|
|
1684
|
+
link = last_mail_link
|
|
1685
|
+
assert link.start_with?("http://localhost:3000/passwords/"), link
|
|
1686
|
+
# Production with CYBERTRAIN_HOSTS (the Host header is checked): the
|
|
1687
|
+
# request's address too.
|
|
1688
|
+
with_config("production", ["shop.example"], "") do
|
|
1689
|
+
assert Cybertrain.config.host_check?
|
|
1690
|
+
request_reset("one@example.com", "shop.example")
|
|
1691
|
+
assert_redirected_to "/session/new"
|
|
1692
|
+
end
|
|
1693
|
+
assert_equal 1, perform_enqueued_jobs
|
|
1694
|
+
link = last_mail_link
|
|
1695
|
+
assert link.start_with?("http://shop.example/passwords/"), link
|
|
1696
|
+
# Production with base_url: base_url, whatever the Host.
|
|
1697
|
+
with_config("production", NO_HOSTS, "https://shop.example") do
|
|
1698
|
+
request_reset("one@example.com", "evil.example")
|
|
1699
|
+
assert_redirected_to "/session/new"
|
|
1700
|
+
end
|
|
1701
|
+
assert_equal 1, perform_enqueued_jobs
|
|
1702
|
+
link = last_mail_link
|
|
1703
|
+
assert link.start_with?("https://shop.example/passwords/"), link
|
|
1704
|
+
# Production with neither: no mail, an [ERROR] line, the same answer.
|
|
1705
|
+
log = StringIO.new
|
|
1706
|
+
with_config("production", NO_HOSTS, "", log) do
|
|
1707
|
+
refute Cybertrain.config.host_check?
|
|
1708
|
+
request_reset("one@example.com", "evil.example")
|
|
1709
|
+
assert_redirected_to "/session/new"
|
|
1710
|
+
follow_redirect!
|
|
1711
|
+
assert_includes response.body, SENT
|
|
1712
|
+
end
|
|
1713
|
+
assert_no_enqueued_jobs "cybertrain mail"
|
|
1714
|
+
assert_includes log.string, "[ERROR] password reset mail not sent: set @BASE_URL_ENV@ (or CYBERTRAIN_HOSTS)\n"
|
|
1715
|
+
end
|
|
1716
|
+
|
|
1717
|
+
test "any other environment without base_url sends no mail and answers the same" do
|
|
1718
|
+
log = StringIO.new
|
|
1719
|
+
# A staging server reached with a forged Host: not production, but
|
|
1720
|
+
# nothing checks its Host header either.
|
|
1721
|
+
with_config("staging", NO_HOSTS, "", log) do
|
|
1722
|
+
refute Cybertrain.config.host_check?
|
|
1723
|
+
request_reset("one@example.com", "evil.example")
|
|
1724
|
+
known = response
|
|
1725
|
+
follow_redirect!
|
|
1726
|
+
assert_includes response.body, SENT
|
|
1727
|
+
request_reset("nobody@example.com", "evil.example")
|
|
1728
|
+
unknown = response
|
|
1729
|
+
assert_equal 303, known.status
|
|
1730
|
+
assert_equal known.status, unknown.status
|
|
1731
|
+
assert_equal "/session/new", known.header("Location")
|
|
1732
|
+
assert_equal known.header("Location"), unknown.header("Location")
|
|
1733
|
+
assert_equal known.body, unknown.body
|
|
1734
|
+
follow_redirect!
|
|
1735
|
+
assert_includes response.body, SENT
|
|
1736
|
+
end
|
|
1737
|
+
assert_no_enqueued_jobs "cybertrain mail"
|
|
1738
|
+
assert_includes log.string, "[ERROR] password reset mail not sent: set @BASE_URL_ENV@ (or CYBERTRAIN_HOSTS)\n"
|
|
1739
|
+
assert log.string.index("evil.example").nil?
|
|
1740
|
+
end
|
|
1741
|
+
|
|
1742
|
+
# Such an address is refused at entry now (User.email_address_format?),
|
|
1743
|
+
# so this one was saved before that check existed.
|
|
1744
|
+
test "an address the mailer refuses gets the same answer, and the log says why" do
|
|
1745
|
+
users(:two).update_columns(email_address: "josé@example.com")
|
|
1746
|
+
log = StringIO.new
|
|
1747
|
+
with_config("test", NO_HOSTS, "https://shop.example", log) do
|
|
1748
|
+
request_reset("josé@example.com")
|
|
1749
|
+
refused = response
|
|
1750
|
+
follow_redirect!
|
|
1751
|
+
assert_includes response.body, SENT
|
|
1752
|
+
request_reset("nobody@example.com")
|
|
1753
|
+
assert_equal 303, refused.status
|
|
1754
|
+
assert_equal refused.status, response.status
|
|
1755
|
+
assert_equal "/session/new", refused.header("Location")
|
|
1756
|
+
assert_equal refused.header("Location"), response.header("Location")
|
|
1757
|
+
assert_equal refused.body, response.body
|
|
1758
|
+
end
|
|
1759
|
+
assert_no_enqueued_jobs "cybertrain mail"
|
|
1760
|
+
assert_includes log.string, "[ERROR] password reset mail not sent: the to address contains a space, a control character or a non-ASCII byte\n"
|
|
1761
|
+
end
|
|
1762
|
+
|
|
1763
|
+
test "the token expires after 15 minutes" do
|
|
1764
|
+
user = users(:one)
|
|
1765
|
+
now = Time.current.to_i
|
|
1766
|
+
assert user.password_reset_token(now).start_with?("#{user.id}--#{now + 15 * 60}--")
|
|
1767
|
+
stale = user.password_reset_token(now - 16 * 60)
|
|
1768
|
+
fresh = user.password_reset_token(now - 14 * 60)
|
|
1769
|
+
assert_nil User.find_by_password_reset_token(stale)
|
|
1770
|
+
found = User.find_by_password_reset_token(fresh)
|
|
1771
|
+
refute found.nil?
|
|
1772
|
+
assert_equal user.id, found.id
|
|
1773
|
+
get "/passwords/#{stale}/edit"
|
|
1774
|
+
assert_redirected_to "/passwords/new"
|
|
1775
|
+
assert_equal "no-referrer", response.header("Referrer-Policy")
|
|
1776
|
+
follow_redirect!
|
|
1777
|
+
assert_includes response.body, INVALID
|
|
1778
|
+
reset_with(stale, NEW_PASSWORD)
|
|
1779
|
+
assert_redirected_to "/passwords/new"
|
|
1780
|
+
assert_equal "no-referrer", response.header("Referrer-Policy")
|
|
1781
|
+
assert_equal user.password_digest, User.find_by(id: user.id).password_digest
|
|
1782
|
+
get "/passwords/#{fresh}/edit"
|
|
1783
|
+
assert_response :ok
|
|
1784
|
+
page = response
|
|
1785
|
+
# The page's address holds the token: no Referer leaves it.
|
|
1786
|
+
assert_equal "no-referrer", page.header("Referrer-Policy")
|
|
1787
|
+
# The address form's page is not the link's.
|
|
1788
|
+
get "/passwords/new"
|
|
1789
|
+
assert_nil response.header("Referrer-Policy")
|
|
1790
|
+
assert_includes page.body, "<form action=\"/passwords/#{fresh}\" method=\"post\"><input type=\"hidden\" name=\"_method\" value=\"put\">"
|
|
1791
|
+
end
|
|
1792
|
+
|
|
1793
|
+
test "the token stops working once the password changed" do
|
|
1794
|
+
ann = users(:one)
|
|
1795
|
+
bob = users(:two)
|
|
1796
|
+
token = ann.password_reset_token
|
|
1797
|
+
earlier = ann.password_reset_token(Time.current.to_i - 60)
|
|
1798
|
+
# A token names its user and is bound to that user's password and
|
|
1799
|
+
# address: one with bob's id and ann's binding, ann's id and her
|
|
1800
|
+
# password alone, or another purpose, is no one's.
|
|
1801
|
+
assert_equal "#{ann.password_digest}:one@example.com", ann.password_reset_binding
|
|
1802
|
+
assert_equal ann.id, User.find_by_password_reset_token(token).id
|
|
1803
|
+
assert_equal bob.id, User.find_by_password_reset_token(bob.password_reset_token).id
|
|
1804
|
+
assert_nil User.find_by_password_reset_token(Cybertrain::Token.issue("password_reset", bob.id, 15 * 60, ann.password_reset_binding))
|
|
1805
|
+
assert_nil User.find_by_password_reset_token(Cybertrain::Token.issue("password_reset", ann.id, 15 * 60, ann.password_digest))
|
|
1806
|
+
assert_nil User.find_by_password_reset_token(Cybertrain::Token.issue("confirmation", ann.id, 15 * 60, ann.password_reset_binding))
|
|
1807
|
+
assert_nil User.find_by_password_reset_token("")
|
|
1808
|
+
# A new password that does not pass: the token still holds.
|
|
1809
|
+
reset_with(token, NEW_PASSWORD, "something-else")
|
|
1810
|
+
assert_redirected_to "/passwords/#{token}/edit"
|
|
1811
|
+
assert_equal "no-referrer", response.header("Referrer-Policy")
|
|
1812
|
+
follow_redirect!
|
|
1813
|
+
assert_includes response.body, "<p class=\"alert\">Password confirmation doesn't match Password</p>"
|
|
1814
|
+
refute User.find_by_password_reset_token(token).nil?
|
|
1815
|
+
reset_with(token, NEW_PASSWORD)
|
|
1816
|
+
assert_redirected_to "/session/new"
|
|
1817
|
+
# Used once, the link is spent, and so is every link sent before.
|
|
1818
|
+
get "/passwords/#{token}/edit"
|
|
1819
|
+
assert_redirected_to "/passwords/new"
|
|
1820
|
+
reset_with(earlier, "an0ther-s3cr3t")
|
|
1821
|
+
assert_redirected_to "/passwords/new"
|
|
1822
|
+
follow_redirect!
|
|
1823
|
+
assert_includes response.body, INVALID
|
|
1824
|
+
post "/session", params: { email_address: "one@example.com", password: NEW_PASSWORD }
|
|
1825
|
+
assert_redirected_to "/"
|
|
1826
|
+
# bob's link is not ann's change: it still works.
|
|
1827
|
+
refute User.find_by_password_reset_token(bob.password_reset_token).nil?
|
|
1828
|
+
# A password changed any other way ends a link too.
|
|
1829
|
+
changed = User.find_by(email_address: "one@example.com")
|
|
1830
|
+
fresh = changed.password_reset_token
|
|
1831
|
+
changed.password = "th1rd-s3cr3t-x"
|
|
1832
|
+
changed.save!
|
|
1833
|
+
assert_nil User.find_by_password_reset_token(fresh)
|
|
1834
|
+
# And so does a new address (accounts_controller_test.rb changes it
|
|
1835
|
+
# through the page).
|
|
1836
|
+
fresh = changed.password_reset_token
|
|
1837
|
+
refute User.find_by_password_reset_token(fresh).nil?
|
|
1838
|
+
changed.email_address = "one@new.example"
|
|
1839
|
+
changed.save!
|
|
1840
|
+
assert_nil User.find_by_password_reset_token(fresh)
|
|
1841
|
+
end
|
|
1842
|
+
|
|
1843
|
+
test "updating the password ends every other session" do
|
|
1844
|
+
ann = users(:one)
|
|
1845
|
+
# The laptop (this browser) signed in a day ago.
|
|
1846
|
+
travel_to 1.day.ago do
|
|
1847
|
+
sign_in_as ann
|
|
1848
|
+
end
|
|
1849
|
+
assert Session.first.created_at.to_i <= Time.current.to_i - 24 * 60 * 60
|
|
1850
|
+
phone = open_session
|
|
1851
|
+
phone.sign_in_as ann
|
|
1852
|
+
bobs = open_session
|
|
1853
|
+
bobs.sign_in_as users(:two)
|
|
1854
|
+
assert_equal 2, Session.where(user_id: ann.id).count
|
|
1855
|
+
assert signed_in?
|
|
1856
|
+
assert browser_signed_in?(phone)
|
|
1857
|
+
stranger = open_session
|
|
1858
|
+
stranger.put "/passwords/#{ann.password_reset_token}", params: { password: NEW_PASSWORD, password_confirmation: NEW_PASSWORD }
|
|
1859
|
+
stranger.assert_redirected_to "/session/new"
|
|
1860
|
+
stranger.follow_redirect!
|
|
1861
|
+
assert_includes stranger.response.body, UPDATED
|
|
1862
|
+
assert_equal 0, Session.where(user_id: ann.id).count
|
|
1863
|
+
refute signed_in?
|
|
1864
|
+
refute browser_signed_in?(phone)
|
|
1865
|
+
# Another user's sessions are left alone.
|
|
1866
|
+
assert_equal 1, Session.count
|
|
1867
|
+
assert browser_signed_in?(bobs, "two@example.com")
|
|
1868
|
+
# The old password no longer signs in; the new one does.
|
|
1869
|
+
stranger.sign_in_as ann
|
|
1870
|
+
stranger.assert_redirected_to "/session/new"
|
|
1871
|
+
stranger.post "/session", params: { email_address: "one@example.com", password: NEW_PASSWORD }
|
|
1872
|
+
stranger.assert_redirected_to "/"
|
|
1873
|
+
end
|
|
1874
|
+
|
|
1875
|
+
test "a reset clears a lockout" do
|
|
1876
|
+
ann = users(:one)
|
|
1877
|
+
attempts = 0
|
|
1878
|
+
while attempts < Cybertrain.config.auth_lockout_attempts
|
|
1879
|
+
Cybertrain::RateLimit.reset_all!
|
|
1880
|
+
post "/session", params: { email_address: "one@example.com", password: "wrong-password" }
|
|
1881
|
+
attempts += 1
|
|
1882
|
+
end
|
|
1883
|
+
locked = User.find_by(id: ann.id)
|
|
1884
|
+
assert locked.locked?
|
|
1885
|
+
# While locked, the right password is refused.
|
|
1886
|
+
Cybertrain::RateLimit.reset_all!
|
|
1887
|
+
sign_in_as ann
|
|
1888
|
+
assert_redirected_to "/session/new"
|
|
1889
|
+
reset_with(locked.password_reset_token, NEW_PASSWORD)
|
|
1890
|
+
assert_redirected_to "/session/new"
|
|
1891
|
+
cleared = User.find_by(id: ann.id)
|
|
1892
|
+
assert_nil cleared.locked_until
|
|
1893
|
+
assert_equal 0, cleared.failed_attempts
|
|
1894
|
+
refute cleared.locked?
|
|
1895
|
+
post "/session", params: { email_address: "one@example.com", password: NEW_PASSWORD }
|
|
1896
|
+
assert_redirected_to "/"
|
|
1897
|
+
end
|
|
1898
|
+
|
|
1899
|
+
test "the mail is stored with the dictionary's subject" do
|
|
1900
|
+
with_config("test", NO_HOSTS, "https://shop.example/") do
|
|
1901
|
+
request_reset("one@example.com")
|
|
1902
|
+
end
|
|
1903
|
+
assert_enqueued_jobs 1, "cybertrain mail"
|
|
1904
|
+
# The sender is Config#smtp_from (CYBERTRAIN_SMTP_FROM): set here,
|
|
1905
|
+
# so that the mail's is pinned whatever the environment gives.
|
|
1906
|
+
from = Cybertrain.config.smtp_from
|
|
1907
|
+
begin
|
|
1908
|
+
Cybertrain.config.smtp_from = "set-by-this-test@example.org"
|
|
1909
|
+
assert_equal 1, perform_enqueued_jobs
|
|
1910
|
+
ensure
|
|
1911
|
+
Cybertrain.config.smtp_from = from
|
|
1912
|
+
end
|
|
1913
|
+
mail = Cybertrain::Mailer.deliveries.last
|
|
1914
|
+
refute mail.nil?
|
|
1915
|
+
assert_equal "Reset your password", mail.mail_subject
|
|
1916
|
+
assert_equal I18n.t("passwords_mailer.reset.subject"), mail.mail_subject
|
|
1917
|
+
assert_equal "one@example.com", mail.mail_to
|
|
1918
|
+
assert_equal "set-by-this-test@example.org", mail.mail_from
|
|
1919
|
+
link = last_mail_link
|
|
1920
|
+
# base_url's trailing slash is not doubled.
|
|
1921
|
+
assert link.start_with?("https://shop.example/passwords/"), link
|
|
1922
|
+
assert_equal "Hello one@example.com,\n\nYou can choose a new password within the next 15 minutes at this address:\n" \
|
|
1923
|
+
"#{link}\n\nIf you did not ask for this, ignore this mail: your password stays as it is.\n", mail.mail_body
|
|
1924
|
+
assert_includes mail.mail_html, "<p>Hello one@example.com,</p>"
|
|
1925
|
+
assert_includes mail.mail_html, "<p><a href=\"#{link}\">Choose a new password</a></p>"
|
|
1926
|
+
end
|
|
1927
|
+
|
|
1928
|
+
test "POST /passwords past ten requests is refused for a while" do
|
|
1929
|
+
Cybertrain::RateLimit.reset_all!
|
|
1930
|
+
count = 0
|
|
1931
|
+
while count < 10
|
|
1932
|
+
post "/passwords", params: { email_address: "nobody@example.com" }
|
|
1933
|
+
assert_redirected_to "/session/new"
|
|
1934
|
+
count += 1
|
|
1935
|
+
end
|
|
1936
|
+
post "/passwords", params: { email_address: "one@example.com" }
|
|
1937
|
+
assert_redirected_to "/passwords/new"
|
|
1938
|
+
follow_redirect!
|
|
1939
|
+
assert_includes response.body, "<p class=\"alert\">Try again later.</p>"
|
|
1940
|
+
assert_no_enqueued_jobs "cybertrain mail"
|
|
1941
|
+
Cybertrain::RateLimit.reset_all!
|
|
1942
|
+
end
|
|
1943
|
+
|
|
1944
|
+
private
|
|
1945
|
+
|
|
1946
|
+
# Sends the address form as a browser would, with the Host header
|
|
1947
|
+
# given. The rate limit's counts are forgotten first.
|
|
1948
|
+
def request_reset(email_address, host = "www.example.com")
|
|
1949
|
+
Cybertrain::RateLimit.reset_all!
|
|
1950
|
+
post "/passwords", params: { email_address: email_address }, headers: { "host" => host }
|
|
1951
|
+
nil
|
|
1952
|
+
end
|
|
1953
|
+
|
|
1954
|
+
# Sends the new-password form of the link with this token.
|
|
1955
|
+
def reset_with(token, password, confirmation = password)
|
|
1956
|
+
put "/passwords/#{token}", params: { password: password, password_confirmation: confirmation }
|
|
1957
|
+
nil
|
|
1958
|
+
end
|
|
1959
|
+
|
|
1960
|
+
# The token in a link ("https://.../passwords/<token>/edit").
|
|
1961
|
+
def token_in(link)
|
|
1962
|
+
start = link.index("/passwords/").to_i + 11
|
|
1963
|
+
stop = link.index("/edit").to_i
|
|
1964
|
+
link[start, stop - start].to_s
|
|
1965
|
+
end
|
|
1966
|
+
|
|
1967
|
+
# Whether this browser is signed in as email_address: the nav names them.
|
|
1968
|
+
def signed_in?(email_address = "one@example.com")
|
|
1969
|
+
get "/session/new"
|
|
1970
|
+
response.body.include?("<span>#{email_address}</span>")
|
|
1971
|
+
end
|
|
1972
|
+
|
|
1973
|
+
@HELPERS@
|
|
1974
|
+
end
|
|
1975
|
+
RUBY
|
|
1976
|
+
end
|
|
1977
|
+
|
|
1978
|
+
# test/controllers/accounts_controller_test.rb: the account page.
|
|
1979
|
+
def self.accounts_controller_test(confirmable = false)
|
|
1980
|
+
text = splice(accounts_controller_test_body, "@ADDRESS_HELPERS@",
|
|
1981
|
+
margin(confirmable ? accounts_test_confirm_helpers : accounts_test_address_helpers, 2))
|
|
1982
|
+
splice(text, "@HELPERS@", with_config_helper)
|
|
1983
|
+
end
|
|
1984
|
+
|
|
1985
|
+
def self.accounts_controller_test_body
|
|
1986
|
+
<<~'RUBY'
|
|
1987
|
+
# The account page (cybertrain generate authentication): a new address or
|
|
1988
|
+
# password only with the current password, what a new password ends and
|
|
1989
|
+
# keeps, deleting the account, and the limits on guessing the password.
|
|
1990
|
+
require "stringio"
|
|
1991
|
+
require "test_helper"
|
|
1992
|
+
|
|
1993
|
+
class AccountsControllerTest < ActionDispatch::IntegrationTest
|
|
1994
|
+
WRONG_PASSWORD = "<p class=\"alert\">That is not your current password.</p>"
|
|
1995
|
+
UPDATED = "<p class=\"notice\">Your account has been updated.</p>"
|
|
1996
|
+
DELETED = "<p class=\"notice\">Your account has been deleted.</p>"
|
|
1997
|
+
NOT_DELETED = "<p class=\"alert\">Your account could not be deleted. Nothing has changed.</p>"
|
|
1998
|
+
NEW_PASSWORD = "n3w-s3cr3t-n3w"
|
|
1999
|
+
# with_config's `hosts` for an app that lists none (CYBERTRAIN_HOSTS unset).
|
|
2000
|
+
NO_HOSTS = Array.new(0) { "" }
|
|
2001
|
+
@ADDRESS_HELPERS@
|
|
2002
|
+
|
|
2003
|
+
test "changing email or password requires the current password" do
|
|
2004
|
+
ann = users(:one)
|
|
2005
|
+
sign_in_as ann
|
|
2006
|
+
digest = ann.password_digest
|
|
2007
|
+
reset_token = ann.password_reset_token
|
|
2008
|
+
# A wrong current password: nothing changes, and the page says why.
|
|
2009
|
+
update_account("wrong-password", { email_address: "one@new.example", password: NEW_PASSWORD, password_confirmation: NEW_PASSWORD })
|
|
2010
|
+
assert_response :unprocessable_entity
|
|
2011
|
+
assert_includes response.body, WRONG_PASSWORD
|
|
2012
|
+
same = User.find(ann.id)
|
|
2013
|
+
assert_equal "one@example.com", same.email_address
|
|
2014
|
+
assert_equal digest, same.password_digest
|
|
2015
|
+
# None at all, for the address alone: the same.
|
|
2016
|
+
update_account("", { email_address: "one@new.example" })
|
|
2017
|
+
assert_response :unprocessable_entity
|
|
2018
|
+
assert_includes response.body, WRONG_PASSWORD
|
|
2019
|
+
assert_equal "one@example.com", User.find(ann.id).email_address
|
|
2020
|
+
# A wrong password ends no session.
|
|
2021
|
+
assert signed_in?
|
|
2022
|
+
assert_equal 1, Session.count
|
|
2023
|
+
session_id = Session.first.id
|
|
2024
|
+
# The right one: the new address (normalized), once it takes effect;
|
|
2025
|
+
# the password and the session stay as they were.
|
|
2026
|
+
update_account("password", { email_address: " One@New.Example ", password: "", password_confirmation: "" })
|
|
2027
|
+
assert_redirected_to "/account/edit"
|
|
2028
|
+
follow_redirect!
|
|
2029
|
+
assert_includes response.body, ADDRESS_CHANGED
|
|
2030
|
+
confirm_new_address(ann)
|
|
2031
|
+
get "/account/edit"
|
|
2032
|
+
assert_includes response.body, "<span>one@new.example</span>"
|
|
2033
|
+
changed = User.find(ann.id)
|
|
2034
|
+
assert_equal "one@new.example", changed.email_address
|
|
2035
|
+
assert_equal digest, changed.password_digest
|
|
2036
|
+
assert_equal 1, Session.count
|
|
2037
|
+
assert_equal session_id, Session.first.id
|
|
2038
|
+
# A password reset link sent to the old address no longer works.
|
|
2039
|
+
assert_nil User.find_by_password_reset_token(reset_token)
|
|
2040
|
+
# Signing in takes the new address, not the old one.
|
|
2041
|
+
other = open_session
|
|
2042
|
+
other.post "/session", params: { email_address: "one@example.com", password: "password" }
|
|
2043
|
+
other.assert_redirected_to "/session/new"
|
|
2044
|
+
other.post "/session", params: { email_address: "one@new.example", password: "password" }
|
|
2045
|
+
other.assert_redirected_to "/"
|
|
2046
|
+
# A taken address does not pass: the errors, and the nav still shows
|
|
2047
|
+
# the address in use.
|
|
2048
|
+
update_account("password", { email_address: "two@example.com" })
|
|
2049
|
+
assert_response :unprocessable_entity
|
|
2050
|
+
assert_includes response.body, "<li>Email address has already been taken</li>"
|
|
2051
|
+
assert_includes response.body, "<span>one@new.example</span>"
|
|
2052
|
+
assert_equal "one@new.example", User.find(ann.id).email_address
|
|
2053
|
+
# Nor does an address list (two mailboxes for one account), and no
|
|
2054
|
+
# link is mailed for it.
|
|
2055
|
+
update_account("password", { email_address: "one@new.example,eve@evil.example" })
|
|
2056
|
+
assert_response :unprocessable_entity
|
|
2057
|
+
assert_includes response.body, "<li>Email address is invalid</li>"
|
|
2058
|
+
assert_equal "one@new.example", User.find(ann.id).email_address
|
|
2059
|
+
# Nor an address the mailer would refuse (Cybertrain::Mailer.address_problem).
|
|
2060
|
+
["one@new.example\u00A0", "one\u3000@new.example", "one\u0085@new.example", "one@new\u2028.example", "one;x@new.example", "one(x)@new.example"].each do |address|
|
|
2061
|
+
update_account("password", { email_address: address })
|
|
2062
|
+
assert_response :unprocessable_entity
|
|
2063
|
+
assert_includes response.body, "<li>Email address is invalid</li>"
|
|
2064
|
+
assert_equal "one@new.example", User.find(ann.id).email_address
|
|
2065
|
+
end
|
|
2066
|
+
# A new password that does not pass: nothing changes.
|
|
2067
|
+
update_account("password", { password: "short", password_confirmation: "short" })
|
|
2068
|
+
assert_response :unprocessable_entity
|
|
2069
|
+
assert_includes response.body, "<li>Password is too short (minimum is 8 characters)</li>"
|
|
2070
|
+
update_account("password", { password: NEW_PASSWORD, password_confirmation: "something-else" })
|
|
2071
|
+
assert_response :unprocessable_entity
|
|
2072
|
+
assert_includes response.body, "<li>Password confirmation doesn't match Password</li>"
|
|
2073
|
+
assert_equal digest, User.find(ann.id).password_digest
|
|
2074
|
+
# The right current password and a new one: only the new one signs in.
|
|
2075
|
+
update_account("password", { password: NEW_PASSWORD, password_confirmation: NEW_PASSWORD })
|
|
2076
|
+
assert_redirected_to "/account/edit"
|
|
2077
|
+
refute User.find(ann.id).password_digest == digest
|
|
2078
|
+
other.post "/session", params: { email_address: "one@new.example", password: "password" }
|
|
2079
|
+
other.assert_redirected_to "/session/new"
|
|
2080
|
+
other.post "/session", params: { email_address: "one@new.example", password: NEW_PASSWORD }
|
|
2081
|
+
other.assert_redirected_to "/"
|
|
2082
|
+
end
|
|
2083
|
+
|
|
2084
|
+
test "a password change ends other sessions and keeps this one" do
|
|
2085
|
+
ann = users(:one)
|
|
2086
|
+
bob = users(:two)
|
|
2087
|
+
# This browser (a laptop) asked to stay signed in; the phone did not.
|
|
2088
|
+
sign_in_as ann, remember: true
|
|
2089
|
+
laptop_id = Session.first.id
|
|
2090
|
+
assert_equal true, Session.first.remember
|
|
2091
|
+
phone = open_session
|
|
2092
|
+
phone.sign_in_as ann
|
|
2093
|
+
bobs = open_session
|
|
2094
|
+
bobs.sign_in_as bob
|
|
2095
|
+
assert_equal 2, Session.where(user_id: ann.id).count
|
|
2096
|
+
token = ann.password_reset_token
|
|
2097
|
+
refute User.find_by_password_reset_token(token).nil?
|
|
2098
|
+
update_account("password", { password: NEW_PASSWORD, password_confirmation: NEW_PASSWORD })
|
|
2099
|
+
assert_redirected_to "/account/edit"
|
|
2100
|
+
# This browser is signed in again at once, still remembered (the
|
|
2101
|
+
# cookie keeps its Max-Age), on a new Session row with the new
|
|
2102
|
+
# password key.
|
|
2103
|
+
assert session_cookie_of(response).end_with?("; Max-Age=2592000"), session_cookie_of(response)
|
|
2104
|
+
follow_redirect!
|
|
2105
|
+
assert_includes response.body, UPDATED
|
|
2106
|
+
rows = Session.where(user_id: ann.id).to_a
|
|
2107
|
+
assert_equal 1, rows.size
|
|
2108
|
+
refute rows[0].id == laptop_id
|
|
2109
|
+
assert_equal true, rows[0].remember
|
|
2110
|
+
assert signed_in?
|
|
2111
|
+
refute browser_signed_in?(phone)
|
|
2112
|
+
assert browser_signed_in?(bobs, "two@example.com")
|
|
2113
|
+
assert_equal 1, Session.where(user_id: bob.id).count
|
|
2114
|
+
# A reset link sent before no longer works.
|
|
2115
|
+
assert_nil User.find_by_password_reset_token(token)
|
|
2116
|
+
# A browser that did not ask to stay signed in still has not: no Max-Age.
|
|
2117
|
+
phone.post "/session", params: { email_address: "one@example.com", password: NEW_PASSWORD }
|
|
2118
|
+
assert browser_signed_in?(phone)
|
|
2119
|
+
Cybertrain::RateLimit.reset_all!
|
|
2120
|
+
phone.patch "/account", params: { user: { current_password: NEW_PASSWORD, password: "th1rd-s3cr3t", password_confirmation: "th1rd-s3cr3t" } }
|
|
2121
|
+
phone.assert_redirected_to "/account/edit"
|
|
2122
|
+
refute session_cookie_of(phone.response).empty?
|
|
2123
|
+
refute session_cookie_of(phone.response).include?("Max-Age")
|
|
2124
|
+
rows = Session.where(user_id: ann.id).to_a
|
|
2125
|
+
assert_equal 1, rows.size
|
|
2126
|
+
assert_equal false, rows[0].remember
|
|
2127
|
+
assert browser_signed_in?(phone)
|
|
2128
|
+
refute signed_in?
|
|
2129
|
+
# A new address alone ends no session.
|
|
2130
|
+
post "/session", params: { email_address: "one@example.com", password: "th1rd-s3cr3t" }
|
|
2131
|
+
assert_equal 2, Session.where(user_id: ann.id).count
|
|
2132
|
+
Cybertrain::RateLimit.reset_all!
|
|
2133
|
+
phone.patch "/account", params: { user: { current_password: "th1rd-s3cr3t", email_address: "one@new.example" } }
|
|
2134
|
+
phone.assert_redirected_to "/account/edit"
|
|
2135
|
+
confirm_new_address(ann)
|
|
2136
|
+
assert_equal 2, Session.where(user_id: ann.id).count
|
|
2137
|
+
assert signed_in?("one@new.example")
|
|
2138
|
+
assert browser_signed_in?(phone, "one@new.example")
|
|
2139
|
+
end
|
|
2140
|
+
|
|
2141
|
+
# The user deleted here is one the test makes (account_holder): a
|
|
2142
|
+
# fixture of the app's own may point at users(:one) (a scaffold's
|
|
2143
|
+
# `user:references` writes `user: one`), and the database keeps a
|
|
2144
|
+
# user that rows point at.
|
|
2145
|
+
test "deleting the account requires the password and removes the user and sessions" do
|
|
2146
|
+
ann = account_holder
|
|
2147
|
+
sign_in_as ann
|
|
2148
|
+
phone = open_session
|
|
2149
|
+
phone.sign_in_as ann
|
|
2150
|
+
bobs = open_session
|
|
2151
|
+
bobs.sign_in_as users(:two)
|
|
2152
|
+
assert_equal 3, Session.count
|
|
2153
|
+
# A wrong password, or none: nothing is deleted.
|
|
2154
|
+
delete_account("wrong-password")
|
|
2155
|
+
assert_response :unprocessable_entity
|
|
2156
|
+
assert_includes response.body, WRONG_PASSWORD
|
|
2157
|
+
delete_account("")
|
|
2158
|
+
assert_response :unprocessable_entity
|
|
2159
|
+
assert_includes response.body, WRONG_PASSWORD
|
|
2160
|
+
refute User.find_by(id: ann.id).nil?
|
|
2161
|
+
assert_equal 3, Session.count
|
|
2162
|
+
assert signed_in?("ann@example.com")
|
|
2163
|
+
# The right one: the user, every session of theirs, and this
|
|
2164
|
+
# browser's cookie session.
|
|
2165
|
+
delete_account("password")
|
|
2166
|
+
assert_redirected_to "/"
|
|
2167
|
+
get "/session/new"
|
|
2168
|
+
assert_includes response.body, DELETED
|
|
2169
|
+
assert_nil User.find_by(id: ann.id)
|
|
2170
|
+
assert_equal 0, Session.where(user_id: ann.id).count
|
|
2171
|
+
assert_equal 1, Session.count
|
|
2172
|
+
# The fixtures' users are still there.
|
|
2173
|
+
assert_equal 2, User.all.count
|
|
2174
|
+
refute signed_in?("ann@example.com")
|
|
2175
|
+
refute browser_signed_in?(phone, "ann@example.com")
|
|
2176
|
+
assert browser_signed_in?(bobs, "two@example.com")
|
|
2177
|
+
# The address signs in no more.
|
|
2178
|
+
sign_in_as ann
|
|
2179
|
+
assert_redirected_to "/session/new"
|
|
2180
|
+
get "/account/edit"
|
|
2181
|
+
assert_redirected_to "/session/new"
|
|
2182
|
+
end
|
|
2183
|
+
|
|
2184
|
+
test "a visitor is sent to sign in" do
|
|
2185
|
+
ann = users(:one)
|
|
2186
|
+
get "/account/edit"
|
|
2187
|
+
assert_redirected_to "/session/new"
|
|
2188
|
+
# The forms' requests too, with the right password: nothing changes.
|
|
2189
|
+
patch "/account", params: { user: { current_password: "password", email_address: "eve@example.com" } }
|
|
2190
|
+
assert_redirected_to "/session/new"
|
|
2191
|
+
delete "/account", params: { password: "password" }
|
|
2192
|
+
assert_redirected_to "/session/new"
|
|
2193
|
+
assert_equal "one@example.com", User.find(ann.id).email_address
|
|
2194
|
+
assert_equal 2, User.all.count
|
|
2195
|
+
# Signed in, the browser comes back to the page it asked for, which
|
|
2196
|
+
# the nav links to.
|
|
2197
|
+
sign_in_as ann
|
|
2198
|
+
assert_redirected_to "/account/edit"
|
|
2199
|
+
get "/account/edit"
|
|
2200
|
+
assert_response :ok
|
|
2201
|
+
assert_includes response.body, "<h1>Account</h1>"
|
|
2202
|
+
assert_includes response.body, "<a href=\"/account/edit\">Account</a>"
|
|
2203
|
+
assert_includes response.body, "<form action=\"/account\" method=\"post\"><input type=\"hidden\" name=\"_method\" value=\"patch\">"
|
|
2204
|
+
assert_includes response.body, "<input type=\"email\" name=\"user[email_address]\" id=\"user_email_address\" value=\"one@example.com\" autocomplete=\"username\">"
|
|
2205
|
+
assert_includes response.body, "<label for=\"user_current_password\">Current password</label>"
|
|
2206
|
+
assert_includes response.body, "<form action=\"/account\" method=\"post\"><input type=\"hidden\" name=\"_method\" value=\"delete\">"
|
|
2207
|
+
end
|
|
2208
|
+
|
|
2209
|
+
test "PATCH /account past ten requests is refused for a while" do
|
|
2210
|
+
ann = users(:one)
|
|
2211
|
+
sign_in_as ann
|
|
2212
|
+
Cybertrain::RateLimit.reset_all!
|
|
2213
|
+
count = 0
|
|
2214
|
+
while count < 10
|
|
2215
|
+
patch "/account", params: { user: { current_password: "wrong-password" } }
|
|
2216
|
+
assert_response :unprocessable_entity
|
|
2217
|
+
count += 1
|
|
2218
|
+
end
|
|
2219
|
+
# Wrong passwords here do not count toward the lockout, which guards
|
|
2220
|
+
# signing in (a stolen session could lock the owner out).
|
|
2221
|
+
assert_equal 0, User.find(ann.id).failed_attempts
|
|
2222
|
+
assert_nil User.find(ann.id).locked_until
|
|
2223
|
+
# The right password now gets the limit's answer and changes nothing;
|
|
2224
|
+
# a deletion counts in the same window.
|
|
2225
|
+
patch "/account", params: { user: { current_password: "password", email_address: "one@new.example" } }
|
|
2226
|
+
assert_redirected_to "/account/edit"
|
|
2227
|
+
follow_redirect!
|
|
2228
|
+
assert_includes response.body, "<p class=\"alert\">Try again later.</p>"
|
|
2229
|
+
assert_equal "one@example.com", User.find(ann.id).email_address
|
|
2230
|
+
delete "/account", params: { password: "password" }
|
|
2231
|
+
assert_redirected_to "/account/edit"
|
|
2232
|
+
refute User.find_by(id: ann.id).nil?
|
|
2233
|
+
# Once the window is over (its counts forgotten), the same change passes.
|
|
2234
|
+
Cybertrain::RateLimit.reset_all!
|
|
2235
|
+
patch "/account", params: { user: { current_password: "password", email_address: "one@new.example" } }
|
|
2236
|
+
assert_redirected_to "/account/edit"
|
|
2237
|
+
confirm_new_address(ann)
|
|
2238
|
+
assert_equal "one@new.example", User.find(ann.id).email_address
|
|
2239
|
+
Cybertrain::RateLimit.reset_all!
|
|
2240
|
+
end
|
|
2241
|
+
|
|
2242
|
+
# The browsers here are Cybertrain::Test::Clients, the client under an
|
|
2243
|
+
# integration test: each request of ann's comes from another address,
|
|
2244
|
+
# which an integration test cannot set. Their responses are checked by
|
|
2245
|
+
# status and Location.
|
|
2246
|
+
test "wrong current passwords are limited per user too, from any address" do
|
|
2247
|
+
ann = users(:one)
|
|
2248
|
+
browser = Cybertrain::Test::Client.new(Cybertrain::Test.app)
|
|
2249
|
+
browser.post("/session", { "email_address" => "one@example.com", "password" => "password" })
|
|
2250
|
+
bobs = Cybertrain::Test::Client.new(Cybertrain::Test.app)
|
|
2251
|
+
bobs.post("/session", { "email_address" => "two@example.com", "password" => "password" })
|
|
2252
|
+
Cybertrain::RateLimit.reset_all!
|
|
2253
|
+
# Ten wrong guesses, each from another address: the per-address limit
|
|
2254
|
+
# never fills.
|
|
2255
|
+
count = 0
|
|
2256
|
+
while count < 10
|
|
2257
|
+
browser.peer_address = "203.0.113.#{count + 1}"
|
|
2258
|
+
res = browser.patch("/account", { "user[current_password]" => "wrong-password" })
|
|
2259
|
+
assert_equal 422, res.status
|
|
2260
|
+
count += 1
|
|
2261
|
+
end
|
|
2262
|
+
# The eleventh, from yet another address and with the right password:
|
|
2263
|
+
# the per-user limit answers, and nothing changes.
|
|
2264
|
+
browser.peer_address = "203.0.113.99"
|
|
2265
|
+
res = browser.patch("/account", { "user[current_password]" => "password", "user[email_address]" => "one@new.example" })
|
|
2266
|
+
assert_equal 303, res.status
|
|
2267
|
+
assert_equal "/account/edit", res.header("Location")
|
|
2268
|
+
assert_includes browser.follow_redirect!.body, "<p class=\"alert\">Try again later.</p>"
|
|
2269
|
+
assert_equal "one@example.com", User.find(ann.id).email_address
|
|
2270
|
+
# Another user, from that same address, is not held back by ann's count.
|
|
2271
|
+
bobs.peer_address = "203.0.113.99"
|
|
2272
|
+
res = bobs.patch("/account", { "user[current_password]" => "password",
|
|
2273
|
+
"user[password]" => NEW_PASSWORD, "user[password_confirmation]" => NEW_PASSWORD })
|
|
2274
|
+
assert_equal 303, res.status
|
|
2275
|
+
assert_equal "/account/edit", res.header("Location")
|
|
2276
|
+
assert_includes bobs.follow_redirect!.body, UPDATED
|
|
2277
|
+
Cybertrain::RateLimit.reset_all!
|
|
2278
|
+
end
|
|
2279
|
+
|
|
2280
|
+
# users(:two) is not an admin, has no failed attempt and no lock.
|
|
2281
|
+
test "the form's fields are the only ones a change takes" do
|
|
2282
|
+
two = users(:two)
|
|
2283
|
+
sign_in_as two
|
|
2284
|
+
# Fields the form does not have, sent with the right password: ignored.
|
|
2285
|
+
update_account("password", { password: NEW_PASSWORD, password_confirmation: NEW_PASSWORD,
|
|
2286
|
+
admin: "1", failed_attempts: "7", locked_until: "2099-01-01 00:00:00" })
|
|
2287
|
+
assert_redirected_to "/account/edit"
|
|
2288
|
+
changed = User.find(two.id)
|
|
2289
|
+
assert changed.authenticate(NEW_PASSWORD)
|
|
2290
|
+
assert_equal false, changed.admin?
|
|
2291
|
+
assert_equal 0, changed.failed_attempts
|
|
2292
|
+
assert_nil changed.locked_until
|
|
2293
|
+
end
|
|
2294
|
+
|
|
2295
|
+
test "PATCH and DELETE /account without the form's token are refused" do
|
|
2296
|
+
ann = users(:one)
|
|
2297
|
+
sign_in_as ann
|
|
2298
|
+
with_forgery_protection do
|
|
2299
|
+
patch "/account", params: { user: { current_password: "password", email_address: "eve@example.com" } }
|
|
2300
|
+
assert_response :forbidden
|
|
2301
|
+
delete "/account", params: { password: "password" }
|
|
2302
|
+
assert_response :forbidden
|
|
2303
|
+
# Nor with another browser's token.
|
|
2304
|
+
other = open_session
|
|
2305
|
+
other.get "/session/new"
|
|
2306
|
+
foreign = authenticity_token_in(other.response.body)
|
|
2307
|
+
patch "/account", params: { authenticity_token: foreign, user: { current_password: "password", email_address: "eve@example.com" } }
|
|
2308
|
+
assert_response :forbidden
|
|
2309
|
+
delete "/account", params: { authenticity_token: foreign, password: "password" }
|
|
2310
|
+
assert_response :forbidden
|
|
2311
|
+
end
|
|
2312
|
+
assert_equal "one@example.com", User.find(ann.id).email_address
|
|
2313
|
+
refute User.find_by(id: ann.id).nil?
|
|
2314
|
+
assert signed_in?
|
|
2315
|
+
end
|
|
2316
|
+
|
|
2317
|
+
test "a new hash of the password stays with this browser when the change fails" do
|
|
2318
|
+
ann = users(:one)
|
|
2319
|
+
sign_in_as ann
|
|
2320
|
+
digest = ann.password_digest
|
|
2321
|
+
begin
|
|
2322
|
+
# The cost raised since the fixture's digest was made: checking
|
|
2323
|
+
# the password saves a new hash of it (User#authenticate).
|
|
2324
|
+
Cybertrain::Password.iterations = Cybertrain::Password::TEST_ITERATIONS + 1
|
|
2325
|
+
update_account("password", { email_address: "two@example.com" })
|
|
2326
|
+
assert_response :unprocessable_entity
|
|
2327
|
+
assert_includes response.body, "<li>Email address has already been taken</li>"
|
|
2328
|
+
rehashed = User.find(ann.id).password_digest
|
|
2329
|
+
refute rehashed == digest
|
|
2330
|
+
assert Cybertrain::Password.verify?("password", rehashed)
|
|
2331
|
+
# This browser is still signed in on its row.
|
|
2332
|
+
assert signed_in?
|
|
2333
|
+
assert_equal 1, Session.where(user_id: ann.id).count
|
|
2334
|
+
ensure
|
|
2335
|
+
Cybertrain::Password.iterations = Cybertrain::Password::TEST_ITERATIONS
|
|
2336
|
+
end
|
|
2337
|
+
end
|
|
2338
|
+
|
|
2339
|
+
test "a deletion the database refuses changes nothing and says so" do
|
|
2340
|
+
ann = account_holder
|
|
2341
|
+
sign_in_as ann
|
|
2342
|
+
phone = open_session
|
|
2343
|
+
phone.sign_in_as ann
|
|
2344
|
+
add_note_for(ann)
|
|
2345
|
+
log = StringIO.new
|
|
2346
|
+
begin
|
|
2347
|
+
with_config("test", NO_HOSTS, "", log) do
|
|
2348
|
+
delete_account("password")
|
|
2349
|
+
assert_response :unprocessable_entity
|
|
2350
|
+
assert_includes response.body, NOT_DELETED
|
|
2351
|
+
end
|
|
2352
|
+
# The user and both sessions are kept (one transaction), and the
|
|
2353
|
+
# log says why.
|
|
2354
|
+
refute User.find_by(id: ann.id).nil?
|
|
2355
|
+
assert_equal 2, Session.where(user_id: ann.id).count
|
|
2356
|
+
assert signed_in?("ann@example.com")
|
|
2357
|
+
assert browser_signed_in?(phone, "ann@example.com")
|
|
2358
|
+
assert_includes log.string, "[ERROR] account not deleted: FOREIGN KEY constraint failed"
|
|
2359
|
+
ensure
|
|
2360
|
+
drop_notes
|
|
2361
|
+
end
|
|
2362
|
+
# Once nothing refers to the user, the same deletion passes.
|
|
2363
|
+
delete_account("password")
|
|
2364
|
+
assert_redirected_to "/"
|
|
2365
|
+
assert_nil User.find_by(id: ann.id)
|
|
2366
|
+
assert_equal 0, Session.where(user_id: ann.id).count
|
|
2367
|
+
end
|
|
2368
|
+
|
|
2369
|
+
private
|
|
2370
|
+
|
|
2371
|
+
# Sends the account form as a browser would: the current password and
|
|
2372
|
+
# the fields given, each as user[name]. The rate limit's counts are
|
|
2373
|
+
# forgotten first.
|
|
2374
|
+
def update_account(current_password, fields)
|
|
2375
|
+
Cybertrain::RateLimit.reset_all!
|
|
2376
|
+
form = { current_password: current_password }
|
|
2377
|
+
fields.each { |name, value| form[name] = value }
|
|
2378
|
+
patch "/account", params: { user: form }
|
|
2379
|
+
nil
|
|
2380
|
+
end
|
|
2381
|
+
|
|
2382
|
+
# Sends the delete form with this password.
|
|
2383
|
+
def delete_account(password)
|
|
2384
|
+
Cybertrain::RateLimit.reset_all!
|
|
2385
|
+
delete "/account", params: { password: password }
|
|
2386
|
+
nil
|
|
2387
|
+
end
|
|
2388
|
+
|
|
2389
|
+
# Whether this browser is signed in as email_address: the nav names them.
|
|
2390
|
+
def signed_in?(email_address = "one@example.com")
|
|
2391
|
+
get "/session/new"
|
|
2392
|
+
response.body.include?("<span>#{email_address}</span>")
|
|
2393
|
+
end
|
|
2394
|
+
|
|
2395
|
+
# The Set-Cookie line of the session cookie in res ("" when none).
|
|
2396
|
+
def session_cookie_of(res)
|
|
2397
|
+
prefix = "#{Cybertrain.config.session_cookie_name}="
|
|
2398
|
+
found = ""
|
|
2399
|
+
res.cookies.each { |line| found = line if line.start_with?(prefix) }
|
|
2400
|
+
found
|
|
2401
|
+
end
|
|
2402
|
+
|
|
2403
|
+
# A table of the app's own whose rows refer to users (as a migration's
|
|
2404
|
+
# `t.references :user` makes one), with a row for user: the database
|
|
2405
|
+
# then refuses to delete that user.
|
|
2406
|
+
def add_note_for(user)
|
|
2407
|
+
Cybertrain::DB.with do |c|
|
|
2408
|
+
c.execute("CREATE TABLE account_notes (id INTEGER PRIMARY KEY, user_id INTEGER NOT NULL REFERENCES users (id))")
|
|
2409
|
+
c.execute("INSERT INTO account_notes (user_id) VALUES (#{user.id})")
|
|
2410
|
+
end
|
|
2411
|
+
nil
|
|
2412
|
+
end
|
|
2413
|
+
|
|
2414
|
+
def drop_notes
|
|
2415
|
+
Cybertrain::DB.with { |c| c.execute("DROP TABLE IF EXISTS account_notes") }
|
|
2416
|
+
nil
|
|
2417
|
+
end
|
|
2418
|
+
|
|
2419
|
+
@HELPERS@
|
|
2420
|
+
end
|
|
2421
|
+
RUBY
|
|
2422
|
+
end
|
|
2423
|
+
|
|
2424
|
+
# What a new address does on the account page, in
|
|
2425
|
+
# accounts_controller_test.rb: it takes effect at once.
|
|
2426
|
+
def self.accounts_test_address_helpers
|
|
2427
|
+
<<~'RUBY'
|
|
2428
|
+
# What the page says after a new address, which takes effect at once.
|
|
2429
|
+
ADDRESS_CHANGED = UPDATED
|
|
2430
|
+
|
|
2431
|
+
# A user of the test's own, ann@example.com, password "password",
|
|
2432
|
+
# for the tests that delete the account.
|
|
2433
|
+
def account_holder
|
|
2434
|
+
user = User.new(email_address: "ann@example.com")
|
|
2435
|
+
user.password = "password"
|
|
2436
|
+
user.save!
|
|
2437
|
+
user
|
|
2438
|
+
end
|
|
2439
|
+
|
|
2440
|
+
# Nothing more to do for a new address to take effect (with
|
|
2441
|
+
# --confirmable, its link is opened here).
|
|
2442
|
+
def confirm_new_address(user)
|
|
2443
|
+
nil
|
|
2444
|
+
end
|
|
2445
|
+
RUBY
|
|
2446
|
+
end
|
|
2447
|
+
|
|
2448
|
+
# --confirmable's: it takes effect once the link mailed to it is opened.
|
|
2449
|
+
def self.accounts_test_confirm_helpers
|
|
2450
|
+
<<~'RUBY'
|
|
2451
|
+
# What the page says after a new address (--confirmable): it takes
|
|
2452
|
+
# effect once the link mailed to it is opened.
|
|
2453
|
+
ADDRESS_CHANGED = "<p class=\"notice\">A link to confirm the new address was sent to it. The address changes once the link is opened.</p>"
|
|
2454
|
+
|
|
2455
|
+
# A user of the test's own, ann@example.com, password "password",
|
|
2456
|
+
# confirmed so it can sign in, for the tests that delete the account.
|
|
2457
|
+
def account_holder
|
|
2458
|
+
user = User.new(email_address: "ann@example.com")
|
|
2459
|
+
user.password = "password"
|
|
2460
|
+
user.confirmed_at = Time.current
|
|
2461
|
+
user.save!
|
|
2462
|
+
user
|
|
2463
|
+
end
|
|
2464
|
+
|
|
2465
|
+
# Opens the link of the new address user is waiting for (from a browser
|
|
2466
|
+
# that is not signed in, as a mail reader's would be), so it takes the
|
|
2467
|
+
# old one's place. confirmations_controller_test.rb follows the mailed
|
|
2468
|
+
# link itself.
|
|
2469
|
+
def confirm_new_address(user)
|
|
2470
|
+
token = User.find(user.id).confirmation_token
|
|
2471
|
+
reader = open_session
|
|
2472
|
+
reader.get "/confirmations/#{token}"
|
|
2473
|
+
reader.assert_redirected_to "/session/new"
|
|
2474
|
+
nil
|
|
2475
|
+
end
|
|
2476
|
+
RUBY
|
|
2477
|
+
end
|
|
2478
|
+
|
|
2479
|
+
# test/controllers/confirmations_controller_test.rb (--confirmable):
|
|
2480
|
+
# the mail, sign-in refused until the address is confirmed, the link,
|
|
2481
|
+
# a new address, the send-again form, and the users that /setup and
|
|
2482
|
+
# the user command make. Its first test is the mode's: sign-up's mail,
|
|
2483
|
+
# or (--invite-only) /setup's confirmed first user.
|
|
2484
|
+
def self.confirmations_controller_test(app, invite_only = false)
|
|
2485
|
+
text = confirmations_controller_test_body
|
|
2486
|
+
text = splice(text, "@FIRST_USER_TEST@", margin(invite_only ? confirmations_setup_test : confirmations_sign_up_test, 2))
|
|
2487
|
+
text = splice(text, "@HELPERS@", with_config_helper)
|
|
2488
|
+
text = text.gsub(BASE_URL_ENV_MARK, base_url_env(app))
|
|
2489
|
+
text.gsub("@APP@", app)
|
|
2490
|
+
end
|
|
2491
|
+
|
|
2492
|
+
def self.confirmations_controller_test_body
|
|
2493
|
+
<<~'RUBY'
|
|
2494
|
+
# Confirming the email address (cybertrain generate authentication
|
|
2495
|
+
# --confirmable): the mail with the link (kept in
|
|
2496
|
+
# Cybertrain::Mailer.deliveries), sign-in refused until the link is
|
|
2497
|
+
# opened, the link itself (three days, once, signing nobody in), a new
|
|
2498
|
+
# address that takes effect only once its own link is opened, and the
|
|
2499
|
+
# form that mails the link again. The fixtures' users are confirmed;
|
|
2500
|
+
# unconfirmed_user makes one who is not.
|
|
2501
|
+
require "stringio"
|
|
2502
|
+
require "test_helper"
|
|
2503
|
+
require_relative "../../app/commands/user_command"
|
|
2504
|
+
|
|
2505
|
+
# start_new_session_for is private (the Authentication concern's): a
|
|
2506
|
+
# controller made for these tests calls it, as any action could.
|
|
2507
|
+
class SessionStartProbeController < ApplicationController
|
|
2508
|
+
def probe_start(user)
|
|
2509
|
+
start_new_session_for(user)
|
|
2510
|
+
end
|
|
2511
|
+
end
|
|
2512
|
+
|
|
2513
|
+
class ConfirmationsControllerTest < ActionDispatch::IntegrationTest
|
|
2514
|
+
WRONG = "<p class=\"alert\">Try another email address or password.</p>"
|
|
2515
|
+
UNCONFIRMED = "<p class=\"alert\">Confirm your email address first: open the link in the mail we sent you.</p>"
|
|
2516
|
+
SEND_AGAIN_LINK = "<a href=\"/confirmations/new\">Send the confirmation link again</a>"
|
|
2517
|
+
CONFIRMED = "<p class=\"notice\">Your email address has been confirmed.</p>"
|
|
2518
|
+
INVALID = "<p class=\"alert\">That confirmation link is invalid or has expired.</p>"
|
|
2519
|
+
TAKEN = "<p class=\"alert\">That email address belongs to another account now, so it was not changed.</p>"
|
|
2520
|
+
SENT = "<p class=\"notice\">If that address is registered and not confirmed yet, a link to confirm it was sent to it.</p>"
|
|
2521
|
+
ADDRESS_CHANGED = "<p class=\"notice\">A link to confirm the new address was sent to it. The address changes once the link is opened.</p>"
|
|
2522
|
+
UPDATED = "<p class=\"notice\">Your account has been updated.</p>"
|
|
2523
|
+
CANCELLED = "<p class=\"notice\">The change of address was cancelled; its link no longer works.</p>"
|
|
2524
|
+
CANCEL_BUTTON = "<form class=\"button_to\" method=\"post\" action=\"/account/pending_email\">"
|
|
2525
|
+
# with_config's `hosts` for an app that lists none (CYBERTRAIN_HOSTS unset).
|
|
2526
|
+
NO_HOSTS = Array.new(0) { "" }
|
|
2527
|
+
|
|
2528
|
+
@FIRST_USER_TEST@
|
|
2529
|
+
|
|
2530
|
+
test "./@APP@ user makes a confirmed user, and mails nothing" do
|
|
2531
|
+
assert_equal 0, UserCommand.run(["Dee@Example.com", "s3cr3t-s3cr3t"])
|
|
2532
|
+
dee = User.find_by(email_address: "dee@example.com")
|
|
2533
|
+
refute dee.nil?
|
|
2534
|
+
assert_equal true, dee.confirmed?
|
|
2535
|
+
assert_no_enqueued_jobs "cybertrain mail"
|
|
2536
|
+
post "/session", params: { email_address: "dee@example.com", password: "s3cr3t-s3cr3t" }
|
|
2537
|
+
assert_redirected_to "/"
|
|
2538
|
+
# A new password for a user who exists leaves the confirmation as it was.
|
|
2539
|
+
ann = unconfirmed_user
|
|
2540
|
+
assert_equal 0, UserCommand.run(["ann@example.com", "an0ther-s3cr3t"])
|
|
2541
|
+
assert_equal false, User.find(ann.id).confirmed?
|
|
2542
|
+
post "/session", params: { email_address: "ann@example.com", password: "an0ther-s3cr3t" }
|
|
2543
|
+
assert_redirected_to "/session/new"
|
|
2544
|
+
follow_redirect!
|
|
2545
|
+
assert_includes response.body, UNCONFIRMED
|
|
2546
|
+
end
|
|
2547
|
+
|
|
2548
|
+
test "sign-in is refused until confirmed, with the send-again link" do
|
|
2549
|
+
ann = unconfirmed_user
|
|
2550
|
+
# A wrong password, or an address no user has: the usual message,
|
|
2551
|
+
# which says nothing of the address.
|
|
2552
|
+
post "/session", params: { email_address: "ann@example.com", password: "wrong-password" }
|
|
2553
|
+
assert_redirected_to "/session/new"
|
|
2554
|
+
follow_redirect!
|
|
2555
|
+
assert_includes response.body, WRONG
|
|
2556
|
+
refute response.body.include?(UNCONFIRMED)
|
|
2557
|
+
post "/session", params: { email_address: "nobody@example.com", password: "password" }
|
|
2558
|
+
assert_redirected_to "/session/new"
|
|
2559
|
+
follow_redirect!
|
|
2560
|
+
assert_includes response.body, WRONG
|
|
2561
|
+
refute response.body.include?(UNCONFIRMED)
|
|
2562
|
+
# The right password: refused all the same, with the reason, and the
|
|
2563
|
+
# sign-in page links to the form that mails the link again.
|
|
2564
|
+
sign_in_as ann
|
|
2565
|
+
assert_redirected_to "/session/new"
|
|
2566
|
+
follow_redirect!
|
|
2567
|
+
assert_includes response.body, UNCONFIRMED
|
|
2568
|
+
refute response.body.include?(WRONG)
|
|
2569
|
+
assert_includes response.body, SEND_AGAIN_LINK
|
|
2570
|
+
assert_equal 0, Session.count
|
|
2571
|
+
refute signed_in?("ann@example.com")
|
|
2572
|
+
# As any right password does, it cleared the wrong one's count.
|
|
2573
|
+
assert_equal 0, User.find(ann.id).failed_attempts
|
|
2574
|
+
# Confirmed, the same password signs in.
|
|
2575
|
+
get "/confirmations/#{ann.confirmation_token}"
|
|
2576
|
+
assert_redirected_to "/session/new"
|
|
2577
|
+
sign_in_as ann
|
|
2578
|
+
assert_redirected_to "/"
|
|
2579
|
+
assert_equal 1, Session.count
|
|
2580
|
+
assert signed_in?("ann@example.com")
|
|
2581
|
+
end
|
|
2582
|
+
|
|
2583
|
+
test "the concern itself starts no session for an unconfirmed account" do
|
|
2584
|
+
ann = unconfirmed_user
|
|
2585
|
+
ctx = Cybertrain::Context.new(Cybertrain::Request.new("POST", "/session", {}, ""))
|
|
2586
|
+
ctx.session = Cybertrain::Session.new
|
|
2587
|
+
SessionStartProbeController.new(ctx).probe_start(ann)
|
|
2588
|
+
assert_equal 0, Session.count
|
|
2589
|
+
assert_nil ctx.session[:session_id]
|
|
2590
|
+
assert_nil ctx.session[:password_key]
|
|
2591
|
+
# Confirmed, the same call signs in.
|
|
2592
|
+
ann.update_columns(confirmed_at: Time.current)
|
|
2593
|
+
SessionStartProbeController.new(ctx).probe_start(User.find(ann.id))
|
|
2594
|
+
assert_equal 1, Session.count
|
|
2595
|
+
assert_equal Session.first.id, ctx.session[:session_id].to_i
|
|
2596
|
+
end
|
|
2597
|
+
|
|
2598
|
+
test "the link confirms, and the user can then sign in" do
|
|
2599
|
+
ann = unconfirmed_user
|
|
2600
|
+
token = ann.confirmation_token
|
|
2601
|
+
before = Time.current.to_i
|
|
2602
|
+
get "/confirmations/#{token}"
|
|
2603
|
+
after = Time.current.to_i
|
|
2604
|
+
assert_redirected_to "/session/new"
|
|
2605
|
+
# The page's address holds the token: no Referer leaves it.
|
|
2606
|
+
assert_equal "no-referrer", response.header("Referrer-Policy")
|
|
2607
|
+
follow_redirect!
|
|
2608
|
+
page = response.body
|
|
2609
|
+
assert_includes page, CONFIRMED
|
|
2610
|
+
# A link opened from a mail signs nobody in.
|
|
2611
|
+
refute page.include?("<span>ann@example.com</span>")
|
|
2612
|
+
assert_equal 0, Session.count
|
|
2613
|
+
confirmed = User.find(ann.id)
|
|
2614
|
+
assert confirmed.confirmed?
|
|
2615
|
+
assert confirmed.confirmed_at.to_i >= before
|
|
2616
|
+
assert confirmed.confirmed_at.to_i <= after
|
|
2617
|
+
assert_equal "ann@example.com", confirmed.email_address
|
|
2618
|
+
# Used once, the link confirms nothing more.
|
|
2619
|
+
assert_nil User.find_by_confirmation_token(token)
|
|
2620
|
+
get "/confirmations/#{token}"
|
|
2621
|
+
assert_redirected_to "/confirmations/new"
|
|
2622
|
+
assert_equal "no-referrer", response.header("Referrer-Policy")
|
|
2623
|
+
follow_redirect!
|
|
2624
|
+
assert_includes response.body, INVALID
|
|
2625
|
+
assert_equal confirmed.confirmed_at.to_i, User.find(ann.id).confirmed_at.to_i
|
|
2626
|
+
# The user then signs in, with the password.
|
|
2627
|
+
sign_in_as ann
|
|
2628
|
+
assert_redirected_to "/"
|
|
2629
|
+
assert signed_in?("ann@example.com")
|
|
2630
|
+
end
|
|
2631
|
+
|
|
2632
|
+
test "the link lasts three days and belongs to its user and address" do
|
|
2633
|
+
ann = unconfirmed_user
|
|
2634
|
+
bob = unconfirmed_user("bob@example.com")
|
|
2635
|
+
assert_equal 259_200, User::CONFIRMATION_TOKEN_TTL
|
|
2636
|
+
now = Time.current.to_i
|
|
2637
|
+
assert ann.confirmation_token(now).start_with?("#{ann.id}--#{now + 259_200}--")
|
|
2638
|
+
assert_equal ":ann@example.com:0", ann.confirmation_binding
|
|
2639
|
+
stale = ann.confirmation_token(now - 259_200 - 60)
|
|
2640
|
+
fresh = ann.confirmation_token(now - 259_200 + 60)
|
|
2641
|
+
assert_nil User.find_by_confirmation_token(stale)
|
|
2642
|
+
assert_equal ann.id, User.find_by_confirmation_token(fresh).id
|
|
2643
|
+
get "/confirmations/#{stale}"
|
|
2644
|
+
assert_redirected_to "/confirmations/new"
|
|
2645
|
+
assert_equal false, User.find(ann.id).confirmed?
|
|
2646
|
+
# Another user's id with ann's binding, another purpose, or nothing
|
|
2647
|
+
# at all: no one's.
|
|
2648
|
+
assert_nil User.find_by_confirmation_token(Cybertrain::Token.issue("confirmation", bob.id, 900, ann.confirmation_binding))
|
|
2649
|
+
assert_nil User.find_by_confirmation_token(ann.password_reset_token)
|
|
2650
|
+
assert_nil User.find_by_password_reset_token(ann.confirmation_token)
|
|
2651
|
+
assert_nil User.find_by_confirmation_token("")
|
|
2652
|
+
get "/confirmations/not-a-token"
|
|
2653
|
+
assert_redirected_to "/confirmations/new"
|
|
2654
|
+
assert_equal bob.id, User.find_by_confirmation_token(bob.confirmation_token).id
|
|
2655
|
+
assert_equal false, User.find(bob.id).confirmed?
|
|
2656
|
+
end
|
|
2657
|
+
|
|
2658
|
+
test "an email change takes effect only when the new address confirms" do
|
|
2659
|
+
ann = users(:one)
|
|
2660
|
+
sign_in_as ann
|
|
2661
|
+
reset_token = ann.password_reset_token
|
|
2662
|
+
change_address(" One@New.Example ")
|
|
2663
|
+
assert_redirected_to "/account/edit"
|
|
2664
|
+
follow_redirect!
|
|
2665
|
+
page = response.body
|
|
2666
|
+
assert_includes page, ADDRESS_CHANGED
|
|
2667
|
+
# Not yet: the old address is in use, the new one waits.
|
|
2668
|
+
assert_includes page, "<span>one@example.com</span>"
|
|
2669
|
+
assert_includes page, "<p class=\"hint\">Waiting for confirmation: one@new.example</p>"
|
|
2670
|
+
waiting = User.find(ann.id)
|
|
2671
|
+
assert_equal "one@example.com", waiting.email_address
|
|
2672
|
+
assert_equal "one@new.example", waiting.unconfirmed_email
|
|
2673
|
+
other = open_session
|
|
2674
|
+
other.post "/session", params: { email_address: "one@new.example", password: "password" }
|
|
2675
|
+
other.assert_redirected_to "/session/new"
|
|
2676
|
+
other.post "/session", params: { email_address: "one@example.com", password: "password" }
|
|
2677
|
+
other.assert_redirected_to "/"
|
|
2678
|
+
refute User.find_by_password_reset_token(reset_token).nil?
|
|
2679
|
+
# A taken address is refused when it is asked for; the one waiting stays.
|
|
2680
|
+
change_address("two@example.com")
|
|
2681
|
+
assert_response :unprocessable_entity
|
|
2682
|
+
assert_includes response.body, "<li>Email address has already been taken</li>"
|
|
2683
|
+
assert_includes response.body, "<p class=\"hint\">Waiting for confirmation: one@new.example</p>"
|
|
2684
|
+
assert_equal "one@new.example", User.find(ann.id).unconfirmed_email
|
|
2685
|
+
# The mail goes to the new address.
|
|
2686
|
+
assert_enqueued_jobs 1, "cybertrain mail"
|
|
2687
|
+
assert_equal 1, perform_enqueued_jobs
|
|
2688
|
+
mail = Cybertrain::Mailer.deliveries.last
|
|
2689
|
+
refute mail.nil?
|
|
2690
|
+
assert_equal "one@new.example", mail.mail_to
|
|
2691
|
+
assert_includes mail.mail_body, "Hello one@new.example,\n"
|
|
2692
|
+
link = last_mail_link
|
|
2693
|
+
assert link.start_with?("http://www.example.com/confirmations/"), link
|
|
2694
|
+
# Opened in the browser that asked: back to the account page, with
|
|
2695
|
+
# the new address in use.
|
|
2696
|
+
path = "/confirmations/#{token_in(link)}"
|
|
2697
|
+
get path
|
|
2698
|
+
assert_redirected_to "/account/edit"
|
|
2699
|
+
follow_redirect!
|
|
2700
|
+
account_page = response.body
|
|
2701
|
+
assert_includes account_page, CONFIRMED
|
|
2702
|
+
assert_includes account_page, "<span>one@new.example</span>"
|
|
2703
|
+
refute account_page.include?("Waiting for confirmation")
|
|
2704
|
+
# The link is used up.
|
|
2705
|
+
get path
|
|
2706
|
+
assert_redirected_to "/confirmations/new"
|
|
2707
|
+
changed = User.find(ann.id)
|
|
2708
|
+
assert_equal "one@new.example", changed.email_address
|
|
2709
|
+
assert_nil changed.unconfirmed_email
|
|
2710
|
+
assert changed.confirmed?
|
|
2711
|
+
# Both sessions go on; the old address signs in no more, the new one does.
|
|
2712
|
+
assert_equal 2, Session.where(user_id: ann.id).count
|
|
2713
|
+
other.post "/session", params: { email_address: "one@example.com", password: "password" }
|
|
2714
|
+
other.assert_redirected_to "/session/new"
|
|
2715
|
+
other.post "/session", params: { email_address: "one@new.example", password: "password" }
|
|
2716
|
+
other.assert_redirected_to "/"
|
|
2717
|
+
# A password reset link sent to the old address is spent.
|
|
2718
|
+
assert_nil User.find_by_password_reset_token(reset_token)
|
|
2719
|
+
end
|
|
2720
|
+
|
|
2721
|
+
test "a new address someone takes before its link is opened is not moved in" do
|
|
2722
|
+
ann = users(:one)
|
|
2723
|
+
sign_in_as ann
|
|
2724
|
+
change_address("carol@example.com")
|
|
2725
|
+
assert_redirected_to "/account/edit"
|
|
2726
|
+
token = User.find(ann.id).confirmation_token
|
|
2727
|
+
# Meanwhile carol signs up with it: an address that only waits is free.
|
|
2728
|
+
carol = unconfirmed_user("carol@example.com")
|
|
2729
|
+
# A pending address someone has taken since blocks nothing else of
|
|
2730
|
+
# ann's account: the user command and the rehash a sign-in makes after
|
|
2731
|
+
# the cost was raised save, and leave it waiting (a password reset
|
|
2732
|
+
# saves too, below, and withdraws it).
|
|
2733
|
+
assert_equal 0, UserCommand.run(["one@example.com", "an0ther-s3cr3t"])
|
|
2734
|
+
assert User.find(ann.id).authenticate("an0ther-s3cr3t")
|
|
2735
|
+
assert_equal 0, UserCommand.run(["one@example.com", "password"])
|
|
2736
|
+
assert User.find(ann.id).authenticate("password")
|
|
2737
|
+
assert_equal "carol@example.com", User.find(ann.id).unconfirmed_email
|
|
2738
|
+
digest = User.find(ann.id).password_digest
|
|
2739
|
+
begin
|
|
2740
|
+
Cybertrain::Password.iterations = Cybertrain::Password::TEST_ITERATIONS + 1
|
|
2741
|
+
sign_in_as ann
|
|
2742
|
+
assert_redirected_to "/"
|
|
2743
|
+
refute User.find(ann.id).password_digest == digest
|
|
2744
|
+
refute Cybertrain::Password.needs_rehash?(User.find(ann.id).password_digest)
|
|
2745
|
+
ensure
|
|
2746
|
+
Cybertrain::Password.iterations = Cybertrain::Password::TEST_ITERATIONS
|
|
2747
|
+
end
|
|
2748
|
+
# The link: the address is not moved in, and the one waiting is
|
|
2749
|
+
# forgotten (it can never be confirmed now).
|
|
2750
|
+
get "/confirmations/#{token}"
|
|
2751
|
+
assert_redirected_to "/account/edit"
|
|
2752
|
+
follow_redirect!
|
|
2753
|
+
assert_includes response.body, TAKEN
|
|
2754
|
+
refute response.body.include?("Waiting for confirmation")
|
|
2755
|
+
assert_equal "one@example.com", User.find(ann.id).email_address
|
|
2756
|
+
assert_nil User.find(ann.id).unconfirmed_email
|
|
2757
|
+
assert_equal "carol@example.com", User.find(carol.id).email_address
|
|
2758
|
+
# Another new address ends the link to the one before.
|
|
2759
|
+
change_address("one@other.example")
|
|
2760
|
+
assert_redirected_to "/account/edit"
|
|
2761
|
+
other_token = User.find(ann.id).confirmation_token
|
|
2762
|
+
change_address("one@third.example")
|
|
2763
|
+
assert_redirected_to "/account/edit"
|
|
2764
|
+
assert_nil User.find_by_confirmation_token(other_token)
|
|
2765
|
+
assert_equal "one@third.example", User.find(ann.id).unconfirmed_email
|
|
2766
|
+
# A password reset saves with a taken address waiting, and withdraws it.
|
|
2767
|
+
change_address("zed@example.com")
|
|
2768
|
+
assert_redirected_to "/account/edit"
|
|
2769
|
+
unconfirmed_user("zed@example.com")
|
|
2770
|
+
stranger = open_session
|
|
2771
|
+
stranger.put "/passwords/#{User.find(ann.id).password_reset_token}", params: { password: "an0ther-s3cr3t", password_confirmation: "an0ther-s3cr3t" }
|
|
2772
|
+
stranger.assert_redirected_to "/session/new"
|
|
2773
|
+
assert User.find(ann.id).authenticate("an0ther-s3cr3t")
|
|
2774
|
+
assert_nil User.find(ann.id).unconfirmed_email
|
|
2775
|
+
end
|
|
2776
|
+
|
|
2777
|
+
test "a password-only change keeps the new address waiting" do
|
|
2778
|
+
ann = users(:one)
|
|
2779
|
+
sign_in_as ann
|
|
2780
|
+
change_address("one@new.example")
|
|
2781
|
+
assert_redirected_to "/account/edit"
|
|
2782
|
+
token = User.find(ann.id).confirmation_token
|
|
2783
|
+
# The form sends the address in use back (it is pre-filled) with a new
|
|
2784
|
+
# password: no change of address, so the one waiting stays.
|
|
2785
|
+
patch "/account", params: { user: { current_password: "password", email_address: "one@example.com",
|
|
2786
|
+
password: "n3w-s3cr3t-n3w", password_confirmation: "n3w-s3cr3t-n3w" } }
|
|
2787
|
+
assert_redirected_to "/account/edit"
|
|
2788
|
+
follow_redirect!
|
|
2789
|
+
assert_includes response.body, UPDATED
|
|
2790
|
+
assert_includes response.body, "<p class=\"hint\">Waiting for confirmation: one@new.example</p>"
|
|
2791
|
+
kept = User.find(ann.id)
|
|
2792
|
+
assert_equal "one@example.com", kept.email_address
|
|
2793
|
+
assert_equal "one@new.example", kept.unconfirmed_email
|
|
2794
|
+
assert kept.authenticate("n3w-s3cr3t-n3w")
|
|
2795
|
+
# The address alone, sent again unchanged: nothing changes either.
|
|
2796
|
+
patch "/account", params: { user: { current_password: "n3w-s3cr3t-n3w", email_address: " One@Example.com " } }
|
|
2797
|
+
assert_redirected_to "/account/edit"
|
|
2798
|
+
assert_equal "one@new.example", User.find(ann.id).unconfirmed_email
|
|
2799
|
+
# The link still confirms it.
|
|
2800
|
+
assert_equal ann.id, User.find_by_confirmation_token(token).id
|
|
2801
|
+
get "/confirmations/#{token}"
|
|
2802
|
+
assert_redirected_to "/account/edit"
|
|
2803
|
+
assert_equal "one@new.example", User.find(ann.id).email_address
|
|
2804
|
+
end
|
|
2805
|
+
|
|
2806
|
+
test "a new address waiting can be withdrawn, and its link then moves nothing" do
|
|
2807
|
+
ann = users(:one)
|
|
2808
|
+
sign_in_as ann
|
|
2809
|
+
# Nothing waits: no button to withdraw it.
|
|
2810
|
+
get "/account/edit"
|
|
2811
|
+
refute response.body.include?("/account/pending_email")
|
|
2812
|
+
refute response.body.include?("Cancel this change")
|
|
2813
|
+
# A mistyped address, a stranger's mailbox: the page offers to withdraw it.
|
|
2814
|
+
change_address("one@stranger.example")
|
|
2815
|
+
assert_redirected_to "/account/edit"
|
|
2816
|
+
token = User.find(ann.id).confirmation_token
|
|
2817
|
+
follow_redirect!
|
|
2818
|
+
assert_includes response.body, CANCEL_BUTTON
|
|
2819
|
+
assert_includes response.body, "Cancel this change"
|
|
2820
|
+
# Without the form's token: refused, and still waiting.
|
|
2821
|
+
with_forgery_protection do
|
|
2822
|
+
delete "/account/pending_email", params: { x: "1" }
|
|
2823
|
+
assert_response :forbidden
|
|
2824
|
+
end
|
|
2825
|
+
assert_equal "one@stranger.example", User.find(ann.id).unconfirmed_email
|
|
2826
|
+
# A visitor is sent to sign in, and nothing changes.
|
|
2827
|
+
visitor = open_session
|
|
2828
|
+
visitor.delete "/account/pending_email"
|
|
2829
|
+
visitor.assert_redirected_to "/session/new"
|
|
2830
|
+
assert_equal "one@stranger.example", User.find(ann.id).unconfirmed_email
|
|
2831
|
+
# The button, with no password (withdrawing is the safe direction).
|
|
2832
|
+
delete "/account/pending_email"
|
|
2833
|
+
assert_redirected_to "/account/edit"
|
|
2834
|
+
follow_redirect!
|
|
2835
|
+
assert_includes response.body, CANCELLED
|
|
2836
|
+
refute response.body.include?("Waiting for confirmation")
|
|
2837
|
+
refute response.body.include?("Cancel this change")
|
|
2838
|
+
withdrawn = User.find(ann.id)
|
|
2839
|
+
assert_nil withdrawn.unconfirmed_email
|
|
2840
|
+
assert_equal "one@example.com", withdrawn.email_address
|
|
2841
|
+
# The stranger's link: invalid now, and the account stays where it was.
|
|
2842
|
+
assert_nil User.find_by_confirmation_token(token)
|
|
2843
|
+
stranger = open_session
|
|
2844
|
+
stranger.get "/confirmations/#{token}"
|
|
2845
|
+
stranger.assert_redirected_to "/confirmations/new"
|
|
2846
|
+
stranger.follow_redirect!
|
|
2847
|
+
assert_includes stranger.response.body, INVALID
|
|
2848
|
+
assert_equal "one@example.com", User.find(ann.id).email_address
|
|
2849
|
+
assert_nil User.find(ann.id).unconfirmed_email
|
|
2850
|
+
assert signed_in?
|
|
2851
|
+
end
|
|
2852
|
+
|
|
2853
|
+
test "two confirmations racing for one address: the second gets the taken answer" do
|
|
2854
|
+
ann = users(:one)
|
|
2855
|
+
sign_in_as ann
|
|
2856
|
+
change_address("carol@example.com")
|
|
2857
|
+
assert_redirected_to "/account/edit"
|
|
2858
|
+
token = User.find(ann.id).confirmation_token
|
|
2859
|
+
# Another account takes the address between confirm's check and its
|
|
2860
|
+
# write (a trigger inserts it just before the UPDATE): the unique index
|
|
2861
|
+
# refuses the write, and the answer is the taken one, not a 500.
|
|
2862
|
+
add_race_trigger
|
|
2863
|
+
begin
|
|
2864
|
+
get "/confirmations/#{token}"
|
|
2865
|
+
assert_redirected_to "/account/edit"
|
|
2866
|
+
follow_redirect!
|
|
2867
|
+
assert_includes response.body, TAKEN
|
|
2868
|
+
ensure
|
|
2869
|
+
drop_race_trigger
|
|
2870
|
+
end
|
|
2871
|
+
raced = User.find(ann.id)
|
|
2872
|
+
assert_equal "one@example.com", raced.email_address
|
|
2873
|
+
assert_nil raced.unconfirmed_email
|
|
2874
|
+
assert raced.confirmed?
|
|
2875
|
+
assert signed_in?
|
|
2876
|
+
end
|
|
2877
|
+
|
|
2878
|
+
test "a password reset confirms a new account's address, and withdraws a new address waiting" do
|
|
2879
|
+
ann = unconfirmed_user
|
|
2880
|
+
# The reset link went to ann's own address: it proves that mailbox, as
|
|
2881
|
+
# the confirmation link does (and someone who signed up first with that
|
|
2882
|
+
# address loses the account to its owner).
|
|
2883
|
+
reset_with(ann.password_reset_token, "an0ther-s3cr3t")
|
|
2884
|
+
assert_redirected_to "/session/new"
|
|
2885
|
+
assert User.find(ann.id).confirmed?
|
|
2886
|
+
post "/session", params: { email_address: "ann@example.com", password: "an0ther-s3cr3t" }
|
|
2887
|
+
assert_redirected_to "/"
|
|
2888
|
+
# A new address waiting for bob: a reset proves only bob's old
|
|
2889
|
+
# mailbox, so it never confirms it; it withdraws it, and its link
|
|
2890
|
+
# (whoever asked for it may be who bob takes the account back from).
|
|
2891
|
+
bob = users(:two)
|
|
2892
|
+
confirmed_at = User.find(bob.id).confirmed_at.to_i
|
|
2893
|
+
bobs = open_session
|
|
2894
|
+
bobs.sign_in_as bob
|
|
2895
|
+
bobs.patch "/account", params: { user: { current_password: "password", email_address: "two@new.example" } }
|
|
2896
|
+
bobs.assert_redirected_to "/account/edit"
|
|
2897
|
+
pending_token = User.find(bob.id).confirmation_token
|
|
2898
|
+
refute User.find_by_confirmation_token(pending_token).nil?
|
|
2899
|
+
reset_with(bob.password_reset_token, "an0ther-s3cr3t")
|
|
2900
|
+
assert_redirected_to "/session/new"
|
|
2901
|
+
reset = User.find(bob.id)
|
|
2902
|
+
assert_equal "two@example.com", reset.email_address
|
|
2903
|
+
assert_nil reset.unconfirmed_email
|
|
2904
|
+
assert_equal confirmed_at, reset.confirmed_at.to_i
|
|
2905
|
+
assert_nil User.find_by_confirmation_token(pending_token)
|
|
2906
|
+
reader = open_session
|
|
2907
|
+
reader.get "/confirmations/#{pending_token}"
|
|
2908
|
+
reader.assert_redirected_to "/confirmations/new"
|
|
2909
|
+
assert_equal "two@example.com", User.find(bob.id).email_address
|
|
2910
|
+
post "/session", params: { email_address: "two@new.example", password: "an0ther-s3cr3t" }
|
|
2911
|
+
assert_redirected_to "/session/new"
|
|
2912
|
+
end
|
|
2913
|
+
|
|
2914
|
+
test "send again answers the same for unknown addresses" do
|
|
2915
|
+
ann = unconfirmed_user
|
|
2916
|
+
get "/confirmations/new"
|
|
2917
|
+
assert_response :ok
|
|
2918
|
+
assert_includes response.body, "<h1>Send the confirmation link again</h1>"
|
|
2919
|
+
assert_includes response.body, "<form action=\"/confirmations\" method=\"post\">"
|
|
2920
|
+
send_again(" Ann@Example.com ")
|
|
2921
|
+
known = response
|
|
2922
|
+
follow_redirect!
|
|
2923
|
+
assert_includes response.body, SENT
|
|
2924
|
+
send_again("nobody@example.com")
|
|
2925
|
+
unknown = response
|
|
2926
|
+
follow_redirect!
|
|
2927
|
+
assert_includes response.body, SENT
|
|
2928
|
+
# An address confirmed already: the same answer too.
|
|
2929
|
+
send_again("two@example.com")
|
|
2930
|
+
confirmed = response
|
|
2931
|
+
assert_equal 303, known.status
|
|
2932
|
+
assert_equal "/session/new", known.header("Location")
|
|
2933
|
+
assert_equal known.status, unknown.status
|
|
2934
|
+
assert_equal known.header("Location"), unknown.header("Location")
|
|
2935
|
+
assert_equal known.body, unknown.body
|
|
2936
|
+
assert_equal known.status, confirmed.status
|
|
2937
|
+
assert_equal known.header("Location"), confirmed.header("Location")
|
|
2938
|
+
assert_equal known.body, confirmed.body
|
|
2939
|
+
follow_redirect!
|
|
2940
|
+
assert_includes response.body, SENT
|
|
2941
|
+
# One mail, to the address that waits; its link confirms it.
|
|
2942
|
+
assert_enqueued_jobs 1, "cybertrain mail"
|
|
2943
|
+
assert_equal 1, perform_enqueued_jobs
|
|
2944
|
+
mail = Cybertrain::Mailer.deliveries.last
|
|
2945
|
+
refute mail.nil?
|
|
2946
|
+
assert_equal "ann@example.com", mail.mail_to
|
|
2947
|
+
get "/confirmations/#{token_in(last_mail_link)}"
|
|
2948
|
+
assert_redirected_to "/session/new"
|
|
2949
|
+
assert User.find(ann.id).confirmed?
|
|
2950
|
+
# Confirmed now, ann gets the same answer and no mail.
|
|
2951
|
+
clear_enqueued_jobs
|
|
2952
|
+
send_again("ann@example.com")
|
|
2953
|
+
assert_equal known.header("Location"), response.header("Location")
|
|
2954
|
+
assert_equal known.body, response.body
|
|
2955
|
+
assert_no_enqueued_jobs "cybertrain mail"
|
|
2956
|
+
end
|
|
2957
|
+
|
|
2958
|
+
test "send again: a refused address, no safe base and the rate limit" do
|
|
2959
|
+
# Refused at entry now (User.email_address_format?), so saved
|
|
2960
|
+
# before that check existed.
|
|
2961
|
+
unconfirmed_user("jose@example.com").update_columns(email_address: "josé@example.com")
|
|
2962
|
+
unconfirmed_user
|
|
2963
|
+
# An address the mailer refuses: the same answer, and the log says why.
|
|
2964
|
+
log = StringIO.new
|
|
2965
|
+
with_config("test", NO_HOSTS, "", log) do
|
|
2966
|
+
send_again("josé@example.com")
|
|
2967
|
+
refused = response
|
|
2968
|
+
send_again("nobody@example.com")
|
|
2969
|
+
assert_equal 303, response.status
|
|
2970
|
+
assert_equal "/session/new", refused.header("Location")
|
|
2971
|
+
assert_equal refused.header("Location"), response.header("Location")
|
|
2972
|
+
assert_equal refused.body, response.body
|
|
2973
|
+
end
|
|
2974
|
+
assert_no_enqueued_jobs "cybertrain mail"
|
|
2975
|
+
assert_includes log.string, "[ERROR] confirmation mail not sent: the to address contains a space, a control character or a non-ASCII byte\n"
|
|
2976
|
+
# Production with neither base_url nor CYBERTRAIN_HOSTS: no mail, an
|
|
2977
|
+
# [ERROR] line, the same answer.
|
|
2978
|
+
log = StringIO.new
|
|
2979
|
+
with_config("production", NO_HOSTS, "", log) do
|
|
2980
|
+
send_again("ann@example.com")
|
|
2981
|
+
assert_redirected_to "/session/new"
|
|
2982
|
+
follow_redirect!
|
|
2983
|
+
assert_includes response.body, SENT
|
|
2984
|
+
end
|
|
2985
|
+
assert_no_enqueued_jobs "cybertrain mail"
|
|
2986
|
+
assert_includes log.string, "[ERROR] confirmation mail not sent: set @BASE_URL_ENV@ (or CYBERTRAIN_HOSTS)\n"
|
|
2987
|
+
# Ten requests per client address, then the form again for a while.
|
|
2988
|
+
Cybertrain::RateLimit.reset_all!
|
|
2989
|
+
count = 0
|
|
2990
|
+
while count < 10
|
|
2991
|
+
post "/confirmations", params: { email_address: "nobody@example.com" }
|
|
2992
|
+
assert_redirected_to "/session/new"
|
|
2993
|
+
count += 1
|
|
2994
|
+
end
|
|
2995
|
+
post "/confirmations", params: { email_address: "ann@example.com" }
|
|
2996
|
+
assert_redirected_to "/confirmations/new"
|
|
2997
|
+
follow_redirect!
|
|
2998
|
+
assert_includes response.body, "<p class=\"alert\">Try again later.</p>"
|
|
2999
|
+
assert_no_enqueued_jobs "cybertrain mail"
|
|
3000
|
+
Cybertrain::RateLimit.reset_all!
|
|
3001
|
+
end
|
|
3002
|
+
|
|
3003
|
+
# Cybertrain's CSRF check, on inside with_forgery_protection: the
|
|
3004
|
+
# send-again form (a visitor's) and the button that withdraws a new
|
|
3005
|
+
# address (on the account page) are refused without the form's
|
|
3006
|
+
# token, and taken with the token of the page that holds them.
|
|
3007
|
+
test "send again and withdrawing a new address take the page's own form token" do
|
|
3008
|
+
unconfirmed_user
|
|
3009
|
+
Cybertrain::RateLimit.reset_all!
|
|
3010
|
+
with_forgery_protection do
|
|
3011
|
+
post "/confirmations", params: { email_address: "ann@example.com" }
|
|
3012
|
+
assert_response :forbidden
|
|
3013
|
+
end
|
|
3014
|
+
assert_no_enqueued_jobs "cybertrain mail"
|
|
3015
|
+
with_forgery_protection do
|
|
3016
|
+
get "/confirmations/new"
|
|
3017
|
+
post "/confirmations", params: { authenticity_token: authenticity_token_in(response.body), email_address: "ann@example.com" }
|
|
3018
|
+
assert_redirected_to "/session/new"
|
|
3019
|
+
end
|
|
3020
|
+
assert_enqueued_jobs 1, "cybertrain mail"
|
|
3021
|
+
# A new address waiting on one's account.
|
|
3022
|
+
one = users(:one)
|
|
3023
|
+
sign_in_as one
|
|
3024
|
+
change_address("one@stranger.example")
|
|
3025
|
+
assert_redirected_to "/account/edit"
|
|
3026
|
+
with_forgery_protection do
|
|
3027
|
+
delete "/account/pending_email", params: { x: "1" }
|
|
3028
|
+
assert_response :forbidden
|
|
3029
|
+
end
|
|
3030
|
+
assert_equal "one@stranger.example", User.find(one.id).unconfirmed_email
|
|
3031
|
+
with_forgery_protection do
|
|
3032
|
+
get "/account/edit"
|
|
3033
|
+
delete "/account/pending_email", params: { authenticity_token: authenticity_token_in(response.body) }
|
|
3034
|
+
assert_redirected_to "/account/edit"
|
|
3035
|
+
end
|
|
3036
|
+
assert_nil User.find(one.id).unconfirmed_email
|
|
3037
|
+
Cybertrain::RateLimit.reset_all!
|
|
3038
|
+
end
|
|
3039
|
+
|
|
3040
|
+
private
|
|
3041
|
+
|
|
3042
|
+
# A user who has not confirmed the address yet, as a sign-up leaves one
|
|
3043
|
+
# (the fixtures' users are confirmed); the password is the fixtures'.
|
|
3044
|
+
def unconfirmed_user(email_address = "ann@example.com")
|
|
3045
|
+
user = User.new(email_address: email_address)
|
|
3046
|
+
user.password = "password"
|
|
3047
|
+
user.save!
|
|
3048
|
+
user
|
|
3049
|
+
end
|
|
3050
|
+
|
|
3051
|
+
# The token in a link ("http://.../confirmations/<token>").
|
|
3052
|
+
def token_in(link)
|
|
3053
|
+
start = link.index("/confirmations/").to_i + 15
|
|
3054
|
+
link[start, link.size - start].to_s
|
|
3055
|
+
end
|
|
3056
|
+
|
|
3057
|
+
# Whether this browser is signed in as email_address: the nav names them.
|
|
3058
|
+
def signed_in?(email_address = "one@example.com")
|
|
3059
|
+
get "/session/new"
|
|
3060
|
+
response.body.include?("<span>#{email_address}</span>")
|
|
3061
|
+
end
|
|
3062
|
+
|
|
3063
|
+
# Sends the form that mails the link again, the rate limit's counts
|
|
3064
|
+
# forgotten first.
|
|
3065
|
+
def send_again(email_address)
|
|
3066
|
+
Cybertrain::RateLimit.reset_all!
|
|
3067
|
+
post "/confirmations", params: { email_address: email_address }
|
|
3068
|
+
nil
|
|
3069
|
+
end
|
|
3070
|
+
|
|
3071
|
+
# Sends the new-password form of a password reset link with this token.
|
|
3072
|
+
def reset_with(token, password)
|
|
3073
|
+
put "/passwords/#{token}", params: { password: password, password_confirmation: password }
|
|
3074
|
+
nil
|
|
3075
|
+
end
|
|
3076
|
+
|
|
3077
|
+
# A trigger that, just before ann's row gets a new address, inserts
|
|
3078
|
+
# another account with that address: what a sign-up that lands
|
|
3079
|
+
# between User#confirm's check and its write does.
|
|
3080
|
+
def add_race_trigger
|
|
3081
|
+
Cybertrain::DB.with do |c|
|
|
3082
|
+
c.execute("CREATE TRIGGER confirmation_race BEFORE UPDATE OF email_address ON users " \
|
|
3083
|
+
"WHEN NEW.email_address <> OLD.email_address BEGIN " \
|
|
3084
|
+
"INSERT INTO users (email_address, password_digest, created_at, updated_at) " \
|
|
3085
|
+
"VALUES (NEW.email_address, 'x', NEW.updated_at, NEW.updated_at); END")
|
|
3086
|
+
end
|
|
3087
|
+
nil
|
|
3088
|
+
end
|
|
3089
|
+
|
|
3090
|
+
def drop_race_trigger
|
|
3091
|
+
Cybertrain::DB.with { |c| c.execute("DROP TRIGGER IF EXISTS confirmation_race") }
|
|
3092
|
+
nil
|
|
3093
|
+
end
|
|
3094
|
+
|
|
3095
|
+
# Sends the account form with the current password and a new address.
|
|
3096
|
+
def change_address(email_address)
|
|
3097
|
+
Cybertrain::RateLimit.reset_all!
|
|
3098
|
+
patch "/account", params: { user: { current_password: "password", email_address: email_address } }
|
|
3099
|
+
nil
|
|
3100
|
+
end
|
|
3101
|
+
|
|
3102
|
+
@HELPERS@
|
|
3103
|
+
end
|
|
3104
|
+
RUBY
|
|
3105
|
+
end
|
|
3106
|
+
|
|
3107
|
+
# Sign-up mode's first test: the mail, and nobody signed in.
|
|
3108
|
+
def self.confirmations_sign_up_test
|
|
3109
|
+
<<~'RUBY'
|
|
3110
|
+
test "sign-up sends a confirmation mail and signs nobody in" do
|
|
3111
|
+
post "/sign_up", params: { user: { email_address: " Ann@Example.com ", password: "s3cr3t-s3cr3t", password_confirmation: "s3cr3t-s3cr3t" } }
|
|
3112
|
+
assert_redirected_to "/session/new"
|
|
3113
|
+
follow_redirect!
|
|
3114
|
+
page = response.body
|
|
3115
|
+
assert_includes page, "<p class=\"notice\">Welcome! A link to confirm your email address was sent to it. Open it, then sign in.</p>"
|
|
3116
|
+
refute page.include?("<span>ann@example.com</span>")
|
|
3117
|
+
user = User.last
|
|
3118
|
+
assert_equal "ann@example.com", user.email_address
|
|
3119
|
+
assert_equal false, user.confirmed?
|
|
3120
|
+
assert_nil user.confirmed_at
|
|
3121
|
+
assert_nil user.unconfirmed_email
|
|
3122
|
+
assert_equal 0, Session.count
|
|
3123
|
+
assert_enqueued_jobs 1, "cybertrain mail"
|
|
3124
|
+
from = Cybertrain.config.smtp_from
|
|
3125
|
+
begin
|
|
3126
|
+
Cybertrain.config.smtp_from = "set-by-this-test@example.org"
|
|
3127
|
+
assert_equal 1, perform_enqueued_jobs
|
|
3128
|
+
ensure
|
|
3129
|
+
Cybertrain.config.smtp_from = from
|
|
3130
|
+
end
|
|
3131
|
+
mail = Cybertrain::Mailer.deliveries.last
|
|
3132
|
+
refute mail.nil?
|
|
3133
|
+
assert_equal "Confirm your email address", mail.mail_subject
|
|
3134
|
+
assert_equal I18n.t("confirmations_mailer.confirmation_instructions.subject"), mail.mail_subject
|
|
3135
|
+
assert_equal "ann@example.com", mail.mail_to
|
|
3136
|
+
assert_equal "set-by-this-test@example.org", mail.mail_from
|
|
3137
|
+
link = last_mail_link
|
|
3138
|
+
assert link.start_with?("http://www.example.com/confirmations/#{user.id}--"), link
|
|
3139
|
+
assert_equal "Hello ann@example.com,\n\nOpen this link within the next 3 days to confirm your email address:\n" \
|
|
3140
|
+
"#{link}\n\nIf you did not ask for this, ignore this mail: nothing changes.\n", mail.mail_body
|
|
3141
|
+
assert_includes mail.mail_html, "<p>Hello ann@example.com,</p>"
|
|
3142
|
+
assert_includes mail.mail_html, "<p><a href=\"#{link}\">Confirm my email address</a></p>"
|
|
3143
|
+
# The mailed link is the one that confirms this user.
|
|
3144
|
+
assert_equal user.id, User.find_by_confirmation_token(token_in(link)).id
|
|
3145
|
+
# Until it is opened, the right password signs nobody in.
|
|
3146
|
+
post "/session", params: { email_address: "ann@example.com", password: "s3cr3t-s3cr3t" }
|
|
3147
|
+
assert_redirected_to "/session/new"
|
|
3148
|
+
assert_equal 0, Session.count
|
|
3149
|
+
end
|
|
3150
|
+
RUBY
|
|
3151
|
+
end
|
|
3152
|
+
|
|
3153
|
+
# --invite-only's first test: /setup's user is confirmed and signed in.
|
|
3154
|
+
def self.confirmations_setup_test
|
|
3155
|
+
<<~'RUBY'
|
|
3156
|
+
# /setup answers only while no user exists: this test starts without
|
|
3157
|
+
# the fixtures' users.
|
|
3158
|
+
test "/setup makes a confirmed first user, and mails nothing" do
|
|
3159
|
+
User.all.delete_all
|
|
3160
|
+
post "/setup", params: { token: SetupController.setup_token,
|
|
3161
|
+
user: { email_address: "ann@example.com", password: "s3cr3t-s3cr3t", password_confirmation: "s3cr3t-s3cr3t" } }
|
|
3162
|
+
assert_redirected_to "/"
|
|
3163
|
+
assert_equal true, User.first.confirmed?
|
|
3164
|
+
assert signed_in?("ann@example.com")
|
|
3165
|
+
assert_no_enqueued_jobs "cybertrain mail"
|
|
3166
|
+
# Signed out, the same password signs in again.
|
|
3167
|
+
sign_out
|
|
3168
|
+
post "/session", params: { email_address: "ann@example.com", password: "s3cr3t-s3cr3t" }
|
|
3169
|
+
assert_redirected_to "/"
|
|
3170
|
+
end
|
|
3171
|
+
RUBY
|
|
3172
|
+
end
|
|
3173
|
+
end
|
|
3174
|
+
end
|
|
3175
|
+
end
|