diff options
Diffstat (limited to 'railties/test/commands/credentials_test.rb')
-rw-r--r-- | railties/test/commands/credentials_test.rb | 70 |
1 files changed, 67 insertions, 3 deletions
diff --git a/railties/test/commands/credentials_test.rb b/railties/test/commands/credentials_test.rb index 0ee36081c0..974b34836b 100644 --- a/railties/test/commands/credentials_test.rb +++ b/railties/test/commands/credentials_test.rb @@ -4,13 +4,14 @@ require "isolation/abstract_unit" require "env_helpers" require "rails/command" require "rails/commands/credentials/credentials_command" +require "fileutils" +require "tempfile" class Rails::Command::CredentialsCommandTest < ActiveSupport::TestCase include ActiveSupport::Testing::Isolation, EnvHelpers - setup { build_app } - - teardown { teardown_app } + setup :build_app + teardown :teardown_app test "edit without editor gives hint" do run_edit_command(editor: "").tap do |output| @@ -88,6 +89,7 @@ class Rails::Command::CredentialsCommandTest < ActiveSupport::TestCase assert_match(/secret_key_base/, output) end + test "show credentials" do assert_match(/access_key_id: 123/, run_show_command) end @@ -120,6 +122,63 @@ class Rails::Command::CredentialsCommandTest < ActiveSupport::TestCase assert_no_match(/secret_key_base/, output) end + + test "diff enroll diffing" do + assert_match("successfully enrolled", run_diff_command(enroll: true)) + + assert_equal <<~EOM, File.read(app_path(".gitattributes")) + config/credentials/*.yml.enc diff=rails_credentials + config/credentials.yml.enc diff=rails_credentials + EOM + end + + test "running edit after enrolling in diffing sets diff driver" do + run_diff_command(enroll: true) + run_edit_command + + Dir.chdir(app_path) do + assert_equal "bin/rails credentials:diff", `git config --get 'diff.rails_credentials.textconv'`.strip + end + end + + test "diff from git diff left file" do + run_edit_command(environment: "development") + + assert_match(/access_key_id: 123/, run_diff_command("config/credentials/development.yml.enc")) + end + + test "diff from git diff right file" do + run_edit_command(environment: "development") + + content_path = app_path("config", "credentials", "KnAM4a_development.yml.enc") + File.write(content_path, + File.read(app_path("config", "credentials", "development.yml.enc"))) + + assert_match(/access_key_id: 123/, run_diff_command(content_path)) + end + + test "diff for main credentials" do + assert_match(/access_key_id: 123/, run_diff_command("config/credentials.yml.enc")) + end + + test "diff when master key is not available" do + remove_file "config/master.key" + + raw_content = File.read(app_path("config", "credentials.yml.enc")) + assert_match(raw_content, run_diff_command("config/credentials.yml.enc")) + end + + test "diff returns raw encrypted content when errors occur" do + run_edit_command(environment: "development") + + content_path = app_path("20190807development.yml.enc") + encrypted_content = File.read(app_path("config", "credentials", "development.yml.enc")) + File.write(content_path, encrypted_content + "ruin decryption") + + assert_match(encrypted_content, run_diff_command(content_path)) + end + + private def run_edit_command(editor: "cat", environment: nil, **options) switch_env("EDITOR", editor) do @@ -132,4 +191,9 @@ class Rails::Command::CredentialsCommandTest < ActiveSupport::TestCase args = environment ? ["--environment", environment] : [] rails "credentials:show", args, **options end + + def run_diff_command(path = nil, enroll: nil, **options) + args = enroll ? ["--enroll"] : [path] + rails "credentials:diff", args, **options + end end |