Add Functional Test to marketing
This commit is contained in:
parent
358227aa44
commit
dd8c6b5d0b
@ -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<LocationsTestsStartup>();
|
||||||
|
|
||||||
|
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/";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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<LocationAuthorizeMiddleware>();
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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<UserLocation>(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<UserLocationDTO>(responseBody);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(userLocation.LocationId, userLocationCampaign.LocationId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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<MarketingTestsStartup>();
|
||||||
|
|
||||||
|
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}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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<AutoAuthorizeMiddleware>();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
base.ConfigureAuth(app);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user