Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Collections.Generic;
- using System.Text;
- using System.Web.Mvc;
- using System.Web.Routing;
- using NUnit.Framework;
- using Rhino.Mocks;
- using Chronicle;
- using Daystar.Core.Accounts;
- using Daystar.Core.Security;
- using DaystarAdmin.Controllers;
- using DaystarAdmin.Models;
- namespace DaystarAdmin.Tests.Controllers
- {
- public class setup
- {
- protected IUserRepository userRepository;
- protected ISessionManager sessionManager;
- protected LoginController loginController;
- public void arrange ()
- {
- userRepository = MockRepository.GenerateMock<IUserRepository>(new object[] { });
- sessionManager = MockRepository.GenerateMock<ISessionManager>();
- userRepository.Stub(x => x.FindUserByUsername ( Arg<string>.Is.Anything ))
- .Return(
- new User
- {
- UserName = "matt",
- Password = new Daystar.Core.Utility.Md5HashingService().GetHash("testpassword")
- });
- loginController = new LoginController(userRepository, sessionManager);
- }
- }
- [TestFixture]
- public class when_login_controller_attempts_login_with_valid_creds : setup
- {
- [Test]
- public void should_redirect_to_home()
- {
- arrange ();
- var viewModel = new LoginViewModel { UserName = "matt", Password = "testpassword", SaveLoginWithCookie = true };
- var results = loginController.TryLogin( viewModel );
- Assert.IsTrue(results is RedirectToRouteResult);
- Assert.IsTrue(((RedirectToRouteResult)results).RouteValues.Count == 1);
- Assert.IsTrue(((RedirectToRouteResult)results).RouteValues["action"].ToString() == "../Home");
- Assert.IsTrue ( viewModel.LoginSuccess );
- }
- // this is a crappy test, so I won't test the reverse
- [Test]
- public void should_save_cookie_when_asked ()
- {
- arrange ();
- var viewModel = new LoginViewModel { UserName = "matt", Password = "testpassword", SaveLoginWithCookie = true };
- var results = loginController.TryLogin(viewModel);
- sessionManager.AssertWasCalled ( x => x.SetAuthorizedUser ( Arg<User>.Is.Anything, Arg<bool>.Is.Equal(true) ));
- }
- // not much different than should_save_cookie_when_asked
- [Test]
- public void should_set_authorized_user()
- {
- arrange();
- var viewModel = new LoginViewModel { UserName = "matt", Password = "testpassword" };
- var results = loginController.TryLogin(viewModel);
- sessionManager.AssertWasCalled(x => x.SetAuthorizedUser(Arg<User>.Is.Anything, Arg<bool>.Is.Anything));
- }
- }
- [TestFixture]
- public class when_login_controller_attempts_login_with_invalid_creds : setup
- {
- [Test]
- public void should_display_index_with_login_failed()
- {
- arrange ();
- var viewModel = new LoginViewModel { UserName = "matt", Password = "notthepassword" };
- var results = loginController.TryLogin(viewModel);
- var resultsAsViewResult = results as ViewResult;
- Assert.IsNotNull(resultsAsViewResult);
- Assert.AreEqual(resultsAsViewResult.ViewName, "Index");
- Assert.IsFalse(viewModel.LoginSuccess);
- }
- }
- }
Add Comment
Please, Sign In to add comment