|
| 1 | +using Azure.Security.KeyVault.Secrets; |
| 2 | + |
| 3 | +using AzureOpenAIProxy.ApiApp.Extensions; |
| 4 | + |
| 5 | +using FluentAssertions; |
| 6 | + |
| 7 | +using Microsoft.Extensions.Configuration; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | + |
| 10 | +namespace AzureOpenAIProxy.ApiApp.Tests.Extensions; |
| 11 | + |
| 12 | +public class ServiceCollectionExtensionsTests |
| 13 | +{ |
| 14 | + [Fact] |
| 15 | + public void Given_ServiceCollection_When_Invoked_AddKeyVaultService_Then_It_Should_Contain_SecretClient() |
| 16 | + { |
| 17 | + // Arrange |
| 18 | + var services = new ServiceCollection(); |
| 19 | + |
| 20 | + // Act |
| 21 | + services.AddKeyVaultService(); |
| 22 | + |
| 23 | + // Assert |
| 24 | + services.SingleOrDefault(p => p.ServiceType == typeof(SecretClient)).Should().NotBeNull(); |
| 25 | + } |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public void Given_ServiceCollection_When_Invoked_AddKeyVaultService_Then_It_Should_Throw_Exception() |
| 29 | + { |
| 30 | + // Arrange |
| 31 | + var services = new ServiceCollection(); |
| 32 | + services.AddKeyVaultService(); |
| 33 | + |
| 34 | + // Act |
| 35 | + Action action = () => services.BuildServiceProvider().GetService<SecretClient>(); |
| 36 | + |
| 37 | + // Assert |
| 38 | + action.Should().Throw<InvalidOperationException>(); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void Given_Empty_AzureSettings_When_Invoked_AddKeyVaultService_Then_It_Should_Throw_Exception() |
| 43 | + { |
| 44 | + // Arrange |
| 45 | + var services = new ServiceCollection(); |
| 46 | + var dict = new Dictionary<string, string>() |
| 47 | + { |
| 48 | + { "Azure", string.Empty }, |
| 49 | + }; |
| 50 | +#pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types. |
| 51 | + var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build(); |
| 52 | +#pragma warning restore CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types. |
| 53 | + services.AddSingleton<IConfiguration>(config); |
| 54 | + services.AddKeyVaultService(); |
| 55 | + |
| 56 | + // Act |
| 57 | + Action action = () => services.BuildServiceProvider().GetService<SecretClient>(); |
| 58 | + |
| 59 | + // Assert |
| 60 | + action.Should().Throw<InvalidOperationException>(); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void Given_Empty_KeyVaultSettings_When_Invoked_AddKeyVaultService_Then_It_Should_Throw_Exception() |
| 65 | + { |
| 66 | + // Arrange |
| 67 | + var services = new ServiceCollection(); |
| 68 | + var dict = new Dictionary<string, string>() |
| 69 | + { |
| 70 | + { "Azure:KeyVault", string.Empty }, |
| 71 | + }; |
| 72 | +#pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types. |
| 73 | + var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build(); |
| 74 | +#pragma warning restore CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types. |
| 75 | + services.AddSingleton<IConfiguration>(config); |
| 76 | + services.AddKeyVaultService(); |
| 77 | + |
| 78 | + // Act |
| 79 | + Action action = () => services.BuildServiceProvider().GetService<SecretClient>(); |
| 80 | + |
| 81 | + // Assert |
| 82 | + action.Should().Throw<InvalidOperationException>(); |
| 83 | + } |
| 84 | + |
| 85 | + [Theory] |
| 86 | + [InlineData("http://localhost")] |
| 87 | + public void Given_AppSettings_When_Invoked_AddKeyVaultService_Then_It_Should_Return_SecretClient(string vaultUri) |
| 88 | + { |
| 89 | + // Arrange |
| 90 | + var services = new ServiceCollection(); |
| 91 | + var dict = new Dictionary<string, string>() |
| 92 | + { |
| 93 | + { "Azure:KeyVault:VaultUri", vaultUri }, |
| 94 | + }; |
| 95 | +#pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types. |
| 96 | + var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build(); |
| 97 | +#pragma warning restore CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types. |
| 98 | + services.AddSingleton<IConfiguration>(config); |
| 99 | + services.AddKeyVaultService(); |
| 100 | + |
| 101 | + // Act |
| 102 | + var result = services.BuildServiceProvider().GetService<SecretClient>(); |
| 103 | + |
| 104 | + // Assert |
| 105 | + result.Should().NotBeNull() |
| 106 | + .And.BeOfType<SecretClient>(); |
| 107 | + } |
| 108 | + |
| 109 | + [Theory] |
| 110 | + [InlineData("http://localhost")] |
| 111 | + public void Given_AppSettings_When_Invoked_AddKeyVaultService_Then_It_Should_Return_VaultUri(string vaultUri) |
| 112 | + { |
| 113 | + // Arrange |
| 114 | + var services = new ServiceCollection(); |
| 115 | + var dict = new Dictionary<string, string>() |
| 116 | + { |
| 117 | + { "Azure:KeyVault:VaultUri", vaultUri }, |
| 118 | + }; |
| 119 | +#pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types. |
| 120 | + var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build(); |
| 121 | +#pragma warning restore CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types. |
| 122 | + services.AddSingleton<IConfiguration>(config); |
| 123 | + services.AddKeyVaultService(); |
| 124 | + |
| 125 | + var expected = new Uri(vaultUri); |
| 126 | + |
| 127 | + // Act |
| 128 | + var result = services.BuildServiceProvider().GetService<SecretClient>(); |
| 129 | + |
| 130 | + // Assert |
| 131 | + result?.VaultUri.Should().BeEquivalentTo(expected); |
| 132 | + } |
| 133 | +} |
0 commit comments