public async Task ShouldReturnFalseIfEntryIsInvalid(string challengeElement1, string challengeElement2, string balance) { _mockAccountRepository.Setup(x => x.Get(It.IsAny <string>(), AccountFieldsSelection.ChallengePayeSchemes)) .Returns(Task.FromResult(new Account())); _mockChallengeRepository.Setup(x => x.CheckData(It.IsAny <Account>(), It.IsAny <ChallengePermissionQuery>())) .Returns(Task.FromResult(true)); //Arrange var challengeEntry = new ChallengeEntry { Id = "qwerty", Balance = balance, Url = string.Empty, FirstCharacterPosition = "1", SecondCharacterPosition = "2", Challenge1 = challengeElement1, Challenge2 = challengeElement2 }; var message = new ChallengePermissionQuery(challengeEntry); //Act var result = await _sut.Handle(message); //Assert result.IsValid.Should().Be(false); }
public async Task ItShouldReturnAnInvalidResponseWhenThereIsAMatchThatHasInvalidData() { var message = new ChallengePermissionQuery { Id = "123", Balance = "£1000", ChallengeElement1 = "1", ChallengeElement2 = "2" }; var account = new Core.Models.Account { HashedAccountId = "ASDAS", AccountId = 123 }; _accountRepository .Setup(x => x.Get(message.Id, AccountFieldsSelection.PayeSchemes)) .ReturnsAsync(account); _challengeRepository.Setup(x => x.CheckData(account, message)).ReturnsAsync(false); var actual = await _unit.Handle(message); _challengeRepository.Verify(x => x.CheckData(account, message)); Assert.IsNotNull(actual); Assert.AreEqual(false, actual.IsValid); }
public async Task <ChallengePermissionResponse> Handle(ChallengePermissionQuery message) { var challenegModel = await Get(message.Id); int balance; var isValidInput = !(string.IsNullOrEmpty(message.Balance) || string.IsNullOrEmpty(message.ChallengeElement1) || string.IsNullOrEmpty(message.ChallengeElement2) || !int.TryParse(message.Balance.Split('.')[0].Replace("£", string.Empty), out balance) || message.ChallengeElement1.Length != 1 || message.ChallengeElement2.Length != 1 ); var response = new ChallengePermissionResponse { Id = message.Id, Url = message.Url, IsValid = false }; if (challenegModel.StatusCode == SearchResponseCodes.Success) { if (isValidInput) { response.IsValid = await _challengeRepository.CheckData(challenegModel.Account, message); } response.Characters = challenegModel.Characters; } return(response); }
public async Task ItShouldReturnAnInvalidResponseIfTheQueryIsNotValid(string balance, string element1, string element2) { var message = new ChallengePermissionQuery { Balance = balance, ChallengeElement1 = element1, ChallengeElement2 = element2 }; var actual = await _unit.Handle(message); Assert.IsNotNull(actual); Assert.AreEqual(false, actual.IsValid); }
public async Task <bool> CheckData(Core.Models.Account record, ChallengePermissionQuery message) { var balance = await _accountRepository.GetAccountBalance(message.Id); var validPayeSchemesData = CheckPayeSchemesData(record.PayeSchemes, message); decimal messageBalance; if (!decimal.TryParse(message.Balance.Replace("£", string.Empty), out messageBalance)) { return(false); } return(Math.Truncate(balance) == Math.Truncate(Convert.ToDecimal(messageBalance)) && validPayeSchemesData); }
private bool CheckPayeSchemesData(IEnumerable <PayeSchemeModel> recordPayeSchemes, ChallengePermissionQuery message) { var challengeInput = new List <string> { message.ChallengeElement1.ToLower(), message.ChallengeElement2.ToLower() }; var list = recordPayeSchemes.Select(x => x.PayeRefWithOutSlash); var index1 = message.FirstCharacterPosition; var index2 = message.SecondCharacterPosition; return(list.Any(x => x[index1].ToString().ToLower() == challengeInput[0] && x[index2].ToString().ToLower() == challengeInput[1])); }
public async Task ItShouldReturnFalseWhenCheckDataHasInvalidBalance() { var account = new Core.Models.Account { Transactions = new List <TransactionViewModel> { new TransactionViewModel { Balance = 300m }, new TransactionViewModel { Balance = 700m } }, PayeSchemes = new List <PayeSchemeModel> { new PayeSchemeModel { AddedDate = DateTime.Today.AddMonths(-12), Ref = "123/456789" }, new PayeSchemeModel { AddedDate = DateTime.Today.AddMonths(-12), Ref = "124/AA6789" } } }; var challengePermissionQuery = new ChallengePermissionQuery { Id = "123", Balance = "£Z000", ChallengeElement1 = "1", ChallengeElement2 = "A", FirstCharacterPosition = 0, SecondCharacterPosition = 4 }; var balance = 1000m; _accountRepository.Setup(x => x.GetAccountBalance(challengePermissionQuery.Id)) .ReturnsAsync(balance); var actual = await _unit.CheckData(account, challengePermissionQuery); Assert.IsFalse(actual); }
public async Task ItShouldReturnAnInvalidResponseIfAValidQueryHasNoMatch() { var message = new ChallengePermissionQuery { Balance = "£1000", ChallengeElement1 = "1", ChallengeElement2 = "2" }; _accountRepository .Setup(x => x.Get(message.Id, AccountFieldsSelection.PayeSchemes)) .ReturnsAsync(null as Core.Models.Account); var actual = await _unit.Handle(message); Assert.IsNotNull(actual); Assert.AreEqual(false, actual.IsValid); }
public async Task ItShouldReturnChallengeValidationJsonResultWhenTheChallengeEntryIsValid() { var challengeEntry = new ChallengeEntry { Id = "123", Balance = "£1000", Challenge1 = "1", Challenge2 = "A", FirstCharacterPosition = 1, SecondCharacterPosition = 4, Url = "https://tempuri.org/challenge/me/to/a/deul/any/time" }; var query = new ChallengePermissionQuery { Id = "123", Balance = "£1000", ChallengeElement1 = "1", ChallengeElement2 = "B", FirstCharacterPosition = 1, SecondCharacterPosition = 4, Url = "https://tempuri.org/challenge/me/to/a/deul/any/time" }; var response = new ChallengePermissionResponse { Id = challengeEntry.Id, Url = challengeEntry.Url, IsValid = true }; MockChallengeHandler.Setup(x => x.Handle(It.IsAny <ChallengePermissionQuery>())) .ReturnsAsync(response); var actual = await Unit.Index(challengeEntry.Id, challengeEntry); Assert.IsNotNull(actual); Assert.IsInstanceOf <JsonResult>(actual); var result = ((JsonResult)actual).Data as ChallengeValidationResult; Assert.IsNotNull(result); Assert.IsTrue(result.IsValidResponse); }
public async Task ItShouldReturnAViewModelWhenTheChallengeEntryIsInvalid() { var challengeEntry = new ChallengeEntry { Id = "123", Balance = "£1000", Challenge1 = "1", Challenge2 = "A", FirstCharacterPosition = 0, SecondCharacterPosition = 1, Url = "https://tempuri.org/challenge/me/to/a/deul/any/time" }; var query = new ChallengePermissionQuery { Id = "123", Balance = "£1000", ChallengeElement1 = "1", ChallengeElement2 = "A", FirstCharacterPosition = 1, SecondCharacterPosition = 2, Url = "https://tempuri.org/challenge/me/to/a/deul/any/time" }; var response = new ChallengePermissionResponse { Id = challengeEntry.Id, Url = challengeEntry.Url, IsValid = false }; MockChallengeHandler.Setup(x => x.Handle(It.IsAny <ChallengePermissionQuery>())) .ReturnsAsync(response); var actual = await Unit.Index(challengeEntry.Id, challengeEntry); Assert.IsNotNull(actual); Assert.IsInstanceOf <ViewResult>(actual); Assert.IsInstanceOf <ChallengeViewModel>(((ViewResult)actual).Model); Assert.AreEqual(true, ((ChallengeViewModel)((ViewResult)actual).Model).HasError); }