public async Task <int> CreateLoginAttempts(LoginAttemptsModel model)
        {
            try
            {
                if (model.IpAddress.Length <= 50)
                {
                    var storedProcedure = "dbo.LoginAttempts_Create";
                    DynamicParameters p = new DynamicParameters();

                    p.Add("IpAddress", model.IpAddress);
                    p.Add("LoginCounter", model.LoginCounter);
                    p.Add("SuspensionEndTime", model.SuspensionEndTime);
                    p.Add("Id", DbType.Int32, direction: ParameterDirection.Output);

                    return(await _dataGateway.Execute(storedProcedure, p, _connectionString.SqlConnectionString));
                }
                else
                {
                    return(0);
                }
            }
            catch (SqlCustomException e)
            {
                throw new SqlCustomException(e.Message, e.InnerException);
            }
        }
Exemple #2
0
        public async Task CreateLoginAttempts_ExecutionTimeLessThan400Milliseconds
            (int loginAttemptId, string ipAddress, int loginCounter, string suspensionEndTime, long expectedMaxExecutionTime)
        {
            // Arrange
            ILoginAttemptsRepository loginAttemptsRepository = new LoginAttemptsRepository
                                                                   (new SQLServerGateway(), new ConnectionStringData());

            LoginAttemptsModel loginAttemptsModel = new LoginAttemptsModel();

            loginAttemptsModel.Id                = loginAttemptId;
            loginAttemptsModel.IpAddress         = ipAddress;
            loginAttemptsModel.LoginCounter      = loginCounter;
            loginAttemptsModel.SuspensionEndTime = DateTimeOffset.Parse(suspensionEndTime);

            // Act
            var timer = Stopwatch.StartNew();
            await loginAttemptsRepository.CreateLoginAttempts(loginAttemptsModel);

            timer.Stop();

            var actualExecutionTime = timer.ElapsedMilliseconds;

            Debug.WriteLine("Actual Execution Time: " + actualExecutionTime);

            // Assert
            Assert.IsTrue(actualExecutionTime <= expectedMaxExecutionTime);
        }
Exemple #3
0
        public async Task CreateLoginAttempts_IpAddressDoesntExist_DataIsAccurate
            (int expectedId, string expectedIpAddress, int expectedLoginCounter, string expectedSuspensionEndTime)
        {
            // Arrange
            ILoginAttemptsRepository loginAttemptsRepository = new LoginAttemptsRepository
                                                                   (new SQLServerGateway(), new ConnectionStringData());

            LoginAttemptsModel loginAttemptsModel = new LoginAttemptsModel();

            loginAttemptsModel.Id                = expectedId;
            loginAttemptsModel.IpAddress         = expectedIpAddress;
            loginAttemptsModel.LoginCounter      = expectedLoginCounter;
            loginAttemptsModel.SuspensionEndTime = DateTimeOffset.Parse(expectedSuspensionEndTime);

            // Act
            await loginAttemptsRepository.CreateLoginAttempts(loginAttemptsModel);

            var actualLoginAttempt = await loginAttemptsRepository.GetLoginAttemptsById(expectedId);

            // Assert
            Assert.IsTrue
            (
                actualLoginAttempt.Id == expectedId &&
                actualLoginAttempt.IpAddress == expectedIpAddress &&
                actualLoginAttempt.LoginCounter == expectedLoginCounter &&
                actualLoginAttempt.SuspensionEndTime == DateTimeOffset.Parse(expectedSuspensionEndTime)
            );
        }
        public async Task GetLoginAttemptsByIpAddress_IpAddressNotFoundFound_ReturnNull(string ipAddress)
        {
            // Arrange
            // Setting up each dependency of LoginAttemptsService as a Mock
            Mock <ILoginAttemptsRepository> mockLoginAttemptsRepository = new Mock <ILoginAttemptsRepository>();

            LoginAttemptsModel loginAttemptsModel = null;

            mockLoginAttemptsRepository.Setup(x => x.GetLoginAttemptsByIpAddress(ipAddress)).Returns
                (Task.FromResult(loginAttemptsModel));

            ILoginAttemptsService loginAttemptsService = new LoginAttemptsService(mockLoginAttemptsRepository.Object);

            // Act
            var actualResult = await loginAttemptsService.GetLoginAttemptsByIpAddress(ipAddress);

            // Assert
            Assert.IsNull(actualResult);
        }
Exemple #5
0
        public async Task Init()
        {
            await TestCleaner.CleanDatabase();

            var numTestRows = 10;

            IDataGateway             dataGateway             = new SQLServerGateway();
            IConnectionStringData    connectionString        = new ConnectionStringData();
            ILoginAttemptsRepository loginAttemptsRepository = new LoginAttemptsRepository(dataGateway, connectionString);

            for (int i = 1; i <= numTestRows; ++i)
            {
                LoginAttemptsModel loginAttemptsModel = new LoginAttemptsModel();
                loginAttemptsModel.Id                = i;
                loginAttemptsModel.IpAddress         = "127.0.0." + i;
                loginAttemptsModel.LoginCounter      = i;
                loginAttemptsModel.SuspensionEndTime = DateTimeOffset.Parse("3/28/2007 7:13:50 PM +00:00");

                await loginAttemptsRepository.CreateLoginAttempts(loginAttemptsModel);
            }
        }
        public async Task GetLoginAttemptsByIpAddress_IpAddressFound_ReturnBusinessLoginAttemptsModel(int id, string ipAddress,
                                                                                                      int loginCounter, string suspensionEndTime)
        {
            // Arrange
            // Setting up each dependency of LoginAttemptsService as a Mock
            Mock <ILoginAttemptsRepository> mockLoginAttemptsRepository = new Mock <ILoginAttemptsRepository>();

            var loginAttemptsModel = new LoginAttemptsModel();

            loginAttemptsModel.Id                = id;
            loginAttemptsModel.IpAddress         = ipAddress;
            loginAttemptsModel.LoginCounter      = loginCounter;
            loginAttemptsModel.SuspensionEndTime = DateTimeOffset.Parse(suspensionEndTime);

            var expectedResult = new BusinessLoginAttemptsModel();

            expectedResult.Id                = id;
            expectedResult.IpAddress         = ipAddress;
            expectedResult.LoginCounter      = loginCounter;
            expectedResult.SuspensionEndTime = DateTimeOffset.Parse(suspensionEndTime);

            mockLoginAttemptsRepository.Setup(x => x.GetLoginAttemptsByIpAddress(ipAddress)).Returns
                (Task.FromResult(loginAttemptsModel));

            ILoginAttemptsService loginAttemptsService = new LoginAttemptsService(mockLoginAttemptsRepository.Object);

            // Act
            var actualResult = await loginAttemptsService.GetLoginAttemptsByIpAddress(ipAddress);

            // Assert
            Assert.IsTrue
            (
                actualResult.Id == expectedResult.Id &&
                actualResult.IpAddress == expectedResult.IpAddress &&
                actualResult.LoginCounter == expectedResult.LoginCounter &&
                actualResult.SuspensionEndTime == expectedResult.SuspensionEndTime
            );
        }
Exemple #7
0
        public async Task <bool> AddIpAddress(string ipAddress, int loginCounter)
        {
            try
            {
                LoginAttemptsModel loginAttemptsModel = new LoginAttemptsModel();

                loginAttemptsModel.IpAddress         = ipAddress;
                loginAttemptsModel.LoginCounter      = loginCounter;
                loginAttemptsModel.SuspensionEndTime = DateTimeOffset.UtcNow;

                var changesMade = await _loginAttemptsRepository.CreateLoginAttempts(loginAttemptsModel);

                if (changesMade == 0)
                {
                    return(false);
                }

                return(true);
            }
            catch (SqlCustomException e)
            {
                throw new SqlCustomException(e.Message, e.InnerException);
            }
        }