add_task(function isValidNumber() { function testValid(number, shouldPass) { if (shouldPass) {
ok(
CreditCard.isValidNumber(number),
`${number} should be considered valid`
);
} else {
ok(
!CreditCard.isValidNumber(number),
`${number} should not be considered valid`
);
}
}
testValid("0000000000000000", true);
testValid("41111111112", false); // passes Luhn but too short
testValid("4111-1111-112", false); // passes Luhn but too short
testValid("55555555555544440018", false); // passes Luhn but too long
testValid("5555 5555 5555 4444 0018", false); // passes Luhn but too long
testValid("378282246310005", true); // American Express test number
testValid("371449635398431", true); // American Express test number
testValid("378734493671000", true); // American Express Corporate test number
testValid("5610591081018250", true); // Australian BankCard test number
testValid("6759649826438453", true); // Maestro test number
testValid("6799990100000000019", true); // 19 digit Maestro test number
testValid("6799-9901-0000-0000019", true); // 19 digit Maestro test number
testValid("30569309025904", true); // 14 digit Diners Club test number
testValid("38520000023237", true); // 14 digit Diners Club test number
testValid("6011111111111117", true); // Discover test number
testValid("6011000990139424", true); // Discover test number
testValid("3530111333300000", true); // JCB test number
testValid("3566002020360505", true); // JCB test number
testValid("3532596776688495393", true); // 19-digit JCB number. JCB, Discover, Maestro could have 16-19 digits
testValid("3532 5967 7668 8495393", true); // 19-digit JCB number. JCB, Discover, Maestro could have 16-19 digits
testValid("5555555555554444", true); // MasterCard test number
testValid("5105105105105100", true); // MasterCard test number
testValid("2221000000000009", true); // 2-series MasterCard test number
testValid("4111111111111111", true); // Visa test number
testValid("4012888888881881", true); // Visa test number
testValid("4222222222222", true); // 13 digit Visa test number
testValid("4222 2222 22222", true); // 13 digit Visa test number
testValid("4035 5010 0000 0008", true); // Visadebit/Cartebancaire test number
add_task(function test_maskNumber() { function testMask(number, expected) {
let card = new CreditCard({ number }); Assert.equal(
card.maskedNumber,
expected, "Masked number should only show the last four digits"
);
}
testMask("0000000000000000", "**** 0000");
testMask("4929001587121045", "**** 1045");
testMask("5103059495477870", "**** 7870");
testMask("6011029476355493", "**** 5493");
testMask("3589993783099582", "**** 9582");
testMask("5415425865751454", "**** 1454");
testMask("344060747836806", "**** 6806");
testMask("6799990100000000019", "**** 0019"); Assert.throws(
() => new CreditCard({ number: "1234" }).maskedNumber,
/Invalid credit card number/, "Four or less numbers should throw when retrieving the maskedNumber"
);
});
add_task(function test_longMaskedNumber() { function testMask(number, expected) {
let card = new CreditCard({ number }); Assert.equal(
card.longMaskedNumber,
expected, "Long masked number should show asterisks for all digits but last four"
);
}
testMask("0000000000000000", "************0000");
testMask("4929001587121045", "************1045");
testMask("5103059495477870", "************7870");
testMask("6011029476355493", "************5493");
testMask("3589993783099582", "************9582");
testMask("5415425865751454", "************1454");
testMask("344060747836806", "***********6806");
testMask("6799990100000000019", "***************0019"); Assert.throws(
() => new CreditCard({ number: "1234" }).longMaskedNumber,
/Invalid credit card number/, "Four or less numbers should throw when retrieving the longMaskedNumber"
);
});
add_task(function test_isValid() { function testValid(
number,
expirationMonth,
expirationYear,
shouldPass,
message
) {
let isValid = false; try {
let card = new CreditCard({
number,
expirationMonth,
expirationYear,
});
isValid = card.isValid();
} catch (ex) {
isValid = false;
} if (shouldPass) {
ok(isValid, message);
} else {
ok(!isValid, message);
}
}
let year = new Date().getFullYear();
let month = new Date().getMonth() + 1;
testValid( "0000000000000000",
month,
year + 2, true, "Valid number and future expiry date (two years) should pass"
);
testValid( "0000000000000000",
month < 11 ? month + 2 : month % 10,
month < 11 ? year : year + 1, true, "Valid number and future expiry date (two months) should pass"
);
testValid( "0000000000000000",
month,
year, true, "Valid number and expiry date equal to this month should pass"
);
testValid( "0000000000000000",
month > 1 ? month - 1 : 12,
month > 1 ? year : year - 1, false, "Valid number but overdue expiry date should fail"
);
testValid( "0000000000000000",
month,
year - 1, false, "Valid number but overdue expiry date (by a year) should fail"
);
testValid( "0000000000000001",
month,
year + 2, false, "Invalid number but future expiry date should fail"
);
});
add_task(function test_normalize() { Assert.equal( new CreditCard({ number: "0000 0000 0000 0000" }).number, "0000000000000000", "Spaces should be removed from card number after it is normalized"
); Assert.equal( new CreditCard({ number: "0000 0000\t 0000\t0000" }).number, "0000000000000000", "Spaces should be removed from card number after it is normalized"
); Assert.equal( new CreditCard({ number: "0000-0000-0000-0000" }).number, "0000000000000000", "Hyphens should be removed from card number after it is normalized"
); Assert.equal( new CreditCard({ number: "0000-0000 0000-0000" }).number, "0000000000000000", "Spaces and hyphens should be removed from card number after it is normalized"
); Assert.equal( new CreditCard({ number: "0000000000000000" }).number, "0000000000000000", "Normalized numbers should not get changed"
);
let card = new CreditCard({ number: "0000000000000000" });
card.expirationYear = "22";
card.expirationMonth = "11"; Assert.equal(
card.expirationYear,
2022, "Years less than four digits are in the third millenium"
);
card.expirationYear = "-200";
ok(isNaN(card.expirationYear), "Negative years are blocked");
card.expirationYear = "1998"; Assert.equal(
card.expirationYear,
1998, "Years with four digits are not changed"
);
card.expirationYear = "test";
ok(isNaN(card.expirationYear), "non-number years are returned as NaN");
card.expirationMonth = "02"; Assert.equal(
card.expirationMonth,
2, "Zero-leading months are converted properly (not octal)"
);
card.expirationMonth = "test";
ok(isNaN(card.expirationMonth), "non-number months are returned as NaN");
card.expirationMonth = "12"; Assert.equal(
card.expirationMonth,
12, "Months formatted correctly are unchanged"
);
card.expirationMonth = "13";
ok(isNaN(card.expirationMonth), "Months above 12 are blocked");
card.expirationMonth = "7"; Assert.equal(
card.expirationMonth,
7, "Changing back to a valid number passes"
);
card.expirationMonth = "0";
ok(isNaN(card.expirationMonth), "Months below 1 are blocked");
card.expirationMonth = card.expirationYear = undefined;
card.expirationString = "2022/01"; Assert.equal(card.expirationMonth, 1, "Month should be parsed correctly"); Assert.equal(card.expirationYear, 2022, "Year should be parsed correctly");
card.expirationString = "2028 / 05"; Assert.equal(card.expirationMonth, 5, "Month should be parsed correctly"); Assert.equal(card.expirationYear, 2028, "Year should be parsed correctly");
card.expirationString = "2023-02"; Assert.equal(card.expirationMonth, 2, "Month should be parsed correctly"); Assert.equal(card.expirationYear, 2023, "Year should be parsed correctly");
card.expirationString = "2029 - 09"; Assert.equal(card.expirationMonth, 9, "Month should be parsed correctly"); Assert.equal(card.expirationYear, 2029, "Year should be parsed correctly");
card.expirationString = "03-2024"; Assert.equal(card.expirationMonth, 3, "Month should be parsed correctly"); Assert.equal(card.expirationYear, 2024, "Year should be parsed correctly");
card.expirationString = "08 - 2024"; Assert.equal(card.expirationMonth, 8, "Month should be parsed correctly"); Assert.equal(card.expirationYear, 2024, "Year should be parsed correctly");
card.expirationString = "04/2025"; Assert.equal(card.expirationMonth, 4, "Month should be parsed correctly"); Assert.equal(card.expirationYear, 2025, "Year should be parsed correctly");
card.expirationString = "01 / 2023"; Assert.equal(card.expirationMonth, 1, "Month should be parsed correctly"); Assert.equal(card.expirationYear, 2023, "Year should be parsed correctly");
card.expirationString = "05/26"; Assert.equal(card.expirationMonth, 5, "Month should be parsed correctly"); Assert.equal(card.expirationYear, 2026, "Year should be parsed correctly");
card.expirationString = " 06 / 27 "; Assert.equal(card.expirationMonth, 6, "Month should be parsed correctly"); Assert.equal(card.expirationYear, 2027, "Year should be parsed correctly");
card.expirationString = "04 / 25"; Assert.equal(card.expirationMonth, 4, "Month should be parsed correctly"); Assert.equal(card.expirationYear, 2025, "Year should be parsed correctly");
card.expirationString = "27-6"; Assert.equal(card.expirationMonth, 6, "Month should be parsed correctly"); Assert.equal(card.expirationYear, 2027, "Year should be parsed correctly");
card.expirationString = "26 - 5"; Assert.equal(card.expirationMonth, 5, "Month should be parsed correctly"); Assert.equal(card.expirationYear, 2026, "Year should be parsed correctly");
card.expirationString = "07/11"; Assert.equal(
card.expirationMonth,
7, "Ambiguous month should be parsed correctly"
); Assert.equal(
card.expirationYear,
2011, "Ambiguous year should be parsed correctly"
);
card.expirationString = "08 / 12"; Assert.equal(
card.expirationMonth,
8, "Ambiguous month should be parsed correctly"
); Assert.equal(
card.expirationYear,
2012, "Ambiguous year should be parsed correctly"
);
card = new CreditCard({
number: "0000000000000000",
expirationMonth: "02",
expirationYear: "2112",
expirationString: "06-2066",
}); Assert.equal(
card.expirationMonth,
2, "expirationString is takes lower precendence than explicit month"
); Assert.equal(
card.expirationYear,
2112, "expirationString is takes lower precendence than explicit year"
);
});
for (let testCase of testCases) {
let { number, name } = testCase; Assert.equal(
await CreditCard.getLabel({ number, name }),
testCase.expectedMaskedLabel, "The expectedMaskedLabel should be shown when showNumbers is false"
);
}
});
add_task(async function test_network() {
let supportedNetworks = CreditCard.getSupportedNetworks(); Assert.ok(
supportedNetworks.length, "There are > 0 supported credit card networks"
);
for (let testCase of testCases) {
let { number, network } = testCase;
let card = new CreditCard({ number, network }); Assert.equal(
await card.network,
testCase.expectedNetwork,
`The expectedNetwork ${card.network} should match the card network ${testCase.expectedNetwork}`
);
}
});
add_task(async function test_isValidNetwork() { for (let network of CreditCard.getSupportedNetworks()) { Assert.ok(CreditCard.isValidNetwork(network), "supported network is valid");
} Assert.ok(!CreditCard.isValidNetwork(), "undefined is not a valid network"); Assert.ok(
!CreditCard.isValidNetwork(""), "empty string is not a valid network"
); Assert.ok(!CreditCard.isValidNetwork(null), "null is not a valid network"); Assert.ok(
!CreditCard.isValidNetwork("Visa"), "network validity is case-sensitive"
); Assert.ok(
!CreditCard.isValidNetwork("madeupnetwork"), "unknown network is invalid"
);
});
["Unknown", null],
["", null],
]; for (let [value, type] of RECOGNIZED_NAMES) { Assert.equal(
CreditCard.getNetworkFromName(value),
type,
`Expected ${value} to be recognized as ${type}`
);
}
});
add_task(async function test_normalizeCardNumber() {
let testCases = [
["5495770093313616", "5495770093313616"],
["5495 7700 9331 3616", "5495770093313616"],
[" 549 57700 93313 616 ", "5495770093313616"],
["5495-7700-9331-3616", "5495770093313616"],
["", null],
[undefined, null],
[null, null],
]; for (let [input, expected] of testCases) {
let actual = CreditCard.normalizeCardNumber(input); Assert.equal(
actual,
expected,
`Expected ${input} to normalize to ${expected}`
);
}
});
Messung V0.5
¤ Dauer der Verarbeitung: 0.2 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.