Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0f40a43
Showing
1,087 changed files
with
362,002 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
For ELMAH Logs, if you choose to have them logged to XML files. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<!-- | ||
Note: Add entries to the App.config file for configuration settings | ||
that apply only to the Test project. | ||
--> | ||
<configuration> | ||
<appSettings> | ||
|
||
</appSettings> | ||
|
||
<connectionStrings> | ||
|
||
</connectionStrings> | ||
</configuration> |
379 changes: 379 additions & 0 deletions
379
VS2010-MVC3-Razor/NerdDinner.Tests/Controllers/AccountControllerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,379 @@ | ||
using System; | ||
using System.Security.Principal; | ||
using System.Web; | ||
using System.Web.Mvc; | ||
using System.Web.Routing; | ||
using System.Web.Security; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using NerdDinner; | ||
using NerdDinner.Controllers; | ||
using NerdDinner.Models; | ||
using NerdDinner.Tests.Mocks; | ||
|
||
namespace NerdDinner.Tests.Controllers | ||
{ | ||
|
||
[TestClass] | ||
public class AccountControllerTest | ||
{ | ||
|
||
[TestMethod] | ||
public void ChangePasswordGetReturnsView() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
ViewResult result = (ViewResult)controller.ChangePassword(); | ||
|
||
// Assert | ||
Assert.AreEqual(6, result.ViewData["PasswordLength"]); | ||
} | ||
|
||
[TestMethod] | ||
public void ChangePasswordPostRedirectsOnSuccess() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
RedirectToRouteResult result = (RedirectToRouteResult)controller.ChangePassword("oldPass", "newPass", "newPass"); | ||
|
||
// Assert | ||
Assert.AreEqual("ChangePasswordSuccess", result.RouteValues["action"]); | ||
} | ||
|
||
[TestMethod] | ||
public void ChangePasswordPostReturnsViewIfCurrentPasswordNotSpecified() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
ViewResult result = (ViewResult)controller.ChangePassword("", "newPassword", "newPassword"); | ||
|
||
// Assert | ||
Assert.AreEqual(6, result.ViewData["PasswordLength"]); | ||
Assert.AreEqual("You must specify a current password.", result.ViewData.ModelState["currentPassword"].Errors[0].ErrorMessage); | ||
} | ||
|
||
[TestMethod] | ||
public void ChangePasswordPostReturnsViewIfNewPasswordDoesNotMatchConfirmPassword() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
ViewResult result = (ViewResult)controller.ChangePassword("currentPassword", "newPassword", "otherPassword"); | ||
|
||
// Assert | ||
Assert.AreEqual(6, result.ViewData["PasswordLength"]); | ||
Assert.AreEqual("The new password and confirmation password do not match.", result.ViewData.ModelState["_FORM"].Errors[0].ErrorMessage); | ||
} | ||
|
||
[TestMethod] | ||
public void ChangePasswordPostReturnsViewIfNewPasswordIsNull() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
ViewResult result = (ViewResult)controller.ChangePassword("currentPassword", null, null); | ||
|
||
// Assert | ||
Assert.AreEqual(6, result.ViewData["PasswordLength"]); | ||
Assert.AreEqual("You must specify a new password of 6 or more characters.", result.ViewData.ModelState["newPassword"].Errors[0].ErrorMessage); | ||
} | ||
|
||
[TestMethod] | ||
public void ChangePasswordPostReturnsViewIfNewPasswordIsTooShort() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
ViewResult result = (ViewResult)controller.ChangePassword("currentPassword", "12345", "12345"); | ||
|
||
// Assert | ||
Assert.AreEqual(6, result.ViewData["PasswordLength"]); | ||
Assert.AreEqual("You must specify a new password of 6 or more characters.", result.ViewData.ModelState["newPassword"].Errors[0].ErrorMessage); | ||
} | ||
|
||
[TestMethod] | ||
public void ChangePasswordPostReturnsViewIfProviderRejectsPassword() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
ViewResult result = (ViewResult)controller.ChangePassword("oldPass", "badPass", "badPass"); | ||
|
||
// Assert | ||
Assert.AreEqual(6, result.ViewData["PasswordLength"]); | ||
Assert.AreEqual("The current password is incorrect or the new password is invalid.", result.ViewData.ModelState["_FORM"].Errors[0].ErrorMessage); | ||
} | ||
|
||
[TestMethod] | ||
public void ChangePasswordSuccess() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
ViewResult result = (ViewResult)controller.ChangePasswordSuccess(); | ||
|
||
// Assert | ||
Assert.IsNotNull(result); | ||
} | ||
|
||
[TestMethod] | ||
public void ConstructorSetsProperties() | ||
{ | ||
// Arrange | ||
IFormsAuthentication formsAuth = new MockFormsAuthenticationService(); | ||
IMembershipService membershipService = new AccountMembershipService(); | ||
|
||
// Act | ||
AccountController controller = new AccountController(formsAuth, membershipService); | ||
|
||
// Assert | ||
Assert.AreEqual(formsAuth, controller.FormsAuth, "FormsAuth property did not match."); | ||
Assert.AreEqual(membershipService, controller.MembershipService, "MembershipService property did not match."); | ||
} | ||
|
||
[TestMethod] | ||
public void ConstructorSetsPropertiesToDefaultValues() | ||
{ | ||
// Act | ||
AccountController controller = new AccountController(); | ||
|
||
// Assert | ||
Assert.IsNotNull(controller.FormsAuth, "FormsAuth property is null."); | ||
Assert.IsNotNull(controller.MembershipService, "MembershipService property is null."); | ||
} | ||
|
||
[TestMethod] | ||
public void LoginGet() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
ViewResult result = (ViewResult)controller.LogOn(); | ||
|
||
// Assert | ||
Assert.IsNotNull(result); | ||
} | ||
|
||
[TestMethod] | ||
public void LoginPostRedirectsHomeIfLoginSuccessfulButNoReturnUrlGiven() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
RedirectToRouteResult result = (RedirectToRouteResult)controller.LogOn(new LogOnModel { UserName = "someUser", Password = "goodPass", RememberMe = true }, null); | ||
|
||
// Assert | ||
Assert.AreEqual("Home", result.RouteValues["controller"]); | ||
Assert.AreEqual("Index", result.RouteValues["action"]); | ||
} | ||
|
||
[TestMethod] | ||
public void LoginPostRedirectsToReturnUrlIfLoginSuccessfulAndReturnUrlGiven() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
RedirectResult result = (RedirectResult)controller.LogOn(new LogOnModel { UserName = "someUser", Password = "goodPass", RememberMe = false }, "/someUrl"); | ||
|
||
// Assert | ||
Assert.AreEqual("/someUrl", result.Url); | ||
} | ||
|
||
[TestMethod] | ||
public void LoginPostReturnsViewIfPasswordNotSpecified() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
ViewResult result = (ViewResult)controller.LogOn(new LogOnModel { UserName = "someUser", Password = "", RememberMe = true }, null); | ||
|
||
// Assert | ||
LogOnModel model = (LogOnModel)result.Model; | ||
Assert.AreEqual(true, model.RememberMe); | ||
Assert.AreEqual("You must specify a password.", result.ViewData.ModelState["password"].Errors[0].ErrorMessage); | ||
} | ||
|
||
[TestMethod] | ||
public void LoginPostReturnsViewIfUsernameNotSpecified() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
ViewResult result = (ViewResult)controller.LogOn(new LogOnModel { UserName = "", Password = "somePass", RememberMe = false }, null); | ||
|
||
// Assert | ||
LogOnModel model = (LogOnModel)result.Model; | ||
Assert.AreEqual(false, model.RememberMe); | ||
Assert.AreEqual("You must specify a username.", result.ViewData.ModelState["username"].Errors[0].ErrorMessage); | ||
} | ||
|
||
[TestMethod] | ||
public void LoginPostReturnsViewIfUsernameOrPasswordIsIncorrect() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
ViewResult result = (ViewResult)controller.LogOn(new LogOnModel { UserName = "someUser", Password = "badPass", RememberMe = true }, null); | ||
|
||
// Assert | ||
LogOnModel model = (LogOnModel)result.Model; | ||
Assert.AreEqual(true, model.RememberMe); | ||
Assert.AreEqual("The username or password provided is incorrect.", result.ViewData.ModelState["_FORM"].Errors[0].ErrorMessage); | ||
} | ||
|
||
[TestMethod] | ||
public void LogOff() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
RedirectToRouteResult result = (RedirectToRouteResult)controller.LogOff(); | ||
|
||
// Assert | ||
Assert.AreEqual("Home", result.RouteValues["controller"]); | ||
Assert.AreEqual("Index", result.RouteValues["action"]); | ||
} | ||
|
||
[TestMethod] | ||
public void RegisterGet() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
ViewResult result = (ViewResult)controller.Register(); | ||
|
||
// Assert | ||
Assert.AreEqual(6, result.ViewData["PasswordLength"]); | ||
} | ||
|
||
[TestMethod] | ||
public void RegisterPostRedirectsHomeIfRegistrationSuccessful() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
RedirectToRouteResult result = (RedirectToRouteResult)controller.Register(new RegisterModel { UserName = "someUser", Email = "email", Password = "goodPass", ConfirmPassword = "goodPass" }); | ||
|
||
|
||
// Assert | ||
Assert.AreEqual("Home", result.RouteValues["controller"]); | ||
Assert.AreEqual("Index", result.RouteValues["action"]); | ||
} | ||
|
||
[TestMethod] | ||
public void RegisterPostReturnsViewIfEmailNotSpecified() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
ViewResult result = (ViewResult)controller.Register(new RegisterModel { UserName = "someUser", Email = "", Password = "goodPass", ConfirmPassword = "goodPass" }); | ||
|
||
// Assert | ||
Assert.AreEqual(6, result.ViewData["PasswordLength"]); | ||
Assert.AreEqual("You must specify an email address.", result.ViewData.ModelState["email"].Errors[0].ErrorMessage); | ||
} | ||
|
||
[TestMethod] | ||
public void RegisterPostReturnsViewIfNewPasswordDoesNotMatchConfirmPassword() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
ViewResult result = (ViewResult)controller.Register(new RegisterModel { UserName = "someUser", Email = "email", Password = "goodPass", ConfirmPassword = "goodPass2" }); | ||
|
||
// Assert | ||
Assert.AreEqual(6, result.ViewData["PasswordLength"]); | ||
Assert.AreEqual("The new password and confirmation password do not match.", result.ViewData.ModelState["_FORM"].Errors[0].ErrorMessage); | ||
} | ||
|
||
[TestMethod] | ||
public void RegisterPostReturnsViewIfPasswordIsNull() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
ViewResult result = (ViewResult)controller.Register(new RegisterModel { UserName = "someUser", Email = "email", Password = null, ConfirmPassword = null }); | ||
|
||
// Assert | ||
Assert.AreEqual(6, result.ViewData["PasswordLength"]); | ||
Assert.AreEqual("You must specify a password of 6 or more characters.", result.ViewData.ModelState["password"].Errors[0].ErrorMessage); | ||
} | ||
|
||
[TestMethod] | ||
public void RegisterPostReturnsViewIfPasswordIsTooShort() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
ViewResult result = (ViewResult)controller.Register(new RegisterModel { UserName = "someUser", Email = "email", Password = "12345", ConfirmPassword = "12345" }); | ||
|
||
// Assert | ||
Assert.AreEqual(6, result.ViewData["PasswordLength"]); | ||
Assert.AreEqual("You must specify a password of 6 or more characters.", result.ViewData.ModelState["password"].Errors[0].ErrorMessage); | ||
} | ||
|
||
[TestMethod] | ||
public void RegisterPostReturnsViewIfRegistrationFails() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
ViewResult result = (ViewResult)controller.Register(new RegisterModel { UserName = "someUser", Email = "DuplicateUserName", Password = "badPass", ConfirmPassword = "badPass" }); | ||
|
||
// Assert | ||
Assert.AreEqual(6, result.ViewData["PasswordLength"]); | ||
Assert.AreEqual("Username already exists. Please enter a different user name.", result.ViewData.ModelState[""].Errors[0].ErrorMessage); | ||
} | ||
|
||
[TestMethod] | ||
public void RegisterPostReturnsViewIfUsernameNotSpecified() | ||
{ | ||
// Arrange | ||
AccountController controller = GetAccountController(); | ||
|
||
// Act | ||
ViewResult result = (ViewResult)controller.Register(new RegisterModel { UserName = "", Email = "email", Password = "badPass", ConfirmPassword = "badPass" }); | ||
|
||
// Assert | ||
Assert.AreEqual(6, result.ViewData["PasswordLength"]); | ||
Assert.AreEqual("You must specify a username.", result.ViewData.ModelState["username"].Errors[0].ErrorMessage); | ||
} | ||
|
||
private static AccountController GetAccountController() | ||
{ | ||
IFormsAuthentication formsAuth = new MockFormsAuthenticationService(); | ||
MembershipProvider membershipProvider = new MockMembershipProvider(); | ||
AccountMembershipService membershipService = new AccountMembershipService(membershipProvider); | ||
AccountController controller = new AccountController(formsAuth, membershipService); | ||
|
||
HttpContextBase contextBase = MvcMockHelpers.FakeHttpContext(); // new MockHttpContext(); | ||
controller.ControllerContext = new ControllerContext(contextBase, new RouteData(), controller); | ||
controller.Url = new UrlHelper(new RequestContext(contextBase, new RouteData()), new RouteCollection()); | ||
return controller; | ||
} | ||
} | ||
} |
Oops, something went wrong.