Skip to content

Commit 08bcf1b

Browse files
committed
Added some example unit tests for the login actions
1 parent 571a5e1 commit 08bcf1b

File tree

6 files changed

+185
-1
lines changed

6 files changed

+185
-1
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System.Web.Mvc;
2+
using System.Web.Routing;
3+
using NSubstitute;
4+
using NUnit.Framework;
5+
using TestStack.FluentMVCTesting.Example.Controllers;
6+
using TestStack.FluentMVCTesting.Example.Models;
7+
using TestStack.FluentMVCTesting.Example.Services;
8+
9+
namespace TestStack.FluentMVCTesting.Example.Tests.Controllers
10+
{
11+
class AccountControllerTests
12+
{
13+
#region Setup
14+
private AccountController _controller;
15+
private IAuthenticationService _authenticationService;
16+
17+
[SetUp]
18+
public void Setup()
19+
{
20+
_authenticationService = Substitute.For<IAuthenticationService>();
21+
_controller = new AccountController(_authenticationService)
22+
{
23+
Url = new UrlHelper(Substitute.For<RequestContext>())
24+
};
25+
}
26+
#endregion
27+
28+
#region Login
29+
30+
[Test]
31+
public void WhenViewingLoginPage_ThenShowDefaultViewWithReturnUrl()
32+
{
33+
const string returnUrl = "http://www.google.com.au/";
34+
35+
_controller.WithCallTo(c => c.Login(returnUrl))
36+
.ShouldRenderDefaultView();
37+
38+
Assert.That(_controller.ViewBag.ReturnUrl, Is.EqualTo(returnUrl));
39+
}
40+
41+
[Test]
42+
public void GivenInvalidSubmission_WhenPostingLoginDetails_ThenShowDefaultViewWithInvalidModelAndReturnUrl()
43+
{
44+
var vm = new LoginModel();
45+
const string returnUrl = "http://www.google.com.au/";
46+
47+
_controller.WithModelErrors()
48+
.WithCallTo(c => c.Login(vm, returnUrl))
49+
.ShouldRenderDefaultView()
50+
.WithModel(vm);
51+
52+
Assert.That(_controller.ViewBag.ReturnUrl, Is.EqualTo(returnUrl));
53+
}
54+
55+
[Test]
56+
public void GivenValidSubmissionButIncorrectDetails_WhenPostingLoginDetails_ThenShowDefaultViewWithInvalidModelAndReturnUrlAndErrorMessage()
57+
{
58+
var vm = new LoginModel();
59+
const string returnUrl = "http://www.google.com.au/";
60+
_authenticationService.Login(vm).Returns(false);
61+
62+
_controller.WithCallTo(c => c.Login(vm, returnUrl))
63+
.ShouldRenderDefaultView()
64+
.WithModel(vm)
65+
.AndModelError("").ThatEquals("The user name or password provided is incorrect.");
66+
67+
Assert.That(_controller.ViewBag.ReturnUrl, Is.EqualTo(returnUrl));
68+
}
69+
70+
[Test]
71+
public void GivenLocalReturnUrlAndValidSubmission_WhenPostingLoginDetails_ThenLogUserInAndRedirectToReturnUrl()
72+
{
73+
var vm = new LoginModel();
74+
const string returnUrl = "/localurl";
75+
_authenticationService.Login(vm).Returns(true);
76+
77+
_controller.WithCallTo(c => c.Login(vm, returnUrl))
78+
.ShouldRedirectTo(returnUrl);
79+
}
80+
81+
[Test]
82+
public void GivenNonLocalReturnUrlAndValidSubmission_WhenPostingLoginDetails_ThenLogUserInAndRedirectToHomepage()
83+
{
84+
var vm = new LoginModel();
85+
const string returnUrl = "http://www.google.com.au/";
86+
_authenticationService.Login(vm).Returns(true);
87+
88+
_controller.WithCallTo(c => c.Login(vm, returnUrl))
89+
.ShouldRedirectTo<HomeController>(c => c.Index());
90+
}
91+
92+
[Test]
93+
public void GivenNoReturnUrlAndValidSubmission_WhenPostingLoginDetails_ThenLogUserInAndRedirectToHomepage([Values(null, "")] string returnUrl)
94+
{
95+
var vm = new LoginModel();
96+
_authenticationService.Login(vm).Returns(true);
97+
98+
_controller.WithCallTo(c => c.Login(vm, returnUrl))
99+
.ShouldRedirectTo<HomeController>(c => c.Index());
100+
}
101+
102+
#endregion
103+
}
104+
}

TestStack.FluentMVCTesting.Example.Tests/TestStack.FluentMVCTesting.Example.Tests.csproj

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,66 @@
3232
<WarningLevel>4</WarningLevel>
3333
</PropertyGroup>
3434
<ItemGroup>
35+
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
36+
<Private>True</Private>
37+
<HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
38+
</Reference>
39+
<Reference Include="NSubstitute">
40+
<HintPath>..\packages\NSubstitute.1.6.1.0\lib\NET40\NSubstitute.dll</HintPath>
41+
</Reference>
3542
<Reference Include="nunit.framework">
3643
<HintPath>..\packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
3744
</Reference>
3845
<Reference Include="System" />
3946
<Reference Include="System.Core" />
47+
<Reference Include="System.Web" />
48+
<Reference Include="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
49+
<Private>True</Private>
50+
<HintPath>..\packages\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\System.Web.Helpers.dll</HintPath>
51+
</Reference>
52+
<Reference Include="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
53+
<Private>True</Private>
54+
<HintPath>..\packages\Microsoft.AspNet.Mvc.4.0.30506.0\lib\net40\System.Web.Mvc.dll</HintPath>
55+
</Reference>
56+
<Reference Include="System.Web.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
57+
<Private>True</Private>
58+
<HintPath>..\packages\Microsoft.AspNet.Razor.2.0.30506.0\lib\net40\System.Web.Razor.dll</HintPath>
59+
</Reference>
60+
<Reference Include="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
61+
<Private>True</Private>
62+
<HintPath>..\packages\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\System.Web.WebPages.dll</HintPath>
63+
</Reference>
64+
<Reference Include="System.Web.WebPages.Deployment, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
65+
<Private>True</Private>
66+
<HintPath>..\packages\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\System.Web.WebPages.Deployment.dll</HintPath>
67+
</Reference>
68+
<Reference Include="System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
69+
<Private>True</Private>
70+
<HintPath>..\packages\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\System.Web.WebPages.Razor.dll</HintPath>
71+
</Reference>
4072
<Reference Include="System.Xml.Linq" />
4173
<Reference Include="System.Data.DataSetExtensions" />
4274
<Reference Include="Microsoft.CSharp" />
4375
<Reference Include="System.Data" />
4476
<Reference Include="System.Xml" />
4577
</ItemGroup>
4678
<ItemGroup>
79+
<Compile Include="Controllers\AccountControllerTests.cs" />
4780
<Compile Include="Properties\AssemblyInfo.cs" />
4881
</ItemGroup>
4982
<ItemGroup>
5083
<None Include="packages.config" />
5184
</ItemGroup>
85+
<ItemGroup>
86+
<ProjectReference Include="..\TestStack.FluentMVCTesting.Example\TestStack.FluentMVCTesting.Example.csproj">
87+
<Project>{0BD41AAC-8155-4328-A1A2-CB634430BB39}</Project>
88+
<Name>TestStack.FluentMVCTesting.Example</Name>
89+
</ProjectReference>
90+
<ProjectReference Include="..\TestStack.FluentMVCTesting\TestStack.FluentMVCTesting.csproj">
91+
<Project>{152ca00f-18d3-4cf5-8ca0-2c5b70cbea19}</Project>
92+
<Name>TestStack.FluentMVCTesting</Name>
93+
</ProjectReference>
94+
</ItemGroup>
5295
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5396
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
5497
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Microsoft.AspNet.Mvc" version="4.0.30506.0" targetFramework="net40" />
4+
<package id="Microsoft.AspNet.Razor" version="2.0.30506.0" targetFramework="net40" />
5+
<package id="Microsoft.AspNet.WebPages" version="2.0.30506.0" targetFramework="net40" />
6+
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net40" />
7+
<package id="NSubstitute" version="1.6.1.0" targetFramework="net40" />
38
<package id="NUnit" version="2.6.2" targetFramework="net40" />
49
</packages>

TestStack.FluentMVCTesting.Example/Controllers/AccountController.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Web.Security;
88
using DotNetOpenAuth.AspNet;
99
using Microsoft.Web.WebPages.OAuth;
10+
using TestStack.FluentMVCTesting.Example.Services;
1011
using WebMatrix.WebData;
1112
using TestStack.FluentMVCTesting.Example.Filters;
1213
using TestStack.FluentMVCTesting.Example.Models;
@@ -17,6 +18,17 @@ namespace TestStack.FluentMVCTesting.Example.Controllers
1718
[InitializeSimpleMembership]
1819
public class AccountController : Controller
1920
{
21+
private readonly IAuthenticationService _authenticationService;
22+
23+
public AccountController()
24+
: this(new AuthenticationService())
25+
{}
26+
27+
public AccountController(IAuthenticationService authenticationService)
28+
{
29+
_authenticationService = authenticationService;
30+
}
31+
2032
//
2133
// GET: /Account/Login
2234

@@ -35,13 +47,14 @@ public ActionResult Login(string returnUrl)
3547
[ValidateAntiForgeryToken]
3648
public ActionResult Login(LoginModel model, string returnUrl)
3749
{
38-
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
50+
if (ModelState.IsValid && _authenticationService.Login(model))
3951
{
4052
return RedirectToLocal(returnUrl);
4153
}
4254

4355
// If we got this far, something failed, redisplay form
4456
ModelState.AddModelError("", "The user name or password provided is incorrect.");
57+
ViewBag.ReturnUrl = returnUrl;
4558
return View(model);
4659
}
4760

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using TestStack.FluentMVCTesting.Example.Models;
2+
using WebMatrix.WebData;
3+
4+
namespace TestStack.FluentMVCTesting.Example.Services
5+
{
6+
public interface IAuthenticationService
7+
{
8+
bool Login(LoginModel model);
9+
}
10+
11+
public class AuthenticationService : IAuthenticationService
12+
{
13+
public bool Login(LoginModel model)
14+
{
15+
return WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe);
16+
}
17+
}
18+
}

TestStack.FluentMVCTesting.Example/TestStack.FluentMVCTesting.Example.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
</Compile>
180180
<Compile Include="Models\AccountModels.cs" />
181181
<Compile Include="Properties\AssemblyInfo.cs" />
182+
<Compile Include="Services\AuthenticationService.cs" />
182183
</ItemGroup>
183184
<ItemGroup>
184185
<Content Include="Content\themes\base\images\ui-bg_flat_0_aaaaaa_40x100.png" />

0 commit comments

Comments
 (0)