aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorMarak <[email protected]>2017-09-08 12:52:43 -0400
committerMarak <[email protected]>2017-09-08 12:52:43 -0400
commitb652617fa5f78279cae3a5f64e7930a19369dd38 (patch)
treeb029cbd80f86be3db39c3b72e7f7514a6624b179 /test
parentf4236a85b904394c729c3efb383c09fa5d137c54 (diff)
parent52d8cc1471080fe67c8abd8115240b6aa933f85f (diff)
downloadfaker-b652617fa5f78279cae3a5f64e7930a19369dd38.tar.xz
faker-b652617fa5f78279cae3a5f64e7930a19369dd38.zip
Merge branch 'creditCard' of https://github.com/zpiman/faker.js
Diffstat (limited to 'test')
-rw-r--r--test/finance.unit.js78
-rw-r--r--test/helpers.unit.js51
-rw-r--r--test/support/luhnCheck.js18
3 files changed, 144 insertions, 3 deletions
diff --git a/test/finance.unit.js b/test/finance.unit.js
index 61ae7b6c..a879d584 100644
--- a/test/finance.unit.js
+++ b/test/finance.unit.js
@@ -222,7 +222,7 @@ describe('finance.js', function () {
describe("bitcoinAddress()", function(){
it("returns a random bitcoin address", function(){
var bitcoinAddress = faker.finance.bitcoinAddress();
-
+
assert.ok(bitcoinAddress.match(/^[A-Z0-9.]{27,34}$/));
});
});
@@ -230,11 +230,83 @@ describe('finance.js', function () {
describe("ethereumAddress()", function(){
it("returns a random ethereum address", function(){
var ethereumAddress = faker.finance.ethereumAddress();
-
assert.ok(ethereumAddress.match(/^(0x)[0-9a-f]{40}$/i));
});
});
+ describe("creditCardNumber()", function(){
+ var luhnFormula = require("./support/luhnCheck.js");
+
+ it("returns a random credit card number", function(){
+ var number = faker.finance.creditCardNumber();
+ number = number.replace(/\D/g,""); // remove formating
+ console.log("version:", process.version, number, number.length);
+ assert.ok(number.length >= 13 && number.length <= 20);
+ assert.ok(number.match(/^[0-9]{13,20}$/));
+ assert.ok(luhnFormula(number));
+ });
+
+ it("returns a valid credit card number", function(){
+ assert.ok(luhnFormula(faker.finance.creditCardNumber("")));
+ assert.ok(luhnFormula(faker.finance.creditCardNumber()));
+ assert.ok(luhnFormula(faker.finance.creditCardNumber()));
+ assert.ok(luhnFormula(faker.finance.creditCardNumber("visa")));
+ assert.ok(luhnFormula(faker.finance.creditCardNumber("mastercard")));
+ assert.ok(luhnFormula(faker.finance.creditCardNumber("discover")));
+ assert.ok(luhnFormula(faker.finance.creditCardNumber()));
+ assert.ok(luhnFormula(faker.finance.creditCardNumber()));
+ });
+ it("returns a correct credit card number when issuer provided", function(){
+ //TODO: implement checks for each format with regexp
+ var visa = faker.finance.creditCardNumber("visa");
+ assert.ok(visa.match(/^4(([0-9]){12}|([0-9]){3}(\-([0-9]){4}){3})$/));
+ assert.ok(luhnFormula(visa));
+
+
+ var mastercard = faker.finance.creditCardNumber("mastercard");
+ assert.ok(mastercard.match(/^(5[1-5]\d{2}|6771)(\-\d{4}){3}$/));
+ assert.ok(luhnFormula(mastercard));
+
+ var discover = faker.finance.creditCardNumber("discover");
+
+ assert.ok(luhnFormula(discover));
+
+ var american_express = faker.finance.creditCardNumber("american_express");
+ assert.ok(luhnFormula(american_express));
+ var diners_club = faker.finance.creditCardNumber("diners_club");
+ assert.ok(luhnFormula(diners_club));
+ var jcb = faker.finance.creditCardNumber("jcb");
+ assert.ok(luhnFormula(jcb));
+ var switchC = faker.finance.creditCardNumber("mastercard");
+ assert.ok(luhnFormula(switchC));
+ var solo = faker.finance.creditCardNumber("solo");
+ assert.ok(luhnFormula(solo));
+ var maestro = faker.finance.creditCardNumber("maestro");
+ assert.ok(luhnFormula(maestro));
+ var laser = faker.finance.creditCardNumber("laser");
+ assert.ok(luhnFormula(laser));
+ var instapayment = faker.finance.creditCardNumber("instapayment");
+ assert.ok(luhnFormula(instapayment));
+ });
+ it("returns custom formated strings",function(){
+ var number = faker.finance.creditCardNumber("###-###-##L");
+ assert.ok(number.match(/^\d{3}\-\d{3}\-\d{3}$/));
+ assert.ok(luhnFormula(number));
+ number =faker.finance.creditCardNumber("234[5-9]#{999}L");
+ assert.ok(number.match(/^234[5-9]\d{1000}$/));
+ assert.ok(luhnFormula(number));
+ });
+ });
+
+ describe("creditCardCVV()", function(){
+ it("returns a random credit card CVV", function(){
+ var cvv = faker.finance.creditCardCVV();
+ assert.ok(cvv.length === 3);
+ assert.ok(cvv.match(/^[0-9]{3}$/));
+ });
+ });
+
+
describe("iban()", function () {
var ibanLib = require('../lib/iban');
it("returns a random yet formally correct IBAN number", function () {
@@ -254,4 +326,4 @@ describe('finance.js', function () {
assert.ok(bic.match(expr));
});
});
-}); \ No newline at end of file
+});
diff --git a/test/helpers.unit.js b/test/helpers.unit.js
index 707575ff..b440f33e 100644
--- a/test/helpers.unit.js
+++ b/test/helpers.unit.js
@@ -67,6 +67,57 @@ describe("helpers.js", function () {
});
});
+ describe("replaceCreditCardSymbols()", function () {
+ var luhnCheck = require("./support/luhnCheck.js");
+ it("returns a credit card number given a schema", function () {
+ var number = faker.helpers.replaceCreditCardSymbols("6453-####-####-####-###L");
+ assert.ok(number.match(/^6453\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}$/));
+ assert.ok(luhnCheck(number));
+ });
+ it("supports different symbols", function () {
+ var number = faker.helpers.replaceCreditCardSymbols("6453-****-****-****-***L","*");
+ assert.ok(number.match(/^6453\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}$/));
+ assert.ok(luhnCheck(number));
+ });
+ it("handles regexp style input", function () {
+ var number = faker.helpers.replaceCreditCardSymbols("6453-*{4}-*{4}-*{4}-*{3}L","*");
+ assert.ok(number.match(/^6453\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}$/));
+ assert.ok(luhnCheck(number));
+ number = faker.helpers.replaceCreditCardSymbols("645[5-9]-#{4,6}-#{1,2}-#{4,6}-#{3}L");
+ assert.ok(number.match(/^645[5-9]\-([0-9]){4,6}\-([0-9]){1,2}\-([0-9]){4,6}\-([0-9]){4}$/));
+ assert.ok(luhnCheck(number));
+ });
+ });
+
+ describe("regexpStyleStringParse()", function () {
+ it("returns an empty string when called without param", function () {
+ assert.ok(faker.helpers.regexpStyleStringParse() === "");
+ });
+ it("deals with range repeat", function () {
+ var string = faker.helpers.regexpStyleStringParse("#{5,10}");
+ assert.ok(string.length <= 10 && string.length >= 5);
+ assert.ok(string.match(/^\#{5,10}$/));
+ });
+ it("flips the range when min > max", function () {
+ var string = faker.helpers.regexpStyleStringParse("#{10,5}");
+ assert.ok(string.length <= 10 && string.length >= 5);
+ assert.ok(string.match(/^\#{5,10}$/));
+ });
+ it("repeats string {n} number of times", function () {
+ assert.ok(faker.helpers.regexpStyleStringParse("%{10}") === faker.helpers.repeatString("%",10));
+ assert.ok(faker.helpers.regexpStyleStringParse("%{30}") === faker.helpers.repeatString("%",30));
+ assert.ok(faker.helpers.regexpStyleStringParse("%{5}") === faker.helpers.repeatString("%",5));
+ });
+ it("creates a numerical range", function () {
+ var string = faker.helpers.regexpStyleStringParse("Hello[0-9]");
+ assert.ok(string.match(/^Hello[0-9]$/));
+ });
+ it("deals with multiple tokens in one string", function () {
+ var string = faker.helpers.regexpStyleStringParse("Test#{5}%{2,5}Testing**[1-5]**{10}END");
+ assert.ok(string.match(/^Test\#{5}%{2,5}Testing\*\*[1-5]\*\*{10}END$/));
+ });
+ });
+
describe("createTransaction()", function() {
it("should create a random transaction", function() {
var transaction = faker.helpers.createTransaction();
diff --git a/test/support/luhnCheck.js b/test/support/luhnCheck.js
new file mode 100644
index 00000000..1e195544
--- /dev/null
+++ b/test/support/luhnCheck.js
@@ -0,0 +1,18 @@
+module.exports = function (number) {
+ number = number.replace(/\D/g,"");
+ var split = number.split("");
+ split = split.map(function(num){return parseInt(num);});
+ var check = split.pop();
+ split.reverse();
+ split = split.map(function(num, index){
+ if(index%2 === 0) {
+ num *= 2;
+ if(num>9) {
+ num -= 9;
+ }
+ }
+ return num;
+ });
+ var sum = split.reduce(function(prev,curr){return prev + curr;});
+ return (sum%10 === check);
+};