From 18a8c4a3d705535c0045d77db9b09cb1c7c3d72a Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Sun, 8 Sep 2024 22:09:16 -0400 Subject: [PATCH 1/2] Hack in registration prevention --- app/controllers/users_controller.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index e8058a3..614d8df 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -26,7 +26,10 @@ class UsersController < ApplicationController @user = User.new(user_params) respond_to do |format| - if @user.save + if ENV["REGISTRATION_ALLOWED"].blank? + format.html { redirect_to new_session_url, alert: "Registration disabled." } + format.json { render json: @user.errors, status: :unprocessable_entity } + elsif @user.save @session = Session.new(session_params).save session[:current_user_id] = @session.user_id -- 2.45.2 From 32c1e6019167574ac42656ac630c81c82569da40 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Sun, 8 Sep 2024 22:17:11 -0400 Subject: [PATCH 2/2] Stub environment in tests --- test/controllers/users_controller_test.rb | 18 ++++++++++-------- test/system/users_test.rb | 12 +++++++----- test/test_helper.rb | 11 +++++++++++ 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index 48ce687..19d552a 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -17,15 +17,17 @@ class UsersControllerTest < ActionDispatch::IntegrationTest end test "should create user" do - assert_difference("User.count") do - params = { - user: { - email: "userthree@example.local", - password: "secret", - password_confirmation: "secret" + stub_environment(REGISTRATION_ALLOWED: "true") do + assert_difference("User.count") do + params = { + user: { + email: "userthree@example.local", + password: "secret", + password_confirmation: "secret" + } } - } - post users_url, params: params + post users_url, params: params + end end assert_redirected_to user_url(User.last) diff --git a/test/system/users_test.rb b/test/system/users_test.rb index 4617f3d..905ea45 100644 --- a/test/system/users_test.rb +++ b/test/system/users_test.rb @@ -15,12 +15,14 @@ class UsersTest < ApplicationSystemTestCase visit users_url click_on "Sign up" - fill_in "Email", with: "userthree@example.local" - fill_in "Password", with: "secret" - fill_in "Password confirmation", with: "secret" - click_on "Create User" + stub_environment(REGISTRATION_ALLOWED: "true") do + fill_in "Email", with: "userthree@example.local" + fill_in "Password", with: "secret" + fill_in "Password confirmation", with: "secret" + click_on "Create User" - assert_text "User was successfully created" + assert_text "User was successfully created" + end click_on "Back" end diff --git a/test/test_helper.rb b/test/test_helper.rb index a45e11f..4ac0406 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -14,4 +14,15 @@ class ActiveSupport::TestCase post sessions_url, params: { session: { email: email, password: password } } assert session[:current_user_id].present? end + + def stub_environment(env) + old_env = ENV.to_hash + ENV.update(env.stringify_keys) + + begin + yield + ensure + ENV.replace(old_env) + end + end end -- 2.45.2