Skip to content

Commit

Permalink
#6 Get base url from request rather than hard coding it
Browse files Browse the repository at this point in the history
  • Loading branch information
rouanw committed Dec 23, 2013
1 parent ea38d25 commit 07a06de
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 4 deletions.
1 change: 1 addition & 0 deletions BookWorm.Tests/BookWorm.Tests.csproj
Expand Up @@ -197,6 +197,7 @@
<Compile Include="Integration\Services\ConfigurationServiceTests.cs" />
<Compile Include="Models\Validations\ValidRoleTest.cs" />
<Compile Include="Services\EmailServiceTests.cs" />
<Compile Include="Services\UrlFormattingHelperTests.cs" />
<Compile Include="Specs\CreateAStaticPage.feature.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
Expand Down
20 changes: 19 additions & 1 deletion BookWorm.Tests/Services/EmailServiceTests.cs
Expand Up @@ -15,6 +15,7 @@ public class EmailServiceTests
private Mock<SmtpClientWrapper> _smtpClientWrapper;
private EmailService _emailService;
private Mock<ConfigurationService> _configService;
private Mock<CurrentHttpContextWrapper> _currentHttpContextWrapper;

[TestInitialize]
public void Setup()
Expand All @@ -23,10 +24,11 @@ public void Setup()
_smtpClientWrapper.Setup(it => it.Send(It.IsAny<MailMessage>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<bool>(),
It.IsAny<NetworkCredential>()));
_configService = new Mock<ConfigurationService>();
_currentHttpContextWrapper = new Mock<CurrentHttpContextWrapper>();
_configService.Setup(it => it.GetEmailSenderAddress()).Returns(string.Empty);
_configService.Setup(it => it.GetEmailSenderPassword()).Returns(string.Empty);
_configService.Setup(it => it.GetEmailServerAddress()).Returns(string.Empty);
_emailService = new EmailService(_smtpClientWrapper.Object, _configService.Object);
_emailService = new EmailService(_smtpClientWrapper.Object, _configService.Object, _currentHttpContextWrapper.Object);
}

[TestMethod]
Expand Down Expand Up @@ -77,9 +79,25 @@ private bool AssertMailIsCorrectlyConfigured(MailMessage mail)
return true;
}

[TestMethod]
public void ShouldGetBaseUrlFromCurrentHttpContextWrapper()
{
_currentHttpContextWrapper.Setup(it => it.GetBaseUrl()).Returns("someUrl");
_emailService.SendConfirmation("from@thoughtworks.com", "to@thoughtworks.com", "security", 1);
_smtpClientWrapper.Verify(it => it.Send(It.Is<MailMessage>(mail => AssertMailContainsBaseUrl(mail, "someUrl/Users/1/RegisterConfirmation/security")), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<bool>(),
It.IsAny<NetworkCredential>()));
}

private bool AssertMailContainsBaseUrl(MailMessage mail, string expectedUrl)
{
mail.Body.Should().Contain(expectedUrl);
return true;
}

[TestMethod]
public void ShouldSetContentOfMailMessageCorrectly()
{
_currentHttpContextWrapper.Setup(it => it.GetBaseUrl()).Returns("http://puku.co.za");
_emailService.SendConfirmation("from@thoughtworks.com", "to@thoughtworks.com", "security", 1);

_smtpClientWrapper.Verify(it => it.Send(It.Is<MailMessage>(mail => AssertMailContentIsCorrect(mail)), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<bool>(),
Expand Down
19 changes: 19 additions & 0 deletions BookWorm.Tests/Services/UrlFormattingHelperTests.cs
@@ -0,0 +1,19 @@
using System;
using BookWorm.Services.Email;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace BookWorm.Tests.Services
{
[TestClass]
public class UrlFormattingHelperTests
{
[TestMethod]
public void ShouldCombineSchemeAndAuthority()
{
var helper = new UrlFormattingHelper();
var baseUrl = helper.GetBaseUrl(new Uri("http://localhost:1234/soup"));
baseUrl.Should().Be("http://localhost:1234");
}
}
}
2 changes: 2 additions & 0 deletions BookWorm/BookWorm.csproj
Expand Up @@ -264,9 +264,11 @@
<Compile Include="Services\Account\AccountService.cs" />
<Compile Include="Services\Account\IAccountService.cs" />
<Compile Include="Services\ConfigurationService.cs" />
<Compile Include="Services\Email\CurrentHttpContextWrapper.cs" />
<Compile Include="Services\Email\EmailService.cs" />
<Compile Include="Services\Email\IEmailService.cs" />
<Compile Include="Services\Email\SmtpClientWrapper.cs" />
<Compile Include="Services\Email\UrlFormattingHelper.cs" />
<Compile Include="Services\FullTextSearch\FullTextSearchService.cs" />
<Compile Include="Services\FullTextSearch\IFullTextSearch.cs" />
<Compile Include="ViewModels\AuthorViewModel.cs" />
Expand Down
11 changes: 11 additions & 0 deletions BookWorm/Services/Email/CurrentHttpContextWrapper.cs
@@ -0,0 +1,11 @@
namespace BookWorm.Services.Email
{
public class CurrentHttpContextWrapper
{
public virtual string GetBaseUrl()
{
var urlFormattingHelper = new UrlFormattingHelper();
return urlFormattingHelper.GetBaseUrl(System.Web.HttpContext.Current.Request.Url);
}
}
}
8 changes: 5 additions & 3 deletions BookWorm/Services/Email/EmailService.cs
Expand Up @@ -6,24 +6,26 @@ namespace BookWorm.Services.Email
{
public class EmailService : IEmailService
{
private readonly CurrentHttpContextWrapper _currentHttpContextWrapper;
private readonly SmtpClientWrapper _smtpClient;
private ConfigurationService _configurationService;

private const int Port = 587;
public const string ConfirmationEmailSubject = "The administror of PUKU created a user for you";
private const string Template = @"Dear User,
The administrator of PUKU has created an account for you. To complete the registration process click on this link
http://puku.co.za/Users/{0}/RegisterConfirmation/{1}";
{0}/Users/{1}/RegisterConfirmation/{2}";

public EmailService(SmtpClientWrapper smtpClient = null, ConfigurationService configurationService = null)
public EmailService(SmtpClientWrapper smtpClient = null, ConfigurationService configurationService = null, CurrentHttpContextWrapper currentHttpContextWrapper = null)
{
_currentHttpContextWrapper = currentHttpContextWrapper ?? new CurrentHttpContextWrapper();
_smtpClient = smtpClient ?? new SmtpClientWrapper();
_configurationService = configurationService ?? new ConfigurationService();
}

public void SendConfirmation(string from, string to, string securityToken, int userId)
{
var mailMessage = new MailMessage(from, to, ConfirmationEmailSubject, string.Format(Template, userId, securityToken))
var mailMessage = new MailMessage(from, to, ConfirmationEmailSubject, string.Format(Template, _currentHttpContextWrapper.GetBaseUrl(), userId, securityToken))
{
BodyEncoding = Encoding.UTF8,
DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
Expand Down
12 changes: 12 additions & 0 deletions BookWorm/Services/Email/UrlFormattingHelper.cs
@@ -0,0 +1,12 @@
using System;

namespace BookWorm.Services.Email
{
public class UrlFormattingHelper
{
public virtual string GetBaseUrl(Uri url)
{
return string.Format("{0}://{1}", url.Scheme, url.Authority );
}
}
}

0 comments on commit 07a06de

Please sign in to comment.