# HG changeset patch # User juanjose.montesdeocaarbos # Date 1319109415 10800 # Node ID e6c041d8e1bf741bcc8f95e0618dc78e4a5c8ee8 # Parent 52fe43e36f5f2f4e93088654761096250c186c5a Ticket #193.1: Al guardar (nuevo/modificar) una persona, que la cuenta de twitter no comience con @ diff -r 52fe43e36f5f -r e6c041d8e1bf Agendas/trunk/src/Agendas.Domain/Services/PersonaService.cs --- a/Agendas/trunk/src/Agendas.Domain/Services/PersonaService.cs Wed Oct 19 22:59:02 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Domain/Services/PersonaService.cs Thu Oct 20 08:16:55 2011 -0300 @@ -83,6 +83,9 @@ public ResultadoAdd Add(string nombre, string twitter, string mail, string blog, IEnumerable roles) { + if (twitter.StartsWith("@")) + return new ResultadoAdd("No debe ingresar el arroba al escribir la cuenta de twitter."); + var persona = _personaRepository.GetByNombre(nombre); if (persona!=null) return new ResultadoAdd("Ya existe una persona con el nombre " + nombre); @@ -118,6 +121,9 @@ if (persona == null) return new Resultado(false, string.Format("No se pudo encontrar la persona cuyo Id sea {0}", id)); + if (twitter.StartsWith("@")) + return new ResultadoAdd("No debe ingresar el arroba al escribir la cuenta de twitter."); + persona.Nombre = nombre; persona.Twitter = twitter; persona.Mail = mail; diff -r 52fe43e36f5f -r e6c041d8e1bf Agendas/trunk/src/Agendas.Web.Tests/Agendas.Web.Tests.csproj --- a/Agendas/trunk/src/Agendas.Web.Tests/Agendas.Web.Tests.csproj Wed Oct 19 22:59:02 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web.Tests/Agendas.Web.Tests.csproj Thu Oct 20 08:16:55 2011 -0300 @@ -57,6 +57,7 @@ + diff -r 52fe43e36f5f -r e6c041d8e1bf Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Web.Tests/Controllers/PersonaControllerTests.cs Thu Oct 20 08:16:55 2011 -0300 @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +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 + { + private void SetearUsuario() + { + var seguridad = new Mock(); + seguridad.Setup(s => s.GetUserName()).Returns("neluz"); + IdentityContext.Init(seguridad.Object, new PersonaRepository(NhHelper.GetSessionFactory())); + } + + [SetUp] + public void Setup() + { + NhHelperTest.CleanDb(); + //PopulatePersona(); + SetearUsuario(); + } + + [Test] + public void PersonaNueva_ErrorAlGuardarTwitterConArroba() + { + var personaController = new PersonaController(); + + 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())) + { + Assert.IsFalse(personaController.TempData.ContainsKey("error")); + var resultNuevo = personaController.Nueva(personaNew); + Assert.IsInstanceOf(typeof (ViewResult), resultNuevo); + Assert.AreEqual("Defaulteditor", ((ViewResult) resultNuevo).ViewName); + Assert.IsTrue(personaController.TempData.ContainsKey("error")); + Assert.AreNotEqual(string.Empty, personaController.TempData["error"]); + } + } + + [Test] + public void PersonaModificar_ErrorAlGuardarTwitterConArroba() + { + var personaController = new PersonaController(); + #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" + }; + + using (new RequestEmulator(NhHelper.GetSessionFactory())) + { + personaController.Nueva(personaNew); + } + #endregion + + PersonaEditModel personaEdit = null; + using (new RequestEmulator(NhHelper.GetSessionFactory())) + { + var personaRepository = new PersonaRepository(NhHelper.GetSessionFactory()); + personaEdit = new PersonaEditModel + { + Id = personaRepository.GetAll().FirstOrDefault().Id.ToString(), + 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())) + { + Assert.IsFalse(personaController.TempData.ContainsKey("error")); + var resultModificar = personaController.Modificar(personaEdit); + Assert.IsInstanceOf(typeof (ViewResult), resultModificar); + Assert.AreEqual("Defaulteditor", ((ViewResult) resultModificar).ViewName); + Assert.IsTrue(personaController.TempData.ContainsKey("error")); + Assert.AreNotEqual(string.Empty, personaController.TempData["error"]); + } + } + } +} diff -r 52fe43e36f5f -r e6c041d8e1bf Agendas/trunk/src/Agendas.Web/Models/PersonaModel.cs --- a/Agendas/trunk/src/Agendas.Web/Models/PersonaModel.cs Wed Oct 19 22:59:02 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/Models/PersonaModel.cs Thu Oct 20 08:16:55 2011 -0300 @@ -1,7 +1,10 @@ using System; using System.Collections.Generic; +//using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations; using System.Web.Mvc; +using DataAnnotationsExtensions; + namespace AltNetHispano.Agendas.Web.Models { @@ -15,12 +18,14 @@ public string Nombre { get; set; } [Required] + [CustomValidation(typeof(PersonaValidation), "IsTwitterAccount")] public string Twitter { get; set; } [Required] + [Email] public string EMail { get; set; } - [DataType(DataType.Url)] + [Url] public string Blog { get; set; } public string[] Roles { get; set; } @@ -36,12 +41,14 @@ public string Nombre { get; set; } [Required] + [CustomValidation(typeof(PersonaValidation), "IsTwitterAccount")] public string Twitter { get; set; } [Required] + [Email] public string EMail { get; set; } - [DataType(DataType.Url)] + [Url] public string Blog { get; set; } public string[] Roles { get; set; } @@ -70,4 +77,17 @@ [HiddenInput] public string Blog { get; set; } } + + //TODO: Ver donde movemos esta clase. + public class PersonaValidation + { + public static ValidationResult IsTwitterAccount(string value) + { + //[RegularExpression(@"^([\w\d\-\.]+)$")] + return value.StartsWith("@") + ? new ValidationResult("No debe ingresar el arroba al escribir la cuenta de twitter.") + : ValidationResult.Success; + } + } + } \ No newline at end of file