Skip to content

Commit 3e47c84

Browse files
committed
Add integration test
1 parent 6dcc6f8 commit 3e47c84

5 files changed

Lines changed: 212 additions & 0 deletions

File tree

test/Services/IntegrationTests/IntegrationTests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
<Content Include="Services\Catalog\settings.json">
2121
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2222
</Content>
23+
<Content Include="Services\Marketing\appsettings.json">
24+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
25+
</Content>
2326
<Content Include="Services\Ordering\settings.json">
2427
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
2528
</Content>
@@ -28,6 +31,7 @@
2831
<ItemGroup>
2932
<ProjectReference Include="..\..\..\src\Services\Basket\Basket.API\Basket.API.csproj" />
3033
<ProjectReference Include="..\..\..\src\Services\Catalog\Catalog.API\Catalog.API.csproj" />
34+
<ProjectReference Include="..\..\..\src\Services\Marketing\Marketing.API\Marketing.API.csproj" />
3135
<ProjectReference Include="..\..\..\src\Services\Ordering\Ordering.API\Ordering.API.csproj" />
3236
</ItemGroup>
3337

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace IntegrationTests.Services.Marketing
2+
{
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.AspNetCore.TestHost;
5+
using System.IO;
6+
7+
public class MarketingScenarioBase
8+
{
9+
private const string _campaignsUrlBase = "api/v1/campaigns";
10+
11+
public TestServer CreateServer()
12+
{
13+
var webHostBuilder = new WebHostBuilder();
14+
webHostBuilder.UseContentRoot(Directory.GetCurrentDirectory() + "\\Services\\Marketing");
15+
webHostBuilder.UseStartup<MarketingTestsStartup>();
16+
17+
return new TestServer(webHostBuilder);
18+
}
19+
20+
public static class Get
21+
{
22+
public static string Campaigns = _campaignsUrlBase;
23+
24+
public static string CampaignBy(int id)
25+
=> $"{_campaignsUrlBase}/{id}";
26+
}
27+
28+
public static class Post
29+
{
30+
public static string AddNewCampaign = _campaignsUrlBase;
31+
}
32+
33+
public static class Put
34+
{
35+
public static string CampaignBy(int id)
36+
=> $"{_campaignsUrlBase}/{id}";
37+
}
38+
39+
public static class Delete
40+
{
41+
public static string CampaignBy(int id)
42+
=> $"{_campaignsUrlBase}/{id}";
43+
}
44+
}
45+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
namespace IntegrationTests.Services.Marketing
2+
{
3+
using System.Net.Http;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
using System;
8+
using Newtonsoft.Json;
9+
using Microsoft.eShopOnContainers.Services.Marketing.API.Model;
10+
using System.Collections.Generic;
11+
using System.Net;
12+
using Microsoft.eShopOnContainers.Services.Marketing.API.Dto;
13+
14+
public class MarketingScenarios
15+
: MarketingScenarioBase
16+
{
17+
[Fact]
18+
public async Task Get_get_all_campaigns_and_response_ok_status_code()
19+
{
20+
using (var server = CreateServer())
21+
{
22+
var response = await server.CreateClient()
23+
.GetAsync(Get.Campaigns);
24+
25+
response.EnsureSuccessStatusCode();
26+
}
27+
}
28+
29+
[Fact]
30+
public async Task Get_get_campaign_by_id_and_response_ok_status_code()
31+
{
32+
using (var server = CreateServer())
33+
{
34+
var response = await server.CreateClient()
35+
.GetAsync(Get.CampaignBy(1));
36+
37+
response.EnsureSuccessStatusCode();
38+
}
39+
}
40+
41+
[Fact]
42+
public async Task Get_get_campaign_by_id_and_response_not_found_status_code()
43+
{
44+
using (var server = CreateServer())
45+
{
46+
var response = await server.CreateClient()
47+
.GetAsync(Get.CampaignBy(9999999));
48+
49+
Assert.True(response.StatusCode == HttpStatusCode.NotFound);
50+
}
51+
}
52+
53+
[Fact]
54+
public async Task Post_add_new_campaign_and_response_ok_status_code()
55+
{
56+
using (var server = CreateServer())
57+
{
58+
var content = new StringContent(JsonConvert.SerializeObject(FakeCampaignDto), Encoding.UTF8, "application/json");
59+
var response = await server.CreateClient()
60+
.PostAsync(Post.AddNewCampaign, content);
61+
62+
response.EnsureSuccessStatusCode();
63+
}
64+
}
65+
66+
[Fact]
67+
public async Task Delete_delete_campaign_and_response_not_content_status_code()
68+
{
69+
using (var server = CreateServer())
70+
{
71+
var content = new StringContent(JsonConvert.SerializeObject(FakeCampaignDto), Encoding.UTF8, "application/json");
72+
73+
//add campaign
74+
var campaignResponse = await server.CreateClient()
75+
.PostAsync(Post.AddNewCampaign, content);
76+
77+
if (int.TryParse(campaignResponse.Headers.Location.Segments[4], out int id))
78+
{
79+
var response = await server.CreateClient()
80+
.DeleteAsync(Delete.CampaignBy(id));
81+
82+
Assert.True(response.StatusCode == HttpStatusCode.NoContent);
83+
}
84+
85+
campaignResponse.EnsureSuccessStatusCode();
86+
}
87+
}
88+
89+
[Fact]
90+
public async Task Put_update_campaign_and_response_not_content_status_code()
91+
{
92+
using (var server = CreateServer())
93+
{
94+
var content = new StringContent(JsonConvert.SerializeObject(FakeCampaignDto), Encoding.UTF8, "application/json");
95+
96+
//add campaign
97+
var campaignResponse = await server.CreateClient()
98+
.PostAsync(Post.AddNewCampaign, content);
99+
100+
if (int.TryParse(campaignResponse.Headers.Location.Segments[4], out int id))
101+
{
102+
FakeCampaignDto.Description = "FakeCampaignUpdatedDescription";
103+
content = new StringContent(JsonConvert.SerializeObject(FakeCampaignDto), Encoding.UTF8, "application/json");
104+
var response = await server.CreateClient()
105+
.PutAsync(Put.CampaignBy(id), content);
106+
107+
Assert.True(response.StatusCode == HttpStatusCode.Created);
108+
}
109+
110+
campaignResponse.EnsureSuccessStatusCode();
111+
}
112+
}
113+
114+
115+
private static CampaignDTO FakeCampaignDto = new CampaignDTO
116+
{
117+
Description = "FakeCampaignDescription",
118+
From = DateTime.Now,
119+
To = DateTime.Now.AddDays(7),
120+
Url = "http://CampaignUrl.test/fdaf91ad0cef5419719f50198",
121+
Rules = new List<RuleDTO>
122+
{
123+
new RuleDTO
124+
{
125+
LocationId = 1,
126+
Description = "testDescription",
127+
RuleTypeId = 3,
128+
}
129+
}
130+
};
131+
}
132+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace IntegrationTests.Services.Marketing
2+
{
3+
using Microsoft.eShopOnContainers.Services.Marketing.API;
4+
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.AspNetCore.Builder;
6+
using IntegrationTests.Middleware;
7+
8+
public class MarketingTestsStartup : Startup
9+
{
10+
public MarketingTestsStartup(IHostingEnvironment env) : base(env)
11+
{
12+
}
13+
14+
protected override void ConfigureAuth(IApplicationBuilder app)
15+
{
16+
if (Configuration["isTest"] == bool.TrueString.ToLowerInvariant())
17+
{
18+
app.UseMiddleware<AutoAuthorizeMiddleware>();
19+
}
20+
else
21+
{
22+
base.ConfigureAuth(app);
23+
}
24+
}
25+
}
26+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"ConnectionString": "Server=tcp:127.0.0.1,5433;Initial Catalog=Microsoft.eShopOnContainers.Services.MarketingDb;User Id=sa;Password=Pass@word",
3+
"IdentityUrl": "http://localhost:5105",
4+
"isTest": "true"
5+
}

0 commit comments

Comments
 (0)