|
| 1 | +using Microsoft.AspNetCore.Http; |
| 2 | +using Microsoft.AspNetCore.Mvc; |
| 3 | +using Microsoft.Extensions.Options; |
| 4 | +using Moq; |
| 5 | +using Vecc.K8s.MultiCluster.Api.Controllers; |
| 6 | +using Vecc.K8s.MultiCluster.Api.Models.Api; |
| 7 | +using Vecc.K8s.MultiCluster.Api.Services; |
| 8 | +using Vecc.K8s.MultiCluster.Api.Services.Authentication; |
| 9 | + |
| 10 | +namespace Vecc.K8s.MultiCluster.Api.Tests.Controllers |
| 11 | +{ |
| 12 | + public class AuthenticationControllerTests |
| 13 | + { |
| 14 | + private readonly Mock<IOptions<ApiAuthenticationHandlerOptions>> _authOptionsMock; |
| 15 | + private readonly Mock<IOptions<MultiClusterOptions>> _optionsMock; |
| 16 | + private readonly ApiAuthenticationHasher _hasher; |
| 17 | + private readonly AuthenticationController _controller; |
| 18 | + |
| 19 | + public AuthenticationControllerTests() |
| 20 | + { |
| 21 | + _authOptionsMock = new Mock<IOptions<ApiAuthenticationHandlerOptions>>(); |
| 22 | + _optionsMock = new Mock<IOptions<MultiClusterOptions>>(); |
| 23 | + |
| 24 | + var authOptions = new ApiAuthenticationHandlerOptions |
| 25 | + { |
| 26 | + ApiKeys = new[] |
| 27 | + { |
| 28 | + new ApiKey { ClusterIdentifier = "cluster1", Key = "key1" } |
| 29 | + } |
| 30 | + }; |
| 31 | + _authOptionsMock.Setup(x => x.Value).Returns(authOptions); |
| 32 | + |
| 33 | + var multiClusterOptions = new MultiClusterOptions |
| 34 | + { |
| 35 | + ClusterIdentifier = "local-cluster", |
| 36 | + ClusterSalt = new byte[] { 1, 2, 3, 4 } |
| 37 | + }; |
| 38 | + _optionsMock.Setup(x => x.Value).Returns(multiClusterOptions); |
| 39 | + |
| 40 | + _hasher = new ApiAuthenticationHasher(_optionsMock.Object); |
| 41 | + |
| 42 | + _controller = new AuthenticationController(_authOptionsMock.Object, _hasher, _optionsMock.Object); |
| 43 | + _controller.ControllerContext = new ControllerContext |
| 44 | + { |
| 45 | + HttpContext = new DefaultHttpContext() |
| 46 | + }; |
| 47 | + _controller.ControllerContext.HttpContext.Request.Scheme = "https"; |
| 48 | + _controller.ControllerContext.HttpContext.Request.Host = new HostString("test.example.com"); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public async Task Auth_WithIdentifier_UsesProvidedIdentifier() |
| 53 | + { |
| 54 | + var result = await _controller.Auth("my-cluster"); |
| 55 | + |
| 56 | + var okResult = Assert.IsType<OkObjectResult>(result.Result); |
| 57 | + var model = Assert.IsType<NewAuthModel>(okResult.Value); |
| 58 | + Assert.Contains("my-cluster", model.LocalAuthModel.ConfigMap.ClusterIdentifier); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public async Task Auth_WithoutIdentifier_GeneratesGuidIdentifier() |
| 63 | + { |
| 64 | + var result = await _controller.Auth(null); |
| 65 | + |
| 66 | + var okResult = Assert.IsType<OkObjectResult>(result.Result); |
| 67 | + var model = Assert.IsType<NewAuthModel>(okResult.Value); |
| 68 | + Assert.False(string.IsNullOrWhiteSpace(model.LocalAuthModel.ConfigMap.ClusterIdentifier)); |
| 69 | + Assert.Contains("Authentication__ApiKeys__", model.LocalAuthModel.ConfigMap.ClusterIdentifier); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public async Task Auth_WithEmptyString_GeneratesGuidIdentifier() |
| 74 | + { |
| 75 | + var result = await _controller.Auth(""); |
| 76 | + |
| 77 | + var okResult = Assert.IsType<OkObjectResult>(result.Result); |
| 78 | + var model = Assert.IsType<NewAuthModel>(okResult.Value); |
| 79 | + Assert.False(string.IsNullOrWhiteSpace(model.LocalAuthModel.ConfigMap.ClusterIdentifier)); |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public async Task Auth_ReturnsCorrectNextIndex_WhenApiKeysExist() |
| 84 | + { |
| 85 | + var result = await _controller.Auth("test"); |
| 86 | + |
| 87 | + var okResult = Assert.IsType<OkObjectResult>(result.Result); |
| 88 | + var model = Assert.IsType<NewAuthModel>(okResult.Value); |
| 89 | + Assert.StartsWith("Authentication__ApiKeys__1__ClusterIdentifier:", model.LocalAuthModel.ConfigMap.ClusterIdentifier); |
| 90 | + } |
| 91 | + |
| 92 | + [Fact] |
| 93 | + public async Task Auth_ReturnsCorrectNextIndex_WhenNoApiKeysExist() |
| 94 | + { |
| 95 | + var emptyAuthOptions = new ApiAuthenticationHandlerOptions { ApiKeys = Array.Empty<ApiKey>() }; |
| 96 | + _authOptionsMock.Setup(x => x.Value).Returns(emptyAuthOptions); |
| 97 | + |
| 98 | + var controller = new AuthenticationController(_authOptionsMock.Object, _hasher, _optionsMock.Object); |
| 99 | + controller.ControllerContext = new ControllerContext |
| 100 | + { |
| 101 | + HttpContext = new DefaultHttpContext() |
| 102 | + }; |
| 103 | + controller.ControllerContext.HttpContext.Request.Scheme = "https"; |
| 104 | + controller.ControllerContext.HttpContext.Request.Host = new HostString("test.example.com"); |
| 105 | + |
| 106 | + var result = await controller.Auth("test"); |
| 107 | + |
| 108 | + var okResult = Assert.IsType<OkObjectResult>(result.Result); |
| 109 | + var model = Assert.IsType<NewAuthModel>(okResult.Value); |
| 110 | + Assert.StartsWith("Authentication__ApiKeys__0__ClusterIdentifier:", model.LocalAuthModel.ConfigMap.ClusterIdentifier); |
| 111 | + } |
| 112 | + |
| 113 | + [Fact] |
| 114 | + public async Task Auth_ReturnsRemoteAuthModel_WithCorrectClusterIdentifier() |
| 115 | + { |
| 116 | + var result = await _controller.Auth("test"); |
| 117 | + |
| 118 | + var okResult = Assert.IsType<OkObjectResult>(result.Result); |
| 119 | + var model = Assert.IsType<NewAuthModel>(okResult.Value); |
| 120 | + Assert.Contains("local-cluster", model.RemoteAuthModel.ConfigMap.ClusterIdentifier); |
| 121 | + } |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public async Task Auth_ReturnsRemoteAuthModel_WithCorrectUrl() |
| 125 | + { |
| 126 | + var result = await _controller.Auth("test"); |
| 127 | + |
| 128 | + var okResult = Assert.IsType<OkObjectResult>(result.Result); |
| 129 | + var model = Assert.IsType<NewAuthModel>(okResult.Value); |
| 130 | + Assert.Contains("https://test.example.com", model.RemoteAuthModel.ConfigMap.Url); |
| 131 | + } |
| 132 | + |
| 133 | + [Fact] |
| 134 | + public async Task Auth_ReturnsLocalSecretHash_NonEmpty() |
| 135 | + { |
| 136 | + var result = await _controller.Auth("test"); |
| 137 | + |
| 138 | + var okResult = Assert.IsType<OkObjectResult>(result.Result); |
| 139 | + var model = Assert.IsType<NewAuthModel>(okResult.Value); |
| 140 | + Assert.False(string.IsNullOrWhiteSpace(model.LocalAuthModel.Secret.Hash)); |
| 141 | + Assert.StartsWith("Authentication__ApiKeys__", model.LocalAuthModel.Secret.Hash); |
| 142 | + } |
| 143 | + |
| 144 | + [Fact] |
| 145 | + public async Task Auth_ReturnsRemoteSecretKey_NonEmpty() |
| 146 | + { |
| 147 | + var result = await _controller.Auth("test"); |
| 148 | + |
| 149 | + var okResult = Assert.IsType<OkObjectResult>(result.Result); |
| 150 | + var model = Assert.IsType<NewAuthModel>(okResult.Value); |
| 151 | + Assert.False(string.IsNullOrWhiteSpace(model.RemoteAuthModel.Secret.Key)); |
| 152 | + Assert.StartsWith("Peers__0__Key:", model.RemoteAuthModel.Secret.Key); |
| 153 | + } |
| 154 | + |
| 155 | + [Fact] |
| 156 | + public async Task Salt_ReturnsOkResult_WithSaltModel() |
| 157 | + { |
| 158 | + var result = await _controller.Salt(); |
| 159 | + |
| 160 | + var okResult = Assert.IsType<OkObjectResult>(result.Result); |
| 161 | + var model = Assert.IsType<NewSaltModel>(okResult.Value); |
| 162 | + Assert.StartsWith("ClusterSalt:", model.Salt); |
| 163 | + } |
| 164 | + |
| 165 | + [Fact] |
| 166 | + public async Task Salt_ReturnsSaltWithBase64Value() |
| 167 | + { |
| 168 | + var result = await _controller.Salt(); |
| 169 | + |
| 170 | + var okResult = Assert.IsType<OkObjectResult>(result.Result); |
| 171 | + var model = Assert.IsType<NewSaltModel>(okResult.Value); |
| 172 | + var saltValue = model.Salt.Replace("ClusterSalt: ", ""); |
| 173 | + var exception = Record.Exception(() => Convert.FromBase64String(saltValue)); |
| 174 | + Assert.Null(exception); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments