Skip to content

Commit

Permalink
Merge pull request #53 from commercetools/Me-IntegrationTests
Browse files Browse the repository at this point in the history
Create Me Integration Test to get My Customer Profile
  • Loading branch information
MicheleRezk authored May 14, 2021
2 parents b3b0c33 + 7f9ba91 commit 49f2c17
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ jobs:
CTP_Client__ClientSecret: ${{ secrets.CTP_CLIENT_SECRET }}
CTP_Client__ProjectKey: ${{ secrets.CTP_PROJECT_KEY }}
CTP_Client__Scope: ${{ secrets.CTP_SCOPE }}
CTP_MeClient__ClientId: ${{ secrets.CTP_MECLIENT_ID }}
CTP_MeClient__ClientSecret: ${{ secrets.CTP_MECLIENT_SECRET }}
CTP_MeClient__ProjectKey: ${{ secrets.CTP_MEPROJECT_KEY }}
CTP_MeClient__Scope: ${{ secrets.CTP_MESCOPES }}
steps:
- name: Checkout
uses: actions/checkout@v2
Expand All @@ -76,6 +80,10 @@ jobs:
CTP_Client__ClientSecret: ${{ secrets.CTP_CLIENT_SECRET_PR }}
CTP_Client__ProjectKey: ${{ secrets.CTP_PROJECT_KEY_PR }}
CTP_Client__Scope: ${{ secrets.CTP_SCOPE_PR }}
CTP_MeClient__ClientId: ${{ secrets.CTP_MECLIENT_ID_PR }}
CTP_MeClient__ClientSecret: ${{ secrets.CTP_MECLIENT_SECRET_PR }}
CTP_MeClient__ProjectKey: ${{ secrets.CTP_MEPROJECT_KEY_PR }}
CTP_MeClient__Scope: ${{ secrets.CTP_MESCOPES_PR }}
- run: dotnet test IntegrationTests/commercetools.ImportApi.IntegrationTests --verbosity=normal --no-build -c Release
if: github.event_name == 'push'
working-directory: ./commercetools.Sdk
Expand All @@ -87,6 +95,10 @@ jobs:
CTP_Client__ClientSecret: ${{ secrets.CTP_CLIENT_SECRET_PR }}
CTP_Client__ProjectKey: ${{ secrets.CTP_PROJECT_KEY_PR }}
CTP_Client__Scope: ${{ secrets.CTP_SCOPE_PR }}
CTP_MeClient__ClientId: ${{ secrets.CTP_MECLIENT_ID_PR }}
CTP_MeClient__ClientSecret: ${{ secrets.CTP_MECLIENT_SECRET_PR }}
CTP_MeClient__ProjectKey: ${{ secrets.CTP_MEPROJECT_KEY_PR }}
CTP_MeClient__Scope: ${{ secrets.CTP_MESCOPES_PR }}

artifacts:
name: Create artifacts
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using commercetools.Api.IntegrationTests.Services;
using commercetools.Api.Models.Customers;
using commercetools.Api.Models.ShoppingLists;
using commercetools.Base.Client;
using commercetools.Base.Client.Tokens;
using commercetools.Sdk.Api.Extensions;
using commercetools.Sdk.Api.Serialization;
using Xunit;

namespace commercetools.Api.IntegrationTests.Me
{
[Collection("Integration Tests")]
public class MeIntegrationTests
{
#region Fields

private readonly IClient _adminClient;
private readonly CustomerServices _customerServices;
private readonly ShoppingListServices _shoppingListServices;
private readonly ServiceProviderFixture _serviceProviderFixture;
private IClient _meClient;
private ICustomer _customer = null;

#endregion

public MeIntegrationTests(ServiceProviderFixture serviceProviderFixture)
{
this._serviceProviderFixture = serviceProviderFixture;
this._adminClient = serviceProviderFixture.GetService<IClient>();
_customerServices = new CustomerServices();
_shoppingListServices = new ShoppingListServices();
CreateMeClient();
}

[Fact]
public async Task GetMyProfile()
{
//Arrange
await CreateEntitiesForTesting();

//Act
var myProfile = await _meClient.WithApi()
.WithProjectKey(GenericFixture.DefaultProjectKey)
.Me()
.Get()
.ExecuteAsync();

//Assert
Assert.NotNull(myProfile);
Assert.Equal(CustomerServices.CustomerEmail, myProfile.Email);
}

[Fact]
public async Task GetMyShoppingLists()
{
//Arrange
await CreateEntitiesForTesting();

//Act
var response = await _meClient.WithApi()
.WithProjectKey(GenericFixture.DefaultProjectKey)
.Me()
.ShoppingLists()
.Get()
.ExecuteAsync();

//Assert
var shoppingList = response.Results.FirstOrDefault();
Assert.NotNull(shoppingList);
Assert.Equal(ShoppingListServices.ShoppingListKey, shoppingList.Key);
}

private async Task CreateEntitiesForTesting()
{
//Get Customer By Key, Create if not exists
_customer = await _customerServices.GetCustomerByKey(_adminClient, CustomerServices.CustomerKey);
if (_customer == null)
{
var customerDraft = _customerServices.CreateCustomerDraft();
_customer = await _customerServices.CreateCustomer(_adminClient, customerDraft);
}

//Create a shoppingList for this customer (if not exists)
IShoppingList shoppingList =
await _shoppingListServices.GetShoppingListByKey(_adminClient, ShoppingListServices.ShoppingListKey);
if (shoppingList == null)
{
var shoppingListDraft = _shoppingListServices.CreateShoppingListDraft(
new CustomerResourceIdentifier()
{
Id = _customer.Id
});
shoppingList = await _shoppingListServices
.CreateShoppingList(_adminClient, shoppingListDraft);
}
}

private void CreateMeClient()
{
var meClientConfig= _serviceProviderFixture.GetClientConfiguration("MeClient");
var httpClientFactory = _serviceProviderFixture.GetService<IHttpClientFactory>();
var serializerService = _serviceProviderFixture.GetService<SerializerService>();

//Create passwordFlow TokenProvider
var passwordTokenProvider = TokenProviderFactory
.CreatePasswordTokenProvider(meClientConfig,
httpClientFactory,
new InMemoryUserCredentialsStoreManager(
CustomerServices.CustomerEmail,
CustomerServices.CustomerPassword));

//Create MeClient
_meClient = ClientFactory.Create(
"MeClient",
meClientConfig,
httpClientFactory,
serializerService,
passwordTokenProvider);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Threading.Tasks;
using commercetools.Api.Models.Customers;
using commercetools.Base.Client;
using commercetools.Base.Client.Error;
using commercetools.Sdk.Api.Extensions;

namespace commercetools.Api.IntegrationTests.Services
{
public class CustomerServices
{
public const string CustomerEmail = "[email protected]";
public const string CustomerPassword = "password";
public const string CustomerKey = "Customer_Me_Key123";
public async Task<ICustomer> GetCustomerByKey(IClient adminClient,string key)
{
try
{
var customer = await adminClient.WithApi().WithProjectKey(GenericFixture.DefaultProjectKey)
.Customers()
.WithKey(key)
.Get()
.ExecuteAsync();
return customer;
}
catch (NotFoundException)
{
// ignored
}

return null;
}
public async Task<ICustomer> CreateCustomer(IClient adminClient,ICustomerDraft customerDraft)
{
var customerSignInResult = await adminClient.WithApi().WithProjectKey(GenericFixture.DefaultProjectKey)
.Customers()
.Post(customerDraft)
.ExecuteAsync();
var customer = customerSignInResult.Customer;
return customer;
}

public ICustomerDraft CreateCustomerDraft()
{
return new CustomerDraft
{
Email = CustomerEmail,
Password = CustomerPassword,
Key = CustomerKey,
FirstName = "FN_Me",
LastName = "LN_Me"
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Threading.Tasks;
using commercetools.Api.Models.Common;
using commercetools.Api.Models.Customers;
using commercetools.Api.Models.ShoppingLists;
using commercetools.Base.Client;
using commercetools.Base.Client.Error;
using commercetools.Sdk.Api.Extensions;

namespace commercetools.Api.IntegrationTests.Services
{
public class ShoppingListServices
{
public const string ShoppingListKey = "ShoppingList_Me_Key123";

public async Task<IShoppingList> GetShoppingListByKey(IClient adminClient,string key)
{
try
{
var shoppingList = await adminClient.WithApi().WithProjectKey(GenericFixture.DefaultProjectKey)
.ShoppingLists()
.WithKey(key)
.Get()
.ExecuteAsync();
return shoppingList;
}
catch (NotFoundException)
{
// ignored
}

return null;
}

public async Task<IShoppingList> CreateShoppingList(IClient adminClient,IShoppingListDraft shoppingListDraft)
{
var shoppingList = await adminClient.WithApi().WithProjectKey(GenericFixture.DefaultProjectKey)
.ShoppingLists()
.Post(shoppingListDraft)
.ExecuteAsync();
return shoppingList;
}

public IShoppingListDraft CreateShoppingListDraft(ICustomerResourceIdentifier customerResourceIdentifier)
{
return new ShoppingListDraft()
{
Key = ShoppingListKey,
Customer = customerResourceIdentifier,
Name = new LocalizedString
{
{"en", ShoppingListKey}
}
};
}
}
}

0 comments on commit 49f2c17

Please sign in to comment.