From 8d44a1680d9996597b17036c9d199497ae692cc7 Mon Sep 17 00:00:00 2001 From: David Roetzel Date: Tue, 16 Aug 2022 10:06:41 +0200 Subject: [PATCH] Add a few tests for disabled auth #54 Also fix rubocop violations. --- app/controllers/application_controller.rb | 2 +- app/models/dummy_user.rb | 4 +- .../required_authentication_test.rb | 107 ++++++++++++------ 3 files changed, 72 insertions(+), 41 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a3de25ce..59f457e5 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -25,7 +25,7 @@ def current_user def authentication_required unless current_user - if User.none? && !(Rails.configuration.hdm.authentication_disabled) + if User.none? && !Rails.configuration.hdm.authentication_disabled redirect_to new_user_path, notice: 'Please create an admin user first.' else redirect_to login_path diff --git a/app/models/dummy_user.rb b/app/models/dummy_user.rb index 7d6f452f..e0b1a79a 100644 --- a/app/models/dummy_user.rb +++ b/app/models/dummy_user.rb @@ -1,8 +1,6 @@ class DummyUser def initialize - unless Rails.configuration.hdm.authentication_disabled - raise "cannot be used unless authentication is disabled" - end + raise "cannot be used unless authentication is disabled" unless Rails.configuration.hdm.authentication_disabled end def id diff --git a/test/integration/required_authentication_test.rb b/test/integration/required_authentication_test.rb index 3639b210..0f1ba59f 100644 --- a/test/integration/required_authentication_test.rb +++ b/test/integration/required_authentication_test.rb @@ -1,52 +1,85 @@ require "test_helper" class RequiredAuthenticationTest < ActionDispatch::IntegrationTest + class AuthenticationEnabledTest < ActionDispatch::IntegrationTest + test "authentication requirements for environments" do + authentication_required_for :get, environments_path + end - test "authentication requirements for environments" do - authentication_required_for :get, environments_path - end + test "authentication requiremens for nodes" do + authentication_required_for :get, environment_nodes_path("development") + end - test "authentication requiremens for nodes" do - authentication_required_for :get, environment_nodes_path("development") - end + test "authentication requirements for keys" do + authentication_required_for :get, + environment_node_keys_path("development", "testhost") + authentication_required_for :get, + environment_node_key_path("development", "testhost", "hdm::integer") + authentication_required_for :patch, + environment_node_key_path("development", "testhost", "hdm::integer") + authentication_required_for :delete, + environment_node_key_path("development", "testhost", "hdm::integer") + end - test "authentication requirements for keys" do - authentication_required_for :get, - environment_node_keys_path("development", "testhost") - authentication_required_for :get, - environment_node_key_path("development", "testhost", "hdm::integer") - authentication_required_for :patch, - environment_node_key_path("development", "testhost", "hdm::integer") - authentication_required_for :delete, - environment_node_key_path("development", "testhost", "hdm::integer") - end + test "authentication requirements for decrypted values" do + authentication_required_for :post, + environment_node_decrypted_values_path("development", "testhost") + end - test "authentication requirements for decrypted values" do - authentication_required_for :post, - environment_node_decrypted_values_path("development", "testhost") - end + test "authentication requirements for encrypted values" do + authentication_required_for :post, + environment_node_encrypted_values_path("development", "testhost") + end - test "authentication requirements for encrypted values" do - authentication_required_for :post, - environment_node_encrypted_values_path("development", "testhost") - end + test "authentication requirements for users" do + user = FactoryBot.create(:user, admin: true) + + authentication_required_for :get, users_path + authentication_required_for :get, user_path(user) + authentication_required_for :get, new_user_path + authentication_required_for :post, users_path + authentication_required_for :get, edit_user_path(user) + authentication_required_for :patch, user_path(user) + authentication_required_for :delete, user_path(user) + end - test "authentication requirements for users" do - user = FactoryBot.create(:user, admin: true) + private - authentication_required_for :get, users_path - authentication_required_for :get, user_path(user) - authentication_required_for :get, new_user_path - authentication_required_for :post, users_path - authentication_required_for :get, edit_user_path(user) - authentication_required_for :patch, user_path(user) - authentication_required_for :delete, user_path(user) + def authentication_required_for(method, path) + send(method, path) + assert_redirected_to login_path + end end - private + class AuthenticationDisabledTest < ActionDispatch::IntegrationTest + setup do + Rails.configuration.hdm["authentication_disabled"] = true + end + + teardown do + Rails.configuration.hdm["authentication_disabled"] = nil + end + + test "authentication requirements for environments" do + no_authentication_required_for :get, environments_path + end + + test "authentication requiremens for nodes" do + no_authentication_required_for :get, environment_nodes_path("development") + end + + test "authentication requirements for keys" do + no_authentication_required_for :get, + environment_node_keys_path("development", "testhost") + no_authentication_required_for :get, + environment_node_key_path("development", "testhost", "hdm::integer") + end + + private - def authentication_required_for(method, path) - send(method, path) - assert_redirected_to login_path + def no_authentication_required_for(method, path) + send(method, path) + assert_response :success + end end end