rails/railties/test/application/credentials_test.rb
Kasper Timm Hansen 37c948ce67
Restructure credentials after environment overrides.
Follow up to: e0d3313

- Revert renames from `encrypted` and `encrypted_file` back to `credentials`.
  They might be using our Encrypted* generators but from that level of abstraction
  they're still about credentials.

- Same vein: extract a `credentials` method for the `encrypted` local variable. But
  don't call it `encrypted` just because it uses that under the hood. It's about
  capturing the credentials. It's also useful in `change_credentials_in_system_editor`.

- Remove lots of needless argument passing. We've abstracted content_path and key_path
  into methods for a reason, so they should be used. Also spares a conspicuous rename
  of content_path into file_path in other methods.

- Reorders private methods so they're grouped into: command building blocks, option
  parsers, and the generators.

- Extracts commonality in the credentials application tests. A tad unsure about this.
  But I do like that we go with key, content thus matching the command and remove the
  yield which isn't really needed.

- Moves test/credentials_test.rb to beneath the test/application directory. It's a
  Rails application test, so it should be in there.

- Uses `root.join` — a neat trick gleaned from the tests! — and composes the configuration
  private methods such that the building block is below the callers.
2019-01-14 20:13:00 +01:00

57 lines
1.7 KiB
Ruby

# frozen_string_literal: true
require "isolation/abstract_unit"
require "env_helpers"
class Rails::CredentialsTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation, EnvHelpers
setup :build_app
teardown :teardown_app
test "reads credentials from environment specific path" do
write_credentials_override(:production)
app("production")
assert_equal "revealed", Rails.application.credentials.mystery
end
test "reads credentials from customized path and key" do
write_credentials_override(:staging)
add_to_env_config("production", "config.credentials.content_path = config.root.join('config/credentials/staging.yml.enc')")
add_to_env_config("production", "config.credentials.key_path = config.root.join('config/credentials/staging.key')")
app("production")
assert_equal "revealed", Rails.application.credentials.mystery
end
test "reads credentials using environment variable key" do
write_credentials_override(:production, with_key: false)
switch_env("RAILS_MASTER_KEY", credentials_key) do
app("production")
assert_equal "revealed", Rails.application.credentials.mystery
end
end
private
def write_credentials_override(name, with_key: true)
Dir.chdir(app_path) do
Dir.mkdir "config/credentials"
File.write "config/credentials/#{name}.key", credentials_key if with_key
# secret_key_base: secret
# mystery: revealed
File.write "config/credentials/#{name}.yml.enc",
"vgvKu4MBepIgZ5VHQMMPwnQNsLlWD9LKmJHu3UA/8yj6x+3fNhz3DwL9brX7UA==--qLdxHP6e34xeTAiI--nrcAsleXuo9NqiEuhntAhw=="
end
end
def credentials_key
"2117e775dc2024d4f49ddf3aeb585919"
end
end