view 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 source

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;
using AltNetHispano.Agendas.Web.Models;
using Moq;
using NUnit.Framework;

namespace Agendas.Web.Tests.Controllers
{
	[TestFixture]
	public class EventoControllerTests : MvcControllerTestsBase
	{
		private void PopulatePersona()
		{
			using (new RequestEmulator(NhHelper.GetSessionFactory()))
			{
				var personaController = new PersonaController();
				personaController.Nueva(new PersonaNewModel
				{
					Nombre = "Persona 1",
					Blog = "http://blog.persona1.com",
					EMail = "info@persona1.com",
					Roles = new string[] { "Administrador", "Usuario" },
					Twitter = "persona1"
				});
			}
		}

		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()))
			{
				var personaController = new PersonaController();
				var viewResult = (ViewResult)personaController.Index();
				return ((PersonaIndexModel)viewResult.Model).Items.FirstOrDefault().Id;
			}
		}

		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>();
			seguridad.Setup(s => s.GetUserName()).Returns("neluz");
			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();
		}

		[Test]
		public void Nuevo_Evento()
		{
			//ViewResult viewResult = null;
			//IEnumerable<EventoDto> eventosDto = null;
			//EventoPublicarModel eventoPublicar = null;
			var eventoController = new EventoController { ControllerContext = ControllerCtx };
			var form = 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"}
                           };
			var eventoNew = BindModel<EventoNewModel>(eventoController, form);

			using (new RequestEmulator(NhHelper.GetSessionFactory()))
			{
				var resultNuevo = eventoController.Nuevo(eventoNew);
				Assert.IsInstanceOf(typeof(RedirectToRouteResult), resultNuevo);
				Assert.AreEqual("Index", ((RedirectToRouteResult)resultNuevo).RouteValues["action"]);
			}
		}

		[Test]
		public void Nuevo_Evento_Erroneo_Sin_Ponente()
		{
			//ViewResult viewResult = null;
			//IEnumerable<EventoDto> eventosDto = null;
			//EventoPublicarModel eventoPublicar = null;
			var eventoController = new EventoController { ControllerContext = ControllerCtx };
			var form = new FormCollection
                           {
                               {"Duracion", (new TimeSpan(0, 0, 0)).ToString()},
                               {"Fecha", (DateTime.Today.AddDays(15)).ToString()},
                               {"Hora", (new TimeSpan(18, 0, 0)).ToString()},
                               {"TipoEvento", ((int)TipoEvento.Van).ToString()},
                               {"Titulo", "Título"}
                           };
			var eventoNew = BindModel<EventoNewModel>(eventoController, form);

			using (new RequestEmulator(NhHelper.GetSessionFactory()))
			{
				var resultNuevo = (ViewResult)eventoController.Nuevo(eventoNew);
				var modelState = resultNuevo.ViewData.ModelState;
				Assert.AreEqual(1, modelState[string.Empty].Errors.Count);
				Assert.AreEqual("Debe indicar el ponente para este tipo de evento", modelState[string.Empty].Errors.FirstOrDefault().ErrorMessage);
			}
		}

		[Test]
		public void Publicar_Evento_Erroneo_Duracion_Fuera_Rango()
		{
			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(0, 0, 0)).ToString()},
										{"Patrocinadores", (Guid.NewGuid()).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[string.Empty].Errors.Count);
				Assert.AreEqual("Duración fuera de rango", modelState[string.Empty].Errors.FirstOrDefault().ErrorMessage);

			}
		}

		[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"]);
			}
		}
	}

}