// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.IO; using System.Collections.Generic; using Microsoft.Extensions.SecretManager.Tools.Internal; using Microsoft.Extensions.Logging; using Xunit; using System; namespace Microsoft.Extensions.SecretManager.Tools.Tests { public class SetCommandTest { [Fact] public void SetsFromPipedInput() { var input = @" { ""Key1"": ""str value"", ""Key2"": 1234, ""Key3"": false }"; var testConsole = new TestConsole { IsInputRedirected = true, In = new StringReader(input) }; var secretStore = new TestSecretsStore(); var command = new SetCommand(); command.Execute(new CommandContext(secretStore, NullLogger.Instance, 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 { IsInputRedirected = true, In = new StringReader(input) }; var secretStore = new TestSecretsStore(); var command = new SetCommand(); command.Execute(new CommandContext(secretStore, NullLogger.Instance, 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 { IsInputRedirected = true, In = new StringReader("") }; var secretStore = new TestSecretsStore(); var command = new SetCommand("key", null); var ex = Assert.Throws( () => command.Execute(new CommandContext(secretStore, NullLogger.Instance, testConsole))); Assert.Equal(Resources.FormatError_MissingArgument("value"), ex.Message); } private class TestSecretsStore : SecretsStore { public TestSecretsStore() : base("xyz", NullLogger.Instance) { } protected override IDictionary Load(string userSecretsId) { return new Dictionary(); } public override void Save() { // noop } } } public class NullLogger : ILogger { public static NullLogger Instance = new NullLogger(); private class NullScope : IDisposable { public void Dispose() { } } public IDisposable BeginScope(TState state) => new NullScope(); public bool IsEnabled(LogLevel logLevel) => true; public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) { } } }