aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/generators/encrypted_secrets_generator_test.rb
blob: 205827e4989311588bdc34c7bccf7bdd777f52d5 (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
# frozen_string_literal: true

require "generators/generators_test_helper"
require "rails/generators/rails/encrypted_secrets/encrypted_secrets_generator"

class EncryptedSecretsGeneratorTest < Rails::Generators::TestCase
  include GeneratorsTestHelper

  def setup
    super
    cd destination_root
  end

  def test_generates_key_file_and_encrypted_secrets_file
    run_generator

    assert_file "config/secrets.yml.key", /\w+/

    assert File.exist?("config/secrets.yml.enc")
    assert_no_match(/production:\n#  external_api_key: \w+/, IO.binread("config/secrets.yml.enc"))
    assert_match(/production:\n#  external_api_key: \w+/, Rails::Secrets.read)
  end

  def test_appends_to_gitignore
    FileUtils.touch(".gitignore")

    run_generator

    assert_file ".gitignore", /config\/secrets.yml.key/, /(?!config\/secrets.yml.enc)/
  end

  def test_warns_when_ignore_is_missing
    assert_match(/Add this to your ignore file/i, run_generator)
  end

  def test_doesnt_generate_a_new_key_file_if_already_opted_in_to_encrypted_secrets
    FileUtils.mkdir("config")
    File.open("config/secrets.yml.enc", "w") { |f| f.puts "already secrety" }

    run_generator

    assert_no_file "config/secrets.yml.key"
  end
end