|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Collections.ObjectModel; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.Net; |
| 9 | +using System.Net.Http; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using System.Web.Http.Results; |
| 12 | +using Microsoft.Azure.WebJobs.Script.Config; |
| 13 | +using Microsoft.Azure.WebJobs.Script.Description; |
| 14 | +using Microsoft.Azure.WebJobs.Script.WebHost; |
| 15 | +using Microsoft.Azure.WebJobs.Script.WebHost.Controllers; |
| 16 | +using Microsoft.Azure.WebJobs.Script.WebHost.Models; |
| 17 | +using Moq; |
| 18 | +using Newtonsoft.Json.Linq; |
| 19 | +using WebJobs.Script.Tests; |
| 20 | +using Xunit; |
| 21 | + |
| 22 | +namespace Microsoft.Azure.WebJobs.Script.Tests |
| 23 | +{ |
| 24 | + public class KeysControllerTests : IDisposable |
| 25 | + { |
| 26 | + private readonly ScriptSettingsManager _settingsManager; |
| 27 | + private readonly TempDirectory _secretsDirectory = new TempDirectory(); |
| 28 | + private Mock<ScriptHost> _hostMock; |
| 29 | + private Mock<WebScriptHostManager> _managerMock; |
| 30 | + private Collection<FunctionDescriptor> _testFunctions; |
| 31 | + private Dictionary<string, Collection<string>> _testFunctionErrors; |
| 32 | + private KeysController _testController; |
| 33 | + private Mock<ISecretManager> _secretsManagerMock; |
| 34 | + |
| 35 | + public KeysControllerTests() |
| 36 | + { |
| 37 | + _settingsManager = ScriptSettingsManager.Instance; |
| 38 | + _testFunctions = new Collection<FunctionDescriptor>(); |
| 39 | + _testFunctionErrors = new Dictionary<string, Collection<string>>(); |
| 40 | + |
| 41 | + var config = new ScriptHostConfiguration(); |
| 42 | + var environment = new NullScriptHostEnvironment(); |
| 43 | + _hostMock = new Mock<ScriptHost>(MockBehavior.Strict, new object[] { environment, config, null }); |
| 44 | + _hostMock.Setup(p => p.Functions).Returns(_testFunctions); |
| 45 | + _hostMock.Setup(p => p.FunctionErrors).Returns(_testFunctionErrors); |
| 46 | + |
| 47 | + WebHostSettings settings = new WebHostSettings(); |
| 48 | + settings.SecretsPath = _secretsDirectory.Path; |
| 49 | + _secretsManagerMock = new Mock<ISecretManager>(MockBehavior.Strict); |
| 50 | + _managerMock = new Mock<WebScriptHostManager>(MockBehavior.Strict, new object[] { config, new TestSecretManagerFactory(_secretsManagerMock.Object), _settingsManager, settings }); |
| 51 | + _managerMock.SetupGet(p => p.Instance).Returns(_hostMock.Object); |
| 52 | + |
| 53 | + var traceWriter = new TestTraceWriter(TraceLevel.Verbose); |
| 54 | + |
| 55 | + _testController = new KeysController(_managerMock.Object, _secretsManagerMock.Object, traceWriter); |
| 56 | + |
| 57 | + // setup some test functions |
| 58 | + string errorFunction = "ErrorFunction"; |
| 59 | + var errors = new Collection<string>(); |
| 60 | + errors.Add("A really really bad error!"); |
| 61 | + _testFunctionErrors.Add(errorFunction, errors); |
| 62 | + |
| 63 | + var keys = new Dictionary<string, string> |
| 64 | + { |
| 65 | + { "key1", "secret1" } |
| 66 | + }; |
| 67 | + _secretsManagerMock.Setup(p => p.GetFunctionSecretsAsync(errorFunction, false)).ReturnsAsync(keys); |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public async Task GetKeys_FunctionInError_ReturnsKeys() |
| 72 | + { |
| 73 | + _testController.Request = new HttpRequestMessage(HttpMethod.Get, "https://local/admin/functions/keys"); |
| 74 | + var result = (OkNegotiatedContentResult<ApiModel>)(await _testController.Get("ErrorFunction")); |
| 75 | + |
| 76 | + var content = (JObject)result.Content; |
| 77 | + var keys = content["keys"]; |
| 78 | + Assert.Equal("key1", keys[0]["name"]); |
| 79 | + Assert.Equal("secret1", keys[0]["value"]); |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public async Task PutKey_FunctionInError_Succeeds() |
| 84 | + { |
| 85 | + _testController.Request = new HttpRequestMessage(HttpMethod.Get, "https://local/admin/functions/keys/key2"); |
| 86 | + |
| 87 | + var key = new Key("key2", "secret2"); |
| 88 | + var keyOperationResult = new KeyOperationResult(key.Value, OperationResult.Updated); |
| 89 | + _secretsManagerMock.Setup(p => p.AddOrUpdateFunctionSecretAsync(key.Name, key.Value, "ErrorFunction")).ReturnsAsync(keyOperationResult); |
| 90 | + |
| 91 | + var result = (OkNegotiatedContentResult<ApiModel>)(await _testController.Put("ErrorFunction", key.Name, key)); |
| 92 | + var content = (JObject)result.Content; |
| 93 | + Assert.Equal("key2", content["name"]); |
| 94 | + Assert.Equal("secret2", content["value"]); |
| 95 | + } |
| 96 | + |
| 97 | + [Fact] |
| 98 | + public async Task DeleteKey_FunctionInError_Succeeds() |
| 99 | + { |
| 100 | + _testController.Request = new HttpRequestMessage(HttpMethod.Get, "https://local/admin/functions/keys/key2"); |
| 101 | + |
| 102 | + _secretsManagerMock.Setup(p => p.DeleteSecretAsync("key2", "ErrorFunction")).ReturnsAsync(true); |
| 103 | + |
| 104 | + var result = (StatusCodeResult)(await _testController.Delete("ErrorFunction", "key2")); |
| 105 | + Assert.Equal(HttpStatusCode.NoContent, result.StatusCode); |
| 106 | + } |
| 107 | + |
| 108 | + protected virtual void Dispose(bool disposing) |
| 109 | + { |
| 110 | + if (disposing) |
| 111 | + { |
| 112 | + _secretsDirectory.Dispose(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public void Dispose() |
| 117 | + { |
| 118 | + // Do not change this code. Put cleanup code in Dispose(bool disposing) above. |
| 119 | + Dispose(true); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments