/NewsManagement_VS2010/src/NewsManagement/NMA.Web.Tests/Controllers/AccountControllerTest.cs

# · C# · 405 lines · 17 code · 64 blank · 324 comment · 0 complexity · be24d5cb630f928bde29197c415b8200 MD5 · raw file

  1. using System;
  2. using System.Security.Principal;
  3. using System.Web;
  4. using System.Web.Mvc;
  5. using System.Web.Routing;
  6. using System.Web.Security;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. using NMA.Web;
  9. using NMA.Web.Models;
  10. using NMA.Web.Areas.Admin.Controllers;
  11. namespace NMA.Web.Tests.Controllers
  12. {
  13. [TestClass]
  14. public class AccountControllerTest
  15. {
  16. //[TestMethod]
  17. //public void ChangePassword_Get_ReturnsView()
  18. //{
  19. // // Arrange
  20. // AccountController controller = GetAccountController();
  21. // // Act
  22. // ActionResult result = controller.ChangePassword();
  23. // // Assert
  24. // Assert.IsInstanceOfType(result, typeof(ViewResult));
  25. //}
  26. //[TestMethod]
  27. //public void ChangePassword_Post_ReturnsRedirectOnSuccess()
  28. //{
  29. // // Arrange
  30. // AccountController controller = GetAccountController();
  31. // ChangePasswordModel model = new ChangePasswordModel()
  32. // {
  33. // OldPassword = "goodOldPassword",
  34. // NewPassword = "goodNewPassword",
  35. // ConfirmPassword = "goodNewPassword"
  36. // };
  37. // // Act
  38. // ActionResult result = controller.ChangePassword(model);
  39. // // Assert
  40. // Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
  41. // RedirectToRouteResult redirectResult = (RedirectToRouteResult)result;
  42. // Assert.AreEqual("ChangePasswordSuccess", redirectResult.RouteValues["action"]);
  43. //}
  44. //[TestMethod]
  45. //public void ChangePassword_Post_ReturnsViewIfChangePasswordFails()
  46. //{
  47. // // Arrange
  48. // AccountController controller = GetAccountController();
  49. // ChangePasswordModel model = new ChangePasswordModel()
  50. // {
  51. // OldPassword = "goodOldPassword",
  52. // NewPassword = "badNewPassword",
  53. // ConfirmPassword = "badNewPassword"
  54. // };
  55. // // Act
  56. // ActionResult result = controller.ChangePassword(model);
  57. // // Assert
  58. // Assert.IsInstanceOfType(result, typeof(ViewResult));
  59. // ViewResult viewResult = (ViewResult)result;
  60. // Assert.AreEqual(model, viewResult.ViewData.Model);
  61. // Assert.AreEqual("The current password is incorrect or the new password is invalid.", controller.ModelState[""].Errors[0].ErrorMessage);
  62. //}
  63. //[TestMethod]
  64. //public void ChangePassword_Post_ReturnsViewIfModelStateIsInvalid()
  65. //{
  66. // // Arrange
  67. // AccountController controller = GetAccountController();
  68. // ChangePasswordModel model = new ChangePasswordModel()
  69. // {
  70. // OldPassword = "goodOldPassword",
  71. // NewPassword = "goodNewPassword",
  72. // ConfirmPassword = "goodNewPassword"
  73. // };
  74. // controller.ModelState.AddModelError("", "Dummy error message.");
  75. // // Act
  76. // ActionResult result = controller.ChangePassword(model);
  77. // // Assert
  78. // Assert.IsInstanceOfType(result, typeof(ViewResult));
  79. // ViewResult viewResult = (ViewResult)result;
  80. // Assert.AreEqual(model, viewResult.ViewData.Model);
  81. //}
  82. //[TestMethod]
  83. //public void ChangePasswordSuccess_ReturnsView()
  84. //{
  85. // // Arrange
  86. // AccountController controller = GetAccountController();
  87. // // Act
  88. // ActionResult result = controller.ChangePasswordSuccess();
  89. // // Assert
  90. // Assert.IsInstanceOfType(result, typeof(ViewResult));
  91. //}
  92. //[TestMethod]
  93. //public void LogOff_LogsOutAndRedirects()
  94. //{
  95. // // Arrange
  96. // AccountController controller = GetAccountController();
  97. // // Act
  98. // ActionResult result = controller.LogOff();
  99. // // Assert
  100. // Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
  101. // RedirectToRouteResult redirectResult = (RedirectToRouteResult)result;
  102. // Assert.AreEqual("Home", redirectResult.RouteValues["controller"]);
  103. // Assert.AreEqual("Index", redirectResult.RouteValues["action"]);
  104. // Assert.IsTrue(((MockFormsAuthenticationService)controller.FormsService).SignOut_WasCalled);
  105. //}
  106. //[TestMethod]
  107. //public void LogOn_Get_ReturnsView()
  108. //{
  109. // // Arrange
  110. // AccountController controller = GetAccountController();
  111. // // Act
  112. // ActionResult result = controller.LogOn();
  113. // // Assert
  114. // Assert.IsInstanceOfType(result, typeof(ViewResult));
  115. //}
  116. //[TestMethod]
  117. //public void LogOn_Post_ReturnsRedirectOnSuccess_WithoutReturnUrl()
  118. //{
  119. // // Arrange
  120. // AccountController controller = GetAccountController();
  121. // LogOnModel model = new LogOnModel()
  122. // {
  123. // UserName = "someUser",
  124. // Password = "goodPassword",
  125. // RememberMe = false
  126. // };
  127. // // Act
  128. // ActionResult result = controller.LogOn(model, null);
  129. // // Assert
  130. // Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
  131. // RedirectToRouteResult redirectResult = (RedirectToRouteResult)result;
  132. // Assert.AreEqual("Home", redirectResult.RouteValues["controller"]);
  133. // Assert.AreEqual("Index", redirectResult.RouteValues["action"]);
  134. // Assert.IsTrue(((MockFormsAuthenticationService)controller.FormsService).SignIn_WasCalled);
  135. //}
  136. //[TestMethod]
  137. //public void LogOn_Post_ReturnsRedirectOnSuccess_WithReturnUrl()
  138. //{
  139. // // Arrange
  140. // AccountController controller = GetAccountController();
  141. // LogOnModel model = new LogOnModel()
  142. // {
  143. // UserName = "someUser",
  144. // Password = "goodPassword",
  145. // RememberMe = false
  146. // };
  147. // // Act
  148. // ActionResult result = controller.LogOn(model, "/someUrl");
  149. // // Assert
  150. // Assert.IsInstanceOfType(result, typeof(RedirectResult));
  151. // RedirectResult redirectResult = (RedirectResult)result;
  152. // Assert.AreEqual("/someUrl", redirectResult.Url);
  153. // Assert.IsTrue(((MockFormsAuthenticationService)controller.FormsService).SignIn_WasCalled);
  154. //}
  155. //[TestMethod]
  156. //public void LogOn_Post_ReturnsViewIfModelStateIsInvalid()
  157. //{
  158. // // Arrange
  159. // AccountController controller = GetAccountController();
  160. // LogOnModel model = new LogOnModel()
  161. // {
  162. // UserName = "someUser",
  163. // Password = "goodPassword",
  164. // RememberMe = false
  165. // };
  166. // controller.ModelState.AddModelError("", "Dummy error message.");
  167. // // Act
  168. // ActionResult result = controller.LogOn(model, null);
  169. // // Assert
  170. // Assert.IsInstanceOfType(result, typeof(ViewResult));
  171. // ViewResult viewResult = (ViewResult)result;
  172. // Assert.AreEqual(model, viewResult.ViewData.Model);
  173. //}
  174. //[TestMethod]
  175. //public void LogOn_Post_ReturnsViewIfValidateUserFails()
  176. //{
  177. // // Arrange
  178. // AccountController controller = GetAccountController();
  179. // LogOnModel model = new LogOnModel()
  180. // {
  181. // UserName = "someUser",
  182. // Password = "badPassword",
  183. // RememberMe = false
  184. // };
  185. // // Act
  186. // ActionResult result = controller.LogOn(model, null);
  187. // // Assert
  188. // Assert.IsInstanceOfType(result, typeof(ViewResult));
  189. // ViewResult viewResult = (ViewResult)result;
  190. // Assert.AreEqual(model, viewResult.ViewData.Model);
  191. // Assert.AreEqual("The user name or password provided is incorrect.", controller.ModelState[""].Errors[0].ErrorMessage);
  192. //}
  193. //[TestMethod]
  194. //public void OnActionExecuting_SetsViewData()
  195. //{
  196. // // Arrange
  197. // AccountController controller = GetAccountController();
  198. // // Act
  199. // ((IActionFilter)controller).OnActionExecuting(null);
  200. // // Assert
  201. // Assert.AreEqual(10, controller.ViewData["PasswordLength"]);
  202. //}
  203. //[TestMethod]
  204. //public void Register_Get_ReturnsView()
  205. //{
  206. // // Arrange
  207. // AccountController controller = GetAccountController();
  208. // // Act
  209. // ActionResult result = controller.Register();
  210. // // Assert
  211. // Assert.IsInstanceOfType(result, typeof(ViewResult));
  212. //}
  213. //[TestMethod]
  214. //public void Register_Post_ReturnsRedirectOnSuccess()
  215. //{
  216. // // Arrange
  217. // AccountController controller = GetAccountController();
  218. // RegisterModel model = new RegisterModel()
  219. // {
  220. // UserName = "someUser",
  221. // Email = "goodEmail",
  222. // Password = "goodPassword",
  223. // ConfirmPassword = "goodPassword"
  224. // };
  225. // // Act
  226. // ActionResult result = controller.Register(model);
  227. // // Assert
  228. // Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
  229. // RedirectToRouteResult redirectResult = (RedirectToRouteResult)result;
  230. // Assert.AreEqual("Home", redirectResult.RouteValues["controller"]);
  231. // Assert.AreEqual("Index", redirectResult.RouteValues["action"]);
  232. //}
  233. //[TestMethod]
  234. //public void Register_Post_ReturnsViewIfRegistrationFails()
  235. //{
  236. // // Arrange
  237. // AccountController controller = GetAccountController();
  238. // RegisterModel model = new RegisterModel()
  239. // {
  240. // UserName = "duplicateUser",
  241. // Email = "goodEmail",
  242. // Password = "goodPassword",
  243. // ConfirmPassword = "goodPassword"
  244. // };
  245. // // Act
  246. // ActionResult result = controller.Register(model);
  247. // // Assert
  248. // Assert.IsInstanceOfType(result, typeof(ViewResult));
  249. // ViewResult viewResult = (ViewResult)result;
  250. // Assert.AreEqual(model, viewResult.ViewData.Model);
  251. // Assert.AreEqual("Username already exists. Please enter a different user name.", controller.ModelState[""].Errors[0].ErrorMessage);
  252. //}
  253. //[TestMethod]
  254. //public void Register_Post_ReturnsViewIfModelStateIsInvalid()
  255. //{
  256. // // Arrange
  257. // AccountController controller = GetAccountController();
  258. // RegisterModel model = new RegisterModel()
  259. // {
  260. // UserName = "someUser",
  261. // Email = "goodEmail",
  262. // Password = "goodPassword",
  263. // ConfirmPassword = "goodPassword"
  264. // };
  265. // controller.ModelState.AddModelError("", "Dummy error message.");
  266. // // Act
  267. // ActionResult result = controller.Register(model);
  268. // // Assert
  269. // Assert.IsInstanceOfType(result, typeof(ViewResult));
  270. // ViewResult viewResult = (ViewResult)result;
  271. // Assert.AreEqual(model, viewResult.ViewData.Model);
  272. //}
  273. //private static AccountController GetAccountController()
  274. //{
  275. // AccountController controller = new AccountController(new MockFormsAuthenticationService(), new MockMembershipService());
  276. // controller.ControllerContext = new ControllerContext()
  277. // {
  278. // Controller = controller,
  279. // RequestContext = new RequestContext(new MockHttpContext(), new RouteData())
  280. // };
  281. // return controller;
  282. //}
  283. //private class MockFormsAuthenticationService : IFormsAuthenticationService
  284. //{
  285. // public bool SignIn_WasCalled;
  286. // public bool SignOut_WasCalled;
  287. // public void SignIn(string userName, bool createPersistentCookie)
  288. // {
  289. // // verify that the arguments are what we expected
  290. // Assert.AreEqual("someUser", userName);
  291. // Assert.IsFalse(createPersistentCookie);
  292. // SignIn_WasCalled = true;
  293. // }
  294. // public void SignOut()
  295. // {
  296. // SignOut_WasCalled = true;
  297. // }
  298. //}
  299. //private class MockHttpContext : HttpContextBase
  300. //{
  301. // private readonly IPrincipal _user = new GenericPrincipal(new GenericIdentity("someUser"), null /* roles */);
  302. // public override IPrincipal User
  303. // {
  304. // get
  305. // {
  306. // return _user;
  307. // }
  308. // set
  309. // {
  310. // base.User = value;
  311. // }
  312. // }
  313. //}
  314. //private class MockMembershipService : IMembershipService
  315. //{
  316. // public int MinPasswordLength
  317. // {
  318. // get { return 10; }
  319. // }
  320. // public bool ValidateUser(string userName, string password)
  321. // {
  322. // return (userName == "someUser" && password == "goodPassword");
  323. // }
  324. // public MembershipCreateStatus CreateUser(string userName, string password, string email)
  325. // {
  326. // if (userName == "duplicateUser")
  327. // {
  328. // return MembershipCreateStatus.DuplicateUserName;
  329. // }
  330. // // verify that values are what we expected
  331. // Assert.AreEqual("goodPassword", password);
  332. // Assert.AreEqual("goodEmail", email);
  333. // return MembershipCreateStatus.Success;
  334. // }
  335. // public bool ChangePassword(string userName, string oldPassword, string newPassword)
  336. // {
  337. // return (userName == "someUser" && oldPassword == "goodOldPassword" && newPassword == "goodNewPassword");
  338. // }
  339. //}
  340. }
  341. }