Add integration test
This commit is contained in:
parent
ae7f1e8e7a
commit
21dc4e3772
@ -20,6 +20,9 @@
|
|||||||
<Content Include="Services\Catalog\settings.json">
|
<Content Include="Services\Catalog\settings.json">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</Content>
|
</Content>
|
||||||
|
<Content Include="Services\Marketing\appsettings.json">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
<Content Include="Services\Ordering\settings.json">
|
<Content Include="Services\Ordering\settings.json">
|
||||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
||||||
</Content>
|
</Content>
|
||||||
@ -28,6 +31,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\..\src\Services\Basket\Basket.API\Basket.API.csproj" />
|
<ProjectReference Include="..\..\..\src\Services\Basket\Basket.API\Basket.API.csproj" />
|
||||||
<ProjectReference Include="..\..\..\src\Services\Catalog\Catalog.API\Catalog.API.csproj" />
|
<ProjectReference Include="..\..\..\src\Services\Catalog\Catalog.API\Catalog.API.csproj" />
|
||||||
|
<ProjectReference Include="..\..\..\src\Services\Marketing\Marketing.API\Marketing.API.csproj" />
|
||||||
<ProjectReference Include="..\..\..\src\Services\Ordering\Ordering.API\Ordering.API.csproj" />
|
<ProjectReference Include="..\..\..\src\Services\Ordering\Ordering.API\Ordering.API.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@ -0,0 +1,45 @@
|
|||||||
|
namespace IntegrationTests.Services.Marketing
|
||||||
|
{
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.TestHost;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
public class MarketingScenarioBase
|
||||||
|
{
|
||||||
|
private const 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 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,132 @@
|
|||||||
|
namespace IntegrationTests.Services.Marketing
|
||||||
|
{
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
using System;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Marketing.API.Model;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Marketing.API.Dto;
|
||||||
|
|
||||||
|
public class MarketingScenarios
|
||||||
|
: MarketingScenarioBase
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Get_get_all_campaigns_and_response_ok_status_code()
|
||||||
|
{
|
||||||
|
using (var server = CreateServer())
|
||||||
|
{
|
||||||
|
var response = await server.CreateClient()
|
||||||
|
.GetAsync(Get.Campaigns);
|
||||||
|
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Get_get_campaign_by_id_and_response_ok_status_code()
|
||||||
|
{
|
||||||
|
using (var server = CreateServer())
|
||||||
|
{
|
||||||
|
var response = await server.CreateClient()
|
||||||
|
.GetAsync(Get.CampaignBy(1));
|
||||||
|
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Get_get_campaign_by_id_and_response_not_found_status_code()
|
||||||
|
{
|
||||||
|
using (var server = CreateServer())
|
||||||
|
{
|
||||||
|
var response = await server.CreateClient()
|
||||||
|
.GetAsync(Get.CampaignBy(9999999));
|
||||||
|
|
||||||
|
Assert.True(response.StatusCode == HttpStatusCode.NotFound);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Post_add_new_campaign_and_response_ok_status_code()
|
||||||
|
{
|
||||||
|
using (var server = CreateServer())
|
||||||
|
{
|
||||||
|
var content = new StringContent(JsonConvert.SerializeObject(FakeCampaignDto), Encoding.UTF8, "application/json");
|
||||||
|
var response = await server.CreateClient()
|
||||||
|
.PostAsync(Post.AddNewCampaign, content);
|
||||||
|
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Delete_delete_campaign_and_response_not_content_status_code()
|
||||||
|
{
|
||||||
|
using (var server = CreateServer())
|
||||||
|
{
|
||||||
|
var content = new StringContent(JsonConvert.SerializeObject(FakeCampaignDto), Encoding.UTF8, "application/json");
|
||||||
|
|
||||||
|
//add campaign
|
||||||
|
var campaignResponse = await server.CreateClient()
|
||||||
|
.PostAsync(Post.AddNewCampaign, content);
|
||||||
|
|
||||||
|
if (int.TryParse(campaignResponse.Headers.Location.Segments[4], out int id))
|
||||||
|
{
|
||||||
|
var response = await server.CreateClient()
|
||||||
|
.DeleteAsync(Delete.CampaignBy(id));
|
||||||
|
|
||||||
|
Assert.True(response.StatusCode == HttpStatusCode.NoContent);
|
||||||
|
}
|
||||||
|
|
||||||
|
campaignResponse.EnsureSuccessStatusCode();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Put_update_campaign_and_response_not_content_status_code()
|
||||||
|
{
|
||||||
|
using (var server = CreateServer())
|
||||||
|
{
|
||||||
|
var content = new StringContent(JsonConvert.SerializeObject(FakeCampaignDto), Encoding.UTF8, "application/json");
|
||||||
|
|
||||||
|
//add campaign
|
||||||
|
var campaignResponse = await server.CreateClient()
|
||||||
|
.PostAsync(Post.AddNewCampaign, content);
|
||||||
|
|
||||||
|
if (int.TryParse(campaignResponse.Headers.Location.Segments[4], out int id))
|
||||||
|
{
|
||||||
|
FakeCampaignDto.Description = "FakeCampaignUpdatedDescription";
|
||||||
|
content = new StringContent(JsonConvert.SerializeObject(FakeCampaignDto), Encoding.UTF8, "application/json");
|
||||||
|
var response = await server.CreateClient()
|
||||||
|
.PutAsync(Put.CampaignBy(id), content);
|
||||||
|
|
||||||
|
Assert.True(response.StatusCode == HttpStatusCode.Created);
|
||||||
|
}
|
||||||
|
|
||||||
|
campaignResponse.EnsureSuccessStatusCode();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static CampaignDTO FakeCampaignDto = new CampaignDTO
|
||||||
|
{
|
||||||
|
Description = "FakeCampaignDescription",
|
||||||
|
From = DateTime.Now,
|
||||||
|
To = DateTime.Now.AddDays(7),
|
||||||
|
Url = "http://CampaignUrl.test/fdaf91ad0cef5419719f50198",
|
||||||
|
Rules = new List<RuleDTO>
|
||||||
|
{
|
||||||
|
new RuleDTO
|
||||||
|
{
|
||||||
|
LocationId = 1,
|
||||||
|
Description = "testDescription",
|
||||||
|
RuleTypeId = 3,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
namespace IntegrationTests.Services.Marketing
|
||||||
|
{
|
||||||
|
using Microsoft.eShopOnContainers.Services.Marketing.API;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using IntegrationTests.Middleware;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"ConnectionString": "Server=tcp:127.0.0.1,5433;Initial Catalog=Microsoft.eShopOnContainers.Services.MarketingDb;User Id=sa;Password=Pass@word",
|
||||||
|
"IdentityUrl": "http://localhost:5105",
|
||||||
|
"isTest": "true"
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user