Skip to content

Commit

Permalink
feat: extend registration with missing clients and token generator (#504
Browse files Browse the repository at this point in the history
)

* Add SubAccountsClient to DI registration

* Add ITokenGenerator to service registration
  • Loading branch information
Tr00d authored Sep 6, 2023
1 parent c11e660 commit 9d2a936
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 103 deletions.
179 changes: 77 additions & 102 deletions Vonage.Test.Unit/Extensions/ServiceCollectionExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Vonage.Redaction;
using Vonage.Request;
using Vonage.ShortCodes;
using Vonage.SubAccounts;
using Vonage.Users;
using Vonage.Verify;
using Vonage.VerifyV2;
Expand All @@ -25,116 +26,90 @@

namespace Vonage.Test.Unit.Extensions
{
public class ServiceCollectionExtensionsTest
{
private readonly Credentials credentials = Credentials.FromApiKeyAndSecret("key", "secret");
public class ServiceCollectionExtensionsTest
{
private readonly Credentials credentials = Credentials.FromApiKeyAndSecret("key", "secret");

private readonly IConfigurationRoot configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage_key", "RandomValue"},
})
.Build();
private readonly IConfigurationRoot configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage_key", "RandomValue"},
})
.Build();

[Fact]
public void AddVonageClientScoped_ShouldProvideScopedClientInstance_GivenConfigurationIsProvided()
{
var provider = BuildScopedProviderWithConfiguration(configuration);
provider.GetRequiredService<VonageClient>().Should().Be(provider.GetRequiredService<VonageClient>());
}
[Theory]
[MemberData(nameof(GetSpecificVonageClients))]
public void AddVonageClientScoped_ShouldProvideScopedSpecificClientInstance_GivenConfigurationIsProvided(
Type type)
{
var provider = BuildScopedProviderWithConfiguration(this.configuration);
provider.GetRequiredService(type).Should().Be(provider.GetRequiredService(type));
}

[Fact]
public void AddVonageClientScoped_ShouldProvideScopedClientInstance_GivenCredentialsAreProvided()
{
var provider = BuildScopedProviderWithCredentials(this.credentials);
provider.GetRequiredService<VonageClient>().Should().Be(provider.GetRequiredService<VonageClient>());
}
[Theory]
[MemberData(nameof(GetSpecificVonageClients))]
public void AddVonageClientScoped_ShouldProvideScopedSpecificClientInstance_GivenCredentialsAreProvided(
Type type)
{
var provider = BuildScopedProviderWithCredentials(this.credentials);
provider.GetRequiredService(type).Should().Be(provider.GetRequiredService(type));
}

[Theory]
[MemberData(nameof(GetSpecificVonageClients))]
public void AddVonageClientScoped_ShouldProvideScopedSpecificClientInstance_GivenConfigurationIsProvided(
Type type)
{
var provider = BuildScopedProviderWithConfiguration(configuration);
provider.GetRequiredService(type).Should().Be(provider.GetRequiredService(type));
}
[Theory]
[MemberData(nameof(GetSpecificVonageClients))]
public void
AddVonageClientTransient_ShouldProvideTransientSpecificClientInstance_GivenConfigurationIsProvided(
Type type)
{
var provider = BuildTransientProviderWithConfiguration(this.configuration);
provider.GetRequiredService(type).Should()
.NotBe(provider.GetRequiredService(type));
}

[Theory]
[MemberData(nameof(GetSpecificVonageClients))]
public void AddVonageClientScoped_ShouldProvideScopedSpecificClientInstance_GivenCredentialsAreProvided(
Type type)
{
var provider = BuildScopedProviderWithCredentials(this.credentials);
provider.GetRequiredService(type).Should().Be(provider.GetRequiredService(type));
}
[Theory]
[MemberData(nameof(GetSpecificVonageClients))]
public void AddVonageClientTransient_ShouldProvideTransientSpecificClientInstance_GivenCredentialsAreProvided(
Type type)
{
var provider = BuildTransientProviderWithCredentials(this.credentials);
provider.GetRequiredService(type).Should()
.NotBe(provider.GetRequiredService(type));
}

[Fact]
public void AddVonageClientTransient_ShouldProvideTransientClientInstance_GivenConfigurationIsProvided()
{
var provider = BuildTransientProviderWithConfiguration(configuration);
provider.GetRequiredService<VonageClient>().Should()
.NotBe(provider.GetRequiredService<VonageClient>());
}
public static IEnumerable<object[]> GetSpecificVonageClients()
{
yield return new object[] {typeof(VonageClient)};
yield return new object[] {typeof(IAccountClient)};
yield return new object[] {typeof(IApplicationClient)};
yield return new object[] {typeof(IConversionClient)};
yield return new object[] {typeof(IMeetingsClient)};
yield return new object[] {typeof(IMessagesClient)};
yield return new object[] {typeof(INumberInsightClient)};
yield return new object[] {typeof(INumbersClient)};
yield return new object[] {typeof(IPricingClient)};
yield return new object[] {typeof(IProactiveConnectClient)};
yield return new object[] {typeof(IRedactClient)};
yield return new object[] {typeof(IShortCodesClient)};
yield return new object[] {typeof(ISubAccountsClient)};
yield return new object[] {typeof(ISmsClient)};
yield return new object[] {typeof(IUsersClient)};
yield return new object[] {typeof(IVerifyClient)};
yield return new object[] {typeof(IVerifyV2Client)};
yield return new object[] {typeof(IVoiceClient)};
yield return new object[] {typeof(ITokenGenerator)};
yield return new object[] {typeof(ITokenGenerator)};
}

[Fact]
public void AddVonageClientTransient_ShouldProvideTransientClientInstance_GivenCredentialsAreProvided()
{
var provider = BuildTransientProviderWithCredentials(this.credentials);
provider.GetRequiredService<VonageClient>().Should()
.NotBe(provider.GetRequiredService<VonageClient>());
}
private static ServiceProvider BuildScopedProviderWithConfiguration(IConfiguration configuration) =>
new ServiceCollection().AddVonageClientScoped(configuration).BuildServiceProvider();

[Theory]
[MemberData(nameof(GetSpecificVonageClients))]
public void
AddVonageClientTransient_ShouldProvideTransientSpecificClientInstance_GivenConfigurationIsProvided(
Type type)
{
var provider = BuildTransientProviderWithConfiguration(configuration);
provider.GetRequiredService(type).Should()
.NotBe(provider.GetRequiredService(type));
}
private static ServiceProvider BuildScopedProviderWithCredentials(Credentials credentials) =>
new ServiceCollection().AddVonageClientScoped(credentials).BuildServiceProvider();

[Theory]
[MemberData(nameof(GetSpecificVonageClients))]
public void AddVonageClientTransient_ShouldProvideTransientSpecificClientInstance_GivenCredentialsAreProvided(
Type type)
{
var provider = BuildTransientProviderWithCredentials(this.credentials);
provider.GetRequiredService(type).Should()
.NotBe(provider.GetRequiredService(type));
}
private static ServiceProvider BuildTransientProviderWithConfiguration(IConfiguration configuration) =>
new ServiceCollection().AddVonageClientTransient(configuration).BuildServiceProvider();

public static IEnumerable<object[]> GetSpecificVonageClients()
{
yield return new object[] {typeof(IAccountClient)};
yield return new object[] {typeof(IApplicationClient)};
yield return new object[] {typeof(IConversionClient)};
yield return new object[] {typeof(IMeetingsClient)};
yield return new object[] {typeof(IMessagesClient)};
yield return new object[] {typeof(INumberInsightClient)};
yield return new object[] {typeof(INumbersClient)};
yield return new object[] {typeof(IPricingClient)};
yield return new object[] {typeof(IProactiveConnectClient)};
yield return new object[] {typeof(IRedactClient)};
yield return new object[] {typeof(IShortCodesClient)};
yield return new object[] {typeof(ISmsClient)};
yield return new object[] {typeof(IUsersClient)};
yield return new object[] {typeof(IVerifyClient)};
yield return new object[] {typeof(IVerifyV2Client)};
yield return new object[] {typeof(IVoiceClient)};
}

private static ServiceProvider BuildScopedProviderWithConfiguration(IConfiguration configuration) =>
new ServiceCollection().AddVonageClientScoped(configuration).BuildServiceProvider();

private static ServiceProvider BuildScopedProviderWithCredentials(Credentials credentials) =>
new ServiceCollection().AddVonageClientScoped(credentials).BuildServiceProvider();

private static ServiceProvider BuildTransientProviderWithConfiguration(IConfiguration configuration) =>
new ServiceCollection().AddVonageClientTransient(configuration).BuildServiceProvider();

private static ServiceProvider BuildTransientProviderWithCredentials(Credentials credentials) =>
new ServiceCollection().AddVonageClientTransient(credentials).BuildServiceProvider();
}
private static ServiceProvider BuildTransientProviderWithCredentials(Credentials credentials) =>
new ServiceCollection().AddVonageClientTransient(credentials).BuildServiceProvider();
}
}
6 changes: 5 additions & 1 deletion Vonage/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ private static void RegisterScopedServices(IServiceCollection services)
services.AddScoped(serviceProvider => serviceProvider.GetService<VonageClient>().PricingClient);
services.AddScoped(serviceProvider => serviceProvider.GetService<VonageClient>().ProactiveConnectClient);
services.AddScoped(serviceProvider => serviceProvider.GetService<VonageClient>().RedactClient);
services.AddScoped(serviceProvider => serviceProvider.GetService<VonageClient>().ShortCodesClient);
services.AddScoped(serviceProvider => serviceProvider.GetService<VonageClient>().ShortCodesClient);
services.AddScoped(serviceProvider => serviceProvider.GetService<VonageClient>().SubAccountsClient);
services.AddScoped(serviceProvider => serviceProvider.GetService<VonageClient>().SmsClient);
services.AddScoped(serviceProvider => serviceProvider.GetService<VonageClient>().UsersClient);
services.AddScoped(serviceProvider => serviceProvider.GetService<VonageClient>().VerifyClient);
services.AddScoped(serviceProvider => serviceProvider.GetService<VonageClient>().VerifyV2Client);
services.AddScoped(serviceProvider => serviceProvider.GetService<VonageClient>().VoiceClient);
services.AddScoped<ITokenGenerator>(_ => new Jwt());
}

private static void RegisterTransientServices(IServiceCollection services)
Expand All @@ -102,10 +104,12 @@ private static void RegisterTransientServices(IServiceCollection services)
services.AddTransient(serviceProvider => serviceProvider.GetService<VonageClient>().ProactiveConnectClient);
services.AddTransient(serviceProvider => serviceProvider.GetService<VonageClient>().RedactClient);
services.AddTransient(serviceProvider => serviceProvider.GetService<VonageClient>().ShortCodesClient);
services.AddTransient(serviceProvider => serviceProvider.GetService<VonageClient>().SubAccountsClient);
services.AddTransient(serviceProvider => serviceProvider.GetService<VonageClient>().SmsClient);
services.AddTransient(serviceProvider => serviceProvider.GetService<VonageClient>().UsersClient);
services.AddTransient(serviceProvider => serviceProvider.GetService<VonageClient>().VerifyClient);
services.AddTransient(serviceProvider => serviceProvider.GetService<VonageClient>().VerifyV2Client);
services.AddTransient(serviceProvider => serviceProvider.GetService<VonageClient>().VoiceClient);
services.AddTransient<ITokenGenerator>(_ => new Jwt());
}
}

0 comments on commit 9d2a936

Please sign in to comment.