-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathSetCommandTest.cs
104 lines (89 loc) · 2.91 KB
/
SetCommandTest.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.IO;
using Microsoft.Extensions.SecretManager.Tools.Internal;
using Microsoft.Extensions.Tools.Internal;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.Extensions.SecretManager.Tools.Tests;
public class SetCommandTest
{
private readonly ITestOutputHelper _output;
public SetCommandTest(ITestOutputHelper output)
{
_output = output;
}
[Fact]
public void SetsFromPipedInput()
{
var input = @"
{
""Key1"": ""str value"",
""Key2"": 1234,
""Key3"": false
}";
var testConsole = new TestConsole(_output)
{
IsInputRedirected = true,
In = new StringReader(input)
};
var secretStore = new TestSecretsStore(_output);
var command = new SetCommand.FromStdInStrategy();
command.Execute(new CommandContext(secretStore, new TestReporter(_output), testConsole));
Assert.Equal(3, secretStore.Count);
Assert.Equal("str value", secretStore["Key1"]);
Assert.Equal("1234", secretStore["Key2"]);
Assert.Equal("False", secretStore["Key3"]);
}
[Fact]
public void ParsesNestedObjects()
{
var input = @"
{
""Key1"": {
""nested"" : ""value""
},
""array"": [ 1, 2 ]
}";
var testConsole = new TestConsole(_output)
{
IsInputRedirected = true,
In = new StringReader(input)
};
var secretStore = new TestSecretsStore(_output);
var command = new SetCommand.FromStdInStrategy();
command.Execute(new CommandContext(secretStore, new TestReporter(_output), testConsole));
Assert.Equal(3, secretStore.Count);
Assert.True(secretStore.ContainsKey("Key1:nested"));
Assert.Equal("value", secretStore["Key1:nested"]);
Assert.Equal("1", secretStore["array:0"]);
Assert.Equal("2", secretStore["array:1"]);
}
[Fact]
public void OnlyPipesInIfNoArgs()
{
var testConsole = new TestConsole(_output)
{
IsInputRedirected = true,
In = new StringReader("")
};
var options = CommandLineOptions.Parse(new[] { "set", "key", "value" }, testConsole);
Assert.IsType<SetCommand.ForOneValueStrategy>(options.Command);
}
private class TestSecretsStore : SecretsStore
{
public TestSecretsStore(ITestOutputHelper output)
: base("xyz", new TestReporter(output))
{
}
protected override IDictionary<string, string> Load(string userSecretsId)
{
return new Dictionary<string, string>();
}
public override void Save()
{
// noop
}
}
}