Guest User

Untitled

a guest
Apr 12th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Web.Mvc;
  7. using System.Web.Routing;
  8.  
  9. using NUnit.Framework;
  10. using Rhino.Mocks;
  11. using Chronicle;
  12. using Daystar.Core.Accounts;
  13. using Daystar.Core.Security;
  14. using DaystarAdmin.Controllers;
  15. using DaystarAdmin.Models;
  16.  
  17. namespace DaystarAdmin.Tests.Controllers
  18. {
  19. public class setup
  20. {
  21. protected IUserRepository userRepository;
  22. protected ISessionManager sessionManager;
  23. protected LoginController loginController;
  24.  
  25. public void arrange ()
  26. {
  27. userRepository = MockRepository.GenerateMock<IUserRepository>(new object[] { });
  28. sessionManager = MockRepository.GenerateMock<ISessionManager>();
  29.  
  30. userRepository.Stub(x => x.FindUserByUsername ( Arg<string>.Is.Anything ))
  31. .Return(
  32. new User
  33. {
  34. UserName = "matt",
  35. Password = new Daystar.Core.Utility.Md5HashingService().GetHash("testpassword")
  36. });
  37.  
  38. loginController = new LoginController(userRepository, sessionManager);
  39. }
  40. }
  41.  
  42. [TestFixture]
  43. public class when_login_controller_attempts_login_with_valid_creds : setup
  44. {
  45. [Test]
  46. public void should_redirect_to_home()
  47. {
  48. arrange ();
  49.  
  50. var viewModel = new LoginViewModel { UserName = "matt", Password = "testpassword", SaveLoginWithCookie = true };
  51. var results = loginController.TryLogin( viewModel );
  52.  
  53. Assert.IsTrue(results is RedirectToRouteResult);
  54. Assert.IsTrue(((RedirectToRouteResult)results).RouteValues.Count == 1);
  55. Assert.IsTrue(((RedirectToRouteResult)results).RouteValues["action"].ToString() == "../Home");
  56. Assert.IsTrue ( viewModel.LoginSuccess );
  57. }
  58.  
  59. // this is a crappy test, so I won't test the reverse
  60. [Test]
  61. public void should_save_cookie_when_asked ()
  62. {
  63. arrange ();
  64.  
  65. var viewModel = new LoginViewModel { UserName = "matt", Password = "testpassword", SaveLoginWithCookie = true };
  66. var results = loginController.TryLogin(viewModel);
  67.  
  68. sessionManager.AssertWasCalled ( x => x.SetAuthorizedUser ( Arg<User>.Is.Anything, Arg<bool>.Is.Equal(true) ));
  69. }
  70.  
  71. // not much different than should_save_cookie_when_asked
  72. [Test]
  73. public void should_set_authorized_user()
  74. {
  75. arrange();
  76.  
  77. var viewModel = new LoginViewModel { UserName = "matt", Password = "testpassword" };
  78. var results = loginController.TryLogin(viewModel);
  79.  
  80. sessionManager.AssertWasCalled(x => x.SetAuthorizedUser(Arg<User>.Is.Anything, Arg<bool>.Is.Anything));
  81. }
  82. }
  83.  
  84. [TestFixture]
  85. public class when_login_controller_attempts_login_with_invalid_creds : setup
  86. {
  87. [Test]
  88. public void should_display_index_with_login_failed()
  89. {
  90. arrange ();
  91.  
  92. var viewModel = new LoginViewModel { UserName = "matt", Password = "notthepassword" };
  93.  
  94. var results = loginController.TryLogin(viewModel);
  95.  
  96. var resultsAsViewResult = results as ViewResult;
  97. Assert.IsNotNull(resultsAsViewResult);
  98. Assert.AreEqual(resultsAsViewResult.ViewName, "Index");
  99.  
  100. Assert.IsFalse(viewModel.LoginSuccess);
  101.  
  102. }
  103. }
  104. }
Add Comment
Please, Sign In to add comment