From 827fd79f94149bb413fa7a2a1bb33769ee6cddef Mon Sep 17 00:00:00 2001 From: Michele George Date: Fri, 7 May 2021 13:51:40 +0200 Subject: [PATCH 1/2] Create Me Integration Test to get My Customer Profile --- .github/workflows/ci.yml | 12 +++ .../CustomerServices.cs | 43 +++++++++ .../Me/MeIntegrationTests.cs | 94 +++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/CustomerServices.cs create mode 100644 commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Me/MeIntegrationTests.cs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 316192cb15a..62a95ef7338 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -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 @@ -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 diff --git a/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/CustomerServices.cs b/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/CustomerServices.cs new file mode 100644 index 00000000000..7e4977c2bc2 --- /dev/null +++ b/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/CustomerServices.cs @@ -0,0 +1,43 @@ +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 +{ + public class CustomerServices + { + public const string CUSTOMER_EMAIL = "Customer_Me@gmail.com"; + public const string CUSTOMER_PASSWORD = "password"; + public const string CUSTOMER_KEY = "Customer_Me_Key123"; + public async Task 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 CreateCustomer(IClient adminClient,CustomerDraft customerDraft) + { + var customerSignInResult = await adminClient.WithApi().WithProjectKey(GenericFixture.DefaultProjectKey) + .Customers() + .Post(customerDraft) + .ExecuteAsync(); + var customer = customerSignInResult.Customer; + return customer; + } + } +} \ No newline at end of file diff --git a/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Me/MeIntegrationTests.cs b/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Me/MeIntegrationTests.cs new file mode 100644 index 00000000000..8e37e8d1584 --- /dev/null +++ b/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Me/MeIntegrationTests.cs @@ -0,0 +1,94 @@ +using System; +using System.Net.Http; +using System.Threading.Tasks; +using commercetools.Api.Models.Customers; +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 +{ + [Collection("Integration Tests")] + public class MeIntegrationTests + { + #region Fields + + private readonly IClient _adminClient; + private readonly CustomerServices _customerServices; + private readonly ServiceProviderFixture _serviceProviderFixture; + private IClient _meClient; + private ICustomer _customer = null; + + #endregion + + public MeIntegrationTests(ServiceProviderFixture serviceProviderFixture) + { + this._serviceProviderFixture = serviceProviderFixture; + this._adminClient = serviceProviderFixture.GetService(); + _customerServices = new CustomerServices(); + } + + [Fact] + public async Task GetMyProfile() + { + //Arrange + await CreateEntitiesForTesting(); + CreateMeClient(); + + //Act + var myProfile = await _meClient.WithApi() + .WithProjectKey(GenericFixture.DefaultProjectKey) + .Me() + .Get() + .ExecuteAsync(); + + //Assert + Assert.NotNull(myProfile); + Assert.Equal(CustomerServices.CUSTOMER_EMAIL, myProfile.Email); + } + + private async Task CreateEntitiesForTesting() + { + //Get Customer By Key, Create if not exists + _customer = await _customerServices.GetCustomerByKey(_adminClient, CustomerServices.CUSTOMER_KEY); + 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" + }; + _customer = await _customerServices.CreateCustomer(_adminClient, customerDraft); + } + } + + private void CreateMeClient() + { + var meClientConfig= _serviceProviderFixture.GetClientConfiguration("MeClient"); + var httpClientFactory = _serviceProviderFixture.GetService(); + var serializerService = _serviceProviderFixture.GetService(); + + //Create passwordFlow TokenProvider + var passwordTokenProvider = TokenProviderFactory + .CreatePasswordTokenProvider(meClientConfig, + httpClientFactory, + new InMemoryUserCredentialsStoreManager( + CustomerServices.CUSTOMER_EMAIL, + CustomerServices.CUSTOMER_PASSWORD)); + + //Create MeClient + _meClient = ClientFactory.Create( + "MeClient", + meClientConfig, + httpClientFactory, + serializerService, + passwordTokenProvider); + } + } +} \ No newline at end of file From 7f9ba911dc1c2a4d2350173b8ed2b37dbf3abe90 Mon Sep 17 00:00:00 2001 From: Michele George Date: Tue, 11 May 2021 14:23:09 +0200 Subject: [PATCH 2/2] Adding Integration Testing for Get My ShoppingLists --- .../Me/MeIntegrationTests.cs | 60 ++++++++++++++----- .../{ => Services}/CustomerServices.cs | 22 +++++-- .../Services/ShoppingListServices.cs | 56 +++++++++++++++++ 3 files changed, 118 insertions(+), 20 deletions(-) rename commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/{ => Services}/CustomerServices.cs (65%) create mode 100644 commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Services/ShoppingListServices.cs diff --git a/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Me/MeIntegrationTests.cs b/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Me/MeIntegrationTests.cs index 8e37e8d1584..1173e0d1ea5 100644 --- a/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Me/MeIntegrationTests.cs +++ b/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Me/MeIntegrationTests.cs @@ -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 @@ -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; @@ -29,6 +31,8 @@ public MeIntegrationTests(ServiceProviderFixture serviceProviderFixture) this._serviceProviderFixture = serviceProviderFixture; this._adminClient = serviceProviderFixture.GetService(); _customerServices = new CustomerServices(); + _shoppingListServices = new ShoppingListServices(); + CreateMeClient(); } [Fact] @@ -36,7 +40,6 @@ public async Task GetMyProfile() { //Arrange await CreateEntitiesForTesting(); - CreateMeClient(); //Act var myProfile = await _meClient.WithApi() @@ -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() @@ -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( diff --git a/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/CustomerServices.cs b/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Services/CustomerServices.cs similarity index 65% rename from commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/CustomerServices.cs rename to commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Services/CustomerServices.cs index 7e4977c2bc2..b20532ff605 100644 --- a/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/CustomerServices.cs +++ b/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Services/CustomerServices.cs @@ -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 = "Customer_Me@gmail.com"; - public const string CUSTOMER_PASSWORD = "password"; - public const string CUSTOMER_KEY = "Customer_Me_Key123"; + public const string CustomerEmail = "Customer_Me@gmail.com"; + public const string CustomerPassword = "password"; + public const string CustomerKey = "Customer_Me_Key123"; public async Task GetCustomerByKey(IClient adminClient,string key) { try @@ -30,7 +30,7 @@ public async Task GetCustomerByKey(IClient adminClient,string key) return null; } - public async Task CreateCustomer(IClient adminClient,CustomerDraft customerDraft) + public async Task CreateCustomer(IClient adminClient,ICustomerDraft customerDraft) { var customerSignInResult = await adminClient.WithApi().WithProjectKey(GenericFixture.DefaultProjectKey) .Customers() @@ -39,5 +39,17 @@ public async Task 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" + }; + } } } \ No newline at end of file diff --git a/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Services/ShoppingListServices.cs b/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Services/ShoppingListServices.cs new file mode 100644 index 00000000000..96abeb2565b --- /dev/null +++ b/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Services/ShoppingListServices.cs @@ -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 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 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} + } + }; + } + } +} \ No newline at end of file