From 31fd93dafede7deecba5c24981146778a7b43685 Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Tue, 13 Jun 2017 17:33:30 +0200 Subject: [PATCH] Add Functional Test to marketing --- .../Locations/LocationsScenariosBase.cs | 39 +++++++++++ .../Locations/LocationsTestsStartup.cs | 45 +++++++++++++ .../Services/Marketing/MarketingScenarios.cs | 65 +++++++++++++++++++ .../Marketing/MarketingScenariosBase.cs | 48 ++++++++++++++ .../Marketing/MarketingTestsStartup.cs | 26 ++++++++ 5 files changed, 223 insertions(+) create mode 100644 test/Services/FunctionalTests/Services/Locations/LocationsScenariosBase.cs create mode 100644 test/Services/FunctionalTests/Services/Locations/LocationsTestsStartup.cs create mode 100644 test/Services/FunctionalTests/Services/Marketing/MarketingScenarios.cs create mode 100644 test/Services/FunctionalTests/Services/Marketing/MarketingScenariosBase.cs create mode 100644 test/Services/FunctionalTests/Services/Marketing/MarketingTestsStartup.cs diff --git a/test/Services/FunctionalTests/Services/Locations/LocationsScenariosBase.cs b/test/Services/FunctionalTests/Services/Locations/LocationsScenariosBase.cs new file mode 100644 index 000000000..d63f3d4e7 --- /dev/null +++ b/test/Services/FunctionalTests/Services/Locations/LocationsScenariosBase.cs @@ -0,0 +1,39 @@ +namespace FunctionalTests.Services.Locations +{ + using Microsoft.AspNetCore.Hosting; + using Microsoft.AspNetCore.TestHost; + using System; + using System.IO; + + public class LocationsScenariosBase + { + public TestServer CreateServer() + { + var webHostBuilder = new WebHostBuilder(); + webHostBuilder.UseContentRoot(Directory.GetCurrentDirectory() + "\\Services\\Locations"); + webHostBuilder.UseStartup(); + + return new TestServer(webHostBuilder); + } + + public static class Get + { + public static string Locations = "api/v1/locations"; + + public static string LocationBy(string id) + { + return $"api/v1/locations/{id}"; + } + + public static string UserLocationBy(Guid id) + { + return $"api/v1/locations/user/{id}"; + } + } + + public static class Post + { + public static string AddNewLocation = "api/v1/locations/"; + } + } +} diff --git a/test/Services/FunctionalTests/Services/Locations/LocationsTestsStartup.cs b/test/Services/FunctionalTests/Services/Locations/LocationsTestsStartup.cs new file mode 100644 index 000000000..b80408388 --- /dev/null +++ b/test/Services/FunctionalTests/Services/Locations/LocationsTestsStartup.cs @@ -0,0 +1,45 @@ +namespace FunctionalTests.Services.Locations +{ + using Microsoft.AspNetCore.Builder; + using Microsoft.AspNetCore.Hosting; + using Microsoft.AspNetCore.Http; + using Microsoft.eShopOnContainers.Services.Locations.API; + using System.Security.Claims; + using System.Threading.Tasks; + + public class LocationsTestsStartup : Startup + { + public LocationsTestsStartup(IHostingEnvironment env) : base(env) + { + } + + protected override void ConfigureAuth(IApplicationBuilder app) + { + if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant()) + { + app.UseMiddleware(); + } + else + { + base.ConfigureAuth(app); + } + } + + class LocationAuthorizeMiddleware + { + private readonly RequestDelegate _next; + public LocationAuthorizeMiddleware(RequestDelegate rd) + { + _next = rd; + } + + public async Task Invoke(HttpContext httpContext) + { + var identity = new ClaimsIdentity("cookies"); + identity.AddClaim(new Claim("sub", "4611ce3f-380d-4db5-8d76-87a8689058ed")); + httpContext.User.AddIdentity(identity); + await _next.Invoke(httpContext); + } + } + } +} diff --git a/test/Services/FunctionalTests/Services/Marketing/MarketingScenarios.cs b/test/Services/FunctionalTests/Services/Marketing/MarketingScenarios.cs new file mode 100644 index 000000000..3f3c86bef --- /dev/null +++ b/test/Services/FunctionalTests/Services/Marketing/MarketingScenarios.cs @@ -0,0 +1,65 @@ +namespace FunctionalTests.Services.Marketing +{ + using UserLocationDTO = Microsoft.eShopOnContainers.Services.Marketing.API.Dto.UserLocationDTO; + using UserLocation = Microsoft.eShopOnContainers.Services.Locations.API.Model.UserLocation; + using LocationRequest = Microsoft.eShopOnContainers.Services.Locations.API.ViewModel.LocationRequest; + using FunctionalTests.Extensions; + using FunctionalTests.Services.Basket; + using FunctionalTests.Services.Locations; + using Microsoft.eShopOnContainers.Services.Basket.API.Model; + using Microsoft.eShopOnContainers.WebMVC.ViewModels; + using Newtonsoft.Json; + using System; + using System.Collections.Generic; + using System.Linq; + using System.Net.Http; + using System.Text; + using System.Threading.Tasks; + using WebMVC.Models; + using Xunit; + + public class MarketingScenarios : MarketingScenariosBase + { + [Fact] + public async Task Set_new_user_location_and_get_location_campaign_by_user_id() + { + using (var locationsServer = new LocationsScenariosBase().CreateServer()) + using (var marketingServer = CreateServer()) + { + var location = new LocationRequest + { + Longitude = -122.315752, + Latitude = 47.604610 + }; + var content = new StringContent(JsonConvert.SerializeObject(location), + Encoding.UTF8, "application/json"); + + var userId = new Guid("4611ce3f-380d-4db5-8d76-87a8689058ed"); + + + // GIVEN a new location of user is created + var response = await locationsServer.CreateClient() + .PostAsync(LocationsScenariosBase.Post.AddNewLocation, content); + + //Get location user from Location.API + var userLocationResponse = await locationsServer.CreateClient() + .GetAsync(LocationsScenariosBase.Get.UserLocationBy(userId)); + + var responseBody = await userLocationResponse.Content.ReadAsStringAsync(); + var userLocation = JsonConvert.DeserializeObject(responseBody); + + await Task.Delay(5000); + + //Get campaing from Marketing.API given a userId + var UserLocationCampaignResponse = await locationsServer.CreateClient() + .GetAsync(Get.UserCampaignByUserId(userId)); + + responseBody = await UserLocationCampaignResponse.Content.ReadAsStringAsync(); + var userLocationCampaign = JsonConvert.DeserializeObject(responseBody); + + // Assert + Assert.Equal(userLocation.LocationId, userLocationCampaign.LocationId); + } + } + } +} diff --git a/test/Services/FunctionalTests/Services/Marketing/MarketingScenariosBase.cs b/test/Services/FunctionalTests/Services/Marketing/MarketingScenariosBase.cs new file mode 100644 index 000000000..7bb17f752 --- /dev/null +++ b/test/Services/FunctionalTests/Services/Marketing/MarketingScenariosBase.cs @@ -0,0 +1,48 @@ +namespace FunctionalTests.Services.Marketing +{ + using Microsoft.AspNetCore.Hosting; + using Microsoft.AspNetCore.TestHost; + using System.IO; + + public class MarketingScenariosBase + { + public static string CampaignsUrlBase => "api/v1/campaigns"; + + public TestServer CreateServer() + { + var webHostBuilder = new WebHostBuilder(); + webHostBuilder.UseContentRoot(Directory.GetCurrentDirectory() + "\\Services\\Marketing"); + webHostBuilder.UseStartup(); + + return new TestServer(webHostBuilder); + } + + public static class Get + { + public static string Campaigns = CampaignsUrlBase; + + public static string CampaignBy(int id) + => $"{CampaignsUrlBase}/{id}"; + + public static string UserCampaignByUserId(System.Guid userId) + => $"{CampaignsUrlBase}/user/{userId}"; + } + + public static class Post + { + public static string AddNewCampaign = CampaignsUrlBase; + } + + public static class Put + { + public static string CampaignBy(int id) + => $"{CampaignsUrlBase}/{id}"; + } + + public static class Delete + { + public static string CampaignBy(int id) + => $"{CampaignsUrlBase}/{id}"; + } + } +} diff --git a/test/Services/FunctionalTests/Services/Marketing/MarketingTestsStartup.cs b/test/Services/FunctionalTests/Services/Marketing/MarketingTestsStartup.cs new file mode 100644 index 000000000..0518eeb78 --- /dev/null +++ b/test/Services/FunctionalTests/Services/Marketing/MarketingTestsStartup.cs @@ -0,0 +1,26 @@ +using FunctionalTests.Middleware; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.eShopOnContainers.Services.Ordering.API; + +namespace FunctionalTests.Services.Marketing +{ + public class MarketingTestsStartup : Startup + { + public MarketingTestsStartup(IHostingEnvironment env) : base(env) + { + } + + protected override void ConfigureAuth(IApplicationBuilder app) + { + if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant()) + { + app.UseMiddleware(); + } + else + { + base.ConfigureAuth(app); + } + } + } +}