public async Task UpdateUserAsync_PerfectWay_NoPhonesOrAddressInDb()
        {
            //Arrange
            var comparison  = new CompareLogic();
            var mocker      = new AutoMocker();
            var serviceMock = mocker.CreateInstance <UserService>();

            var request        = GetRequestToUpdate(TestCase.PerfectWay);
            var userDb         = GetUserToUpdate(TestCase.PerfectWay);
            var expectedResult = GetResponseByUser(userDb);

            var phones = request.Phones
                         .Select(phone => new Tuple <string, string>(phone.LocalCode, phone.Number))
                         .ToList();

            mocker.GetMock <IUserBus>()
            .Setup(b => b.GetByIdAsync(request.Id ?? 0))
            .ReturnsAsync(userDb)
            .Verifiable();

            var token = new Entities.TokenModel(userDb.Id, userDb.Email, null);

            mocker.GetMock <IUserBus>()
            .Setup(b => b.GetTokenAsync())
            .ReturnsAsync(token)
            .Verifiable();

            mocker.GetMock <IUserBus>()
            .Setup(b => b.ExistsEmailAsync(request.Email, request.Id))
            .ReturnsAsync(false)
            .Verifiable();

            mocker.GetMock <IAddressBus>()
            .Setup(b => b.GetByInfoAsync(request.Address.Street, request.Address.Number, request.Address.Complement, request.Address.District, request.Address.City, request.Address.State, request.Address.PostalCode))
            .ReturnsAsync((Entities.Address)null)
            .Verifiable();

            mocker.GetMock <IPhoneBus>()
            .Setup
            (
                b => b.GetByNumbersAsync
                (
                    It.Is <IList <Tuple <string, string> > >(p => comparison.Compare(p, phones).AreEqual)
                )
            )
            .ReturnsAsync(new List <Entities.Phone>())
            .Verifiable();

            mocker.GetMock <IUserBus>()
            .Setup(b => b.UpdateUserAsync(It.Is <Entities.User>(u => comparison.Compare(u, userDb).AreEqual)))
            .Verifiable();

            //Act
            var result = await serviceMock.UpdateUserAsync(request).ConfigureAwait(false);

            //Assert
            serviceMock.IsSatisfied().Should().BeTrue();
            Assert.True(comparison.Compare(expectedResult, result).AreEqual);
            mocker.Verify();
        }
        public async Task UpdateUserAsync_NotLoggedUser()
        {
            //Arrange
            var comparison  = new CompareLogic();
            var mocker      = new AutoMocker();
            var serviceMock = mocker.CreateInstance <UserService>();

            var request = GetRequestToUpdate(TestCase.EmailExists);
            var userDb  = GetUserToUpdate(TestCase.EmailExists);

            mocker.GetMock <IUserBus>()
            .Setup(b => b.GetByIdAsync(request.Id ?? 0))
            .ReturnsAsync(userDb)
            .Verifiable();

            var token = new Entities.TokenModel(userDb.Id + 1, "*****@*****.**", null);

            mocker.GetMock <IUserBus>()
            .Setup(b => b.GetTokenAsync())
            .ReturnsAsync(token)
            .Verifiable();

            var expectedMessages = new string[] { UserServiceMessages.EditarProprioUsuario };

            //Act
            var response = await serviceMock.UpdateUserAsync(request).ConfigureAwait(false);

            //Assert
            response.Should().BeNull();
            serviceMock.IsSatisfied().Should().BeFalse();
            (serviceMock.GetStatusCode() == HttpStatusCode.Forbidden).Should().BeTrue();
            comparison.Compare(serviceMock.GetMessages(), expectedMessages).AreEqual.Should().BeTrue();
            mocker.Verify();
        }
        public async Task UpdateUserAsync_InvalidCpf()
        {
            //Arrange
            var mocker  = new AutoMocker();
            var service = mocker.CreateInstance <UserService>();

            var request = new UserRequest
            {
                Id    = 8,
                Email = "*****@*****.**",
                Cpf   = "0",

                FirstName  = "Fulano",
                MiddleName = "de Tal",
                LastName   = "da Silva",
            };

            var userDb = GetUserToUpdate(TestCase.EmailExists);

            mocker.GetMock <IUserBus>()
            .Setup(b => b.GetByIdAsync(request.Id ?? 0))
            .ReturnsAsync(userDb)
            .Verifiable();

            var token = new Entities.TokenModel(userDb.Id, userDb.Email, null);

            mocker.GetMock <IUserBus>()
            .Setup(b => b.GetTokenAsync())
            .ReturnsAsync(token)
            .Verifiable();

            mocker.GetMock <IUserBus>()
            .Setup(b => b.ExistsEmailAsync(request.Email, request.Id))
            .ReturnsAsync(false)
            .Verifiable();

            var expectedMessages = new string[] { UserSpecMessages.CpfInvalido };

            //Act
            var result = await service.UpdateUserAsync(request).ConfigureAwait(false);

            //Assert
            result.Should().BeNull();
            service.GetStatusCode().Should().BeEquivalentTo(HttpStatusCode.BadRequest);
            new CompareLogic().Compare(expectedMessages, service.GetMessages()).AreEqual.Should().BeTrue();
        }
        public async Task UpdateUserAsync_InvalidPhone()
        {
            //Arrange
            var comparison  = new CompareLogic();
            var mocker      = new AutoMocker();
            var serviceMock = mocker.CreateInstance <UserService>();

            var request = GetRequestToUpdate(TestCase.InvalidPhone);
            var userDb  = GetUserToUpdate(TestCase.InvalidPhone);

            var tuples           = request.Phones.Select(phone => new Tuple <string, string>(phone.LocalCode, phone.Number)).ToList();
            var expectedMessages = new string[] { PhoneSpecMessages.DddInvalido, PhoneSpecMessages.NumeroInvalido, PhoneSpecMessages.DddInvalido, PhoneSpecMessages.NumeroInvalido };

            mocker.GetMock <IUserBus>()
            .Setup(b => b.GetByIdAsync(request.Id ?? 0))
            .ReturnsAsync(userDb)
            .Verifiable();

            var token = new Entities.TokenModel(userDb.Id, userDb.Email, null);

            mocker.GetMock <IUserBus>()
            .Setup(b => b.GetTokenAsync())
            .ReturnsAsync(token)
            .Verifiable();

            mocker.GetMock <IUserBus>()
            .Setup(b => b.ExistsEmailAsync(request.Email, request.Id))
            .ReturnsAsync(false)
            .Verifiable();

            mocker.GetMock <IPhoneBus>()
            .Setup(b => b.GetByNumbersAsync(It.Is <IList <Tuple <string, string> > >(ts => comparison.Compare(ts, tuples).AreEqual)))
            .ReturnsAsync(new List <Entities.Phone>())
            .Verifiable();

            //Act
            var response = await serviceMock.UpdateUserAsync(request).ConfigureAwait(false);

            //Assert
            response.Should().BeNull();
            serviceMock.IsSatisfied().Should().BeFalse();
            Assert.True(serviceMock.GetStatusCode() == HttpStatusCode.BadRequest);
            Assert.True(comparison.Compare(serviceMock.GetMessages(), expectedMessages).AreEqual);
            mocker.Verify();
        }
        public async Task UpdateUserAsync_InvalidAddress()
        {
            //Arrange
            var comparison  = new CompareLogic();
            var mocker      = new AutoMocker();
            var serviceMock = mocker.CreateInstance <UserService>();

            var request          = GetRequestToUpdate(TestCase.InvalidAddress);
            var userDb           = GetUserToUpdate(TestCase.InvalidAddress);
            var expectedMessages = new string[] { AddressSpecMessages.BairroObrigatorio, AddressSpecMessages.EstadoInvalido };

            mocker.GetMock <IUserBus>()
            .Setup(b => b.GetByIdAsync(request.Id ?? 0))
            .ReturnsAsync(userDb)
            .Verifiable();

            var token = new Entities.TokenModel(userDb.Id, userDb.Email, null);

            mocker.GetMock <IUserBus>()
            .Setup(b => b.GetTokenAsync())
            .ReturnsAsync(token)
            .Verifiable();

            mocker.GetMock <IUserBus>()
            .Setup(b => b.ExistsEmailAsync(request.Email, request.Id))
            .ReturnsAsync(false)
            .Verifiable();

            mocker.GetMock <IAddressBus>()
            .Setup(b => b.GetByInfoAsync(request.Address.Street, request.Address.Number, request.Address.Complement, request.Address.District, request.Address.City, request.Address.State, request.Address.PostalCode))
            .ReturnsAsync((Entities.Address)null)
            .Verifiable();

            //Act
            var response = await serviceMock.UpdateUserAsync(request).ConfigureAwait(false);

            //Assert
            response.Should().BeNull();
            serviceMock.IsSatisfied().Should().BeFalse();
            Assert.True(serviceMock.GetStatusCode() == HttpStatusCode.BadRequest);
            Assert.True(comparison.Compare(serviceMock.GetMessages(), expectedMessages).AreEqual);
            mocker.Verify();
        }
        public async Task UpdateUserAsync_EmailExists()
        {
            //Arrange
            var comparison  = new CompareLogic();
            var mocker      = new AutoMocker();
            var serviceMock = mocker.CreateInstance <UserService>();

            var request          = GetRequestToUpdate(TestCase.EmailExists);
            var userDb           = GetUserToUpdate(TestCase.EmailExists);
            var expectedMessages = new string[] { UserServiceMessages.EmailJaCadastrado };

            mocker.GetMock <IUserBus>()
            .Setup(b => b.GetByIdAsync(request.Id ?? 0))
            .ReturnsAsync(userDb)
            .Verifiable();

            var token = new Entities.TokenModel(userDb.Id, userDb.Email, null);

            mocker.GetMock <IUserBus>()
            .Setup(b => b.GetTokenAsync())
            .ReturnsAsync(token)
            .Verifiable();

            mocker.GetMock <IUserBus>()
            .Setup(b => b.ExistsEmailAsync(request.Email, request.Id))
            .ReturnsAsync(true)
            .Verifiable();

            //Act
            var response = await serviceMock.UpdateUserAsync(request).ConfigureAwait(false);

            //Assert
            response.Should().BeNull();
            serviceMock.IsSatisfied().Should().BeFalse();
            Assert.True(serviceMock.GetStatusCode() == HttpStatusCode.Conflict);
            Assert.True(comparison.Compare(serviceMock.GetMessages(), expectedMessages).AreEqual);
            mocker.Verify();
        }