diff options
Diffstat (limited to 'railties/test/commands')
-rw-r--r-- | railties/test/commands/console_test.rb | 50 | ||||
-rw-r--r-- | railties/test/commands/credentials_test.rb | 52 | ||||
-rw-r--r-- | railties/test/commands/dbconsole_test.rb | 67 | ||||
-rw-r--r-- | railties/test/commands/encrypted_test.rb | 80 | ||||
-rw-r--r-- | railties/test/commands/secrets_test.rb | 62 | ||||
-rw-r--r-- | railties/test/commands/server_test.rb | 46 |
6 files changed, 318 insertions, 39 deletions
diff --git a/railties/test/commands/console_test.rb b/railties/test/commands/console_test.rb index 4fc082e4ca..45ab8d87ff 100644 --- a/railties/test/commands/console_test.rb +++ b/railties/test/commands/console_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "abstract_unit" require "env_helpers" require "rails/command" @@ -47,7 +49,7 @@ class Rails::ConsoleTest < ActiveSupport::TestCase end def test_console_with_environment - start ["-e production"] + start ["-e", "production"] assert_match(/\sproduction\s/, output) end @@ -82,24 +84,35 @@ class Rails::ConsoleTest < ActiveSupport::TestCase assert_match(/\sspecial-production\s/, output) end + def test_e_option_is_properly_expanded + start ["-e", "prod"] + assert_match(/\sproduction\s/, output) + end + def test_environment_option start ["--environment=special-production"] assert_match(/\sspecial-production\s/, output) end def test_rails_env_is_production_when_first_argument_is_p - start ["p"] - assert_match(/\sproduction\s/, output) + assert_deprecated do + start ["p"] + assert_match(/\sproduction\s/, output) + end end def test_rails_env_is_test_when_first_argument_is_t - start ["t"] - assert_match(/\stest\s/, output) + assert_deprecated do + start ["t"] + assert_match(/\stest\s/, output) + end end def test_rails_env_is_development_when_argument_is_d - start ["d"] - assert_match(/\sdevelopment\s/, output) + assert_deprecated do + start ["d"] + assert_match(/\sdevelopment\s/, output) + end end def test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present @@ -111,7 +124,9 @@ class Rails::ConsoleTest < ActiveSupport::TestCase end end - assert_match("dev", parse_arguments(["dev"])[:environment]) + assert_deprecated do + assert_match("dev", parse_arguments(["dev"])[:environment]) + end ensure Rails::Command::ConsoleCommand.class_eval do undef_method :available_environments @@ -157,21 +172,8 @@ class Rails::ConsoleTest < ActiveSupport::TestCase end def parse_arguments(args) - Rails::Command::ConsoleCommand.class_eval do - alias_method :old_perform, :perform - define_method(:perform) do - extract_environment_option_from_argument - - options - end - end - - Rails::Command.invoke(:console, args) - ensure - Rails::Command::ConsoleCommand.class_eval do - undef_method :perform - alias_method :perform, :old_perform - undef_method :old_perform - end + command = Rails::Command::ConsoleCommand.new([], args) + command.send(:extract_environment_option_from_argument) + command.options end end diff --git a/railties/test/commands/credentials_test.rb b/railties/test/commands/credentials_test.rb new file mode 100644 index 0000000000..4ef827fcf1 --- /dev/null +++ b/railties/test/commands/credentials_test.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +require "isolation/abstract_unit" +require "env_helpers" +require "rails/command" +require "rails/commands/credentials/credentials_command" + +class Rails::Command::CredentialsCommandTest < ActiveSupport::TestCase + include ActiveSupport::Testing::Isolation, EnvHelpers + + setup { build_app } + + teardown { teardown_app } + + test "edit without editor gives hint" do + run_edit_command(editor: "").tap do |output| + assert_match "No $EDITOR to open file in", output + assert_match "bin/rails credentials:edit", output + end + end + + test "edit credentials" do + # Run twice to ensure credentials can be reread after first edit pass. + 2.times do + assert_match(/access_key_id: 123/, run_edit_command) + end + end + + test "show credentials" do + assert_match(/access_key_id: 123/, run_show_command) + end + + test "edit command does not add master key to gitignore when already exist" do + run_edit_command + + Dir.chdir(app_path) do + gitignore = File.read(".gitignore") + assert_equal 1, gitignore.scan(%r|config/master\.key|).length + end + end + + private + def run_edit_command(editor: "cat") + switch_env("EDITOR", editor) do + rails "credentials:edit" + end + end + + def run_show_command + rails "credentials:show" + end +end diff --git a/railties/test/commands/dbconsole_test.rb b/railties/test/commands/dbconsole_test.rb index 0f8c5dbb79..6ad96b28c7 100644 --- a/railties/test/commands/dbconsole_test.rb +++ b/railties/test/commands/dbconsole_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "abstract_unit" require "minitest/mock" require "rails/command" @@ -98,14 +100,24 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase end def test_rails_env_is_development_when_argument_is_dev + assert_deprecated do + stub_available_environments([ "development", "test" ]) do + assert_match("development", parse_arguments([ "dev" ])[:environment]) + end + end + end + + def test_rails_env_is_development_when_environment_option_is_dev stub_available_environments([ "development", "test" ]) do - assert_match("development", parse_arguments([ "dev" ])[:environment]) + assert_match("development", parse_arguments([ "-e", "dev" ])[:environment]) end end def test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present - stub_available_environments([ "dev" ]) do - assert_match("dev", parse_arguments([ "dev" ])[:environment]) + assert_deprecated do + stub_available_environments([ "dev" ]) do + assert_match("dev", parse_arguments([ "dev" ])[:environment]) + end end end @@ -194,12 +206,61 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase assert_equal ["sqlplus", "user/secret@db"], dbconsole.find_cmd_and_exec_args end + def test_sqlserver + start(adapter: "sqlserver", database: "db", username: "user", password: "secret", host: "localhost", port: 1433) + assert_not aborted + assert_equal ["sqsh", "-D", "db", "-U", "user", "-P", "secret", "-S", "localhost:1433"], dbconsole.find_cmd_and_exec_args + end + def test_unknown_command_line_client start(adapter: "unknown", database: "db") assert aborted assert_match(/Unknown command-line client for db/, output) end + def test_primary_is_automatically_picked_with_3_level_configuration + sample_config = { + "test" => { + "primary" => { + "adapter" => "postgresql" + } + } + } + + app_db_config(sample_config) do + assert_equal "postgresql", Rails::DBConsole.new.config["adapter"] + end + end + + def test_specifying_a_custom_connection_and_environment + stub_available_environments(["development"]) do + dbconsole = parse_arguments(["-c", "custom", "-e", "development"]) + + assert_equal "development", dbconsole[:environment] + assert_equal "custom", dbconsole.connection + end + end + + def test_specifying_a_missing_connection + app_db_config({}) do + e = assert_raises(ActiveRecord::AdapterNotSpecified) do + Rails::Command.invoke(:dbconsole, ["-c", "i_do_not_exist"]) + end + + assert_includes e.message, "'i_do_not_exist' connection is not configured." + end + end + + def test_specifying_a_missing_environment + app_db_config({}) do + e = assert_raises(ActiveRecord::AdapterNotSpecified) do + Rails::Command.invoke(:dbconsole) + end + + assert_includes e.message, "'test' database is not configured." + end + end + def test_print_help_short stdout = capture(:stdout) do Rails::Command.invoke(:dbconsole, ["-h"]) diff --git a/railties/test/commands/encrypted_test.rb b/railties/test/commands/encrypted_test.rb new file mode 100644 index 0000000000..0461493f2a --- /dev/null +++ b/railties/test/commands/encrypted_test.rb @@ -0,0 +1,80 @@ +# frozen_string_literal: true + +require "isolation/abstract_unit" +require "env_helpers" +require "rails/command" +require "rails/commands/encrypted/encrypted_command" + +class Rails::Command::EncryptedCommandTest < ActiveSupport::TestCase + include ActiveSupport::Testing::Isolation, EnvHelpers + + setup :build_app + teardown :teardown_app + + test "edit without editor gives hint" do + run_edit_command("config/tokens.yml.enc", editor: "").tap do |output| + assert_match "No $EDITOR to open file in", output + assert_match "bin/rails encrypted:edit", output + end + end + + test "edit encrypted file" do + # Run twice to ensure file can be reread after first edit pass. + 2.times do + assert_match(/access_key_id: 123/, run_edit_command("config/tokens.yml.enc")) + end + end + + test "edit command does not add master key to gitignore when already exist" do + run_edit_command("config/tokens.yml.enc") + + Dir.chdir(app_path) do + assert_match "/config/master.key", File.read(".gitignore") + end + end + + test "edit encrypts file with custom key" do + run_edit_command("config/tokens.yml.enc", key: "config/tokens.key") + + Dir.chdir(app_path) do + assert File.exist?("config/tokens.yml.enc") + assert File.exist?("config/tokens.key") + + assert_match "/config/tokens.key", File.read(".gitignore") + end + + assert_match(/access_key_id: 123/, run_edit_command("config/tokens.yml.enc", key: "config/tokens.key")) + end + + test "show encrypted file with custom key" do + run_edit_command("config/tokens.yml.enc", key: "config/tokens.key") + + assert_match(/access_key_id: 123/, run_show_command("config/tokens.yml.enc", key: "config/tokens.key")) + end + + test "won't corrupt encrypted file when passed wrong key" do + run_edit_command("config/tokens.yml.enc", key: "config/tokens.key") + + assert_match "passed the wrong key", + run_edit_command("config/tokens.yml.enc", allow_failure: true) + + assert_match(/access_key_id: 123/, run_show_command("config/tokens.yml.enc", key: "config/tokens.key")) + end + + private + def run_edit_command(file, key: nil, editor: "cat", **options) + switch_env("EDITOR", editor) do + rails "encrypted:edit", prepare_args(file, key), **options + end + end + + def run_show_command(file, key: nil) + rails "encrypted:show", prepare_args(file, key) + end + + def prepare_args(file, key) + args = [ file ] + args.push("--key", key) if key + args + end +end diff --git a/railties/test/commands/secrets_test.rb b/railties/test/commands/secrets_test.rb index 00b0343397..6b9f284a0c 100644 --- a/railties/test/commands/secrets_test.rb +++ b/railties/test/commands/secrets_test.rb @@ -1,37 +1,77 @@ +# frozen_string_literal: true + require "isolation/abstract_unit" +require "env_helpers" require "rails/command" require "rails/commands/secrets/secrets_command" class Rails::Command::SecretsCommandTest < ActiveSupport::TestCase - include ActiveSupport::Testing::Isolation + include ActiveSupport::Testing::Isolation, EnvHelpers + + setup :build_app + teardown :teardown_app - def setup - build_app + test "edit without editor gives hint" do + assert_match "No $EDITOR to open decrypted secrets in", run_edit_command(editor: "") end - def teardown - teardown_app + test "encrypted secrets are deprecated when using credentials" do + assert_match "Encrypted secrets is deprecated", run_setup_command + assert_equal 1, $?.exitstatus + assert_not File.exist?("config/secrets.yml.enc") end - test "edit without editor gives hint" do - assert_match "No $EDITOR to open decrypted secrets in", run_edit_command(editor: "") + test "encrypted secrets are deprecated when running edit without setup" do + assert_match "Encrypted secrets is deprecated", run_setup_command + assert_equal 1, $?.exitstatus + assert_not File.exist?("config/secrets.yml.enc") + end + + test "encrypted secrets are deprecated for 5.1 config/secrets.yml apps" do + Dir.chdir(app_path) do + FileUtils.rm("config/credentials.yml.enc") + FileUtils.touch("config/secrets.yml") + + assert_match "Encrypted secrets is deprecated", run_setup_command + assert_equal 1, $?.exitstatus + assert_not File.exist?("config/secrets.yml.enc") + end end test "edit secrets" do - run_setup_command + prevent_deprecation # Run twice to ensure encrypted secrets can be reread after first edit pass. 2.times do - assert_match(/external_api_key: 1466aac22e6a869134be3d09b9e89232fc2c2289…/, run_edit_command) + assert_match(/external_api_key: 1466aac22e6a869134be3d09b9e89232fc2c2289/, run_edit_command) end end + test "show secrets" do + prevent_deprecation + + assert_match(/external_api_key: 1466aac22e6a869134be3d09b9e89232fc2c2289/, run_show_command) + end + private + def prevent_deprecation + Dir.chdir(app_path) do + File.write("config/secrets.yml.key", "f731758c639da2604dfb6bf3d1025de8") + File.write("config/secrets.yml.enc", "sEB0mHxDbeP1/KdnMk00wyzPFACl9K6t0cZWn5/Mfx/YbTHvnI07vrneqHg9kaH3wOS7L6pIQteu1P077OtE4BSx/ZRc/sgQPHyWu/tXsrfHqnPNpayOF/XZqizE91JacSFItNMWpuPsp9ynbzz+7cGhoB1S4aPNIU6u0doMrzdngDbijsaAFJmsHIQh6t/QHoJx--8aMoE0PvUWmw1Iqz--ldFqnM/K0g9k17M8PKoN/Q==") + end + end + def run_edit_command(editor: "cat") - Dir.chdir(app_path) { `EDITOR="#{editor}" bin/rails secrets:edit` } + switch_env("EDITOR", editor) do + rails "secrets:edit", allow_failure: true + end + end + + def run_show_command + rails "secrets:show", allow_failure: true end def run_setup_command - Dir.chdir(app_path) { `bin/rails secrets:setup` } + rails "secrets:setup", allow_failure: true end end diff --git a/railties/test/commands/server_test.rb b/railties/test/commands/server_test.rb index 2d1f071969..33715ea75f 100644 --- a/railties/test/commands/server_test.rb +++ b/railties/test/commands/server_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "abstract_unit" require "env_helpers" require "rails/command" @@ -20,6 +22,18 @@ class Rails::ServerTest < ActiveSupport::TestCase assert_nil options[:server] end + def test_daemon_with_option + args = ["-d"] + options = parse_arguments(args) + assert_equal true, options[:daemonize] + end + + def test_daemon_without_option + args = [] + options = parse_arguments(args) + assert_equal false, options[:daemonize] + end + def test_server_option_without_environment args = ["thin"] with_rack_env nil do @@ -79,6 +93,18 @@ class Rails::ServerTest < ActiveSupport::TestCase assert_equal false, options[:caching] end + def test_early_hints_with_option + args = ["--early-hints"] + options = parse_arguments(args) + assert_equal true, options[:early_hints] + end + + def test_early_hints_is_nil_by_default + args = [] + options = parse_arguments(args) + assert_nil options[:early_hints] + end + def test_log_stdout with_rack_env nil do with_rails_env nil do @@ -140,6 +166,18 @@ class Rails::ServerTest < ActiveSupport::TestCase end def test_argument_precedence_over_environment_variable + switch_env "PORT", "1234" do + args = ["-p", "5678"] + options = parse_arguments(args) + assert_equal 5678, options[:Port] + end + + switch_env "PORT", "1234" do + args = ["-p", "3000"] + options = parse_arguments(args) + assert_equal 3000, options[:Port] + end + switch_env "HOST", "1.2.3.4" do args = ["-b", "127.0.0.1"] options = parse_arguments(args) @@ -153,6 +191,12 @@ class Rails::ServerTest < ActiveSupport::TestCase server_options = parse_arguments(["--port", 3001]) assert_equal [:Port], server_options[:user_supplied_options] + + server_options = parse_arguments(["-p3001", "-C", "--binding", "127.0.0.1"]) + assert_equal [:Port, :Host, :caching], server_options[:user_supplied_options] + + server_options = parse_arguments(["--port=3001"]) + assert_equal [:Port], server_options[:user_supplied_options] end def test_default_options @@ -170,7 +214,7 @@ class Rails::ServerTest < ActiveSupport::TestCase ARGV.replace args options = parse_arguments(args) - expected = "bin/rails server -p 4567 -b 127.0.0.1 -c dummy_config.ru -d -e test -P tmp/server.pid -C" + expected = "bin/rails server -p 4567 -b 127.0.0.1 -c dummy_config.ru -d -e test -P tmp/server.pid -C --restart" assert_equal expected, options[:restart_cmd] ensure |