Skip to content

Commit

Permalink
PP-12240: Card length validation matches CARD_RANGE_LENGTH
Browse files Browse the repository at this point in the history
  • Loading branch information
nataliecarey committed Feb 16, 2024
1 parent df29b87 commit 9535031
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import javax.validation.constraints.Size;

import static java.lang.String.format;
import static uk.gov.pay.card.db.CardInformationStore.CARD_RANGE_LENGTH;

public class CardInformationRequest {

/**
* PANs can be between 10 and 19 characters
* @see <a href="https://www.ansi.org/news_publications/news_story?articleid=da7bcb04-0654-4e03-af54-0e55d50b93a8">ANSI IIN</a>
*/
@JsonProperty("cardNumber") @NotEmpty @Size(min = 10, max = 19)
@JsonProperty("cardNumber") @NotEmpty @Size(min = CARD_RANGE_LENGTH, max = 19)
private String cardNumber;

public String getCardNumber() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.dropwizard.testing.junit5.DropwizardAppExtension;
import io.dropwizard.testing.junit5.DropwizardExtensionsSupport;
import io.restassured.response.ValidatableResponse;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.gov.pay.card.app.CardApi;
Expand All @@ -13,6 +14,7 @@
import static io.restassured.RestAssured.given;
import static io.restassured.http.ContentType.JSON;
import static org.hamcrest.core.Is.is;
import static uk.gov.pay.card.db.CardInformationStore.CARD_RANGE_LENGTH;

/**
* @deprecated The usefulness of many of these tests is unclear - move them to pay-cardid-data?
Expand Down Expand Up @@ -68,6 +70,34 @@ public void shouldFindTestCardInformation() {
.body("corporate", is(false));
}

@Test
public void shouldErrorWhenShortCardNumberProvided() {
getCardInformation(buildCardNumberFromPrefixAndLength("2221", CARD_RANGE_LENGTH - 2))
.statusCode(422)
.contentType(JSON)
.body("errors[0]", is(String.format("cardNumber size must be between %s and 19", CARD_RANGE_LENGTH)));
}

@Test
public void shouldErrorWhenOneCharTooShortCardNumberProvided() {
getCardInformation(buildCardNumberFromPrefixAndLength("2221", CARD_RANGE_LENGTH - 1))
.statusCode(422)
.contentType(JSON)
.body("errors[0]", is(String.format("cardNumber size must be between %s and 19", CARD_RANGE_LENGTH)));
}

@Test
public void shouldFindTestCardInformationWithShortestAllowableCardNumber() {
getCardInformation(buildCardNumberFromPrefixAndLength("2221", CARD_RANGE_LENGTH))
.statusCode(200)
.contentType(JSON)
.body("brand", is("master-card"))
.body("label", is("MC"))
.body("type", is("C"))
.body("prepaid", is("NOT_PREPAID"))
.body("corporate", is(false));
}

@Test
public void shouldFindWorldpayCardInformation() {
getCardInformation("2225670000000000")
Expand Down Expand Up @@ -154,6 +184,15 @@ public void shouldReturn422WhenJSONIsMissing() {
.then().statusCode(422);
}

@NotNull
private static String buildCardNumberFromPrefixAndLength(String cardStart, int cardLength) {
StringBuilder cardNumberBuilder = new StringBuilder(cardStart);
while (cardNumberBuilder.length() < cardLength) {
cardNumberBuilder.append('0');
}
return cardNumberBuilder.toString();
}

private ValidatableResponse getCardInformation(String cardNumber) {
return given().port(app.getLocalPort())
.contentType(JSON)
Expand Down

0 comments on commit 9535031

Please sign in to comment.