view Agendas/trunk/src/Agendas.Web.Tests/Controllers/EventoControllerTests.cs @ 243:59f8fa2f835e

Ticket 177: Valor de Duración de VAN en 0.
author juanjose.montesdeocaarbos
date Thu, 06 Oct 2011 08:13:05 -0300
parents 5cc1681d3789
children bdc946dd5c94
line wrap: on
line source

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 Publicar_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 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);
                
            }
        }
    }

}