Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add interac e-transfer question/answer validation #1845

Merged
merged 1 commit into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/src/main/resources/i18n/displayStrings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ public void addFormForAddAccount() {
});

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();
Expand Down Expand Up @@ -143,8 +143,8 @@ public void updateAllInputsValid() {
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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import bisq.core.locale.Res;
import bisq.core.util.validation.InputValidator;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;

/*
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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
}

}
Original file line number Diff line number Diff line change
@@ -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
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ public void setup() {

@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);
Expand Down
Loading