-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathEnvReaderTest.cs
314 lines (287 loc) · 8.7 KB
/
EnvReaderTest.cs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright (c) Mohamed Hassan & Contributors. All rights reserved. See License.md in the project root for license information.
namespace OData2Poco.Tests;
using InfraStructure.FileSystem;
public class EnvReaderTest : BaseTest
{
#region Setup
[OneTimeSetUp]
public void SetUp()
{
CreateEnv("aa", "abc123");
CreateEnv("myToken", "abc123");
CreateEnv("key1", "abc");
CreateEnv("user", "user1");
CreateEnv("password", "secret");
CreateEnv("zone", "north");
CreateEnv("client_id", "a-123-456");
CreateEnv("client_secret", "abc-456-xyz");
}
[OneTimeTearDown]
public void TearDown()
{
DelEnv("aa", "myToken", "key1", "user", "password", "zone", "client_id", "client_secret");
}
#endregion
[Test]
[TestCase("client_secret")]
[TestCase("myToken")]
[TestCase("client_id")]
public void Arg_with_existing_env_test(string arg)
{
//Arrange
//Act
var flag = new EnvReader(_fileSystem)
.TryReadEnv(arg, out var value);
//Assert
flag.Should().BeTrue();
value.Should().NotBeEmpty();
}
[Test]
#if IsWindows
[TestCase("%client_secret%")]
[TestCase("%myToken%")]
[TestCase("%client_id%")]
#else
[TestCase("$client_secret")]
[TestCase("$myToken")]
[TestCase("$client_id")]
#endif
public void ResolveEnv_with_existing_env_test(string arg)
{
//Arrange
//Act
var text = new EnvReader(_fileSystem)
.ResolveEnv(arg, out var errors);
//Assert
text.Should().NotBeEmpty();
errors.Count.Should().Be(0);
}
[Test]
public void Args_without_env_vars()
{
//Arrange
string[] args = ["-a", "aa", "-b"];
//Act
var resolvedArgs = new EnvReader(_fileSystem)
.ResolveArgEnv(args, out var errors);
//Assert
resolvedArgs.Length.Should().Be(3);
resolvedArgs.Should().BeEquivalentTo(args);
errors.Should().BeEmpty();
}
[Test]
[TestCase("%aa%")]
[TestCase("$aa")]
public void Args_wit_env_exist_test(string envName)
{
//Arrange
string[] args = ["-a", envName, "-b"];
//Act
var sut = new EnvReader(_fileSystem)
.ResolveArgEnv(args, out var errors);
//Assert
sut.Length.Should().Be(3);
sut.Should().BeEquivalentTo("-a", "abc123", "-b");
errors.Count.Should().Be(0);
}
[Test]
[TestCase("%aa%")]
[TestCase("$aa")]
public void Args_wit_expression_have_env_var_exist_test(string envName)
{
//Arrange
string[] args = ["-a", $"Agent {envName}", "-b"];
//Act
var sut = new EnvReader(_fileSystem)
.ResolveArgEnv(args, out var errors);
//Assert
sut.Length.Should().Be(3);
sut.Should().BeEquivalentTo("-a", "Agent abc123", "-b");
errors.Count.Should().Be(0);
}
[Test]
public void Args_with_env_var_not_exist_test()
{
//Arrange
string[] args = ["-a", "%aa2%", "-b"];
//Act
var sut = new EnvReader(_fileSystem)
.ResolveArgEnv(args, out var errors);
//Assert
sut.Length.Should().Be(3);
sut.Should().BeEquivalentTo(args);
errors.Count.Should().Be(1);
}
[Test]
public void Arg_as_at_at_file_exist_test()
{
//Arrange
var text = "abc.123.xyz";
Fakes.Mock("myFile.txt", text);
var arg = "@@myFile.txt";
//Act
var flag = new EnvReader(_fileSystem)
.TryResolveFile(arg, out var value, out var error);
Fakes.Remove("myFile.txt");
//Assert
flag.Should().BeTrue();
value.Should().Be("abc.123.xyz");
error.Should().BeNull();
}
[Test]
public void Args_wit_at_at_file_exist_including_env_var_test()
{
//Arrange
var text = "abc.123.xyz";
Fakes.Mock("myFile.txt", text);
string[] args = ["-a", "@@myFile.txt", "-p", "%password%"];
//Act
var sut = new EnvReader(_fileSystem)
.ResolveArgEnv(args, out var errors);
Fakes.Remove("myFile.txt");
//Assert
sut.Length.Should().Be(4);
sut.Should().BeEquivalentTo("-a", "abc.123.xyz", "-p", "secret");
errors.Count.Should().Be(0);
}
[Test]
public void Args_wit_at_at_file_not_exist_test()
{
//Arrange
string[] args = ["-a", "@@file.txt", "-b"];
//Act
var sut = new EnvReader(_fileSystem)
.ResolveArgEnv(args, out var errors);
//Assert
sut.Length.Should().Be(3);
sut.Should().BeEquivalentTo(args);
errors.Count.Should().Be(1);
}
[Test]
[TestCase("%myToken%")]
[TestCase("$myToken")]
public void Resolve_arg_expression_with_exist_env_var_test(string envVar)
{
//Arrange
var input = $"Authorization=Bearer {envVar}";
var expected = "Authorization=Bearer abc123";
//Act
var sut = new EnvReader(_fileSystem);
var text = sut.ResolveEnv(input, out var errors);
//Assert
text.Should().Be(expected);
errors.Count.Should().Be(0);
}
[Test]
[TestCase("%myToken2%")]
[TestCase("$myToken2")]
public void Arg_expression_with_not_exist_env_var_test(string envVar)
{
//Arrange
var input = $"Authorization=Bearer {envVar}";
//Act
var sut = new EnvReader(_fileSystem);
var text = sut.ResolveEnv(input, out var errors);
//Assert
text.Should().Be(input);
errors.Count.Should().Be(1);
}
[Test]
public void Arg_has_dollar_sign_should_be_kept_if_no_env_var()
{
//Arrange
var input = "Authorization=Bearer $key1:$key2";
//Act
var sut = new EnvReader(_fileSystem);
var text = sut.ResolveEnv(input, out var errors);
//Assert
text.Should().Be("Authorization=Bearer abc:$key2");
errors.Count.Should().Be(1);
}
[Test]
[TestCase("Authorization=Basic %user%:%password%")]
[TestCase("Authorization=Basic $user:$password")]
public void Arg_with_multiple_env_var_existing_test(string input)
{
//Arrange
//Act
var sut = new EnvReader(_fileSystem);
var text = sut.ResolveEnv(input, out var errors);
//Assert
text.Should().Be("Authorization=Basic user1:secret");
errors.Count.Should().Be(0);
}
[Test]
[TestCase("Authorization=Basic %user2%:%password2%")]
[TestCase("Authorization=Basic $user2:$password2")]
public void Arg_with_multiple_env_var_not_existing_test(string input)
{
//Arrange
var sut = new EnvReader(_fileSystem);
//Act
var text = sut.ResolveEnv(input, out var errors);
//Assert
text.Should().Be(input);
errors.Count.Should().Be(2);
}
[Test]
public void Resolve_env_var_include_newline_test()
{
//Arrange
var input = @"
-r http://myservice/$zone/v1
-auth token
-p $myToken
-u $user1
";
var expected = @"
-r http://myservice/north/v1
-auth token
-p abc123
-u $user1
";
//Act
var sut = new EnvReader(_fileSystem);
var text = sut.ResolveEnv(input, out var errors);
//Assert
text.Trim().Should().Be(expected.Trim());
errors.Count.Should().Be(1);
}
[Test]
public void Configuration_file_including_env_var_test()
{
//Arrange
var cmd = """
-r http://localhost/odata
--auth oauth2
-u %client_id%
-p %client_secret%
--token-endpoint https://dev-identity.com/oauth/token
--token-params audience=https://www.todo.com
-v
""";
var expected = new[]
{
"-r", "http://localhost/odata", "--auth", "oauth2", "-u", "a-123-456", "-p", "abc-456-xyz",
"--token-endpoint", "https://dev-identity.com/oauth/token", "--token-params",
"audience=https://www.todo.com", "-v"
};
Fakes.Mock("myFile.txt", cmd);
//Act
var cfg = new OptionConfiguration(_fileSystem);
var args = new[]
{
"@myFile.txt"
};
var flag = cfg.TryGetConfigurationFile(args, out var args2, out var error, out var fileName);
var newArgs2 = new EnvReader(_fileSystem)
.ResolveArgEnv(args2, out var errors);
Fakes.Remove("myFile.txt");
//Assert
flag.Should().BeTrue();
newArgs2.Should().BeEquivalentTo(expected);
errors.Should().BeEmpty();
error.Should().BeNull();
fileName.Should().Be("myFile.txt");
}
}