using System.Web.Mvc;
using NUnit.Framework;
using Wierszowki.Controllers;
using Wierszowki.Core.Linq;
using Wierszowki.Services;
using Wierszowki.Services.Interfaces;

namespace Wierszowki.Tests.Web
{
    [TestFixture]
    public class UserControllerTests
    {
        [Test]
        public void Create_New_Successfully_Creates_New_User()
        {
            //Arrange
            IUserService service = new TestUserService();
            var user = GetValidUser();

            //Act
            var controller = new UserController(service);
            var result = controller.Create(user);
            
            //Assert
            Assert.That(controller.ModelState.IsValid, Is.True);
            Assert.That(result, Is.Not.Null);
            Assert.That(((ViewResult)result).ViewName, Is.EqualTo("Confirm"));

        }

        [Test]
        public void Create_New_Not_Creating_User()
        {
            //Arrange
            var user = GetInvalidUser();
            var service = new TestUserService();
            service.ShouldThrowValidationException = true;

            //Act
            var controller = new UserController(service);
            var result = controller.Create(user);

            //Assert
            Assert.That(controller.ModelState.IsValid, Is.False);
            Assert.That(result, Is.Not.Null);
        }

        private static User GetValidUser()
        {
            return new User {FirstName = "tester", LastName = "tester", Login = "tester", Password = "tester"};
        }

        private static User GetInvalidUser()
        {
            return new User { FirstName = "tester", Login = "tester", Password = "tester" };
        }
    }
}
