Mercurial > altnet-hispano
view Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs @ 302:11dddcc9a862 tip
Historico de Eventos, no muestra bien la Url del Patrocinador.
author | juanjose.montesdeocaarbos |
---|---|
date | Tue, 14 Aug 2012 21:54:30 -0300 |
parents | 3bb779434ae8 |
children |
line wrap: on
line source
using System.Linq; using System.Web.Mvc; using Agendas.NHibernate; using Agendas.Repositories.Tests.Infraestructure; using AltNetHispano.Agendas.Domain; using AltNetHispano.Agendas.Repositories.NHibernate; using AltNetHispano.Agendas.Web.Controllers; using AltNetHispano.Agendas.Web.Models; using Moq; using NUnit.Framework; namespace Agendas.Web.Tests.Controllers { [TestFixture] public class PersonaControllerTests : MvcControllerTestsBase { private void SetearUsuario() { var seguridad = new Mock<ISeguridad>(); seguridad.Setup(s => s.GetUserName()).Returns("neluz"); IdentityContext.Init(seguridad.Object, new PersonaRepository(NhHelper.GetSessionFactory())); } private void PersonaNueva(ref PersonaController controller) { var personaNew = new PersonaNewModel { Blog = "http://juanjose.montesdeocaarbos.com.ar/blog/", EMail = "juanjose.montesdeocaarbos@gmail.com", Nombre = "Juan José Montes de Oca Arbós", Roles = new string[] { "Administrador", "Usuario" }, Twitter = "jjmoa" }; using (new RequestEmulator(NhHelper.GetSessionFactory())) { controller.Nueva(personaNew); controller = new PersonaController { ControllerContext = ControllerCtx }; } personaNew = new PersonaNewModel { Blog = "http://nelopauselli.blogspot.com/", EMail = "nelo@gmail.com", Nombre = "Nelo Mariano Pauselli", Roles = new string[] { "Administrador", "Usuario" }, Twitter = "npauselli" }; using (new RequestEmulator(NhHelper.GetSessionFactory())) { controller.Nueva(personaNew); controller = new PersonaController { ControllerContext = ControllerCtx }; } } [SetUp] public void Setup() { NhHelperTest.CleanDb(); SetearUsuario(); ControllerCtx = new ControllerContext(); } [Test] public void PersonaNuevaGET() { var personaController = new PersonaController { ControllerContext = ControllerCtx }; using (new RequestEmulator(NhHelper.GetSessionFactory())) { var resultNuevo = personaController.Nueva(); Assert.IsInstanceOf(typeof(ViewResult), resultNuevo); Assert.AreEqual("Defaulteditor", ((ViewResult)resultNuevo).ViewName); } } [Test] public void PersonaModificarGET() { var personaController = new PersonaController { ControllerContext = ControllerCtx }; PersonaNueva(ref personaController); using (new RequestEmulator(NhHelper.GetSessionFactory())) { var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory()); var resultModificar = personaController.Modificar(personaRepository.GetAll().FirstOrDefault().Id.ToString()); Assert.IsInstanceOf(typeof(ViewResult), resultModificar); Assert.AreEqual("Defaulteditor", ((ViewResult)resultModificar).ViewName); } } [Test] public void PersonaModificarGET_PersonaInexistente() { var personaController = new PersonaController { ControllerContext = ControllerCtx }; using (new RequestEmulator(NhHelper.GetSessionFactory())) { var resultModificar = personaController.Modificar("11111111-1111-1111-1111-111111111111"); Assert.IsInstanceOf(typeof(RedirectToRouteResult), resultModificar); Assert.AreEqual("Index", ((RedirectToRouteResult)resultModificar).RouteValues["action"]); Assert.AreEqual("No se encontró la persona que intenta modificar", personaController.TempData["error"]); } } [Test] public void PersonaNuevaPOST_RequiredFields() { var personaController = new PersonaController { ControllerContext = ControllerCtx }; var form = new FormCollection { {"Blog", ""}, {"EMail", ""}, {"Nombre", ""}, {"Roles", "Administrador"}, {"Roles", "Usuario"}, {"Twitter", ""} }; var personaNew = BindModel<PersonaNewModel>(personaController, form); using (new RequestEmulator(NhHelper.GetSessionFactory())) { var resultNuevo = personaController.Nueva(personaNew); Assert.IsInstanceOf(typeof(ViewResult), resultNuevo); var viewResultNuevo = (ViewResult) resultNuevo; Assert.AreEqual("Defaulteditor", viewResultNuevo.ViewName); Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["EMail"].Errors.Count); Assert.AreEqual("El campo EMail es obligatorio.", viewResultNuevo.ViewData.ModelState["EMail"].Errors[0].ErrorMessage); Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Nombre"].Errors.Count); Assert.AreEqual("El campo Nombre es obligatorio.", viewResultNuevo.ViewData.ModelState["Nombre"].Errors[0].ErrorMessage); Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Twitter"].Errors.Count); Assert.AreEqual("El campo Twitter es obligatorio.", viewResultNuevo.ViewData.ModelState["Twitter"].Errors[0].ErrorMessage); } } [Test] public void PersonaNuevaPOST_FormatFields() { var personaController = new PersonaController { ControllerContext = ControllerCtx }; var form = new FormCollection { {"Blog", "montesdeocaarbos.com.ar/blog/"}, {"EMail", "juanjose.montesdeocaarbos gmail.com"}, {"Nombre", "Juan José Montes de Oca Arbós"}, {"Roles", "Administrador"}, {"Roles", "Usuario"}, {"Twitter", "@jjmoa"} }; var personaNew = BindModel<PersonaNewModel>(personaController, form); using (new RequestEmulator(NhHelper.GetSessionFactory())) { var resultNuevo = personaController.Nueva(personaNew); Assert.IsInstanceOf(typeof(ViewResult), resultNuevo); var viewResultNuevo = (ViewResult)resultNuevo; Assert.AreEqual("Defaulteditor", viewResultNuevo.ViewName); Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Twitter"].Errors.Count); Assert.AreEqual("No debe ingresar el arroba al escribir la cuenta de twitter.", viewResultNuevo.ViewData.ModelState["Twitter"].Errors[0].ErrorMessage); Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Blog"].Errors.Count); Assert.AreEqual("The Blog field is not a valid fully-qualified http, https, or ftp URL.", viewResultNuevo.ViewData.ModelState["Blog"].Errors[0].ErrorMessage); Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["EMail"].Errors.Count); Assert.AreEqual("The EMail field is not a valid e-mail address.", viewResultNuevo.ViewData.ModelState["EMail"].Errors[0].ErrorMessage); } } [Test] public void PersonaNuevaPOST_PersonaExistente() { var personaController = new PersonaController { ControllerContext = ControllerCtx }; PersonaNueva(ref personaController); var form = new FormCollection { {"Blog", "http://montesdeocaarbos.com.ar/blog/"}, {"EMail", "juanjose.montesdeocaarbos@gmail.com"}, {"Nombre", "Juan José Montes de Oca Arbós"}, {"Roles", "Administrador"}, {"Roles", "Usuario"}, {"Twitter", "jjmoa"} }; var personaNew = BindModel<PersonaNewModel>(personaController, form); using (new RequestEmulator(NhHelper.GetSessionFactory())) { var resultNuevo = personaController.Nueva(personaNew); Assert.IsInstanceOf(typeof(ViewResult), resultNuevo); var viewResultNuevo = (ViewResult)resultNuevo; Assert.AreEqual("Defaulteditor", viewResultNuevo.ViewName); Assert.AreEqual("Ya existe una persona con el nombre " + personaNew.Nombre, personaController.TempData["error"]); } } [Test] public void PersonaModificarPOST_RequiredFields() { var personaController = new PersonaController { ControllerContext = ControllerCtx }; PersonaNueva(ref personaController); PersonaEditModel personaEdit = null; using (new RequestEmulator(NhHelper.GetSessionFactory())) { var form = new FormCollection { {"Id", ""}, {"Blog", ""}, {"EMail", ""}, {"Nombre", ""}, {"Roles", "Administrador"}, {"Roles", "Usuario"}, {"Twitter", ""} }; personaEdit = BindModel<PersonaEditModel>(personaController, form); } using (new RequestEmulator(NhHelper.GetSessionFactory())) { var resultNuevo = personaController.Modificar(personaEdit); Assert.IsInstanceOf(typeof(ViewResult), resultNuevo); var viewResultNuevo = (ViewResult)resultNuevo; Assert.AreEqual("Defaulteditor", viewResultNuevo.ViewName); Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Id"].Errors.Count); Assert.AreEqual("El campo Id es obligatorio.", viewResultNuevo.ViewData.ModelState["Id"].Errors[0].ErrorMessage); Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["EMail"].Errors.Count); Assert.AreEqual("El campo EMail es obligatorio.", viewResultNuevo.ViewData.ModelState["EMail"].Errors[0].ErrorMessage); Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Nombre"].Errors.Count); Assert.AreEqual("El campo Nombre es obligatorio.", viewResultNuevo.ViewData.ModelState["Nombre"].Errors[0].ErrorMessage); Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Twitter"].Errors.Count); Assert.AreEqual("El campo Twitter es obligatorio.", viewResultNuevo.ViewData.ModelState["Twitter"].Errors[0].ErrorMessage); } } [Test] public void PersonaModificarPOST_FormatFields() { var personaController = new PersonaController { ControllerContext = ControllerCtx }; PersonaNueva(ref personaController); PersonaEditModel personaEdit = null; using (new RequestEmulator(NhHelper.GetSessionFactory())) { var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory()); var form = new FormCollection { {"Id", personaRepository.GetAll().FirstOrDefault().Id.ToString()}, {"Blog", "montesdeocaarbos.com.ar/blog/"}, {"EMail", "juanjose.montesdeocaarbos gmail.com"}, {"Nombre", "Juan José Montes de Oca Arbós"}, {"Roles", "Administrador"}, {"Roles", "Usuario"}, {"Twitter", "@jjmoa"} }; personaEdit = BindModel<PersonaEditModel>(personaController, form); } using (new RequestEmulator(NhHelper.GetSessionFactory())) { var resultModificar = personaController.Modificar(personaEdit); Assert.IsInstanceOf(typeof(ViewResult), resultModificar); var viewResultModificar = (ViewResult)resultModificar; Assert.AreEqual("Defaulteditor", viewResultModificar.ViewName); Assert.AreEqual(1, viewResultModificar.ViewData.ModelState["Twitter"].Errors.Count); Assert.AreEqual("No debe ingresar el arroba al escribir la cuenta de twitter.", viewResultModificar.ViewData.ModelState["Twitter"].Errors[0].ErrorMessage); Assert.AreEqual(1, viewResultModificar.ViewData.ModelState["Blog"].Errors.Count); Assert.AreEqual("The Blog field is not a valid fully-qualified http, https, or ftp URL.", viewResultModificar.ViewData.ModelState["Blog"].Errors[0].ErrorMessage); Assert.AreEqual(1, viewResultModificar.ViewData.ModelState["EMail"].Errors.Count); Assert.AreEqual("The EMail field is not a valid e-mail address.", viewResultModificar.ViewData.ModelState["EMail"].Errors[0].ErrorMessage); } } [Test] public void PersonaModificarPOST_PersonaExistente() { var personaController = new PersonaController { ControllerContext = ControllerCtx }; PersonaNueva(ref personaController); PersonaEditModel personaEdit = null; using (new RequestEmulator(NhHelper.GetSessionFactory())) { var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory()); var form = new FormCollection { {"Id", personaRepository.GetAll().FirstOrDefault().Id.ToString()}, {"Blog", "http://nelopauselli.blogspot.com/"}, {"EMail", "nelo@gmail.com"}, {"Nombre", "Nelo Mariano Pauselli"}, {"Roles", "Administrador"}, {"Roles", "Usuario"}, {"Twitter", "npauselli"} }; personaEdit = BindModel<PersonaEditModel>(personaController, form); } using (new RequestEmulator(NhHelper.GetSessionFactory())) { var resultEdit = personaController.Modificar(personaEdit); Assert.IsInstanceOf(typeof(ViewResult), resultEdit); var viewResultNuevo = (ViewResult)resultEdit; Assert.AreEqual("Defaulteditor", viewResultNuevo.ViewName); Assert.AreEqual("Ya existe una persona con el twitter " + personaEdit.Twitter, personaController.TempData["error"]); } } [Test] public void PersonaModificarPOST_Successful() { var personaController = new PersonaController { ControllerContext = ControllerCtx }; PersonaNueva(ref personaController); PersonaEditModel personaEdit = null; using (new RequestEmulator(NhHelper.GetSessionFactory())) { var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory()); var form = new FormCollection { {"Id", personaRepository.GetAll().FirstOrDefault().Id.ToString()}, {"Blog", "http://montesdeocaarbos.com.ar/blog/"}, {"EMail", "juanjose.montesdeocaarbos@gmail.com"}, {"Nombre", "Juan José Montes de Oca Arbós"}, {"Roles", "Administrador"}, {"Roles", "Usuario"}, {"Twitter", "jjmoa"} }; personaEdit = BindModel<PersonaEditModel>(personaController, form); } using (new RequestEmulator(NhHelper.GetSessionFactory())) { var resultModificar = personaController.Modificar(personaEdit); Assert.IsInstanceOf(typeof(RedirectToRouteResult), resultModificar); Assert.AreEqual("Index", ((RedirectToRouteResult)resultModificar).RouteValues["action"]); } } [Test] public void PersonaQuitarGET() { var personaController = new PersonaController { ControllerContext = ControllerCtx }; PersonaNueva(ref personaController); using (new RequestEmulator(NhHelper.GetSessionFactory())) { var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory()); var resultQuitar = personaController.Quitar(personaRepository.GetAll().FirstOrDefault().Id.ToString()); Assert.IsInstanceOf(typeof(RedirectToRouteResult), resultQuitar); var redirectResultQuitar = (RedirectToRouteResult)resultQuitar; Assert.AreEqual("Index", redirectResultQuitar.RouteValues["action"]); Assert.AreEqual(1, personaController.TempData.Count); Assert.AreEqual("Se ha eliminado correctamente a la persona", personaController.TempData["notification"]); Assert.IsNull(personaController.TempData["warning"]); Assert.IsNull(personaController.TempData["error"]); } } [Test] public void PersonaQuitarGET_PersonaInexistente() { var personaController = new PersonaController { ControllerContext = ControllerCtx }; using (new RequestEmulator(NhHelper.GetSessionFactory())) { const string personaId = "11111111-1111-1111-1111-111111111111"; var resultVer = personaController.Quitar(personaId); Assert.IsInstanceOf(typeof(RedirectToRouteResult), resultVer); Assert.AreEqual("Index", ((RedirectToRouteResult)resultVer).RouteValues["action"]); Assert.AreEqual(string.Format("No se pudo encontrar la persona cuyo Id sea {0}", personaId), personaController.TempData["error"]); } } [Test] public void PersonaVerGET() { var personaController = new PersonaController { ControllerContext = ControllerCtx }; PersonaNueva(ref personaController); using (new RequestEmulator(NhHelper.GetSessionFactory())) { var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory()); var resultModificar = personaController.Ver(personaRepository.GetAll().FirstOrDefault().Id.ToString()); Assert.IsInstanceOf(typeof(ViewResult), resultModificar); Assert.AreEqual("DefaultViewer", ((ViewResult)resultModificar).ViewName); } } [Test] public void PersonaVerGET_PersonaInexistente() { var personaController = new PersonaController { ControllerContext = ControllerCtx }; using (new RequestEmulator(NhHelper.GetSessionFactory())) { var resultVer = personaController.Ver("11111111-1111-1111-1111-111111111111"); Assert.IsInstanceOf(typeof(RedirectToRouteResult), resultVer); Assert.AreEqual("Index", ((RedirectToRouteResult)resultVer).RouteValues["action"]); Assert.AreEqual("No se encontró la persona que intenta visualizar", personaController.TempData["error"]); } } } }