@@ -22,7 +22,7 @@ class Rails::Command::CredentialsCommandTest < ActiveSupport::TestCase
22
22
test "edit credentials" do
23
23
# Run twice to ensure credentials can be reread after first edit pass.
24
24
2 . times do
25
- assert_match ( /access_key_id: 123/ , run_edit_command )
25
+ assert_match DEFAULT_CREDENTIALS_PATTERN , run_edit_command
26
26
end
27
27
end
28
28
@@ -36,11 +36,11 @@ class Rails::Command::CredentialsCommandTest < ActiveSupport::TestCase
36
36
end
37
37
38
38
test "edit command does not overwrite by default if credentials already exists" do
39
- run_edit_command ( editor : 'ruby -e "File.write ARGV[0], %(api_key: abc)"' )
40
- assert_match ( /api_key: abc/ , run_show_command )
39
+ write_credentials "foo: bar"
40
+ output = run_edit_command
41
41
42
- run_edit_command
43
- assert_match ( /api_key: abc/ , run_show_command )
42
+ assert_match %r/foo: bar/ , output
43
+ assert_no_match DEFAULT_CREDENTIALS_PATTERN , output
44
44
end
45
45
46
46
test "edit command does not add master key when `RAILS_MASTER_KEY` env specified" do
@@ -49,48 +49,51 @@ class Rails::Command::CredentialsCommandTest < ActiveSupport::TestCase
49
49
FileUtils . rm ( "config/master.key" )
50
50
51
51
switch_env ( "RAILS_MASTER_KEY" , key ) do
52
- assert_match ( /access_key_id: 123/ , run_edit_command )
52
+ assert_match DEFAULT_CREDENTIALS_PATTERN , run_edit_command
53
53
assert_not File . exist? ( "config/master.key" )
54
54
end
55
55
end
56
56
end
57
57
58
58
test "edit command modifies file specified by environment option" do
59
- assert_match ( /access_key_id: 123/ , run_edit_command ( environment : "production" ) )
60
- Dir . chdir ( app_path ) do
61
- assert File . exist? ( "config/credentials/production.key" )
62
- assert File . exist? ( "config/credentials/production.yml.enc" )
63
- end
59
+ remove_file "config/credentials.yml.enc"
60
+
61
+ assert_match DEFAULT_CREDENTIALS_PATTERN , run_edit_command ( environment : "production" )
62
+
63
+ assert_no_file "config/credentials.yml.enc"
64
+ assert_file "config/credentials/production.key"
65
+ assert_file "config/credentials/production.yml.enc"
64
66
end
65
67
66
68
test "edit command properly expands environment option" do
67
- assert_match ( /access_key_id: 123/ , run_edit_command ( environment : "prod" ) )
68
- Dir . chdir ( app_path ) do
69
- assert File . exist? ( "config/credentials/production.key" )
70
- assert File . exist? ( "config/credentials/production.yml.enc" )
71
- end
69
+ remove_file "config/credentials.yml.enc"
70
+
71
+ assert_match DEFAULT_CREDENTIALS_PATTERN , run_edit_command ( environment : "prod" )
72
+
73
+ assert_no_file "config/credentials.yml.enc"
74
+ assert_file "config/credentials/production.key"
75
+ assert_file "config/credentials/production.yml.enc"
72
76
end
73
77
74
78
test "edit command does not raise when an initializer tries to access non-existent credentials" do
75
79
app_file "config/initializers/raise_when_loaded.rb" , <<-RUBY
76
80
Rails.application.credentials.missing_key!
77
81
RUBY
78
82
79
- assert_match ( /access_key_id: 123/ , run_edit_command ( environment : "qa" ) )
83
+ assert_match DEFAULT_CREDENTIALS_PATTERN , run_edit_command ( environment : "qa" )
80
84
end
81
85
82
- test "edit command generates template file when the file does not exist" do
83
- FileUtils . rm ( "#{ app_path } /config/credentials.yml.enc" )
84
- run_edit_command
86
+ test "edit command generates credentials file when it does not exist" do
87
+ remove_file "config/credentials.yml.enc"
85
88
86
- output = run_show_command
87
- assert_match ( /access_key_id: 123/ , output )
88
- assert_match ( /secret_key_base/ , output )
89
+ assert_match DEFAULT_CREDENTIALS_PATTERN , run_edit_command
90
+
91
+ assert_file "config/credentials.yml.enc"
89
92
end
90
93
91
94
92
95
test "show credentials" do
93
- assert_match ( /access_key_id: 123/ , run_show_command )
96
+ assert_match DEFAULT_CREDENTIALS_PATTERN , run_show_command
94
97
end
95
98
96
99
test "show command raises error when require_master_key is specified and key does not exist" do
@@ -108,17 +111,15 @@ class Rails::Command::CredentialsCommandTest < ActiveSupport::TestCase
108
111
end
109
112
110
113
test "show command displays content specified by environment option" do
111
- run_edit_command ( environment : "production" )
114
+ write_credentials "foo: bar" , environment : "production"
112
115
113
- assert_match ( /access_key_id: 123 /, run_show_command ( environment : "production" ) )
116
+ assert_match %r/foo: bar /, run_show_command ( environment : "production" )
114
117
end
115
118
116
119
test "show command properly expands environment option" do
117
- run_edit_command ( environment : "production" )
120
+ write_credentials "foo: bar" , environment : "production"
118
121
119
- output = run_show_command ( environment : "prod" )
120
- assert_match ( /access_key_id: 123/ , output )
121
- assert_no_match ( /secret_key_base/ , output )
122
+ assert_match %r/foo: bar/ , run_show_command ( environment : "prod" )
122
123
end
123
124
124
125
@@ -213,6 +214,8 @@ class Rails::Command::CredentialsCommandTest < ActiveSupport::TestCase
213
214
end
214
215
215
216
private
217
+ DEFAULT_CREDENTIALS_PATTERN = /access_key_id: 123\n .*secret_key_base: \h {128}\n /m
218
+
216
219
def run_edit_command ( editor : "cat" , environment : nil , **options )
217
220
switch_env ( "EDITOR" , editor ) do
218
221
args = environment ? [ "--environment" , environment ] : [ ]
@@ -229,4 +232,18 @@ def run_diff_command(path = nil, enroll: nil, disenroll: nil, **options)
229
232
args = [ path , ( "--enroll" if enroll ) , ( "--disenroll" if disenroll ) ] . compact
230
233
rails "credentials:diff" , args , **options
231
234
end
235
+
236
+ def write_credentials ( content , **options )
237
+ switch_env ( "CONTENT" , content ) do
238
+ run_edit_command ( editor : %(ruby -e "File.write ARGV[0], ENV['CONTENT']") , **options )
239
+ end
240
+ end
241
+
242
+ def assert_file ( relative )
243
+ assert File . exist? ( app_path ( relative ) ) , "Expected file #{ relative . inspect } to exist, but it does not"
244
+ end
245
+
246
+ def assert_no_file ( relative )
247
+ assert_not File . exist? ( app_path ( relative ) ) , "Expected file #{ relative . inspect } to not exist, but it does"
248
+ end
232
249
end
0 commit comments