public async void TestSuccess_AddKVSecret() { var moqHandler = NewMoqHttpHandler(HttpStatusCode.OK); // Make the method call var httpClient = new HttpClient(moqHandler.Object); VaultCom vcom = new VaultCom("http://test.com", "vault_token", "vault_path", httpClient); bool callResult = await vcom.AddKVSecret("test-secret", correctSecretData); // Verify returned result Assert.IsType <bool>(callResult); Assert.True(callResult); // Verify the call to the API Uri expectedUri = new Uri("http://test.com/v1/vault_path/data/test-secret"); moqHandler.Protected().Verify( "SendAsync", Times.Once(), ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Post && req.RequestUri == expectedUri && req.Headers.Contains("X-Vault-Token") && new List <string>(req.Headers.GetValues("X-Vault-Token")).Contains("vault_token") ), ItExpr.IsAny <CancellationToken>() ); }
public async void TestFailure_AddKVSecret() { var moqHandler = NewMoqHttpHandler(HttpStatusCode.BadRequest); // Make the method call var httpClient = new HttpClient(moqHandler.Object); VaultCom vcom = new VaultCom("http://test.com", "vault_token", "vault_path", httpClient); bool callResult = await vcom.AddKVSecret("test-secret", correctSecretData); // Verify returned result Assert.IsType <bool>(callResult); Assert.False(callResult); }
///<summary> /// Adds or updates secret based on user input ///</summary> public void AddSecret() { // Gather user input Console.WriteLine(); // Get secret name string secretName = GetSecretName(); // Get number of KV pairs in the secret uint kvNum = 0; while (kvNum <= 0) { Console.Write("Enter number of KV pairs: "); uint.TryParse(userInput.GetUserInput(), out kvNum); } // Get the KV pairs var secretData = new Dictionary <string, string>(); for (uint i = 1; i <= kvNum; i++) { Console.WriteLine(); string key = String.Empty; string value = String.Empty; // Get key, ensuring it does not exist while (String.IsNullOrEmpty(key)) { Console.Write("Enter key{0}: ", i); key = userInput.GetUserInput(); if (secretData.ContainsKey(key)) { Console.Error.WriteLine("Key already exists!"); key = string.Empty; } } Console.WriteLine(); // Get Vaule while (String.IsNullOrEmpty(value)) { Console.Write("Enter value{0}: ", i); value = userInput.GetUserInput(); } secretData.Add(key, value); } // Call vault API and handle response Console.WriteLine(); if (vaultCommunicator.AddKVSecret(secretName, secretData).Result) { Console.WriteLine("Secret written successfully!"); } else { Console.WriteLine("Writing secret failed!"); } Console.Write("Press any key to continue..."); userInput.GetUserInput(); }