public DataProtectionVm Build()
        {
            var model = new DataProtectionVm();

            PopulateDateComponents(model);

            return(model);
        }
        public void DateOfBirth_WhenSetViaComponents_WithInvalidDayMonthYear_Then_ReturnsNull(int?day, string month,
                                                                                              int?year)
        {
            DataProtectionVm model = new DataProtectionVm()
            {
                Day   = day,
                Month = month,
                Year  = year
            };

            Assert.AreEqual(null, model.DateOfBirth);
        }
        public DataProtectionDto Build(DataProtectionVm model)
        {
            // Before we reach this point, the incoming VM should have been validated
            Debug.Assert(model.DateOfBirth.HasValue);

            var dto = new DataProtectionDto
            {
                LowellReference = model.LowellReference,
                Postcode        = model.Postcode.ToUpper(),
                DateOfBirth     = model.DateOfBirth.Value
            };

            return(dto);
        }
        public async Task <ResultDto> CheckDataProtection(DataProtectionVm dataProtectionVm)
        {
            // VM will have been validated before we hit the service
            Debug.Assert(dataProtectionVm.DateOfBirth != null, "dataProtectionVm.DateOfBirth != null");

            DataProtectionDto dto = new DataProtectionDto()
            {
                LowellReference = dataProtectionVm.LowellReference,
                DateOfBirth     = dataProtectionVm.DateOfBirth.Value,
                Postcode        = dataProtectionVm.Postcode
            };

            return(await _checkDataProtectionAnonymousProcess.CheckDataProtection(dto));
        }
        public void DateOfBirth_WhenSetViaComponents_WithValidDayMonthYear_Then_ReturnsDateTime(int?day, string month,
                                                                                                int?year, int expectedMonthNumber)
        {
            DataProtectionVm model = new DataProtectionVm()
            {
                Day   = day,
                Month = month,
                Year  = year
            };

            // To prevent warnings
            Assert.IsNotNull(day);
            Assert.IsNotNull(year);

            DateTime expected = new DateTime(year.Value, expectedMonthNumber, day.Value);

            Assert.AreEqual(expected, model.DateOfBirth);
        }
        public void Build_WhenCallWithValidModel_ConvertsIntoDto()
        {
            DataProtectionVm vm = new DataProtectionVm()
            {
                LowellReference = "123abc",
                Day             = 17,
                Month           = "September",
                Year            = 2018,
                Postcode        = "S4 7UL"
            };

            BuildDataProtectionDtoProcess process = new BuildDataProtectionDtoProcess(_mockLogger.Object);
            DataProtectionDto             dto     = process.Build(vm);

            Assert.AreEqual("123abc", dto.LowellReference);
            Assert.AreEqual(new DateTime(2018, 9, 17), dto.DateOfBirth);
            Assert.AreEqual("S4 7UL", dto.Postcode);
        }
        public void Initialise()
        {
            _validator = new DataProtectionVmValidator();

            // Contains sufficient setup for a model to be valid
            // Tests will change this to be invalid and create error states
            _model = new DataProtectionVm
            {
                // Below must be set to avoid other validation errors
                LowellReference = "123abc",

                Day   = 15,
                Month = "May",
                Year  = 1993,

                Postcode = "s4 7ul"
            };

            // Ensure valid
            Assert.IsTrue(_validator.Validate(_model).IsValid);
        }
        public void Build_WhenCallWithValidModel_ConvertsIntoDto_AndMakesPostCodeUpperCase()
        {
            // Converts postcode to upper case = to ensure correct for CaseFlow
            // Not good enough to just do on client as per user requirements

            DataProtectionVm vm = new DataProtectionVm()
            {
                LowellReference = "123abc",
                Day             = 17,
                Month           = "September",
                Year            = 2018,
                Postcode        = "s4 7ul"
            };

            BuildDataProtectionDtoProcess process = new BuildDataProtectionDtoProcess(_mockLogger.Object);
            DataProtectionDto             dto     = process.Build(vm);

            Assert.AreEqual("123abc", dto.LowellReference);
            Assert.AreEqual(new DateTime(2018, 9, 17), dto.DateOfBirth);
            Assert.AreEqual("S4 7UL", dto.Postcode);
        }
        public async Task <IActionResult> Index(DataProtectionVm model)
        {
            if (!ModelState.IsValid)
            {
                _buildDataProtectionVm.PopulateDateComponents(model);
                model.NotificationMessage = ValidationMessages.DPAFailure;
                AddErrors(ValidationMessages.DPAFailure);
                _gtmService.RaiseDPACheckEvent(model, LoggedInUserId, "Invalid account details");
                await _webActivityService.LogDPACheckFail(model.LowellReference, LoggedInUserId);

                return(View(model));
            }

            var resultDto = await _dataProtectionService.CheckDataProtection(model);

            if (resultDto.IsSuccessful)
            {
                var lowellReferenceSurrogateKey =
                    ApplicationSessionState.AddLowellReferenceSurrogateKey(model.LowellReference);

                ApplicationSessionState.SaveHasPassedDataProtection();

                _gtmService.RaiseDPACheckEvent(model, LoggedInUserId, null);
                await _webActivityService.LogDPACheckSuccess(model.LowellReference, LoggedInUserId);

                TempData["GTMEvents"] = JsonConvert.SerializeObject(model.GtmEvents);

                return(RedirectToAction("Index", "PaymentOptions", new { id = lowellReferenceSurrogateKey }));
            }

            model.NotificationMessage = resultDto.MessageForUser;
            AddErrors(ValidationMessages.DPAFailure);
            _buildDataProtectionVm.PopulateDateComponents(model);
            _gtmService.RaiseDPACheckEvent(model, LoggedInUserId, "Incorrect account details");
            await _webActivityService.LogDPACheckFail(model.LowellReference, LoggedInUserId);

            return(View(model));
        }
        public async Task <IActionResult> Index(DataProtectionVm model)
        {
            if (!ModelState.IsValid)
            {
                // List of days, months, years will not have been posted back, therefore repopulate
                _buildDataProtectionVm.PopulateDateComponents(model);
                _gtmService.RaiseRegistrationEvent_AccountDetailsEntered(model, "Invalid account details");

                return(View(model));
            }

            var dataProtectionDto    = _mapper.Map <DataProtectionVm, DataProtectionDto>(model);
            var dataProtectionResult = await _registerService.CheckDataProtection(dataProtectionDto);

            if (!dataProtectionResult.IsSuccessful)
            {
                AddErrors(dataProtectionResult.MessageForUser);

                model.NotificationMessage = dataProtectionResult.MessageForUser;
                _gtmService.RaiseRegistrationEvent_AccountDetailsEntered(model, "Incorrect account details");
                _buildDataProtectionVm.PopulateDateComponents(model);

                return(View(model));
            }

            var webRegisteredDto      = _mapper.Map <DataProtectionVm, WebRegisteredDto>(model);
            var isWebRegisteredResult = await _registerService.CheckIsWebRegistered(webRegisteredDto);

            if (!isWebRegisteredResult.IsSuccessful)
            {
                AddErrors(isWebRegisteredResult.MessageForUser);

                model.NotificationMessage = isWebRegisteredResult.MessageForUser;
                _gtmService.RaiseRegistrationEvent_AccountDetailsEntered(model, "Already registered");
                _buildDataProtectionVm.PopulateDateComponents(model);

                return(View(model));
            }

            var registrationPending =
                await _registerService.IsPendingRegistration(model.LowellReference);

            if (registrationPending)
            {
                var error = "Account details provided is already registered. Please confirm your email account and login.";

                AddErrors(error);

                model.NotificationMessage = error;

                ModelState.AddModelError(string.Empty,
                                         "Account details provided is already registered. Please confirm your email account and login.");

                return(View(model));
            }

            ApplicationSessionState.SaveHasPassedDataProtection();

            var registerVm = new RegisterVm {
                LowellReference = model.LowellReference
            };

            _gtmService.RaiseRegistrationEvent_AccountDetailsEntered(registerVm, null);

            return(View("CompleteRegistration", registerVm));
        }
 public void PopulateDateComponents(DataProtectionVm existingVm)
 {
     existingVm.Days   = _getDaysForDoB.Build();
     existingVm.Months = _getMonthsForDoB.Build();
     existingVm.Years  = _getYearsForDoB.Build(1850, DateTime.Now.Year);
 }