changeset 269:17694f70cfb6

Test de PersonaController.
author juanjose.montesdeocaarbos
date Tue, 25 Oct 2011 23:24:57 -0300
parents d255b0621503
children 3bb779434ae8
files Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs
diffstat 1 files changed, 261 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs	Tue Oct 25 23:24:32 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs	Tue Oct 25 23:24:57 2011 -0300
@@ -1,7 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Web;
+using System.Linq;
 using System.Web.Mvc;
 using Agendas.NHibernate;
 using Agendas.Repositories.Tests.Infraestructure;
@@ -24,6 +21,37 @@
             IdentityContext.Init(seguridad.Object, new PersonaRepository(NhHelper.GetSessionFactory()));
         }
 
+        private void PersonaNueva(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);
+            }
+
+            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);
+            }
+        }
+
         [SetUp]
         public void Setup()
         {
@@ -32,13 +60,88 @@
         }
 
         [Test]
-        public void PersonaNueva_ErrorAlGuardarTwitterConArroba()
+        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(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 personaRepository = new PersonaRepository(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", "http://juanjose.montesdeocaarbos.com.ar/blog/"},
+                               {"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"},
@@ -50,32 +153,91 @@
             using (new RequestEmulator(NhHelper.GetSessionFactory()))
             {
                 var resultNuevo = personaController.Nueva(personaNew);
-                Assert.IsInstanceOf(typeof (ViewResult), resultNuevo);
-                Assert.AreEqual("Defaulteditor", ((ViewResult) resultNuevo).ViewName);
-                Assert.IsFalse(personaController.TempData.ContainsKey("error"));
+                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(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 PersonaModificar_ErrorAlGuardarTwitterConArroba()
+        public void PersonaModificarPOST_RequiredFields()
         {
-            var personaController = new PersonaController {ControllerContext = ControllerCtx};
+            var personaController = new PersonaController { ControllerContext = ControllerCtx };
+            PersonaNueva(personaController);
+
+            PersonaEditModel personaEdit = null;
+            using (new RequestEmulator(NhHelper.GetSessionFactory()))
+            {
+                var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory());
 
-            #region PersonaNew
-            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"
-                                 };
+                var form = new FormCollection
+                               {
+                                   {"Id", ""},
+                                   {"Blog", ""},
+                                   {"EMail", ""},
+                                   {"Nombre", ""},
+                                   {"Roles", "Administrador"},
+                                   {"Roles", "Usuario"},
+                                   {"Twitter", ""}
+                               };
+                personaEdit = BindModel<PersonaEditModel>(personaController, form);
+            }
 
             using (new RequestEmulator(NhHelper.GetSessionFactory()))
             {
-                personaController.Nueva(personaNew);
+                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);
             }
-            #endregion
+        }
+
+        [Test]
+        public void PersonaModificarPOST_FormatFields()
+        {
+            var personaController = new PersonaController { ControllerContext = ControllerCtx };
+            PersonaNueva(personaController);
 
             PersonaEditModel personaEdit = null;
             using (new RequestEmulator(NhHelper.GetSessionFactory()))
@@ -85,7 +247,7 @@
                 var form = new FormCollection
                                {
                                    {"Id", personaRepository.GetAll().FirstOrDefault().Id.ToString()},
-                                   {"Blog", "http://juanjose.montesdeocaarbos.com.ar/blog/"},
+                                   {"Blog", "montesdeocaarbos.com.ar/blog/"},
                                    {"EMail", "juanjose.montesdeocaarbos gmail.com"},
                                    {"Nombre", "Juan José Montes de Oca Arbós"},
                                    {"Roles", "Administrador"},
@@ -94,13 +256,85 @@
                                };
                 personaEdit = BindModel<PersonaEditModel>(personaController, form);
             }
-            
+
             using (new RequestEmulator(NhHelper.GetSessionFactory()))
             {
                 var resultModificar = personaController.Modificar(personaEdit);
-                Assert.IsInstanceOf(typeof (ViewResult), resultModificar);
-                Assert.AreEqual("Defaulteditor", ((ViewResult) resultModificar).ViewName);
-                Assert.IsFalse(personaController.TempData.ContainsKey("error"));
+                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(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(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"]);
             }
         }
     }