﻿using ParusnikServiceDesk.Parus.API.Areas.IdentityService.Services;
using System;
using System.Threading.Tasks;
using Moq;
using Xunit;

namespace ParusnikServiceDesk.Parus.API.Test
{
    public class SignInManagerTest
    {
        [Fact]
        public void CtorNullCheck()
        {
            Assert.Throws<ArgumentNullException>("customerManager", () => new SignInManager<TestCustomer>(null, null, null));
        }

        [Fact]
        public async Task FailsValidateCredentials()
        {
            var customer = new TestCustomer
            {
                Inn = "1234567890",
                RegistrationPrefix = "PR",
                RegistrationNumber = "0000"
            };
            var customerManager = SetupCustomerManager(customer);
            var logger = MockHelpers.MockLoggerManager<SignInManager<TestCustomer>>();
            var manager = new SignInManager<TestCustomer>(customerManager.Object, new IdentityErrorDescriber(), logger.Object);

            var result = await manager.ValidateCredentialsAsync("12345678900", customer.RegistrationPrefix, customer.RegistrationNumber);

            Assert.False(result.Succeeded);
            customerManager.Verify();
        }

        private static Mock<CustomerManager<TestCustomer>> SetupCustomerManager(TestCustomer customer)
        {
            var manager = MockHelpers.MockCustomerManager<TestCustomer>();
            manager.Setup(m => m.FindByInnAsync(customer.Inn)).ReturnsAsync(customer);
            manager.Setup(m => m.GetInnAsync(customer)).ReturnsAsync(customer.Inn);
            manager.Setup(m => m.GetRegistrationPrefixAsync(customer)).ReturnsAsync(customer.RegistrationPrefix);
            manager.Setup(m => m.GetRegistrationNumberAsync(customer)).ReturnsAsync(customer.RegistrationNumber);
            return manager;
        }
    }
}