|
1 | 1 | require_relative "../test_helper"
|
2 | 2 | require_relative "runner_helper"
|
| 3 | +require_relative "file_helper" |
3 | 4 |
|
4 | 5 | module Rails::AppEnv::FeaturesTest
|
5 | 6 | class CredentialsTest < ActiveSupport::TestCase
|
6 | 7 | include RunnerHelper
|
| 8 | + include FileHelper |
7 | 9 |
|
8 |
| - CREDENTIALS_PATH_TEST_CASES = [ |
9 |
| - { |
10 |
| - name: "is {APP_ENV}.yml.enc when both APP_ENV and RAILS_ENV are present", |
| 10 | + CREDENTIALS_PATH_TEST_CASES = { |
| 11 | + "is {APP_ENV}.yml.enc when both APP_ENV and RAILS_ENV are present": { |
11 | 12 | expected_path: "config/credentials/foo.yml.enc",
|
12 |
| - app_env: "foo", |
13 |
| - rails_env: "bar", |
14 |
| - secret_key_base_dummy: "1", |
15 |
| - touch_file: "config/credentials/foo.yml.enc" |
| 13 | + touch_file: "config/credentials/foo.yml.enc", |
| 14 | + env: { |
| 15 | + "APP_ENV" => "foo", |
| 16 | + "RAILS_ENV" => "bar" |
| 17 | + } |
16 | 18 | },
|
17 |
| - { |
18 |
| - name: "falls back to credentials.yml.enc when {APP_ENV}.yml.enc is absent and both APP_ENV and RAILS_ENV are present", |
| 19 | + "falls back to credentials.yml.enc when {APP_ENV}.yml.enc is absent and both APP_ENV and RAILS_ENV are present": { |
19 | 20 | expected_path: "config/credentials.yml.enc",
|
20 |
| - app_env: "foo", |
21 |
| - rails_env: "bar", |
22 |
| - secret_key_base_dummy: "1" |
| 21 | + env: { |
| 22 | + "APP_ENV" => "foo", |
| 23 | + "RAILS_ENV" => "bar" |
| 24 | + } |
23 | 25 | },
|
24 |
| - { |
25 |
| - name: "is {APP_ENV}.yml.enc when APP_ENV is present but RAILS_ENV is blank", |
| 26 | + "is {APP_ENV}.yml.enc when APP_ENV is present but RAILS_ENV is blank": { |
26 | 27 | expected_path: "config/credentials/foo.yml.enc",
|
27 |
| - app_env: "foo", |
28 |
| - touch_file: "config/credentials/foo.yml.enc" |
| 28 | + touch_file: "config/credentials/foo.yml.enc", |
| 29 | + env: { |
| 30 | + "APP_ENV" => "foo" |
| 31 | + } |
29 | 32 | },
|
30 |
| - { |
31 |
| - name: "falls back to credentials.yml.enc when {APP_ENV}.yml.enc is absent and APP_ENV is present but RAILS_ENV is blank", |
| 33 | + "falls back to credentials.yml.enc when {APP_ENV}.yml.enc is absent and APP_ENV is present but RAILS_ENV is blank": { |
32 | 34 | expected_path: "config/credentials.yml.enc",
|
33 |
| - app_env: "foo", |
34 |
| - secret_key_base_dummy: "1" |
| 35 | + env: { |
| 36 | + "APP_ENV" => "foo" |
| 37 | + } |
35 | 38 | },
|
36 |
| - { |
37 |
| - name: "is {RAILS_ENV}.yml.enc when APP_ENV is blank but RAILS_ENV is present", |
| 39 | + "is {RAILS_ENV}.yml.enc when APP_ENV is blank but RAILS_ENV is present": { |
38 | 40 | expected_path: "config/credentials/foo.yml.enc",
|
39 |
| - rails_env: "foo", |
40 |
| - secret_key_base_dummy: "1", |
41 |
| - touch_file: "config/credentials/foo.yml.enc" |
| 41 | + touch_file: "config/credentials/foo.yml.enc", |
| 42 | + env: { |
| 43 | + "RAILS_ENV" => "foo" |
| 44 | + } |
42 | 45 | },
|
43 |
| - { |
44 |
| - name: "falls back to credentials.yml.enc when {RAILS_ENV}.yml.enc is absent and when APP_ENV is blank but RAILS_ENV is present", |
| 46 | + "falls back to credentials.yml.enc when {RAILS_ENV}.yml.enc is absent and when APP_ENV is blank but RAILS_ENV is present": { |
45 | 47 | expected_path: "config/credentials.yml.enc",
|
46 |
| - rails_env: "foo", |
47 |
| - secret_key_base_dummy: "1" |
| 48 | + env: { |
| 49 | + "RAILS_ENV" => "foo" |
| 50 | + } |
48 | 51 | },
|
49 |
| - { |
50 |
| - name: "is {DEFAULT_RAILS_ENV}.yml.enc when both APP_ENV and RAILS_ENV are blank", |
| 52 | + "is {DEFAULT_RAILS_ENV}.yml.enc when both APP_ENV and RAILS_ENV are blank": { |
51 | 53 | expected_path: "config/credentials/#{DEFAULT_RAILS_ENV}.yml.enc",
|
52 | 54 | touch_file: "config/credentials/#{DEFAULT_RAILS_ENV}.yml.enc"
|
53 | 55 | },
|
54 |
| - { |
55 |
| - name: "falls back to credentials.yml.enc when {DEFAULT_RAILS_ENV}.yml.enc is absent and both APP_ENV and RAILS_ENV are blank", |
| 56 | + "falls back to credentials.yml.enc when {DEFAULT_RAILS_ENV}.yml.enc is absent and both APP_ENV and RAILS_ENV are blank": { |
56 | 57 | expected_path: "config/credentials.yml.enc"
|
57 | 58 | }
|
58 |
| - ] |
| 59 | + } |
59 | 60 |
|
60 |
| - KEY_PATH_TEST_CASES = [ |
61 |
| - { |
62 |
| - name: "is {APP_ENV}.key when both APP_ENV and RAILS_ENV are present", |
| 61 | + KEY_PATH_TEST_CASES = { |
| 62 | + "is {APP_ENV}.key when both APP_ENV and RAILS_ENV are present": { |
63 | 63 | expected_path: "config/credentials/foo.key",
|
64 |
| - app_env: "foo", |
65 |
| - rails_env: "bar", |
66 |
| - secret_key_base_dummy: "1", |
67 |
| - touch_file: "config/credentials/foo.key" |
| 64 | + touch_file: "config/credentials/foo.key", |
| 65 | + env: { |
| 66 | + "APP_ENV" => "foo", |
| 67 | + "RAILS_ENV" => "bar" |
| 68 | + } |
68 | 69 | },
|
69 |
| - { |
70 |
| - name: "falls back to master.key when {APP_ENV}.key is absent and both APP_ENV and RAILS_ENV are present", |
| 70 | + "falls back to master.key when {APP_ENV}.key is absent and both APP_ENV and RAILS_ENV are present": { |
71 | 71 | expected_path: "config/master.key",
|
72 |
| - app_env: "foo", |
73 |
| - rails_env: "bar", |
74 |
| - secret_key_base_dummy: "1" |
| 72 | + env: { |
| 73 | + "APP_ENV" => "foo", |
| 74 | + "RAILS_ENV" => "bar" |
| 75 | + } |
75 | 76 | },
|
76 |
| - { |
77 |
| - name: "is {APP_ENV}.key when APP_ENV is present but RAILS_ENV is blank", |
| 77 | + "is {APP_ENV}.key when APP_ENV is present but RAILS_ENV is blank": { |
78 | 78 | expected_path: "config/credentials/foo.key",
|
79 |
| - app_env: "foo", |
80 |
| - touch_file: "config/credentials/foo.key" |
| 79 | + touch_file: "config/credentials/foo.key", |
| 80 | + env: { |
| 81 | + "APP_ENV" => "foo" |
| 82 | + } |
81 | 83 | },
|
82 |
| - { |
83 |
| - name: "falls back to master.key when {APP_ENV}.key is absent and APP_ENV is present but RAILS_ENV is blank", |
| 84 | + "falls back to master.key when {APP_ENV}.key is absent and APP_ENV is present but RAILS_ENV is blank": { |
84 | 85 | expected_path: "config/master.key",
|
85 |
| - app_env: "foo" |
| 86 | + env: { |
| 87 | + "APP_ENV" => "foo" |
| 88 | + } |
86 | 89 | },
|
87 |
| - { |
88 |
| - name: "is {RAILS_ENV}.key when APP_ENV is blank but RAILS_ENV is present", |
| 90 | + "is {RAILS_ENV}.key when APP_ENV is blank but RAILS_ENV is present": { |
89 | 91 | expected_path: "config/credentials/foo.key",
|
90 |
| - rails_env: "foo", |
91 | 92 | touch_file: "config/credentials/foo.key",
|
92 |
| - secret_key_base_dummy: "1" |
| 93 | + env: { |
| 94 | + "RAILS_ENV" => "foo" |
| 95 | + } |
93 | 96 | },
|
94 |
| - { |
95 |
| - name: "falls back to master.key when {RAILS_ENV}.key is absent and when APP_ENV is blank but RAILS_ENV is present", |
| 97 | + "falls back to master.key when {RAILS_ENV}.key is absent and when APP_ENV is blank but RAILS_ENV is present": { |
96 | 98 | expected_path: "config/master.key",
|
97 |
| - rails_env: "foo", |
98 |
| - secret_key_base_dummy: "1" |
| 99 | + env: { |
| 100 | + "RAILS_ENV" => "foo" |
| 101 | + } |
99 | 102 | },
|
100 |
| - { |
101 |
| - name: "is {DEFAULT_RAILS_ENV}.key when both APP_ENV and RAILS_ENV are blank", |
| 103 | + "is {DEFAULT_RAILS_ENV}.key when both APP_ENV and RAILS_ENV are blank": { |
102 | 104 | expected_path: "config/credentials/#{DEFAULT_RAILS_ENV}.key",
|
103 | 105 | touch_file: "config/credentials/#{DEFAULT_RAILS_ENV}.key"
|
104 | 106 | },
|
105 |
| - { |
106 |
| - name: "falls back to master.key when {DEFAULT_RAILS_ENV}.key is absent and both APP_ENV and RAILS_ENV are blank", |
| 107 | + "falls back to master.key when {DEFAULT_RAILS_ENV}.key is absent and both APP_ENV and RAILS_ENV are blank": { |
107 | 108 | expected_path: "config/master.key"
|
108 | 109 | }
|
109 |
| - ] |
| 110 | + } |
110 | 111 |
|
111 |
| - CREDENTIALS_PATH_TEST_CASES.each_with_index do |args, i| |
| 112 | + CREDENTIALS_PATH_TEST_CASES.each_key do |name| |
112 | 113 | class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
113 |
| - test "content path #{args[:name]}" do |
114 |
| - assert_credentials_path(**CREDENTIALS_PATH_TEST_CASES[#{i}]) |
| 114 | + test "content path #{name}" do |
| 115 | + assert_credentials_path(**CREDENTIALS_PATH_TEST_CASES[:"#{name}"]) |
115 | 116 | end
|
116 | 117 | RUBY
|
117 | 118 | end
|
118 | 119 |
|
119 |
| - KEY_PATH_TEST_CASES.each_with_index do |args, i| |
| 120 | + KEY_PATH_TEST_CASES.each_key do |name| |
120 | 121 | class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
121 |
| - test "key path #{args[:name]}" do |
122 |
| - assert_key_path(**KEY_PATH_TEST_CASES[#{i}]) |
| 122 | + test "key path #{name}" do |
| 123 | + assert_key_path(**KEY_PATH_TEST_CASES[:"#{name}"]) |
123 | 124 | end
|
124 | 125 | RUBY
|
125 | 126 | end
|
126 | 127 |
|
127 | 128 | private
|
128 | 129 |
|
129 |
| - def assert_credentials_path(expected_path:, **options) |
130 |
| - assert_runner Rails.root.join(expected_path), "Rails.configuration.credentials.content_path", **options |
| 130 | + def assert_credentials_path(expected_path:, touch_file: nil, **options) |
| 131 | + assert_runner_puts_with_file Rails.root.join(expected_path), "Rails.configuration.credentials.content_path", touch_file, **options |
| 132 | + end |
| 133 | + |
| 134 | + def assert_key_path(expected_path:, touch_file: nil, **options) |
| 135 | + assert_runner_puts_with_file Rails.root.join(expected_path), "Rails.configuration.credentials.key_path", touch_file, **options |
131 | 136 | end
|
132 | 137 |
|
133 |
| - def assert_key_path(expected_path:, **options) |
134 |
| - assert_runner Rails.root.join(expected_path), "Rails.configuration.credentials.key_path", **options |
| 138 | + def assert_runner_puts_with_file(expected, subject, file_path, **options) |
| 139 | + with_file(file_path) do |
| 140 | + assert_runner_puts expected, subject, **options |
| 141 | + end |
135 | 142 | end
|
136 | 143 | end
|
137 | 144 | end
|
0 commit comments