Skip to content

Commit

Permalink
Add ITokenGenerator to service registration
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Sep 6, 2023
1 parent e3bd33f commit 7870e1c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 102 deletions.
177 changes: 75 additions & 102 deletions Vonage.Test.Unit/Extensions/ServiceCollectionExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,117 +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>());
}

[Fact]
public void AddVonageClientTransient_ShouldProvideTransientClientInstance_GivenCredentialsAreProvided()
{
var provider = BuildTransientProviderWithCredentials(this.credentials);
provider.GetRequiredService<VonageClient>().Should()
.NotBe(provider.GetRequiredService<VonageClient>());
}

[Theory]
[MemberData(nameof(GetSpecificVonageClients))]
public void
AddVonageClientTransient_ShouldProvideTransientSpecificClientInstance_GivenConfigurationIsProvided(
Type type)
{
var provider = BuildTransientProviderWithConfiguration(configuration);
provider.GetRequiredService(type).Should()
.NotBe(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));
}

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)};
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(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)};
}

private static ServiceProvider BuildScopedProviderWithConfiguration(IConfiguration configuration) =>
new ServiceCollection().AddVonageClientScoped(configuration).BuildServiceProvider();
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 BuildScopedProviderWithCredentials(Credentials credentials) =>
new ServiceCollection().AddVonageClientScoped(credentials).BuildServiceProvider();

private static ServiceProvider BuildTransientProviderWithConfiguration(IConfiguration configuration) =>
new ServiceCollection().AddVonageClientTransient(configuration).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();
}
}
2 changes: 2 additions & 0 deletions Vonage/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ private static void RegisterScopedServices(IServiceCollection services)
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 @@ -109,5 +110,6 @@ private static void RegisterTransientServices(IServiceCollection services)
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 7870e1c

Please sign in to comment.