Skip to content

Commit

Permalink
Merge branch 'master' of github.com:GiveCampUK/GiveCRM
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonySteele committed Oct 30, 2011
2 parents 962a2d8 + 05bfc80 commit 0dc4923
Show file tree
Hide file tree
Showing 20 changed files with 674 additions and 154 deletions.
Binary file modified src/GiveCRM.DataAccess.Test/TestDB.sdf
Binary file not shown.
2 changes: 1 addition & 1 deletion src/GiveCRM.Database/GiveCRM.Database.dbproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<OutputPath>.\sql\debug\</OutputPath>
<BuildScriptName>$(MSBuildProjectName).sql</BuildScriptName>
<TargetConnectionString>Data Source=.;Integrated Security=True;Pooling=False</TargetConnectionString>
<TargetConnectionString>Data Source=(local);Integrated Security=True;Pooling=False</TargetConnectionString>
<TargetDatabase>GiveCRM</TargetDatabase>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
<SuppressWarnings>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<TargetConnectionString>Data Source=.;Integrated Security=True;Pooling=False</TargetConnectionString>
<TargetConnectionString>Data Source=(local);Integrated Security=True;Pooling=False</TargetConnectionString>
<TargetDatabase>GiveCRM</TargetDatabase>
<DeployToDatabase>True</DeployToDatabase>
<DeployToScript>True</DeployToScript>
Expand Down
7 changes: 7 additions & 0 deletions src/GiveCRM.Web.Tests/GiveCRM.Web.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="Moq">
<HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
</Reference>
<Reference Include="MvcContrib.TestHelper">
<HintPath>..\packages\MvcContrib.Mvc3.TestHelper-ci.3.0.90.0\lib\MvcContrib.TestHelper.dll</HintPath>
</Reference>
Expand All @@ -49,14 +52,17 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="System.Web.Routing" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="controllers\AccountController.Tests.cs" />
<Compile Include="controllers\CampaignController.Tests.cs" />
<Compile Include="controllers\DonationController.Tests.cs" />
<Compile Include="controllers\HomeController.Tests.cs" />
Expand All @@ -65,6 +71,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\ExcelImport\ExcelImportService_Import_Should.cs" />
<Compile Include="Services\ExcelImport\MemberFactory_CreateMember_Should.cs" />
<Compile Include="Services\UrlValidationService.Tests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
Expand Down
140 changes: 140 additions & 0 deletions src/GiveCRM.Web.Tests/Services/UrlValidationService.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using GiveCRM.Web.Controllers;
using GiveCRM.Web.Services;
using Moq;
using NUnit.Framework;
using System.Web.Routing;

namespace GiveCRM.Web.Tests.Services
{
[TestFixture]
public class UrlValidationServiceTest:AssertionHelper
{

private UrlValidationService CreateService()
{
var rules = new List<IAmAUrlValidationRule>
{
new IsLocal(),
new LengthIsGreaterThanOne(),
new BeginsWithForwardSlash(),
new DoesNotBeginWithDoubleForwardSlash(),
new DoesNotBeginWithForwardSlashBackslash()
};
return new UrlValidationService(rules);
}

[Test]
public void ShouldReturnTrueForValidUrl()
{
var routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);

var request = new Mock<HttpRequestBase>(MockBehavior.Strict);
request.SetupGet(x => x.ApplicationPath).Returns("/");
request.SetupGet(x => x.Url).Returns(new Uri("http://localhost/Home", UriKind.Absolute));
request.SetupGet(x => x.ServerVariables).Returns(new System.Collections.Specialized.NameValueCollection());

var response = new Mock<HttpResponseBase>(MockBehavior.Strict);
response.Setup(x => x.ApplyAppPathModifier("/Home")).Returns("http://localhost/Home");

var context = new Mock<HttpContextBase>(MockBehavior.Strict);
context.SetupGet(x => x.Request).Returns(request.Object);
context.SetupGet(x => x.Response).Returns(response.Object);

var controller = new AccountController(null,null,null);
controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);
controller.Url = new UrlHelper(new RequestContext(context.Object, new RouteData()), routes);

var service = CreateService();
var result = service.IsRedirectable(controller, @"/Home");
Expect(result, Is.True);
}

[Test]
public void ShouldReturnFalseForEmptyStringUrl()
{
var routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);

var request = new Mock<HttpRequestBase>(MockBehavior.Strict);
request.SetupGet(x => x.ApplicationPath).Returns("/");
request.SetupGet(x => x.Url).Returns(new Uri("http://localhost/Home", UriKind.Absolute));
request.SetupGet(x => x.ServerVariables).Returns(new System.Collections.Specialized.NameValueCollection());

var response = new Mock<HttpResponseBase>(MockBehavior.Strict);
response.Setup(x => x.ApplyAppPathModifier("/Home")).Returns("http://localhost/Home");

var context = new Mock<HttpContextBase>(MockBehavior.Strict);
context.SetupGet(x => x.Request).Returns(request.Object);
context.SetupGet(x => x.Response).Returns(response.Object);

var controller = new AccountController(null, null, null);
controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);
controller.Url = new UrlHelper(new RequestContext(context.Object, new RouteData()), routes);

var service = CreateService();
var result = service.IsRedirectable(controller, string.Empty);
Expect(result, Is.False);
}

[Test]
public void ShouldReturnFalseForUrlNotBeginingWithForwardSlash()
{
var routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);

var request = new Mock<HttpRequestBase>(MockBehavior.Strict);
request.SetupGet(x => x.ApplicationPath).Returns("/");
request.SetupGet(x => x.Url).Returns(new Uri("http://localhost/Home", UriKind.Absolute));
request.SetupGet(x => x.ServerVariables).Returns(new System.Collections.Specialized.NameValueCollection());

var response = new Mock<HttpResponseBase>(MockBehavior.Strict);
response.Setup(x => x.ApplyAppPathModifier("/Home")).Returns("http://localhost/Home");

var context = new Mock<HttpContextBase>(MockBehavior.Strict);
context.SetupGet(x => x.Request).Returns(request.Object);
context.SetupGet(x => x.Response).Returns(response.Object);

var controller = new AccountController(null, null, null);
controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);
controller.Url = new UrlHelper(new RequestContext(context.Object, new RouteData()), routes);

var service = CreateService();
var result = service.IsRedirectable(controller, "muppet");
Expect(result, Is.False);
}

[Test]
public void ShouldReturnFalseForUrlBeginingWithDoubleForwardSlash()
{
var routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);

var request = new Mock<HttpRequestBase>(MockBehavior.Strict);
request.SetupGet(x => x.ApplicationPath).Returns("/");
request.SetupGet(x => x.Url).Returns(new Uri("http://localhost/Home", UriKind.Absolute));
request.SetupGet(x => x.ServerVariables).Returns(new System.Collections.Specialized.NameValueCollection());

var response = new Mock<HttpResponseBase>(MockBehavior.Strict);
response.Setup(x => x.ApplyAppPathModifier("/Home")).Returns("http://localhost/Home");

var context = new Mock<HttpContextBase>(MockBehavior.Strict);
context.SetupGet(x => x.Request).Returns(request.Object);
context.SetupGet(x => x.Response).Returns(response.Object);

var controller = new AccountController(null, null, null);
controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);
controller.Url = new UrlHelper(new RequestContext(context.Object, new RouteData()), routes);

var service = CreateService();
var result = service.IsRedirectable(controller, "//muppet");
Expect(result, Is.False);
}
}
}
191 changes: 191 additions & 0 deletions src/GiveCRM.Web.Tests/controllers/AccountController.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
using System.Globalization;
using System.Web.Mvc;
using GiveCRM.Web.Controllers;
using GiveCRM.Web.Models;
using GiveCRM.Web.Services;
using Moq;
using MvcContrib.TestHelper;
using NUnit.Framework;

namespace GiveCRM.Web.Tests.controllers
{
[TestFixture]
public class AccountControllerTests:AssertionHelper
{
private Mock<IMembershipService> mockMembershipService;
private Mock<IAuthenticationService> mockAuthenticationService;
private Mock<IUrlValidationService> mockUrlValidationService;

[SetUp]
public void SetUp()
{
mockMembershipService = new Mock<IMembershipService>();
mockAuthenticationService = new Mock<IAuthenticationService>();
mockUrlValidationService = new Mock<IUrlValidationService>();
}

private AccountController CreateController()
{
return new AccountController(mockMembershipService.Object,
mockAuthenticationService.Object,
mockUrlValidationService.Object);
}

[Test]
public void ShouldLogOnUserAndRedirectToHome()
{
var controller = CreateController();


mockMembershipService.Setup(ms=>ms.ValidateUser("test","password")).Returns(true);
mockUrlValidationService.Setup(uvs=>uvs.IsRedirectable(controller,"")).Returns(false);


var model = new LogOnModel();
model.UserName = "test";
model.Password = "password";
var url = string.Empty;

var actionResult = controller.LogOn(model, url);
Expect(controller.ModelState.IsValid, Is.True);
actionResult.AssertActionRedirect();
}

[Test]
public void ShouldLogOnUserAndRedirectToUrl()
{
var controller = CreateController();

mockMembershipService.Setup(ms => ms.ValidateUser("test", "password")).Returns(true);
mockUrlValidationService.Setup(uvs => uvs.IsRedirectable(controller, "testurl")).Returns(true);

var model = new LogOnModel();
model.UserName = "test";
model.Password = "password";
var url = "testurl";

var actionResult = controller.LogOn(model, url);
Expect(controller.ModelState.IsValid, Is.True);
Expect(actionResult.AssertHttpRedirect().Url, Is.EqualTo(url));
actionResult.AssertHttpRedirect();
}

[Test]
public void ShouldNotLogOnForIncorrectCredentials()
{
var controller = CreateController();

mockMembershipService.Setup(ms => ms.ValidateUser("test", "password")).Returns(false);

var model = new LogOnModel();
model.UserName = "test";
model.Password = "password";
var url = string.Empty;

var actionResult = controller.LogOn(model, url);
Expect(controller.ModelState.IsValid, Is.False);
Expect(controller.ModelState[""].Errors.Count,Is.EqualTo(1));
Expect(controller.ModelState[""].Errors[0].ErrorMessage,Is.EqualTo("The user name or password provided is incorrect."));
actionResult.AssertViewRendered().WithViewData<LogOnModel>();
}

[Test]
public void ShouldLogOff()
{
mockAuthenticationService.Setup(a => a.SignOut()).Verifiable();
var controller = CreateController();
var actionResult = controller.LogOff();
mockAuthenticationService.Verify();
actionResult.AssertActionRedirect();
}

[Test]
public void ShouldRegister()
{
var error = string.Empty;
mockMembershipService.Setup(s => s.CreateUser("test", "password", "a@a.a", out error)).Returns(true);
var controller = CreateController();
var model = new RegisterModel
{
UserName = "test",
Password ="password",
Email = "a@a.a"
};
var actionResult = controller.Register(model);
actionResult.AssertActionRedirect();
}

[Test]
public void ShouldFailToRegister()
{
var error = string.Empty;
mockMembershipService.Setup(s => s.CreateUser("test", "password", "a@a.a", out error)).Returns(false);
var controller = CreateController();
var model = new RegisterModel
{
UserName = "test",
Password = "password",
Email = "a@a.a"
};
var actionResult = controller.Register(model);
actionResult.AssertViewRendered().WithViewData<RegisterModel>();
}

[Test]
public void ShouldChangePassword()
{
var model = new ChangePasswordModel
{
NewPassword = "Slartibartfast",
OldPassword = "password",
ConfirmPassword = "Slartibartfast"
};

mockMembershipService.Setup(s => s.ChangePassword(It.IsAny<string>(),"password","Slartibartfast")).Returns(true);

var controller = CreateController();

var actionResult = controller.ChangePassword(model);
Expect(controller.ModelState.IsValid,Is.True);
actionResult.AssertActionRedirect();
}

[Test]
public void ShouldNotChangePasswordInvalidModel()
{
var model = new ChangePasswordModel
{
NewPassword = "Slartibartfast",
OldPassword = "password",
ConfirmPassword = "ZaphodBeeblebrox"
};

var controller = CreateController();
controller.ModelState.AddModelError("NewPassword","The new password and confirmation password do not match.");

var actionResult = controller.ChangePassword(model);
Expect(controller.ViewData.ModelState.IsValid, Is.False);
actionResult.AssertViewRendered().WithViewData<ChangePasswordModel>();
}

[Test]
public void ShouldNotChangePasswordIFailForChangePassword()
{
var model = new ChangePasswordModel
{
NewPassword = "Slartibartfast",
OldPassword = "password",
ConfirmPassword = "Slartibartfast"
};

mockMembershipService.Setup(s => s.ChangePassword(It.IsAny<string>(), "password", "Slartibartfast")).Returns(false);
var controller = CreateController();

var actionResult = controller.ChangePassword(model);
Expect(controller.ViewData.ModelState.IsValid, Is.False);
Expect(controller.ModelState[""].Errors.Count,Is.EqualTo(1));
Expect(controller.ModelState[""].Errors[0].ErrorMessage, Is.EqualTo("The current password is incorrect or the new password is invalid."));
actionResult.AssertViewRendered().WithViewData<ChangePasswordModel>();
}
}
}
1 change: 1 addition & 0 deletions src/GiveCRM.Web.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
<package id="NSubstitute" version="1.2.1.0" />
<package id="NUnit" version="2.5.10.11092" />
<package id="RhinoMocks" version="3.6" />
<package id="Moq" version="4.0.10827" />
</packages>
Binary file added src/GiveCRM.Web/App_Data/ASPNETDB.MDF
Binary file not shown.
Binary file added src/GiveCRM.Web/App_Data/aspnetdb_log.ldf
Binary file not shown.
Loading

0 comments on commit 0dc4923

Please sign in to comment.