diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 45a73dffbc4..b12593263e5 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/Me/MeIntegrationTests.cs b/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Me/MeIntegrationTests.cs new file mode 100644 index 00000000000..1173e0d1ea5 --- /dev/null +++ b/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Me/MeIntegrationTests.cs @@ -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(); + _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(); + var serializerService = _serviceProviderFixture.GetService(); + + //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); + } + } +} \ No newline at end of file diff --git a/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Services/CustomerServices.cs b/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Services/CustomerServices.cs new file mode 100644 index 00000000000..b20532ff605 --- /dev/null +++ b/commercetools.Sdk/IntegrationTests/commercetools.Api.IntegrationTests/Services/CustomerServices.cs @@ -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 = "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 + { + 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,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" + }; + } + } +} \ 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