Skip to content

Commit

Permalink
Create Configuration from IConfiguration, create Credentials from Con…
Browse files Browse the repository at this point in the history
…figuration
  • Loading branch information
Tr00d committed Sep 4, 2023
1 parent 86db798 commit a091a18
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 137 deletions.
151 changes: 132 additions & 19 deletions Vonage.Test.Unit/ConfigurationTest.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,140 @@
using System;
using System.Collections.Generic;
using FluentAssertions;
using Microsoft.Extensions.Configuration;
using Vonage.Cryptography;
using Xunit;

namespace Vonage.Test.Unit
{
public class ConfigurationTest
{
[Fact]
public void FromConfiguration_ShouldLoadEmptyCredentials_GivenConfigurationContainsNoElement()
{
var credentials = Configuration.FromConfiguration(new ConfigurationBuilder().Build());
credentials.ApiKey.Should().BeEmpty();
credentials.ApiSecret.Should().BeEmpty();
credentials.ApplicationId.Should().BeEmpty();
credentials.ApplicationKey.Should().BeEmpty();
credentials.SecuritySecret.Should().BeEmpty();
credentials.SigningMethod.Should().BeEmpty();
credentials.UserAgent.Should().BeEmpty();
credentials.EuropeApiUrl.Should().Be(new Uri("https://api-eu.vonage.com"));
credentials.NexmoApiUrl.Should().Be(new Uri("https://api.nexmo.com"));
credentials.RestApiUrl.Should().Be(new Uri("https://rest.nexmo.com"));
credentials.VideoApiUrl.Should().Be(new Uri("https://video.api.vonage.com"));
}
}
public class ConfigurationTest
{
[Fact]
public void BuildCredentials_ShouldCreateEmptyCredentials_GivenConfigurationContainsNoElement()
{
var credentials = Configuration.FromConfiguration(new ConfigurationBuilder().Build()).BuildCredentials();
credentials.ApiKey.Should().BeEmpty();
credentials.ApiSecret.Should().BeEmpty();
credentials.ApplicationId.Should().BeEmpty();
credentials.ApplicationKey.Should().BeEmpty();
credentials.SecuritySecret.Should().BeEmpty();
credentials.Method.Should().Be(SmsSignatureGenerator.Method.md5hash);
}

[Fact]
public void FromConfiguration_ShouldCreateEmptyConfiguration_GivenConfigurationContainsNoElement()
{
var configuration = Configuration.FromConfiguration(new ConfigurationBuilder().Build());
configuration.ApiKey.Should().BeEmpty();
configuration.ApiSecret.Should().BeEmpty();
configuration.ApplicationId.Should().BeEmpty();
configuration.ApplicationKey.Should().BeEmpty();
configuration.SecuritySecret.Should().BeEmpty();
configuration.SigningMethod.Should().BeEmpty();
configuration.UserAgent.Should().BeEmpty();
configuration.EuropeApiUrl.Should().Be(new Uri("https://api-eu.vonage.com"));
configuration.NexmoApiUrl.Should().Be(new Uri("https://api.nexmo.com"));
configuration.RestApiUrl.Should().Be(new Uri("https://rest.nexmo.com"));
configuration.VideoApiUrl.Should().Be(new Uri("https://video.api.vonage.com"));
}

[Fact]
public void FromConfiguration_ShouldSetApiKey_GivenConfigurationContainsApiKey() =>
Configuration.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage_key", "RandomValue"},
})
.Build()).ApiKey.Should().Be("RandomValue");

[Fact]
public void FromConfiguration_ShouldSetApiKSecret_GivenConfigurationContainsApiSecret() =>
Configuration.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage_secret", "RandomValue"},
})
.Build()).ApiSecret.Should().Be("RandomValue");

[Fact]
public void FromConfiguration_ShouldSetApplicationId_GivenConfigurationContainsApplicationId() =>
Configuration.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.Application.Id", "RandomValue"},
})
.Build()).ApplicationId.Should().Be("RandomValue");

[Fact]
public void FromConfiguration_ShouldSetApplicationKey_GivenConfigurationContainsApplicationKey() =>
Configuration.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.Application.Key", "RandomValue"},
})
.Build()).ApplicationKey.Should().Be("RandomValue");

[Fact]
public void FromConfiguration_ShouldSetEuropeApiUrl_GivenConfigurationContainsEuropeApiUrl() =>
Configuration.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.Url.Api.Europe", "https://api.vonage.com"},
})
.Build()).EuropeApiUrl.Should().Be(new Uri("https://api.vonage.com"));

[Fact]
public void FromConfiguration_ShouldSetNexmoApiUrl_GivenConfigurationContainsNexmoApiUrl() =>
Configuration.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.Url.Api", "https://api.vonage.com"},
})
.Build()).NexmoApiUrl.Should().Be(new Uri("https://api.vonage.com"));

[Fact]
public void FromConfiguration_ShouldSetRestApiUrl_GivenConfigurationContainsRestApiUrl() =>
Configuration.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.Url.Rest", "https://api.vonage.com"},
})
.Build()).RestApiUrl.Should().Be(new Uri("https://api.vonage.com"));

[Fact]
public void FromConfiguration_ShouldSetSecuritySecret_GivenConfigurationContainsSecuritySecret() =>
Configuration.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.security_secret", "RandomValue"},
})
.Build()).SecuritySecret.Should().Be("RandomValue");

[Fact]
public void FromConfiguration_ShouldSetSigningMethod_GivenConfigurationContainsSigningMethod() =>
Configuration.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.signing_method", "sha512"},
})
.Build()).SigningMethod.Should().Be("sha512");

[Fact]
public void FromConfiguration_ShouldSetUserAgent_GivenConfigurationContainsUserAgent() =>
Configuration.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.UserAgent", "RandomValue"},
})
.Build()).UserAgent.Should().Be("RandomValue");

[Fact]
public void FromConfiguration_ShouldSetVideoApiUrl_GivenConfigurationContainsVideoApiUrl() =>
Configuration.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.Url.Api.Video", "https://api.vonage.com"},
})
.Build()).VideoApiUrl.Should().Be(new Uri("https://api.vonage.com"));
}
}
85 changes: 0 additions & 85 deletions Vonage.Test.Unit/Request/CredentialsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,91 +13,6 @@ namespace Vonage.Test.Unit.Request
{
public class CredentialsTest
{
[Fact]
public void FromConfiguration_ShouldLoadEmptyCredentials_GivenConfigurationContainsNoElement()
{
var credentials = Credentials.FromConfiguration(new ConfigurationBuilder().Build());
credentials.ApiKey.Should().BeNull();
credentials.ApiSecret.Should().BeNull();
credentials.ApplicationId.Should().BeNull();
credentials.ApplicationKey.Should().BeNull();
credentials.Method.Should().Be(SmsSignatureGenerator.Method.md5hash);
credentials.SecuritySecret.Should().BeNull();
credentials.AppUserAgent.Should().BeNull();
}

[Fact]
public void FromConfiguration_ShouldSetApiKey_GivenConfigurationContainsApiKey() =>
Credentials.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage_key", "testKey"},
})
.Build()).ApiKey.Should().Be("testKey");

[Fact]
public void FromConfiguration_ShouldSetApiKSecret_GivenConfigurationContainsApiSecret() =>
Credentials.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage_secret", "testSecret"},
})
.Build()).ApiSecret.Should().Be("testSecret");

[Fact]
public void FromConfiguration_ShouldSetApplicationId_GivenConfigurationContainsApplicationId() =>
Credentials.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.Application.Id", "testApplicationId"},
})
.Build()).ApplicationId.Should().Be("testApplicationId");

[Fact]
public void FromConfiguration_ShouldSetApplicationKey_GivenConfigurationContainsApplicationKey() =>
Credentials.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.Application.Key", "testApplicationKey"},
})
.Build()).ApplicationKey.Should().Be("testApplicationKey");

[Fact]
public void FromConfiguration_ShouldSetDefaultSigningMethod_GivenConfigurationContainsInvalidSigningMethod() =>
Credentials.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.signing_method", "invalid"},
})
.Build()).Method.Should().Be(SmsSignatureGenerator.Method.md5hash);

[Fact]
public void FromConfiguration_ShouldSetSecuritySecret_GivenConfigurationContainsSecuritySecret() =>
Credentials.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.security_secret", "testSecuritySecret"},
})
.Build()).SecuritySecret.Should().Be("testSecuritySecret");

[Fact]
public void FromConfiguration_ShouldSetSigningMethod_GivenConfigurationContainsSigningMethod() =>
Credentials.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.signing_method", "sha512"},
})
.Build()).Method.Should().Be(SmsSignatureGenerator.Method.sha512);

[Fact]
public void FromConfiguration_ShouldSetUserAgent_GivenConfigurationContainsUserAgent() =>
Credentials.FromConfiguration(new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"appSettings:Vonage.UserAgent", "testUserAgent"},
})
.Build()).AppUserAgent.Should().Be("testUserAgent");

[Fact]
public void GetAuthenticationHeader_ReturnsBasicScheme_GivenContainsApiKeyAndApiSecret() =>
BuildBasicCredentials()
Expand Down
42 changes: 29 additions & 13 deletions Vonage/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Vonage.Common.Monads;
using Vonage.Cryptography;
using Vonage.Logger;
using Vonage.Request;

Expand Down Expand Up @@ -35,6 +33,7 @@ static Configuration()
private Configuration(IConfiguration configuration)
{
this.Settings = configuration;
this.LogAuthenticationCapabilities(LogProvider.GetLogger(LoggerCategory));
}

/// <summary>
Expand Down Expand Up @@ -126,7 +125,6 @@ private Configuration(IConfiguration configuration)

internal Configuration()
{
var logger = LogProvider.GetLogger(LoggerCategory);
var builder = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
Expand All @@ -137,11 +135,13 @@ internal Configuration()
{"appSettings:Vonage.EnsureSuccessStatusCode", "false"},
})
.AddJsonFile("settings.json", true, true)
.AddJsonFile("appsettings.json", true, true)
;
.AddJsonFile("appsettings.json", true, true);
this.Settings = builder.Build();
this.LogAuthenticationCapabilities(LogProvider.GetLogger(LoggerCategory));
}

// verify we have a minimum amount of configuration
private void LogAuthenticationCapabilities(ILogger logger)
{
var authCapabilities = new List<string>();
if (!string.IsNullOrWhiteSpace(this.ApiKey) &&
!string.IsNullOrWhiteSpace(this.ApiSecret))
Expand Down Expand Up @@ -169,15 +169,31 @@ internal Configuration()
logger.LogInformation("Available authentication: {0}", string.Join(",", authCapabilities));
}
}

/// <summary>
/// Builds a Configuration from an IConfiguration.
/// </summary>
/// <param name="configuration"></param>
/// <returns></returns>
public static Configuration FromConfiguration(IConfiguration configuration)
/// <param name="configuration">The configuration properties.</param>
/// <returns>The Configuration.</returns>
public static Configuration FromConfiguration(IConfiguration configuration) => new(configuration);

/// <summary>
/// Builds a Credentials from the current Configuration.
/// </summary>
/// <returns>The Credentials.</returns>
public Credentials BuildCredentials() => new()
{
throw new NotImplementedException();
}
ApiKey = this.ApiKey,
ApiSecret = this.ApiSecret,
ApplicationId = this.ApplicationId,
ApplicationKey = this.ApplicationKey,
SecuritySecret = this.SecuritySecret,
AppUserAgent = this.UserAgent,
Method = Enum.TryParse(this.SigningMethod,
out SmsSignatureGenerator.Method result)
? result
: default,
};

private HttpClient BuildDefaultClient() =>
this.ClientHandler == null
Expand Down
20 changes: 0 additions & 20 deletions Vonage/Request/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,26 +148,6 @@ public static Credentials FromAppIdAndPrivateKeyPath(string appId, string privat
return new Credentials {ApplicationId = appId, ApplicationKey = privateKey};
}

/// <summary>
/// Initializes a Credentials from configuration.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <returns>The Credentials.</returns>
public static Credentials FromConfiguration(IConfiguration configuration) =>
new()
{
ApiKey = configuration["appSettings:Vonage_key"],
ApiSecret = configuration["appSettings:Vonage_secret"],
ApplicationId = configuration["appSettings:Vonage.Application.Id"],
ApplicationKey = configuration["appSettings:Vonage.Application.Key"],
SecuritySecret = configuration["appSettings:Vonage.security_secret"],
AppUserAgent = configuration["appSettings:Vonage.UserAgent"],
Method = Enum.TryParse(configuration["appSettings:Vonage.signing_method"],
out SmsSignatureGenerator.Method result)
? result
: default,
};

/// <summary>
/// Provides the preferred authentication based on authentication type.
/// </summary>
Expand Down

0 comments on commit a091a18

Please sign in to comment.