Skip to content

Commit dda5c61

Browse files
author
dylan
committed
Working on mailservice integration tests
1 parent fb9a9cc commit dda5c61

10 files changed

Lines changed: 114 additions & 121 deletions

File tree

lambda/MAS.Tests/Infrastructure/TestAppSettings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ public static CMSConfig GetInvalidURI()
1010
{
1111
return new CMSConfig()
1212
{
13-
URI = new Uri("file://" + Directory.GetCurrentDirectory() + "/Feeds/nonexistanturl").ToString()
13+
BaseUrl = new Uri("file://" + Directory.GetCurrentDirectory() + "/Feeds/nonexistanturl").ToString()
1414
};
1515
}
1616

1717
public static CMSConfig GetMultipleItemsFeed()
1818
{
1919
return new CMSConfig()
2020
{
21-
URI = new Uri("file://" + Directory.GetCurrentDirectory() + "/Feeds/multiple-items.json").ToString()
21+
BaseUrl = new Uri("file://" + Directory.GetCurrentDirectory() + "/Feeds/multiple-items.json").ToString()
2222
};
2323
}
2424
}
Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,60 @@
11
using MAS.Configuration;
2+
using Microsoft.AspNetCore;
23
using Microsoft.AspNetCore.Hosting;
34
using Microsoft.AspNetCore.TestHost;
45
using Microsoft.Extensions.Configuration;
56
using System;
67
using System.Net.Http;
8+
using Microsoft.Extensions.DependencyInjection;
79

810
namespace MAS.Tests.Infrastructure
911
{
1012
public class TestBase : IDisposable
1113
{
14+
protected readonly IWebHostBuilder _builder;
1215
protected readonly TestServer _server;
1316
protected readonly HttpClient _client;
14-
protected readonly IConfigurationRoot _config;
17+
//protected readonly IConfigurationRoot _config;
1518

1619
public TestBase()
1720
{
18-
_config = new ConfigurationBuilder()
19-
.AddJsonFile("appsettings.json")
20-
.AddUserSecrets("adafe3d8-65fb-49fd-885e-03341a36dc88")
21-
.Build();
21+
//_config = new ConfigurationBuilder()
22+
// .AddJsonFile("appsettings.json")
23+
// //.AddUserSecrets("adafe3d8-65fb-49fd-885e-03341a36dc88")
24+
// .Build();
2225

23-
var builder = new WebHostBuilder()
26+
//var builder = new WebHostBuilder()
27+
_builder = WebHost.CreateDefaultBuilder(new string[0])
2428
.UseContentRoot("../../../../MAS")
2529
.ConfigureServices(services =>
2630
{
27-
AppSettings.Configure(services, _config);
31+
//AppSettings.Configure(services, _config);
2832
})
2933
.UseEnvironment("Production")
3034
.UseStartup(typeof(Startup));
31-
_server = new TestServer(builder);
32-
_client = _server.CreateClient();
35+
36+
_server = new TestServer(_builder);
37+
_client = _server.CreateClient();
38+
//_client.BaseAddress = new Uri("http://localhost:5000");
39+
}
40+
41+
protected void WithImplementation<TService, TImplementation>()
42+
where TService : class where TImplementation : class, TService
43+
{
44+
_builder.ConfigureServices(serviceCollection => serviceCollection.AddTransient<TService, TImplementation>());
45+
}
46+
47+
protected void WithImplementation<TService>(TService implementation)
48+
where TService : class
49+
{
50+
_builder.ConfigureServices(serviceCollection => serviceCollection.AddTransient(serviceProvier => implementation));
3351
}
3452

3553
public virtual void Dispose()
3654
{
3755
// Do "global" teardown here; Called after every test method.
56+
_client.Dispose();
57+
_server.Dispose();
3858
}
3959
}
4060
}

lambda/MAS.Tests/IntergrationTests/DailyEmailTests.cs

Lines changed: 69 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,17 @@
2222

2323
namespace MAS.Tests.IntergrationTests
2424
{
25-
public class FakeController : Microsoft.AspNetCore.Mvc.Controller
26-
{
25+
//public class FakeMailChimpManager : IMailChimpManager
26+
//{
2727

28-
}
28+
//}
2929

3030
public class DailyEmailTests : TestBase
3131
{
32-
IMailService MailService;
33-
34-
public DailyEmailTests(IMailService mailService)
35-
{
3632

37-
MailService = mailService;
38-
}
3933

40-
public override void Dispose()
34+
public DailyEmailTests()
4135
{
42-
4336
}
4437

4538
Item exampleItem = new Item()
@@ -99,56 +92,82 @@ public override void Dispose()
9992
}
10093
};
10194

102-
//[Fact]
103-
//public void CanCreateSingleItemEmail()
104-
//{
95+
[Fact]
96+
public async void SomeTest()
97+
{
98+
var fakeMailService = new Mock<IMailService>();
99+
100+
string bodyHtml = string.Empty;
101+
fakeMailService.Setup(s => s.CreateAndSendDailyAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
102+
.Callback<string, string, string>((subject, previewText, body) => bodyHtml = body)
103+
.ReturnsAsync("1234");
104+
105+
WithImplementation(fakeMailService);
106+
107+
await _client.PutAsync("/api/mail/daily", null);
108+
109+
bodyHtml.ShouldMatchApproved();
110+
}
111+
105112

106-
// var items = new List<Item> { exampleItem };
107-
// var actualHtml = this.MailService.CreateDailyEmailBody(items, mockController.Object);
108113

109-
// actualHtml.ShouldMatchApproved();
110114

111-
//}
112115

113-
//[Fact]
114-
//public void CanCreateEmailWithTwoItemsSharingEvidenceType()
115-
//{
116-
// var items = new List<Item> { exampleItem, exampleItem };
117-
// var actualHtml = this.MailService.CreateDailyEmailBody(items, new FakeController());
118116

119-
// actualHtml.ShouldMatchApproved();
120117

121-
//}
122118

123-
//[Fact]
124-
//public void CanCreateEmailWithTwoItemsDifferentEvidenceType()
125-
//{
126-
// var items = new List<Item> { exampleItem, exampleItem2 };
127-
// var actualHtml = "";
128-
// try
129-
// {
130-
// actualHtml = this.MailService.CreateDailyEmailBody(items, new FakeController());
131-
// }
132-
// catch(Exception e)
133-
// {
134-
// var p = e.Message;
135-
// }
136-
137119

138-
// actualHtml.ShouldMatchApproved();
139120

140-
//}
121+
//[Fact]
122+
//public void CanCreateSingleItemEmail()
123+
//{
141124

142-
//[Fact]
143-
//public void ItemsWithManySpecialitiesRenderCorrectly()
144-
//{
145-
// exampleItem.Specialities.Add(new Speciality() { Key = "abcd", Title = "Another speciality" });
146-
// var items = new List<Item> { exampleItem };
147-
// var actualHtml = this.MailService.CreateDailyEmailBody(items, new FakeController());
125+
// var items = new List<Item> { exampleItem };
126+
// var actualHtml = this.MailService.CreateDailyEmailBody(items, mockController.Object);
148127

149-
// actualHtml.ShouldMatchApproved();
128+
// actualHtml.ShouldMatchApproved();
150129

151-
//}
130+
//}
152131

153-
}
132+
//[Fact]
133+
//public void CanCreateEmailWithTwoItemsSharingEvidenceType()
134+
//{
135+
// var items = new List<Item> { exampleItem, exampleItem };
136+
// var actualHtml = this.MailService.CreateDailyEmailBody(items, new FakeController());
137+
138+
// actualHtml.ShouldMatchApproved();
139+
140+
//}
141+
142+
//[Fact]
143+
//public void CanCreateEmailWithTwoItemsDifferentEvidenceType()
144+
//{
145+
// var items = new List<Item> { exampleItem, exampleItem2 };
146+
// var actualHtml = "";
147+
// try
148+
// {
149+
// actualHtml = this.MailService.CreateDailyEmailBody(items, new FakeController());
150+
// }
151+
// catch(Exception e)
152+
// {
153+
// var p = e.Message;
154+
// }
155+
156+
157+
// actualHtml.ShouldMatchApproved();
158+
159+
//}
160+
161+
//[Fact]
162+
//public void ItemsWithManySpecialitiesRenderCorrectly()
163+
//{
164+
// exampleItem.Specialities.Add(new Speciality() { Key = "abcd", Title = "Another speciality" });
165+
// var items = new List<Item> { exampleItem };
166+
// var actualHtml = this.MailService.CreateDailyEmailBody(items, new FakeController());
167+
168+
// actualHtml.ShouldMatchApproved();
169+
170+
//}
171+
172+
}
154173
}

lambda/MAS.Tests/MAS.Tests.csproj

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,10 @@
1010
</Content>
1111
</ItemGroup>
1212
<ItemGroup>
13-
<None Remove="appsettings.Development.json" />
14-
<None Remove="appsettings.json" />
1513
<None Remove="Feeds\multiple-items.json" />
1614
<None Remove="Feeds\single-item.json5daeb5af22565a82530d7373" />
1715
<None Remove="single-item.json" />
1816
</ItemGroup>
19-
<ItemGroup>
20-
<Content Include="appsettings.Development.json">
21-
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
22-
</Content>
23-
<Content Include="appsettings.json">
24-
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
25-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
26-
</Content>
27-
</ItemGroup>
2817
<ItemGroup>
2918
<EmbeddedResource Include="Feeds\multiple-items.json">
3019
<CopyToOutputDirectory>Always</CopyToOutputDirectory>

lambda/MAS.Tests/UnitTests/Services/ContentServiceTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public async Task ReadMultipleItems()
2323
var contentService = new ContentService(mockLogger.Object);
2424

2525
//Act
26-
var result = await contentService.GetItemsAsync();
26+
var result = await contentService.GetAllItemsAsync();
2727

2828
//Assert
2929
result.Count().ShouldBe(4);
@@ -43,7 +43,7 @@ public async Task InvalidURIThrowsError()
4343
var contentService = new ContentService(mockLogger.Object);
4444

4545
//Act + Assert
46-
await Should.ThrowAsync<Exception>(() => contentService.GetItemsAsync());
46+
await Should.ThrowAsync<Exception>(() => contentService.GetAllItemsAsync());
4747
}
4848
}
4949
}

lambda/MAS.Tests/appsettings.Development.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

lambda/MAS.Tests/appsettings.json

Lines changed: 0 additions & 35 deletions
This file was deleted.

lambda/MAS/Configuration/CMSConfig.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
{
33
public class CMSConfig
44
{
5-
public string URI { get; set; }
5+
public string BaseUrl { get; set; }
6+
public string AllItemsPath { get; set; }
7+
public string DailyItemsPath { get; set; }
68
}
79
}

lambda/MAS/Services/ContentService.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace MAS.Services
1111
{
1212
public interface IContentService
1313
{
14-
Task<IEnumerable<Item>> GetItemsAsync();
14+
Task<IEnumerable<Item>> GetAllItemsAsync();
1515
Task<IEnumerable<Item>> GetDailyItemsAsync(DateTime? date = null);
1616
}
1717

@@ -24,13 +24,13 @@ public ContentService(ILogger<ContentService> logger)
2424
_logger = logger;
2525
}
2626

27-
public async Task<IEnumerable<Item>> GetItemsAsync()
27+
public async Task<IEnumerable<Item>> GetAllItemsAsync()
2828
{
2929
using (WebClient client = new WebClient())
3030
{
3131
try
3232
{
33-
var jsonStr = await client.DownloadStringTaskAsync(new Uri(AppSettings.CMSConfig.URI));
33+
var jsonStr = await client.DownloadStringTaskAsync(new Uri(AppSettings.CMSConfig.BaseUrl + AppSettings.CMSConfig.AllItemsPath));
3434
var item = JsonConvert.DeserializeObject<Item[]>(jsonStr);
3535
return item;
3636
}
@@ -48,9 +48,10 @@ public async Task<IEnumerable<Item>> GetDailyItemsAsync(DateTime? date = null)
4848

4949
using (WebClient client = new WebClient())
5050
{
51+
var path = string.Format(AppSettings.CMSConfig.DailyItemsPath, date.Value.ToString("yyyy-MM-dd"));
5152
try
5253
{
53-
var jsonStr = await client.DownloadStringTaskAsync(new Uri(AppSettings.CMSConfig.URI)); //TODO Use date
54+
var jsonStr = await client.DownloadStringTaskAsync(new Uri(AppSettings.CMSConfig.BaseUrl + path));
5455
var item = JsonConvert.DeserializeObject<Item[]>(jsonStr);
5556
return item;
5657
}

lambda/MAS/appsettings.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
"StaticURL": ""
2727
},
2828
"CMS": {
29-
"URI": "don't add this or anything else that needs securing. see here: https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?tabs=visual-studio"
29+
"BaseUrl": "don't add this or anything else that needs securing. see here: https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?tabs=visual-studio",
30+
"AllItemsPath": "/items",
31+
"DailyItemsPath": "/items/daily/{0}"
3032
},
3133
"MailChimp": {
3234
"ApiKey": "don't add this or anything else that needs securing. see here: https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?tabs=visual-studio",
3335
"ListId": "",
34-
"DailyTemplateId": "",
36+
"DailyTemplateId": 0,
3537
"CampaignFolderId": ""
3638
}
3739
}

0 commit comments

Comments
 (0)