Skip to content

Commit

Permalink
Adding Integration Testing for Get My ShoppingLists
Browse files Browse the repository at this point in the history
  • Loading branch information
MicheleRezk committed May 11, 2021
1 parent 827fd79 commit 7f9ba91
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
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 Microsoft.Extensions.Configuration;
using Xunit;

namespace commercetools.Api.IntegrationTests.Me
Expand All @@ -18,6 +19,7 @@ public class MeIntegrationTests

private readonly IClient _adminClient;
private readonly CustomerServices _customerServices;
private readonly ShoppingListServices _shoppingListServices;
private readonly ServiceProviderFixture _serviceProviderFixture;
private IClient _meClient;
private ICustomer _customer = null;
Expand All @@ -29,14 +31,15 @@ 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();
CreateMeClient();

//Act
var myProfile = await _meClient.WithApi()
Expand All @@ -47,25 +50,52 @@ public async Task GetMyProfile()

//Assert
Assert.NotNull(myProfile);
Assert.Equal(CustomerServices.CUSTOMER_EMAIL, myProfile.Email);
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.CUSTOMER_KEY);
_customer = await _customerServices.GetCustomerByKey(_adminClient, CustomerServices.CustomerKey);
if (_customer == null)
{
var customerDraft = new CustomerDraft
{
Email = CustomerServices.CUSTOMER_EMAIL,
Password = CustomerServices.CUSTOMER_PASSWORD,
Key = CustomerServices.CUSTOMER_KEY,
FirstName = "FN_Me",
LastName = "LN_Me"
};
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()
Expand All @@ -79,8 +109,8 @@ private void CreateMeClient()
.CreatePasswordTokenProvider(meClientConfig,
httpClientFactory,
new InMemoryUserCredentialsStoreManager(
CustomerServices.CUSTOMER_EMAIL,
CustomerServices.CUSTOMER_PASSWORD));
CustomerServices.CustomerEmail,
CustomerServices.CustomerPassword));

//Create MeClient
_meClient = ClientFactory.Create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
using commercetools.Base.Client.Error;
using commercetools.Sdk.Api.Extensions;

namespace commercetools.Api.IntegrationTests
namespace commercetools.Api.IntegrationTests.Services
{
public class CustomerServices
{
public const string CUSTOMER_EMAIL = "[email protected]";
public const string CUSTOMER_PASSWORD = "password";
public const string CUSTOMER_KEY = "Customer_Me_Key123";
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
Expand All @@ -30,7 +30,7 @@ public async Task<ICustomer> GetCustomerByKey(IClient adminClient,string key)

return null;
}
public async Task<ICustomer> CreateCustomer(IClient adminClient,CustomerDraft customerDraft)
public async Task<ICustomer> CreateCustomer(IClient adminClient,ICustomerDraft customerDraft)
{
var customerSignInResult = await adminClient.WithApi().WithProjectKey(GenericFixture.DefaultProjectKey)
.Customers()
Expand All @@ -39,5 +39,17 @@ public async Task<ICustomer> CreateCustomer(IClient adminClient,CustomerDraft cu
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 7f9ba91

Please sign in to comment.