aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/commands/secrets_test.rb
blob: 377191984983f3ca68762ed2a2b5ec7ce898d264 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
require "isolation/abstract_unit"
require "rails/command"
require "rails/commands/secrets/secrets_command"

class Rails::Command::SecretsCommandTest < ActiveSupport::TestCase
  include ActiveSupport::Testing::Isolation

  def setup
    build_app
  end

  def teardown
    teardown_app
  end

  test "edit without editor gives hint" do
    assert_match "No $EDITOR to open decrypted secrets in", run_edit_command(editor: "")
  end

  test "edit secrets" do
    # Runs setup before first edit.
    assert_match(/Adding config\/secrets\.yml\.key to store the encryption key/, run_edit_command)

    # 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)
    end
  end

  test "show secrets" do
    run_setup_command
    assert_match(/external_api_key: 1466aac22e6a869134be3d09b9e89232fc2c2289/, run_show_command)
  end

  private
    def run_edit_command(editor: "cat")
      Dir.chdir(app_path) { `EDITOR="#{editor}" bin/rails secrets:edit` }
    end

    def run_show_command
      Dir.chdir(app_path) { `bin/rails secrets:show` }
    end

    def run_setup_command
      Dir.chdir(app_path) { `bin/rails secrets:setup` }
    end
end