Skip to content

Commit

Permalink
add tests (#317)
Browse files Browse the repository at this point in the history
Related to: #300
  • Loading branch information
sikutisa authored Sep 13, 2024
1 parent 999dfd7 commit 4edf7a8
Showing 1 changed file with 159 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Azure.Security.KeyVault.Secrets;
using Azure;
using Azure.Data.Tables;
using Azure.Security.KeyVault.Secrets;

using AzureOpenAIProxy.ApiApp.Extensions;

Expand All @@ -7,6 +9,8 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

using NSubstitute;

namespace AzureOpenAIProxy.ApiApp.Tests.Extensions;

public class ServiceCollectionExtensionsTests
Expand Down Expand Up @@ -206,4 +210,157 @@ public void Given_AppSettings_When_Invoked_AddKeyVaultService_Then_It_Should_Ret
// Assert
result?.VaultUri.Should().BeEquivalentTo(expected);
}
}

[Fact]
public void Given_ServiceCollection_When_Invoked_AddTableStorageService_Then_It_Should_Contain_TableServiceClient()
{
// Arrange
var services = new ServiceCollection();

// Act
services.AddTableStorageService();

// Assert
services.SingleOrDefault(p => p.ServiceType == typeof(TableServiceClient)).Should().NotBeNull();
}

[Fact]
public void Given_ServiceCollection_When_Invoked_AddTableStorageService_Then_It_Should_Throw_Exception()
{
// Arrange
var services = new ServiceCollection();
services.AddTableStorageService();

// Act
Action action = () => services.BuildServiceProvider().GetService<TableServiceClient>();

// Assert
action.Should().Throw<InvalidOperationException>();
}

[Fact]
public void Given_Empty_AzureSettings_When_Invoked_AddTableStorageService_Then_It_Should_Throw_Exception()
{
// Arrange
var services = new ServiceCollection();
var dict = new Dictionary<string, string>()
{
{ "Azure", string.Empty},
};
#pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types.
var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build();
#pragma warning restore CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types.
services.AddSingleton<IConfiguration>(config);
services.AddTableStorageService();

// Act
Action action = () => services.BuildServiceProvider().GetService<TableServiceClient>();

// Assert
action.Should().Throw<InvalidOperationException>();
}

[Fact]
public void Given_Empty_KeyVaultSettings_When_Invoked_AddTableStorageService_Then_It_Should_Throw_Exception()
{
// Arrange
var services = new ServiceCollection();
var dict = new Dictionary<string, string>()
{
{ "Azure:KeyVault", string.Empty },
};
#pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types.
var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build();
#pragma warning restore CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types.
services.AddSingleton<IConfiguration>(config);
services.AddTableStorageService();

// Act
Action action = () => services.BuildServiceProvider().GetService<TableServiceClient>();

// Assert
action.Should().Throw<InvalidOperationException>();
}

[Fact]
public void Given_Missing_SecretClient_When_Invoked_AddTableStorageService_Then_It_Should_Throw_Exception()
{
// Arrange
var services = new ServiceCollection();
var dict = new Dictionary<string, string>()
{
{ "Azure:KeyVault:SecretNames:Storage", "secret-name" },
};
#pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types.
var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build();
#pragma warning restore CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types.
services.AddSingleton<IConfiguration>(config);
services.AddTableStorageService();

// Act
Action action = () => services.BuildServiceProvider().GetService<TableServiceClient>();

// Assert
action.Should().Throw<InvalidOperationException>();
}

[Theory]
[InlineData(default(string), typeof(KeyNotFoundException))]
[InlineData("", typeof(InvalidOperationException))]
public void Given_NullOrEmpty_SecretName_When_Invoked_AddTableStorageService_Then_It_Shoud_Throw_Exception(string? secretName, Type exceptionType)
{
// Arrange
var services = new ServiceCollection();
var dict = new Dictionary<string, string>()
{
{ "Azure:KeyVault:SecretNames:Storage", secretName },
};
#pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types.
var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build();
#pragma warning restore CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types.
services.AddSingleton<IConfiguration>(config);

var sc = Substitute.For<SecretClient>();
services.AddSingleton(sc);

services.AddTableStorageService();

// Act
Action action = () => services.BuildServiceProvider().GetService<TableServiceClient>();

// Assert
action.Should().Throw<Exception>().Which.Should().BeOfType(exceptionType);
}

[Theory]
[InlineData("secret-name", "DefaultEndpointsProtocol=https;AccountName=account;AccountKey=ZmFrZWtleQ==;EndpointSuffix=core.windows.net")]
public void Given_AppSettings_When_Invoked_AddTableStorageService_Then_It_Should_Return_TableServiceClient(string secretName, string connectionString)
{
// Arrange
var services = new ServiceCollection();
var dict = new Dictionary<string, string>()
{
{ "Azure:KeyVault:SecretNames:Storage", secretName },
};
#pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types.
var config = new ConfigurationBuilder().AddInMemoryCollection(dict).Build();
#pragma warning restore CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types.
services.AddSingleton<IConfiguration>(config);

var sc = Substitute.For<SecretClient>();
var sp = new SecretProperties(secretName);
var secret = SecretModelFactory.KeyVaultSecret(sp,connectionString);

sc.GetSecret(secretName).Returns(Response.FromValue(secret, Substitute.For<Response>()));
services.AddSingleton(sc);

services.AddTableStorageService();

// Act
var result = services.BuildServiceProvider().GetService<TableServiceClient>();

// Assert
result.Should().NotBeNull()
.And.BeOfType<TableServiceClient>();
}
}

0 comments on commit 4edf7a8

Please sign in to comment.