Skip to content

Commit 4f806b3

Browse files
committed
Adding UserController tests.
1 parent e8ab9d2 commit 4f806b3

File tree

9 files changed

+198
-17
lines changed

9 files changed

+198
-17
lines changed

.gitignore

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
2-
/_ReSharper.Note
3-
/Note/bin
4-
/Note/obj
5-
/Note.Core/bin
6-
/Note.Core/obj
7-
/Note.Core.Test/bin
8-
/Note.Core.Test/obj
1+
bin
2+
obj
3+
/_ReSharper*
94
*.user
10-
*.suo
5+
*.suo
6+
*.vs10x

Note.Test/Note.Test.csproj

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{F56D6E93-085B-4223-81CA-68366EE108E3}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Note.Test</RootNamespace>
12+
<AssemblyName>Note.Test</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="Moq">
35+
<HintPath>..\lib\Moq.dll</HintPath>
36+
</Reference>
37+
<Reference Include="nunit.framework, Version=2.5.7.10213, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
38+
<SpecificVersion>False</SpecificVersion>
39+
<HintPath>..\lib\nunit.framework.dll</HintPath>
40+
</Reference>
41+
<Reference Include="System" />
42+
<Reference Include="System.Core" />
43+
<Reference Include="System.Web" />
44+
<Reference Include="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
45+
<Reference Include="System.Web.Routing" />
46+
<Reference Include="System.Xml.Linq" />
47+
<Reference Include="System.Data.DataSetExtensions" />
48+
<Reference Include="Microsoft.CSharp" />
49+
<Reference Include="System.Data" />
50+
<Reference Include="System.Xml" />
51+
</ItemGroup>
52+
<ItemGroup>
53+
<Compile Include="Properties\AssemblyInfo.cs" />
54+
<Compile Include="UserControllerTests.cs" />
55+
</ItemGroup>
56+
<ItemGroup>
57+
<ProjectReference Include="..\Note.Core\Note.Core.csproj">
58+
<Project>{D0FF2D27-58CD-448D-A0E7-756D5A3A91EB}</Project>
59+
<Name>Note.Core</Name>
60+
</ProjectReference>
61+
<ProjectReference Include="..\Note\Note.csproj">
62+
<Project>{C68D55E8-9889-4E19-94A1-B088E9FA8F4E}</Project>
63+
<Name>Note</Name>
64+
</ProjectReference>
65+
</ItemGroup>
66+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
67+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
68+
Other similar extension points exist, see Microsoft.Common.targets.
69+
<Target Name="BeforeBuild">
70+
</Target>
71+
<Target Name="AfterBuild">
72+
</Target>
73+
-->
74+
</Project>

Note.Test/Properties/AssemblyInfo.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Note.Test")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Microsoft")]
12+
[assembly: AssemblyProduct("Note.Test")]
13+
[assembly: AssemblyCopyright("Copyright © Microsoft 2010")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("dd561d96-5fa3-4a6e-884b-d4849417a1d3")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Note.Test/UserControllerTests.cs

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.Web.Mvc;
2+
using Moq;
3+
using Note.Controllers;
4+
using Note.Core.Services;
5+
using NUnit.Framework;
6+
using Note.ViewModels;
7+
namespace Note.Test
8+
{
9+
[TestFixture]
10+
public class UserControllerTests
11+
{
12+
private Mock<IMembershipService> mockMembershipService;
13+
private Mock<IAuthenticationService> mockAuthenticationService;
14+
15+
[SetUp]
16+
public void Setup()
17+
{
18+
mockMembershipService = new Mock<IMembershipService>();
19+
mockAuthenticationService = new Mock<IAuthenticationService>();
20+
}
21+
22+
[Test]
23+
public void SignInShouldValidateAndAuthenticateUserWhenPostedTo()
24+
{
25+
mockMembershipService.Setup(ms => ms.ValidateUser("username", "password")).Returns(true);
26+
var userController = new UserController(mockAuthenticationService.Object, mockMembershipService.Object);
27+
var model = new UserSignInViewModel { Password = "password", StaySignedIn = false, Username = "username" };
28+
29+
userController.SignIn(model, null);
30+
31+
mockMembershipService.Verify(ms => ms.ValidateUser("username", "password"), Times.Once());
32+
mockAuthenticationService.Verify(auth => auth.SignIn("username", false), Times.Once());
33+
}
34+
35+
[Test]
36+
public void SignInShouldRedirectToReturnUrlWhenAuthenticationIsSuccessful()
37+
{
38+
mockMembershipService.Setup(ms => ms.ValidateUser(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
39+
var userController = new UserController(mockAuthenticationService.Object, mockMembershipService.Object);
40+
var model = new UserSignInViewModel { Password = "password", StaySignedIn = false, Username = "username" };
41+
42+
var result = userController.SignIn(model, "/test/page") as RedirectResult;
43+
44+
Assert.IsNotNull(result);
45+
Assert.AreEqual("/test/page", result.Url);
46+
}
47+
48+
[Test]
49+
public void SignInShouldRedirectToNotesListWhenAuthenticationIsSuccessfulAndReturnUrlIsNotSpecified()
50+
{
51+
mockMembershipService.Setup(ms => ms.ValidateUser(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
52+
var userController = new UserController(mockAuthenticationService.Object, mockMembershipService.Object);
53+
var model = new UserSignInViewModel { Password = "password", StaySignedIn = false, Username = "username" };
54+
55+
var result = userController.SignIn(model, null) as RedirectToRouteResult;
56+
57+
Assert.IsNotNull(result);
58+
Assert.AreEqual("list", result.RouteValues["action"]);
59+
Assert.AreEqual("notes", result.RouteValues["controller"]);
60+
}
61+
62+
}
63+
}

Note.sln

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Note", "Note\Note.csproj",
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Note.Core.Test", "Note.Core.Test\Note.Core.Test.csproj", "{F06F7232-DEF5-4218-A1AA-A79E238CE6CE}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Note.Test", "Note.Test\Note.Test.csproj", "{F56D6E93-085B-4223-81CA-68366EE108E3}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -25,6 +27,10 @@ Global
2527
{F06F7232-DEF5-4218-A1AA-A79E238CE6CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
2628
{F06F7232-DEF5-4218-A1AA-A79E238CE6CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
2729
{F06F7232-DEF5-4218-A1AA-A79E238CE6CE}.Release|Any CPU.Build.0 = Release|Any CPU
30+
{F56D6E93-085B-4223-81CA-68366EE108E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31+
{F56D6E93-085B-4223-81CA-68366EE108E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{F56D6E93-085B-4223-81CA-68366EE108E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
33+
{F56D6E93-085B-4223-81CA-68366EE108E3}.Release|Any CPU.Build.0 = Release|Any CPU
2834
EndGlobalSection
2935
GlobalSection(SolutionProperties) = preSolution
3036
HideSolutionNode = FALSE

Note/Content/Site.css

+6
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,12 @@ input[type="password"]
243243
border: 1px solid #CCC;
244244
}
245245

246+
textarea#Title
247+
{
248+
width:600px;
249+
height:300px;
250+
}
251+
246252
/* TABLE
247253
----------------------------------------------------------*/
248254

Note/Controllers/UserController.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public ActionResult SignIn(UserSignInViewModel model, string returnUrl)
3333
return Redirect(returnUrl);
3434
}
3535

36-
return RedirectToAction("New", "Notes");
36+
return RedirectToAction("list", "notes");
3737
}
3838

3939
ModelState.AddModelError("", "The username or password provided is incorrect.");
@@ -60,7 +60,7 @@ public ActionResult Register(UserRegisterViewModel model)
6060
if (status == MembershipCreateStatus.Success)
6161
{
6262
ViewData["RegistrationSuccessMessage"] = "You have registered. Hooray for you!";
63-
return RedirectToAction("List", "Notes");
63+
return RedirectToAction("list", "notes");
6464
}
6565
}
6666
// Something has gone wrong if we are here so go back to the registration page.

Note/Views/Notes/New.aspx

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
<h2>Add a note</h2>
1010
<% using(Html.BeginForm("new", "notes", FormMethod.Post)) {%>
1111
<fieldset>
12-
<ul>
13-
<li><%= Html.LabelFor(x=>x.Title) %> <%=Html.TextBoxFor(note => note.Title) %></li>
14-
<li><%= Html.LabelFor(x=>x.Content) %> <%=Html.TextAreaFor(note => note.Content) %></li>
12+
<ul>
13+
<li><%= Html.LabelFor(x=>x.Title) %> <%=Html.TextBoxFor(note => note.Title) %></li>
14+
<li><%= Html.LabelFor(x=>x.Content) %> <%=Html.TextAreaFor(note => note.Content) %></li>
1515
</ul>
16-
</fieldset>
17-
<input type="submit" />
16+
</fieldset>
17+
<input type="submit" />
1818
<%} %>
1919
</asp:Content>

Note/Web.config

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</compilation>
2626

2727
<authentication mode="Forms">
28-
<forms loginUrl="~/User/SignIn" timeout="2880" />
28+
<forms loginUrl="~/user/signin" timeout="2880" />
2929
</authentication>
3030

3131
<membership>

0 commit comments

Comments
 (0)