Skip to content

[Admin] Add Test Codes for AddTableStorageService #317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
// 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 },

Check warning on line 316 in test/AzureOpenAIProxy.ApiApp.Tests/Extensions/ServiceCollectionExtensionsTests.cs

View workflow job for this annotation

GitHub Actions / build-test

Possible null reference argument for parameter 'value' in 'void Dictionary<string, string>.Add(string key, string value)'.

Check warning on line 316 in test/AzureOpenAIProxy.ApiApp.Tests/Extensions/ServiceCollectionExtensionsTests.cs

View workflow job for this annotation

GitHub Actions / build-test

Possible null reference argument for parameter 'value' in 'void Dictionary<string, string>.Add(string key, string value)'.
};
#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>();
}
}
Loading