-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from commercetools/Me-IntegrationTests
Create Me Integration Test to get My Customer Profile
- Loading branch information
Showing
4 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...rcetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Me/MeIntegrationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...ools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Services/CustomerServices.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}; | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
....Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Services/ShoppingListServices.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} | ||
}; | ||
} | ||
} | ||
} |