view Agendas/trunk/src/Agendas.Web.Tests/Controllers/EventoControllerTests.cs @ 257:730b80afa70d

Ticket #191: Perfil de Usuarios
author juanjose.montesdeocaarbos
date Wed, 19 Oct 2011 09:19:20 -0300
parents bdc946dd5c94
children cef4fc893333
line wrap: on
line source

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

		private void SetearUsuario()
		{
			var seguridad = new Mock<ISeguridad>();
			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 Nuevo_Evento()
        {
            ViewResult viewResult = null;
            IEnumerable<EventoDto> eventosDto = null;
            EventoPublicarModel eventoPublicar = null;
            var eventoController = new EventoController();

            var eventoNew = new EventoNewModel
            {
                Duracion = new TimeSpan(0, 0, 0),
                Fecha = DateTime.Today.AddDays(15),
                Hora = new TimeSpan(18, 0, 0),
                Ponentes = new[] { GetGuidPersona() },
                TipoEvento = (int)TipoEvento.Van,
                Titulo = "Título"
            };

            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();

            var eventoNew = new EventoNewModel
            {
                Duracion = new TimeSpan(0, 0, 0),
                Fecha = DateTime.Today.AddDays(15),
                Hora = new TimeSpan(18, 0, 0),
                TipoEvento = (int)TipoEvento.Van,
                Titulo = "Título"
            };

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

	    [Test]
		public void Publicar_Evento_Erroneo_Duracion_Fuera_Rango()
		{
		    ViewResult viewResult = null;
		    IEnumerable<EventoDto> eventosDto = null;
		    EventoPublicarModel eventoPublicar = null;
			var eventoController = new EventoController();

			var eventoNew = new EventoNewModel
			                	{
			                		Duracion = new TimeSpan(0, 0, 0),
			                		Fecha = DateTime.Today.AddDays(15),
			                		Hora = new TimeSpan(18, 0, 0),
                                    Ponentes = new[] { GetGuidPersona()},
			                		TipoEvento = (int) TipoEvento.Van,
			                		Titulo = "Título 2"
			                	};
		    
			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();

                eventoPublicar = new EventoPublicarModel
                {
                    Id = firstEvent.Id,
                    NroOrden = 1,
                    //Titulo = "Título 2",
                    UrlWiki = "http://www.altnethistpano.org/vans/titulo-1.ashx",
                    DuracionReal = new TimeSpan(0, 0, 0)
                };
            }
            using (new RequestEmulator(NhHelper.GetSessionFactory()))
            {
                var resultPublicar = (ViewResult)eventoController.Publicar(eventoPublicar);
                var modelState = resultPublicar.ViewData.ModelState.FirstOrDefault().Value;

                Assert.AreEqual(1, modelState.Errors.Count);
                Assert.AreEqual("Duración fuera de rango", modelState.Errors.FirstOrDefault().ErrorMessage);
                
            }
        }
	}

}