Skip to content

Commit 859352a

Browse files
committed
Refactor tests
1 parent d329f90 commit 859352a

92 files changed

Lines changed: 223 additions & 301 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

test/controllers/api/base_controller_test.rb

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ class Api::BaseControllerTest < ActionDispatch::IntegrationTest
1313
end
1414

1515
teardown do
16-
# Restore feature flags so later tests (e.g. file_push, url push) don't see them false
17-
Settings.require_mfa = false
18-
Settings.enable_file_pushes = true
19-
Settings.enable_url_pushes = true
16+
Settings.reload!
2017
Rails.application.reload_routes!
2118
end
2219

@@ -278,8 +275,6 @@ class Api::BaseControllerTest < ActionDispatch::IntegrationTest
278275
end
279276

280277
test "/p/create with files requires authentication even when allow_anonymous is true" do
281-
previous_allow_anonymous = Settings.allow_anonymous
282-
previous_enable_file_pushes = Settings.enable_file_pushes
283278
Settings.allow_anonymous = true
284279
Settings.enable_file_pushes = true
285280
Rails.application.reload_routes!
@@ -296,10 +291,6 @@ class Api::BaseControllerTest < ActionDispatch::IntegrationTest
296291
}
297292

298293
assert_response :unauthorized
299-
ensure
300-
Settings.allow_anonymous = previous_allow_anonymous
301-
Settings.enable_file_pushes = previous_enable_file_pushes
302-
Rails.application.reload_routes!
303294
end
304295

305296
test "/p/create with files works with valid token when allow_anonymous is true" do
@@ -322,10 +313,6 @@ class Api::BaseControllerTest < ActionDispatch::IntegrationTest
322313
assert_response :created
323314
json = JSON.parse(response.body)
324315
assert json["url_token"].present?
325-
ensure
326-
Settings.allow_anonymous = true
327-
Settings.enable_file_pushes = false
328-
Rails.application.reload_routes!
329316
end
330317

331318
test "/p/create with empty files key requires authentication when allow_anonymous is true" do
@@ -345,10 +332,6 @@ class Api::BaseControllerTest < ActionDispatch::IntegrationTest
345332
}
346333

347334
assert_response :unauthorized
348-
ensure
349-
Settings.allow_anonymous = true
350-
Settings.enable_file_pushes = false
351-
Rails.application.reload_routes!
352335
end
353336

354337
# When allow_anonymous is false, Api::V1::PushesController#create calls
@@ -372,8 +355,6 @@ class Api::BaseControllerTest < ActionDispatch::IntegrationTest
372355
if response.redirect?
373356
assert_match(%r{/users/sign_in}, response.location)
374357
end
375-
ensure
376-
Settings.allow_anonymous = true
377358
end
378359

379360
test "/p/create with valid token succeeds when allow_anonymous is false" do
@@ -393,8 +374,6 @@ class Api::BaseControllerTest < ActionDispatch::IntegrationTest
393374
assert_response :created, "authenticated create should succeed when allow_anonymous is false"
394375
json = JSON.parse(response.body)
395376
assert json["url_token"].present?
396-
ensure
397-
Settings.allow_anonymous = true
398377
end
399378

400379
test "v2 show requires authentication when allow_anonymous is false" do
@@ -407,8 +386,6 @@ class Api::BaseControllerTest < ActionDispatch::IntegrationTest
407386
}
408387

409388
assert_response :unauthorized
410-
ensure
411-
Settings.allow_anonymous = true
412389
end
413390

414391
# Test path-based authentication requirements for /f paths
@@ -428,9 +405,6 @@ class Api::BaseControllerTest < ActionDispatch::IntegrationTest
428405
}
429406

430407
assert_response :unauthorized
431-
ensure
432-
Settings.enable_file_pushes = false
433-
Rails.application.reload_routes!
434408
end
435409

436410
test "/f/create works with valid token" do
@@ -451,9 +425,6 @@ class Api::BaseControllerTest < ActionDispatch::IntegrationTest
451425

452426
# Should not be unauthorized (may be other validation errors)
453427
assert_not_equal :unauthorized, response.status
454-
ensure
455-
Settings.enable_file_pushes = false
456-
Rails.application.reload_routes!
457428
end
458429

459430
test "/f/audit requires authentication" do
@@ -465,9 +436,6 @@ class Api::BaseControllerTest < ActionDispatch::IntegrationTest
465436

466437
get "/f/#{push.url_token}/audit.json"
467438
assert_response :unauthorized
468-
ensure
469-
Settings.enable_file_pushes = false
470-
Rails.application.reload_routes!
471439
end
472440

473441
test "/f/active requires authentication" do
@@ -476,9 +444,6 @@ class Api::BaseControllerTest < ActionDispatch::IntegrationTest
476444

477445
get "/f/active.json"
478446
assert_response :unauthorized
479-
ensure
480-
Settings.enable_file_pushes = false
481-
Rails.application.reload_routes!
482447
end
483448

484449
test "/f/expired requires authentication" do
@@ -487,9 +452,6 @@ class Api::BaseControllerTest < ActionDispatch::IntegrationTest
487452

488453
get "/f/expired.json"
489454
assert_response :unauthorized
490-
ensure
491-
Settings.enable_file_pushes = false
492-
Rails.application.reload_routes!
493455
end
494456

495457
# Test path-based authentication requirements for /r paths
@@ -508,9 +470,6 @@ class Api::BaseControllerTest < ActionDispatch::IntegrationTest
508470
}
509471

510472
assert_response :unauthorized
511-
ensure
512-
Settings.enable_url_pushes = false
513-
Rails.application.reload_routes!
514473
end
515474

516475
test "/r/create works with valid token" do
@@ -530,9 +489,6 @@ class Api::BaseControllerTest < ActionDispatch::IntegrationTest
530489

531490
# Should not be unauthorized (may be other validation errors)
532491
assert_not_equal :unauthorized, response.status
533-
ensure
534-
Settings.enable_url_pushes = false
535-
Rails.application.reload_routes!
536492
end
537493

538494
test "/r/audit requires authentication" do
@@ -544,9 +500,6 @@ class Api::BaseControllerTest < ActionDispatch::IntegrationTest
544500

545501
get "/r/#{push.url_token}/audit.json"
546502
assert_response :unauthorized
547-
ensure
548-
Settings.enable_url_pushes = false
549-
Rails.application.reload_routes!
550503
end
551504

552505
test "/r/active requires authentication" do
@@ -555,9 +508,6 @@ class Api::BaseControllerTest < ActionDispatch::IntegrationTest
555508

556509
get "/r/active.json"
557510
assert_response :unauthorized
558-
ensure
559-
Settings.enable_url_pushes = false
560-
Rails.application.reload_routes!
561511
end
562512

563513
test "/r/expired requires authentication" do
@@ -566,9 +516,6 @@ class Api::BaseControllerTest < ActionDispatch::IntegrationTest
566516

567517
get "/r/expired.json"
568518
assert_response :unauthorized
569-
ensure
570-
Settings.enable_url_pushes = false
571-
Rails.application.reload_routes!
572519
end
573520

574521
# Test user already signed in (session-based auth)
@@ -664,9 +611,6 @@ class Api::BaseControllerTest < ActionDispatch::IntegrationTest
664611
assert_response :bad_request
665612
json_response = JSON.parse(@response.body)
666613
assert json_response.key?("error")
667-
ensure
668-
Settings.enable_file_pushes = false
669-
Rails.application.reload_routes!
670614
end
671615

672616
# Test token with blank/nil values

test/controllers/file_push_controller_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class FilePushControllerTest < ActionDispatch::IntegrationTest
1111
end
1212

1313
teardown do
14-
@luca = users(:luca)
15-
sign_out @luca
14+
Settings.reload!
15+
Rails.application.reload_routes!
1616
end
1717

1818
test "New push form is NOT available anonymous" do

test/controllers/first_run_controller_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def stub_boot_code_file
3030
teardown do
3131
User.destroy_all
3232
FirstRunBootCode.clear!
33-
Settings.disable_logins = false
34-
Settings.disable_signups = false
33+
Settings.reload!
34+
Rails.application.reload_routes!
3535
end
3636

3737
test "any page redirects to first run when no users exist" do

test/controllers/qr_push_controller_test.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ class QrPushControllerTest < ActionDispatch::IntegrationTest
1010
end
1111

1212
teardown do
13-
@luca = users(:luca)
14-
sign_out @luca
13+
Settings.reload!
1514
end
1615

1716
test "New push form is available when anonymous" do
@@ -113,8 +112,6 @@ class QrPushControllerTest < ActionDispatch::IntegrationTest
113112
assert_redirected_to root_path
114113
follow_redirect!
115114
assert_match(/QR code pushes are disabled\./i, flash[:notice])
116-
ensure
117-
Settings.enable_qr_pushes = true
118115
end
119116

120117
test "when QR pushes disabled, creating a QR push redirects to root with notice" do
@@ -129,8 +126,6 @@ class QrPushControllerTest < ActionDispatch::IntegrationTest
129126

130127
assert_redirected_to root_path
131128
assert_equal I18n._("QR code pushes are disabled."), flash[:notice]
132-
ensure
133-
Settings.enable_qr_pushes = true
134129
end
135130

136131
test "when QR pushes disabled, logged-in user creating QR push redirects to root with notice" do
@@ -147,7 +142,5 @@ class QrPushControllerTest < ActionDispatch::IntegrationTest
147142

148143
assert_redirected_to root_path
149144
assert_equal I18n._("QR code pushes are disabled."), flash[:notice]
150-
ensure
151-
Settings.enable_qr_pushes = true
152145
end
153146
end

test/controllers/text_push_controller_test.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ class TextPushControllerTest < ActionDispatch::IntegrationTest
1212
end
1313

1414
teardown do
15-
Settings.enable_file_pushes = false
16-
Settings.enable_url_pushes = false
15+
Settings.reload!
1716
end
1817

1918
test "New push form is available anonymous" do
@@ -217,8 +216,6 @@ class TextPushControllerTest < ActionDispatch::IntegrationTest
217216
assert_response :redirect
218217
follow_redirect!
219218
assert response.body.include?("You need to sign in or sign up before continuing.")
220-
ensure
221-
Settings.allow_anonymous = true
222219
end
223220

224221
test "when allow_anonymous disabled, anonymous user cannot create password push" do
@@ -234,8 +231,6 @@ class TextPushControllerTest < ActionDispatch::IntegrationTest
234231
assert_response :redirect
235232
follow_redirect!
236233
assert response.body.include?("You need to sign in or sign up before continuing.")
237-
ensure
238-
Settings.allow_anonymous = true
239234
end
240235

241236
test "when allow_anonymous disabled, logged-in user can access new and create password push" do
@@ -253,7 +248,5 @@ class TextPushControllerTest < ActionDispatch::IntegrationTest
253248
}
254249
}
255250
assert_response :redirect
256-
ensure
257-
Settings.allow_anonymous = true
258251
end
259252
end

test/controllers/urls_controller_test.rb

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class UrlsControllerTest < ActionDispatch::IntegrationTest
1111
end
1212

1313
teardown do
14-
@luca = users(:luca)
15-
sign_out @luca
14+
Settings.reload!
15+
Rails.application.reload_routes!
1616
end
1717

1818
test "New push form is available when anonymous" do
@@ -93,9 +93,6 @@ class UrlsControllerTest < ActionDispatch::IntegrationTest
9393
assert_redirected_to root_path
9494
follow_redirect!
9595
assert_match(/URL pushes are disabled\./i, flash[:notice])
96-
ensure
97-
Settings.enable_url_pushes = true
98-
Rails.application.reload_routes!
9996
end
10097

10198
test "when URL pushes disabled, creating a URL push redirects to root with notice" do
@@ -111,9 +108,6 @@ class UrlsControllerTest < ActionDispatch::IntegrationTest
111108

112109
assert_redirected_to root_path
113110
assert_equal I18n._("URL pushes are disabled."), flash[:notice]
114-
ensure
115-
Settings.enable_url_pushes = true
116-
Rails.application.reload_routes!
117111
end
118112

119113
test "when URL pushes disabled, logged-in user creating URL push redirects to root with notice" do
@@ -131,8 +125,5 @@ class UrlsControllerTest < ActionDispatch::IntegrationTest
131125

132126
assert_redirected_to root_path
133127
assert_equal I18n._("URL pushes are disabled."), flash[:notice]
134-
ensure
135-
Settings.enable_url_pushes = true
136-
Rails.application.reload_routes!
137128
end
138129
end

test/controllers/users/registrations_controller_test.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ class Users::RegistrationsControllerTest < ActionDispatch::IntegrationTest
1010
@other_user = users(:one)
1111
end
1212

13-
teardown do
14-
Settings.disable_logins = false
15-
end
16-
1713
# DELETE /users (destroy action)
1814

1915
test "authenticated user can delete their own account" do

test/helpers/application_helper_test.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,11 @@ class ApplicationHelperTest < ActionView::TestCase
77

88
setup do
99
@push = pushes(:test_push)
10-
# Ensure Settings are available
11-
@original_title = Settings.brand.title
12-
@original_enabled_language_codes = Settings.enabled_language_codes.dup
13-
@original_override_base_url = Settings.override_base_url
1410
end
1511

1612
teardown do
1713
# Restore original settings
18-
Settings.brand.title = @original_title
19-
Settings.enabled_language_codes = @original_enabled_language_codes
20-
Settings.override_base_url = @original_override_base_url
14+
Settings.reload!
2115
ENV.delete("FORCE_SSL")
2216
end
2317

test/helpers/language_helper_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ class LanguageHelperTest < ActionView::TestCase
77

88
setup do
99
@original_available_locales = I18n.available_locales.dup
10-
@original_language_codes = Settings.language_codes.dup
1110
end
1211

1312
teardown do
1413
I18n.available_locales = @original_available_locales
15-
Settings.language_codes = @original_language_codes
14+
Settings.reload!
1615
end
1716

1817
test "language_options_for_select returns array of language code and locale pairs" do

test/initializers/session_store_test.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@
33
require "test_helper"
44

55
class SessionStoreTest < ActiveSupport::TestCase
6-
def setup
7-
# Store the original setting to restore it later
8-
@original_secure_cookies = Settings.secure_cookies
9-
end
10-
116
def teardown
12-
# Restore the original setting
13-
Settings.secure_cookies = @original_secure_cookies
7+
Settings.reload!
148
# Reload the initializer to restore the original configuration
159
load Rails.root.join("config/initializers/session_store.rb")
1610
end

0 commit comments

Comments
 (0)