Skip to content

Commit

Permalink
Cardholder Name Validation (#107)
Browse files Browse the repository at this point in the history
* Stub out solution for disallowing sensitive information in the cardholder name field.

* Stub out tests for disallowing sensitive information.

* Add regex to check for cardholder name data validity.

* Update CHANGELOG.

* Update CardForm/src/test/java/com/braintreepayments/cardform/view/CardholderNameEditTextTest.java

Co-authored-by: Sarah Koop <skoop@paypal.com>

Co-authored-by: Steven <sshropshire@paypal.com>
Co-authored-by: Sarah Koop <skoop@paypal.com>
  • Loading branch information
3 people authored Apr 5, 2021
1 parent 8feef99 commit ddd6880
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Android Card Form Release Notes

## unreleased
* Validate Cardholder Name field to prevent sensitive data from being input

## 5.1.0
* Bump `compileSdkVersion` and `targetSdkVersion` to API level 30
* Add support for Maestro cards beginning in 5043
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package com.braintreepayments.cardform.view;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputFilter.LengthFilter;
import android.text.InputType;
import android.util.AttributeSet;

import com.braintreepayments.cardform.R;

import java.util.regex.Pattern;

/**
* Input for cardholder name. Validated for presence only.
*/
public class CardholderNameEditText extends ErrorEditText {

private static final Pattern sensitiveDataRegex = Pattern.compile("^[\\d\\s-]+$");

public CardholderNameEditText(Context context) {
super(context);
init();
Expand All @@ -36,7 +41,24 @@ private void init() {

@Override
public boolean isValid() {
return isOptional() || !getText().toString().trim().isEmpty();
if (isOptional()) {
return hasValidCardholderNameText();
} else {
return !isTextEmpty() && hasValidCardholderNameText();
}
}

private boolean isTextEmpty() {
return getText().toString().trim().isEmpty();
}

private boolean hasValidCardholderNameText() {
Editable text = getText();
if (text != null) {
return !sensitiveDataRegex.matcher(text).matches();
}
// empty text does not contain sensitive data
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,46 @@ public void setup() {
}

@Test
public void invalidIfEmpty() {
public void isValid_isRequiredByDefault() {
assertFalse(mView.isOptional());
}

@Test
public void isValid_whenIsRequiredAndTextIsEmpty_returnsFalse() {
assertFalse(mView.isValid());
}

@Test
public void validIfNotEmpty() {
public void isValid_whenIsRequiredAndTextIsNotEmpty_returnsTrue() {
mView.setText("John Doe");
assertTrue(mView.isValid());
}

@Test
public void validIfEmptyAndOptional() {
public void isValid_whenIsOptionalAndTextIsEmpty_returnsTrue() {
mView.setOptional(true);
assertTrue(mView.isValid());
}

@Test
public void isValid_whenIsRequiredAndIsAlphanumericWithSpaces_returnsTrue() {
mView.setText("Jane Doe 123");
assertTrue(mView.isValid());
}

@Test
public void isValid_whenIsRequiredAndIsOnlyNumericWithHyphensAndSpaces_returnsFalse() {
mView.setText("4111-111-1111 1111");
assertFalse(mView.isValid());
}

@Test
public void isValid_whenIsOptionalAndIsOnlyNumericWithHyphensAndSpaces_returnsFalse() {
mView.setOptional(true);
mView.setText("4111-111-1111 1111");
assertFalse(mView.isValid());
}

@Test
public void hasMaxAllowedLength() {
int maxLength = 255;
Expand Down

0 comments on commit ddd6880

Please sign in to comment.