Skip to content

Commit

Permalink
Merge pull request #64 from commercetools/Fix-ValidateClientConfig
Browse files Browse the repository at this point in the history
Validate Client Configuration Model
  • Loading branch information
MicheleRezk authored May 25, 2021
2 parents 49f2c17 + 214ace1 commit e37d90c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<SerializerService>();
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<ValidationException>(validationEx);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ public class ClientConfiguration : IClientConfiguration

public string ProjectKey { get; set; }

/// <summary>
/// Authorization Base Address Url like: "https://auth.europe-west1.gcp.commercetools.com/"
/// </summary>
[RegularExpression(@"^.*/$", ErrorMessage = "ClientConfiguration AuthorizationBaseAddress URI should end with slash.")]
public string AuthorizationBaseAddress { get; set; } = "https://auth.europe-west1.gcp.commercetools.com/";

/// <summary>
/// Api Base Address Url like: "https://api.europe-west1.gcp.commercetools.com/"
/// </summary>
[RegularExpression(@"^.*/$", ErrorMessage = "ClientConfiguration ApiBaseAddress URI should end with slash.")]
public string ApiBaseAddress { get; set; } = "https://api.europe-west1.gcp.commercetools.com/";
}
Expand Down
2 changes: 2 additions & 0 deletions commercetools.Sdk/commercetools.Base.Client/ClientFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Dynamic;
using System.Net.Http;
using System.Security;
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.ComponentModel.DataAnnotations;
using System.Net.Http;
using commercetools.Base.Client.Tokens;

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit e37d90c

Please sign in to comment.