diff Agendas/trunk/src/Agendas.Web.Tests/Controllers/EventoControllerTests.cs @ 284:79942e030118

#123: Patrocinadores de las vans: Se verifica que para publicar una Van, debe tener al menos un Patrocinador.
author juanjose.montesdeocaarbos
date Wed, 28 Dec 2011 09:35:27 -0300
parents 2e17dfd1ba35
children
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Web.Tests/Controllers/EventoControllerTests.cs	Mon Dec 26 14:34:46 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web.Tests/Controllers/EventoControllerTests.cs	Wed Dec 28 09:35:27 2011 -0300
@@ -1,9 +1,11 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Web;
 using System.Web.Mvc;
 using Agendas.NHibernate;
 using Agendas.Repositories.Tests.Infraestructure;
+using Agendas.Web.Tests.Helpers;
 using AltNetHispano.Agendas.Domain;
 using AltNetHispano.Agendas.Repositories.NHibernate;
 using AltNetHispano.Agendas.Web.Controllers;
@@ -32,6 +34,39 @@
 			}
 		}
 
+		private void PopulatePatrocinador()
+		{
+			//using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			//{
+			//    var patrocinadorController = new PatrocinadorController(new HttpServerStub()) 
+			//    { ControllerContext = ControllerCtx };
+			//    var form = new FormCollection
+			//                {
+			//                    {"Nombre", "APRESS"},
+			//                    {"Logo", @"images\apress.gif"}
+			//                };
+			//    var patrocinadorNew = BindModel<PatrocinadorNewModel>(patrocinadorController, form);
+			//    patrocinadorController.Nuevo(patrocinadorNew);
+
+			//}
+			//using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			//{
+			//    var patrocinadorController = new PatrocinadorController(new HttpServerStub());
+			//    patrocinadorController.Nuevo(new PatrocinadorNewModel
+			//                                    {
+			//                                        Nombre = "jetbrains"
+			//                                    });
+			//}
+			//using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			//{
+			//    var patrocinadorController = new PatrocinadorController(new HttpServerStub());
+			//    patrocinadorController.Nuevo(new PatrocinadorNewModel
+			//                                    {
+			//                                        Nombre = "oreilly"
+			//                                    });
+			//}
+		}
+
 		private Guid GetGuidPersona()
 		{
 			using (new RequestEmulator(NhHelper.GetSessionFactory()))
@@ -42,6 +77,16 @@
 			}
 		}
 
+		private static IEnumerable<PatrocinadorDto> GetPatrocinadores()
+		{
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				var patrocinadorController = new PatrocinadorController();
+				var viewResult = (ViewResult)patrocinadorController.Index();
+				return ((PatrocinadorIndexModel)viewResult.Model).Items;
+			}
+		}
+
 		private void SetearUsuario()
 		{
 			var seguridad = new Mock<ISeguridad>();
@@ -49,11 +94,69 @@
 			IdentityContext.Init(seguridad.Object, new PersonaRepository(NhHelper.GetSessionFactory()));
 		}
 
+		private IEnumerable<EventoDto> PrepararEventoParaConfirmar(EventoController eventoController)
+		{
+			ViewResult viewResult = null;
+			IEnumerable<EventoDto> eventosDto = null;
+			var formEvento = new FormCollection
+                           {
+                               {"Duracion", (new TimeSpan(0, 0, 0)).ToString()},
+                               {"Fecha", (DateTime.Today.AddDays(15)).ToString()},
+                               {"Hora", (new TimeSpan(18, 0, 0)).ToString()},
+                               {"Ponentes", GetGuidPersona().ToString()},
+                               {"TipoEvento", ((int)TipoEvento.Van).ToString()},
+                               {"Titulo", "Título 2"}
+                           };
+			var eventoNew = BindModel<EventoNewModel>(eventoController, formEvento);
+
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				eventoController.Nuevo(eventoNew);
+			}
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				viewResult = (ViewResult)eventoController.Index();
+			}
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				eventosDto = ((EventoIndexModel)viewResult.Model).ProximosEventos;
+			}
+
+			return eventosDto;
+		}
+
+		private IEnumerable<EventoDto> PrepararEventoParaPublicar(EventoController eventoController)
+		{
+			var eventosDto = PrepararEventoParaConfirmar(eventoController);
+
+			foreach (var model in eventosDto.Select(eventoDto => new EventoConfirmModel()
+			                                                     	{
+			                                                     		Id = eventoDto.Id,
+																		//Recordatorios = null,// Lo comentamos para no agregar la referencia a Agendas.Twitter
+																		Titulo = eventoDto.Titulo
+			                                                     	}))
+			{
+				using (new RequestEmulator(NhHelper.GetSessionFactory()))
+				{
+					eventoController.Confirmar(model);
+				}
+			}
+
+			return eventosDto;
+		}
+
+		internal void RegisterBinders()
+		{
+			ModelBinders.Binders[typeof(HttpPostedFileBase)] = new HttpPostedFileBaseModelBinder();
+		}
+
 		[SetUp]
 		public void Setup()
 		{
 			NhHelperTest.CleanDb();
 			PopulatePersona();
+			PopulatePatrocinador();
+			RegisterBinders();
 			SetearUsuario();
 		}
 
@@ -112,35 +215,12 @@
 		[Test]
 		public void Publicar_Evento_Erroneo_Duracion_Fuera_Rango()
 		{
-			ViewResult viewResult = null;
-			IEnumerable<EventoDto> eventosDto = null;
+			var eventoController = new EventoController { ControllerContext = ControllerCtx };
+			var eventosDto = PrepararEventoParaPublicar(eventoController);
 			EventoPublicarModel eventoPublicar = null;
-			var eventoController = new EventoController { ControllerContext = ControllerCtx };
-			var formEvento = new FormCollection
-                           {
-                               {"Duracion", (new TimeSpan(0, 0, 0)).ToString()},
-                               {"Fecha", (DateTime.Today.AddDays(15)).ToString()},
-                               {"Hora", (new TimeSpan(18, 0, 0)).ToString()},
-                               {"Ponentes", GetGuidPersona().ToString()},
-                               {"TipoEvento", ((int)TipoEvento.Van).ToString()},
-                               {"Titulo", "Título 2"}
-                           };
-			var eventoNew = BindModel<EventoNewModel>(eventoController, formEvento);
 
 			using (new RequestEmulator(NhHelper.GetSessionFactory()))
 			{
-				eventoController.Nuevo(eventoNew);
-			}
-			using (new RequestEmulator(NhHelper.GetSessionFactory()))
-			{
-				viewResult = (ViewResult)eventoController.Index();
-			}
-			using (new RequestEmulator(NhHelper.GetSessionFactory()))
-			{
-				eventosDto = ((EventoIndexModel)viewResult.Model).ProximosEventos;
-			}
-			using (new RequestEmulator(NhHelper.GetSessionFactory()))
-			{
 				var firstEvent = eventosDto.LastOrDefault();
 				var formPublicar = new FormCollection
                            {
@@ -148,7 +228,8 @@
                                {"Titulo", "Título 2"},
                                {"NroOrden", "1"},
                                {"UrlWiki", "http://www.altnethistpano.org/vans/titulo-1.ashx"},
-                               {"DuracionReal", (new TimeSpan(0, 0, 0)).ToString()}
+                               {"DuracionReal", (new TimeSpan(0, 0, 0)).ToString()},
+										{"Patrocinadores", (Guid.NewGuid()).ToString()}
                            };
 				eventoPublicar = BindModel<EventoPublicarModel>(eventoController, formPublicar);
 			}
@@ -162,6 +243,71 @@
 
 			}
 		}
+
+		[Test]
+		public void Publicar_Evento_Sin_Patrocinador()
+		{
+			var eventoController = new EventoController { ControllerContext = ControllerCtx };
+			var eventosDto = PrepararEventoParaPublicar(eventoController);
+			EventoPublicarModel eventoPublicar = null;
+
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				var firstEvent = eventosDto.LastOrDefault();
+				var formPublicar = new FormCollection
+                           {
+                               {"Id", (firstEvent.Id).ToString()},
+                               {"Titulo", "Título 2"},
+                               {"NroOrden", "1"},
+                               {"UrlWiki", "http://www.altnethistpano.org/vans/titulo-1.ashx"},
+                               {"DuracionReal", (new TimeSpan(2, 0, 0)).ToString()}
+                           };
+				eventoPublicar = BindModel<EventoPublicarModel>(eventoController, formPublicar);
+			}
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				var resultPublicar = (ViewResult)eventoController.Publicar(eventoPublicar);
+				var modelState = resultPublicar.ViewData.ModelState;
+
+				Assert.AreEqual(1, modelState["Patrocinadores"].Errors.Count);
+				Assert.AreEqual("El campo Patrocinadores es obligatorio.", modelState["Patrocinadores"].Errors.FirstOrDefault().ErrorMessage);
+
+			}
+		}
+
+		[Test]
+		public void Publicar_Evento_Satisfactoriamente()
+		{
+			var eventoController = new EventoController {ControllerContext = ControllerCtx};
+			var eventosDto = PrepararEventoParaPublicar(eventoController);
+			EventoPublicarModel eventoPublicar = null;
+			//var patrocinadores = GetPatrocinadores();
+			EventoDto lastEvent = null;
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				lastEvent = eventosDto.LastOrDefault();
+			}
+			
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				var formPublicar = new FormCollection
+				                   	{
+				                   		{"Id", (lastEvent.Id).ToString()},
+				                   		{"Titulo", "Título 2"},
+				                   		{"NroOrden", "1"},
+				                   		{"UrlWiki", "http://www.altnethistpano.org/vans/titulo-1.ashx"},
+				                   		{"DuracionReal", (new TimeSpan(2, 0, 0)).ToString()},
+										{"Patrocinadores", (Guid.NewGuid()).ToString()}
+				                   	};
+				eventoPublicar = BindModel<EventoPublicarModel>(eventoController, formPublicar);
+			}
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				var resultPublicar = eventoController.Publicar(eventoPublicar);
+				Assert.IsInstanceOf(typeof (RedirectToRouteResult), resultPublicar);
+				Assert.AreEqual("Index", ((RedirectToRouteResult) resultPublicar).RouteValues["action"]);
+			}
+		}
 	}
 
 }
\ No newline at end of file