|
| 1 | +require_relative "../test_helper" |
| 2 | +require_relative "file_helpers" |
| 3 | + |
| 4 | +module Rails::AppEnv::FeaturesTest |
| 5 | + class CredentialsCommandTest < ActiveSupport::TestCase |
| 6 | + include FileHelpers |
| 7 | + |
| 8 | + def teardown |
| 9 | + cleanup_credentials_files |
| 10 | + end |
| 11 | + |
| 12 | + test "does not override when --environment is custom" do |
| 13 | + arg_env = "foo" |
| 14 | + |
| 15 | + ["foo", "production", "development", "test", nil].each do |app_env| |
| 16 | + ["foo", "production", "development", "test", nil].each do |rails_env| |
| 17 | + cleanup_credentials_files |
| 18 | + |
| 19 | + run_edit_command(app_env:, rails_env:, arg_env:) |
| 20 | + |
| 21 | + refute_files %w[ |
| 22 | + config/credentials.yml.enc |
| 23 | + config/master.key |
| 24 | + config/credentials/production.yml.enc |
| 25 | + config/credentials/production.key |
| 26 | + config/credentials/development.yml.enc |
| 27 | + config/credentials/development.key |
| 28 | + config/credentials/test.yml.enc |
| 29 | + config/credentials/test.key |
| 30 | + config/credentials/.yml.enc |
| 31 | + config/credentials/.key |
| 32 | + ], "when APP_ENV is #{app_env.inspect} and RAILS_ENV is #{rails_env.inspect} and arg_env is #{arg_env.inspect}" |
| 33 | + |
| 34 | + assert_files %w[ |
| 35 | + config/credentials/foo.yml.enc |
| 36 | + config/credentials/foo.key |
| 37 | + ], "when APP_ENV is #{app_env.inspect} and RAILS_ENV is #{rails_env.inspect} and arg_env is #{arg_env.inspect}" |
| 38 | + end |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + test "does not override when --environment is production" do |
| 43 | + arg_env = "production" |
| 44 | + |
| 45 | + ["foo", "production", "development", "test", nil].each do |app_env| |
| 46 | + ["foo", "production", "development", "test", nil].each do |rails_env| |
| 47 | + cleanup_credentials_files |
| 48 | + |
| 49 | + run_edit_command(app_env:, rails_env:, arg_env:) |
| 50 | + |
| 51 | + refute_files %w[ |
| 52 | + config/credentials.yml.enc |
| 53 | + config/master.key |
| 54 | + config/credentials/foo.yml.enc |
| 55 | + config/credentials/foo.key |
| 56 | + config/credentials/development.yml.enc |
| 57 | + config/credentials/development.key |
| 58 | + config/credentials/test.yml.enc |
| 59 | + config/credentials/test.key |
| 60 | + config/credentials/.yml.enc |
| 61 | + config/credentials/.key |
| 62 | + ], "when APP_ENV is #{app_env.inspect} and RAILS_ENV is #{rails_env.inspect} and arg_env is #{arg_env.inspect}" |
| 63 | + |
| 64 | + assert_files %w[ |
| 65 | + config/credentials/production.yml.enc |
| 66 | + config/credentials/production.key |
| 67 | + ], "when APP_ENV is #{app_env.inspect} and RAILS_ENV is #{rails_env.inspect} and arg_env is #{arg_env.inspect}" |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + test "does not override when --environment is development" do |
| 73 | + arg_env = "development" |
| 74 | + |
| 75 | + ["foo", "production", "development", "test", nil].each do |app_env| |
| 76 | + ["foo", "production", "development", "test", nil].each do |rails_env| |
| 77 | + cleanup_credentials_files |
| 78 | + |
| 79 | + run_edit_command(app_env:, rails_env:, arg_env:) |
| 80 | + |
| 81 | + refute_files %w[ |
| 82 | + config/credentials.yml.enc |
| 83 | + config/master.key |
| 84 | + config/credentials/foo.yml.enc |
| 85 | + config/credentials/foo.key |
| 86 | + config/credentials/production.yml.enc |
| 87 | + config/credentials/production.key |
| 88 | + config/credentials/test.yml.enc |
| 89 | + config/credentials/test.key |
| 90 | + config/credentials/.yml.enc |
| 91 | + config/credentials/.key |
| 92 | + ], "when APP_ENV is #{app_env.inspect} and RAILS_ENV is #{rails_env.inspect} and arg_env is #{arg_env.inspect}" |
| 93 | + |
| 94 | + assert_files %w[ |
| 95 | + config/credentials/development.yml.enc |
| 96 | + config/credentials/development.key |
| 97 | + ], "when APP_ENV is #{app_env.inspect} and RAILS_ENV is #{rails_env.inspect} and arg_env is #{arg_env.inspect}" |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + test "does not override when --environment is test" do |
| 103 | + arg_env = "test" |
| 104 | + |
| 105 | + ["foo", "production", "development", "test", nil].each do |app_env| |
| 106 | + ["foo", "production", "development", "test", nil].each do |rails_env| |
| 107 | + cleanup_credentials_files |
| 108 | + |
| 109 | + run_edit_command(app_env:, rails_env:, arg_env:) |
| 110 | + |
| 111 | + refute_files %W[ |
| 112 | + config/credentials.yml.enc |
| 113 | + config/master.key |
| 114 | + config/credentials/foo.yml.enc |
| 115 | + config/credentials/foo.key |
| 116 | + config/credentials/production.yml.enc |
| 117 | + config/credentials/production.key |
| 118 | + config/credentials/development.yml.enc |
| 119 | + config/credentials/development.key |
| 120 | + config/credentials/.yml.enc |
| 121 | + config/credentials/.key |
| 122 | + ], "when APP_ENV is #{app_env.inspect} and RAILS_ENV is #{rails_env.inspect} and arg_env is #{arg_env.inspect}" |
| 123 | + |
| 124 | + assert_files %w[ |
| 125 | + config/credentials/test.yml.enc |
| 126 | + config/credentials/test.key |
| 127 | + ], "when APP_ENV is #{app_env.inspect} and RAILS_ENV is #{rails_env.inspect} and arg_env is #{arg_env.inspect}" |
| 128 | + end |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + test "does not override when --environment are blank" do |
| 133 | + arg_env = nil |
| 134 | + |
| 135 | + ["foo", "production", "development", "test", nil].each do |app_env| |
| 136 | + ["foo", "production", "development", "test", nil].each do |rails_env| |
| 137 | + cleanup_credentials_files |
| 138 | + |
| 139 | + run_edit_command(app_env:, rails_env:, arg_env:) |
| 140 | + |
| 141 | + refute_files %w[ |
| 142 | + config/credentials/foo.yml.enc |
| 143 | + config/credentials/foo.key |
| 144 | + config/credentials/production.yml.enc |
| 145 | + config/credentials/production.key |
| 146 | + config/credentials/development.yml.enc |
| 147 | + config/credentials/development.key |
| 148 | + config/credentials/test.yml.enc |
| 149 | + config/credentials/test.key |
| 150 | + config/credentials/.yml.enc |
| 151 | + config/credentials/.key |
| 152 | + ], "when APP_ENV is #{app_env.inspect} and RAILS_ENV is #{rails_env.inspect} and arg_env is #{arg_env.inspect}" |
| 153 | + |
| 154 | + assert_files %w[ |
| 155 | + config/credentials.yml.enc |
| 156 | + config/master.key |
| 157 | + ], "when APP_ENV is #{app_env.inspect} and RAILS_ENV is #{rails_env.inspect} and arg_env is #{arg_env.inspect}" |
| 158 | + end |
| 159 | + end |
| 160 | + end |
| 161 | + |
| 162 | + private |
| 163 | + |
| 164 | + def run_edit_command(arg_env: nil, app_env: nil, rails_env: nil) |
| 165 | + env = {"VISUAL" => "cat", "EDITOR" => "cat", "APP_ENV" => app_env, "RAILS_ENV" => rails_env} |
| 166 | + args = arg_env ? ["--environment", arg_env] : [] |
| 167 | + |
| 168 | + _, status = Open3.capture2(env, "bin/rails", "credentials:edit", *args, {chdir: DUMMY_ROOT}) |
| 169 | + |
| 170 | + assert_predicate status, :success? |
| 171 | + end |
| 172 | + |
| 173 | + def cleanup_credentials_files |
| 174 | + FileUtils.remove_dir dummy_path("config/credentials"), true |
| 175 | + FileUtils.remove_file dummy_path("config/credentials.yml.enc"), true |
| 176 | + FileUtils.remove_file dummy_path("config/master.key"), true |
| 177 | + end |
| 178 | + end |
| 179 | +end |
0 commit comments