diff --git a/commercetools.Sdk/Tests/commercetools.Api.Tests/ClientsFactoryTests.cs b/commercetools.Sdk/Tests/commercetools.Api.Tests/ClientsFactoryTests.cs new file mode 100644 index 00000000000..7b1f0043794 --- /dev/null +++ b/commercetools.Sdk/Tests/commercetools.Api.Tests/ClientsFactoryTests.cs @@ -0,0 +1,49 @@ +using System; +using System.ComponentModel.DataAnnotations; +using commercetools.Base.Client; +using commercetools.Sdk.Api; +using commercetools.Sdk.Api.Serialization; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +namespace commercetools.Api.Tests +{ + public class ClientsFactoryTests + { + [Fact] + public void TestClientConfigValidation() + { + //arrange + var s = new ServiceCollection(); + s.UseCommercetoolsApiSerialization(); + var p = s.BuildServiceProvider(); + var serializerService = p.GetService(); + var clientConfig = new ClientConfiguration + { + ClientId = "ClientId", + ClientSecret = "ClientSecret", + ProjectKey = "test", + ApiBaseAddress = "https://api.europe-west1.gcp.commercetools.com", + AuthorizationBaseAddress = "https://auth.europe-west1.gcp.commercetools.com/" + }; + + //act + Exception validationEx = null; + try + { + var tokenProvider = TokenProviderFactory + .CreateClientCredentialsTokenProvider(clientConfig, null); + + ClientFactory.Create("test", clientConfig, null, serializerService, tokenProvider); + } + catch (Exception e) + { + validationEx = e; + } + + //assert + Assert.NotNull(validationEx); + Assert.IsType(validationEx); + } + } +} \ No newline at end of file diff --git a/commercetools.Sdk/commercetools.Base.Client/ClientConfiguration.cs b/commercetools.Sdk/commercetools.Base.Client/ClientConfiguration.cs index ed37e2697d1..ea1a78c7a9d 100644 --- a/commercetools.Sdk/commercetools.Base.Client/ClientConfiguration.cs +++ b/commercetools.Sdk/commercetools.Base.Client/ClientConfiguration.cs @@ -12,9 +12,15 @@ public class ClientConfiguration : IClientConfiguration public string ProjectKey { get; set; } + /// + /// Authorization Base Address Url like: "https://auth.europe-west1.gcp.commercetools.com/" + /// [RegularExpression(@"^.*/$", ErrorMessage = "ClientConfiguration AuthorizationBaseAddress URI should end with slash.")] public string AuthorizationBaseAddress { get; set; } = "https://auth.europe-west1.gcp.commercetools.com/"; + /// + /// Api Base Address Url like: "https://api.europe-west1.gcp.commercetools.com/" + /// [RegularExpression(@"^.*/$", ErrorMessage = "ClientConfiguration ApiBaseAddress URI should end with slash.")] public string ApiBaseAddress { get; set; } = "https://api.europe-west1.gcp.commercetools.com/"; } diff --git a/commercetools.Sdk/commercetools.Base.Client/ClientFactory.cs b/commercetools.Sdk/commercetools.Base.Client/ClientFactory.cs index 6c05083f714..67230c6388e 100644 --- a/commercetools.Sdk/commercetools.Base.Client/ClientFactory.cs +++ b/commercetools.Sdk/commercetools.Base.Client/ClientFactory.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Dynamic; using System.Net.Http; using System.Security; @@ -18,6 +19,7 @@ public static IClient Create( ISerializerService serializerService, ITokenProvider tokenProvider) { + Validator.ValidateObject(configuration, new ValidationContext(configuration), true); return new CtpClient( CreateMiddlewareStack(clientName, configuration, factory, tokenProvider), serializerService, diff --git a/commercetools.Sdk/commercetools.Base.Client/TokenProviderFactory.cs b/commercetools.Sdk/commercetools.Base.Client/TokenProviderFactory.cs index e581b487e0d..a9940205dfe 100644 --- a/commercetools.Sdk/commercetools.Base.Client/TokenProviderFactory.cs +++ b/commercetools.Sdk/commercetools.Base.Client/TokenProviderFactory.cs @@ -1,3 +1,4 @@ +using System.ComponentModel.DataAnnotations; using System.Net.Http; using commercetools.Base.Client.Tokens; @@ -43,6 +44,7 @@ private static ITokenProvider Create( IAnonymousCredentialsStoreManager anonymousCredentials = null ) { + Validator.ValidateObject(configuration, new ValidationContext(configuration), true); ITokenProvider provider = null; var authClient = factory.CreateClient(DefaultClientNames.Authorization); var serializerService = new TokenSerializerService();