Merge pull request #1845 from j3ko/master

add interac e-transfer question/answer validation
This commit is contained in:
Manfred Karrer 2018-10-30 13:32:05 -05:00 committed by GitHub
commit 9888f93bfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 427 additions and 7 deletions

View File

@ -2406,6 +2406,10 @@ validation.iban.checkSumInvalid=IBAN checksum is invalid
validation.iban.invalidLength=Number must have length 15 to 34 chars.
validation.interacETransfer.invalidAreaCode=Non-Canadian area code
validation.interacETransfer.invalidPhone=Invalid phone number format and not an email address
validation.interacETransfer.invalidQuestion=Must contain only letters, numbers, spaces and/or the symbols ' _ , . ? -
validation.interacETransfer.invalidAnswer=Must be one word and contain only letters, numbers, and/or the symbol -
validation.inputTooLarge=Input must not be larger than {0}
validation.inputTooSmall=Input has to be larger than {0}
validation.amountBelowDust=The amount below the dust limit of {0} is not allowed.
validation.length=Length must be between {0} and {1}
validation.pattern=Input must be of format: {0}

View File

@ -86,14 +86,14 @@ public class InteracETransferForm extends PaymentMethodForm {
});
InputTextField questionInputTextField = FormBuilder.addLabelInputTextField(gridPane, ++gridRow, Res.get("payment.secret")).second;
questionInputTextField.setValidator(inputValidator);
questionInputTextField.setValidator(interacETransferValidator.questionValidator);
questionInputTextField.textProperty().addListener((ov, oldValue, newValue) -> {
interacETransferAccount.setQuestion(newValue);
updateFromInputs();
});
InputTextField answerInputTextField = FormBuilder.addLabelInputTextField(gridPane, ++gridRow, Res.get("payment.answer")).second;
answerInputTextField.setValidator(inputValidator);
answerInputTextField.setValidator(interacETransferValidator.answerValidator);
answerInputTextField.textProperty().addListener((ov, oldValue, newValue) -> {
interacETransferAccount.setAnswer(newValue);
updateFromInputs();
@ -143,8 +143,8 @@ public class InteracETransferForm extends PaymentMethodForm {
allInputsValid.set(isAccountNameValid()
&& interacETransferValidator.validate(interacETransferAccount.getEmail()).isValid
&& inputValidator.validate(interacETransferAccount.getHolderName()).isValid
&& inputValidator.validate(interacETransferAccount.getQuestion()).isValid
&& inputValidator.validate(interacETransferAccount.getAnswer()).isValid
&& interacETransferValidator.questionValidator.validate(interacETransferAccount.getQuestion()).isValid
&& interacETransferValidator.answerValidator.validate(interacETransferAccount.getAnswer()).isValid
&& interacETransferAccount.getTradeCurrencies().size() > 0);
}
}

View File

@ -0,0 +1,35 @@
package bisq.desktop.util.validation;
import bisq.core.locale.Res;
import bisq.core.util.validation.InputValidator;
import javax.inject.Inject;
public class InteracETransferAnswerValidator extends InputValidator {
private LengthValidator lengthValidator;
private RegexValidator regexValidator;
@Inject
public InteracETransferAnswerValidator(LengthValidator lengthValidator, RegexValidator regexValidator) {
lengthValidator.setMinLength(3);
lengthValidator.setMaxLength(25);
this.lengthValidator = lengthValidator;
regexValidator.setPattern("[A-Za-z0-9\\-]+");
regexValidator.setErrorMessage(Res.get("validation.interacETransfer.invalidAnswer"));
this.regexValidator = regexValidator;
}
@Override
public ValidationResult validate(String input) {
ValidationResult result = super.validate(input);
if (result.isValid)
result = lengthValidator.validate(input);
if (result.isValid)
result = regexValidator.validate(input);
return result;
}
}

View File

@ -0,0 +1,35 @@
package bisq.desktop.util.validation;
import bisq.core.locale.Res;
import bisq.core.util.validation.InputValidator;
import javax.inject.Inject;
public class InteracETransferQuestionValidator extends InputValidator {
private LengthValidator lengthValidator;
private RegexValidator regexValidator;
@Inject
public InteracETransferQuestionValidator(LengthValidator lengthValidator, RegexValidator regexValidator) {
lengthValidator.setMinLength(1);
lengthValidator.setMaxLength(40);
this.lengthValidator = lengthValidator;
regexValidator.setPattern("[A-Za-z0-9\\-\\_\\'\\,\\.\\? ]+");
regexValidator.setErrorMessage(Res.get("validation.interacETransfer.invalidQuestion"));
this.regexValidator = regexValidator;
}
@Override
public ValidationResult validate(String input) {
ValidationResult result = super.validate(input);
if (result.isValid)
result = lengthValidator.validate(input);
if (result.isValid)
result = regexValidator.validate(input);
return result;
}
}

View File

@ -20,6 +20,8 @@ package bisq.desktop.util.validation;
import bisq.core.locale.Res;
import bisq.core.util.validation.InputValidator;
import javax.inject.Inject;
import org.apache.commons.lang3.StringUtils;
/*
@ -42,10 +44,17 @@ public final class InteracETransferValidator extends InputValidator {
// Public methods
///////////////////////////////////////////////////////////////////////////////////////////
public InteracETransferValidator() {
emailValidator = new EmailValidator();
@Inject
public InteracETransferValidator(EmailValidator emailValidator, InteracETransferQuestionValidator questionValidator, InteracETransferAnswerValidator answerValidator) {
this.emailValidator = emailValidator;
this.questionValidator = questionValidator;
this.answerValidator = answerValidator;
}
public final InputValidator answerValidator;
public final InputValidator questionValidator;
@Override
public ValidationResult validate(String input) {
ValidationResult result = validateIfNotEmpty(input);

View File

@ -0,0 +1,37 @@
package bisq.desktop.util.validation;
import bisq.core.locale.Res;
import bisq.core.util.validation.InputValidator;
public class LengthValidator extends InputValidator {
private int minLength;
private int maxLength;
public LengthValidator() {
this(0, Integer.MAX_VALUE);
}
public LengthValidator(int min, int max) {
this.minLength = min;
this.maxLength = max;
}
@Override
public ValidationResult validate(String input) {
ValidationResult result = new ValidationResult(true);
int length = (input == null) ? 0 : input.length();
if (length < this.minLength || length > this.maxLength)
result = new ValidationResult(false, String.format(Res.get("validation.length", this.minLength, this.maxLength)));
return result;
}
public void setMinLength(int minLength) {
this.minLength = minLength;
}
public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}
}

View File

@ -0,0 +1,36 @@
package bisq.desktop.util.validation;
import bisq.core.locale.Res;
import bisq.core.util.validation.InputValidator;
public class RegexValidator extends InputValidator {
private String pattern;
private String errorMessage;
@Override
public ValidationResult validate(String input) {
ValidationResult result = new ValidationResult(true);
String message = (this.errorMessage == null) ? Res.get("validation.pattern", this.pattern) : this.errorMessage;
String testStr = input == null ? "" : input;
if (this.pattern == null)
return result;
if (!testStr.matches(this.pattern))
result = new ValidationResult(false, message);
return result;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}

View File

@ -0,0 +1,59 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.desktop.util.validation;
import bisq.core.app.BisqEnvironment;
import bisq.core.btc.BaseCurrencyNetwork;
import bisq.core.locale.CurrencyUtil;
import bisq.core.locale.Res;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class InteracETransferAnswerValidatorTest {
@Before
public void setup() {
final BaseCurrencyNetwork baseCurrencyNetwork = BisqEnvironment.getBaseCurrencyNetwork();
final String currencyCode = baseCurrencyNetwork.getCurrencyCode();
Res.setBaseCurrencyCode(currencyCode);
Res.setBaseCurrencyName(baseCurrencyNetwork.getCurrencyName());
CurrencyUtil.setBaseCurrencyCode(currencyCode);
}
@Test
public void validate() throws Exception {
InteracETransferAnswerValidator validator = new InteracETransferAnswerValidator(new LengthValidator(), new RegexValidator());
assertTrue(validator.validate("abcdefghijklmnopqrstuvwxy").isValid);
assertTrue(validator.validate("ABCDEFGHIJKLMNOPQRSTUVWXY").isValid);
assertTrue(validator.validate("1234567890").isValid);
assertTrue(validator.validate("zZ-").isValid);
assertFalse(validator.validate(null).isValid); // null
assertFalse(validator.validate("").isValid); // empty
assertFalse(validator.validate("two words").isValid); // two words
assertFalse(validator.validate("ab").isValid); // too short
assertFalse(validator.validate("abcdefghijklmnopqrstuvwxyz").isValid); // too long
assertFalse(validator.validate("abc !@#").isValid); // invalid characters
}
}

View File

@ -0,0 +1,58 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.desktop.util.validation;
import bisq.core.app.BisqEnvironment;
import bisq.core.btc.BaseCurrencyNetwork;
import bisq.core.locale.CurrencyUtil;
import bisq.core.locale.Res;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class InteracETransferQuestionValidatorTest {
@Before
public void setup() {
final BaseCurrencyNetwork baseCurrencyNetwork = BisqEnvironment.getBaseCurrencyNetwork();
final String currencyCode = baseCurrencyNetwork.getCurrencyCode();
Res.setBaseCurrencyCode(currencyCode);
Res.setBaseCurrencyName(baseCurrencyNetwork.getCurrencyName());
CurrencyUtil.setBaseCurrencyCode(currencyCode);
}
@Test
public void validate() throws Exception {
InteracETransferQuestionValidator validator = new InteracETransferQuestionValidator(new LengthValidator(), new RegexValidator());
assertTrue(validator.validate("abcdefghijklmnopqrstuvwxyz").isValid);
assertTrue(validator.validate("ABCDEFGHIJKLMNOPQRSTUVWXYZ").isValid);
assertTrue(validator.validate("1234567890").isValid);
assertTrue(validator.validate("' _ , . ? -").isValid);
assertTrue(validator.validate("what is 2-1?").isValid);
assertFalse(validator.validate(null).isValid); // null
assertFalse(validator.validate("").isValid); // empty
assertFalse(validator.validate("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ").isValid); // too long
assertFalse(validator.validate("abc !@#").isValid); // invalid characters
}
}

View File

@ -41,7 +41,11 @@ public class InteracETransferValidatorTest {
@Test
public void validate() throws Exception {
InteracETransferValidator validator = new InteracETransferValidator();
InteracETransferValidator validator = new InteracETransferValidator(
new EmailValidator(),
new InteracETransferQuestionValidator(new LengthValidator(), new RegexValidator()),
new InteracETransferAnswerValidator(new LengthValidator(), new RegexValidator())
);
assertTrue(validator.validate("name@domain.tld").isValid);
assertTrue(validator.validate("n1.n2@c.dd").isValid);

View File

@ -0,0 +1,72 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.desktop.util.validation;
import bisq.core.app.BisqEnvironment;
import bisq.core.btc.BaseCurrencyNetwork;
import bisq.core.locale.CurrencyUtil;
import bisq.core.locale.Res;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class LengthValidatorTest {
@Before
public void setup() {
final BaseCurrencyNetwork baseCurrencyNetwork = BisqEnvironment.getBaseCurrencyNetwork();
final String currencyCode = baseCurrencyNetwork.getCurrencyCode();
Res.setBaseCurrencyCode(currencyCode);
Res.setBaseCurrencyName(baseCurrencyNetwork.getCurrencyName());
CurrencyUtil.setBaseCurrencyCode(currencyCode);
}
@Test
public void validate() throws Exception {
LengthValidator validator = new LengthValidator();
assertTrue(validator.validate("").isValid);
assertTrue(validator.validate(null).isValid);
assertTrue(validator.validate("123456789").isValid);
validator.setMinLength(2);
validator.setMaxLength(5);
assertTrue(validator.validate("12").isValid);
assertTrue(validator.validate("12345").isValid);
assertFalse(validator.validate("1").isValid); // too short
assertFalse(validator.validate("").isValid); // too short
assertFalse(validator.validate(null).isValid); // too short
assertFalse(validator.validate("123456789").isValid); // too long
LengthValidator validator2 = new LengthValidator(2, 5);
assertTrue(validator2.validate("12").isValid);
assertTrue(validator2.validate("12345").isValid);
assertFalse(validator2.validate("1").isValid); // too short
assertFalse(validator2.validate("").isValid); // too short
assertFalse(validator2.validate(null).isValid); // too short
assertFalse(validator2.validate("123456789").isValid); // too long
}
}

View File

@ -0,0 +1,71 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.desktop.util.validation;
import bisq.core.app.BisqEnvironment;
import bisq.core.btc.BaseCurrencyNetwork;
import bisq.core.locale.CurrencyUtil;
import bisq.core.locale.Res;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class RegexValidatorTest {
@Before
public void setup() {
final BaseCurrencyNetwork baseCurrencyNetwork = BisqEnvironment.getBaseCurrencyNetwork();
final String currencyCode = baseCurrencyNetwork.getCurrencyCode();
Res.setBaseCurrencyCode(currencyCode);
Res.setBaseCurrencyName(baseCurrencyNetwork.getCurrencyName());
CurrencyUtil.setBaseCurrencyCode(currencyCode);
}
@Test
public void validate() throws Exception {
RegexValidator validator = new RegexValidator();
assertTrue(validator.validate("").isValid);
assertTrue(validator.validate(null).isValid);
assertTrue(validator.validate("123456789").isValid);
validator.setPattern("[a-z]*");
assertTrue(validator.validate("abcdefghijklmnopqrstuvwxyz").isValid);
assertTrue(validator.validate("").isValid);
assertTrue(validator.validate(null).isValid);
assertFalse(validator.validate("123").isValid); // invalid
assertFalse(validator.validate("ABC").isValid); // invalid
validator.setPattern("[a-z]+");
assertTrue(validator.validate("abcdefghijklmnopqrstuvwxyz").isValid);
assertFalse(validator.validate("123").isValid); // invalid
assertFalse(validator.validate("ABC").isValid); // invalid
assertFalse(validator.validate("").isValid); // invalid
assertFalse(validator.validate(null).isValid); // invalid
}
}