maskdata 1.1.6 → 1.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,7 +4,7 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
4
4
  # Table of Contents
5
5
  - [Features](#features)
6
6
  - [Install maskdata](#install-maskdata)
7
- - [Version 1.1.6 Features](#release-features)
7
+ - [Version 1.1.10 Features](#release-features)
8
8
  - [How to Use](#how-to-use)
9
9
  - [Mask Phone Number](#mask-phone-number)
10
10
  - [Mask Phone Number with the default configuration](#mask-phone-number-with-the-default-configuration)
@@ -19,6 +19,7 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
19
19
  - [Mask words/characters in a string](#mask-the-characters-or-words-in-the-string)
20
20
  - [Mask Card number](#mask-card-number)
21
21
  - [Report Bugs](#report-bugs)
22
+ - [How to run test cases](#run-tests)
22
23
  - [Give a Star](#give-a-star)
23
24
  - [LICENSE - "MIT"](#license---mit)
24
25
 
@@ -36,6 +37,13 @@ maskdata is a Node.js module to mask various kinds of data. With the help of mas
36
37
  > npm i maskdata
37
38
 
38
39
  # Release Features
40
+ ### Version: 1.1.10
41
+ - Support of maxMaskedCharacters in masking json fields: https://github.com/Sumukha1496/maskdata/issues/21
42
+ - This is applicable only if the type of value is string. Details: [Mask JSON fields](#mask-fields-in-a-json)
43
+ ### Version: 1.1.8
44
+ - Mocha test cases for most of the usecases.
45
+ https://github.com/Sumukha1496/maskdata/pull/23
46
+ - Handling null/Empty inputs --> Mask function will return the input itself.
39
47
  ### Version: 1.1.6
40
48
  - Feature to mask all the characters in the String along with mask/not mask spaces in the string.
41
49
  - [Mask all characters in the String](#mask-all-characters-in-the-string)
@@ -298,7 +306,10 @@ const maskJSONOptions = {
298
306
 
299
307
  // It should be an array
300
308
  // Field names to mask. Can give multiple fields.
301
- fields: ['password', 'firstName']
309
+ fields: ['password', 'firstName'],
310
+
311
+ // If the type of value is string and if we want to limit the number of characters in the output, then we can use this. Default value is -1 means, by default this feature is disabled. It is enabled if we provide positive integer value. This is not a mandatory field in the config and if not provided, it will take the default value -1 and disable the feature.
312
+ maxMaskedCharactersStr: 4 // Default value is -1 (Disable feature)
302
313
  };
303
314
 
304
315
  const obj = {
@@ -306,9 +317,10 @@ const obj = {
306
317
  firstName: "Jon",
307
318
  lastName: "Snoww"
308
319
  };
320
+
309
321
  const maskedObj = MaskData.maskJSONFields(obj, maskJSONOptions);
310
322
 
311
- //Output: { password: '************', firstName: '***', lastName: 'Snoww' }
323
+ //Output: { password: '****', firstName: '***', lastName: 'Snoww' }
312
324
 
313
325
  ```
314
326
 
@@ -384,7 +396,7 @@ Limititions:
384
396
  2. It will not work for the nested fields like ARRAY[*].FIELD1.FIELD2
385
397
  3. It will not mask all array elements if given ARRAY[*] or ARRAY[*].
386
398
  4. Will not mask null values.
387
- 5. If ARRAY[*].FIELD is an object, then it will mask that entire object include key.
399
+ 5. If ARRAY[*].FIELD is an object, then it will mask that entire object including the key.
388
400
 
389
401
  const nestedObject = {
390
402
  level1: {
@@ -542,6 +554,10 @@ const cardAfterMasking = MaskData.maskCard(cardNumber, maskCardOptions);
542
554
  # Report Bugs
543
555
  Please raise an issue in github: https://github.com/Sumukha1496/maskdata/issues
544
556
 
557
+ # Run Tests
558
+ Below command will run mocha tests and also the test cases present in test.js file. Install all the dependencies using npm i before runnning the below command.
559
+ > npm run test
560
+
545
561
  # Give a Star
546
562
  You can give a start at: https://github.com/Sumukha1496/maskdata/stargazers
547
563
 
package/index.js CHANGED
@@ -3,8 +3,7 @@
3
3
  const MaskHelper = require('./lib/helpers/MaskHelper');
4
4
  const MaskEmail = require('./lib/emailMask/EmailMask');
5
5
  const MaskCard = require('./lib/cardMask/CardMask');
6
- const get = require('lodash.get');
7
- const set = require('lodash.set');
6
+ const {get, set} = require('lodash');
8
7
 
9
8
  const defaultPhoneMaskOptions = {
10
9
  maskWith: "*",
@@ -14,7 +13,8 @@ const defaultPhoneMaskOptions = {
14
13
 
15
14
  const defaultJsonMaskOptions = {
16
15
  maskWith: "*",
17
- fields: []
16
+ fields: [],
17
+ maxMaskedCharactersStr: -1
18
18
  };
19
19
 
20
20
  const defaultPasswordMaskOptions = {
@@ -72,6 +72,7 @@ class MaskData {
72
72
  }
73
73
 
74
74
  static maskPhone(phone, options) {
75
+ if(!phone) return phone;
75
76
  if(options) {
76
77
  options = MaskHelper.mapWithDefaultValues(options, defaultPhoneMaskOptions);
77
78
  MaskHelper.validatePhoneOptions(options);
@@ -115,8 +116,14 @@ class MaskData {
115
116
  const arrayValue = get(maskedObj, arrayFieldName);
116
117
  for(const arrayElement of arrayValue) {
117
118
  const value = arrayElement[subField];
118
- if(value !== undefined && value !== null) {
119
- set(arrayElement, subField, (`${options.maskWith}`.repeat(value.toString().length)))
119
+ if(value === undefined || value === null) {
120
+ continue;
121
+ } else {
122
+ if(typeof(value) == 'string' && options.maxMaskedCharactersStr != -1 && options.maxMaskedCharactersStr < value.length) {
123
+ set(arrayElement, subField, (`${options.maskWith}`.repeat(options.maxMaskedCharactersStr)))
124
+ } else {
125
+ set(arrayElement, subField, (`${options.maskWith}`.repeat(value.toString().length)));
126
+ }
120
127
  }
121
128
  }
122
129
  } else if(field.includes('.*')) {
@@ -124,15 +131,26 @@ class MaskData {
124
131
  const innerObject = get(maskedObj, subField);
125
132
  for(const innerObjectField of Object.keys(innerObject)) {
126
133
  const value = innerObject[innerObjectField];
127
- if(value !== undefined && value !== null) {
128
- set(innerObject, innerObjectField, (`${options.maskWith}`.repeat(value.toString().length)))
134
+ if(value === undefined || value === null) {
135
+ continue;
136
+ } else {
137
+ if(typeof(value) == 'string' && options.maxMaskedCharactersStr != -1 && options.maxMaskedCharactersStr < value.length) {
138
+ set(innerObject, innerObjectField, (`${options.maskWith}`.repeat(options.maxMaskedCharactersStr)))
139
+ } else {
140
+ set(innerObject, innerObjectField, (`${options.maskWith}`.repeat(value.toString().length)));
141
+ }
129
142
  }
130
143
  }
131
144
  } else {
132
- const value = get(maskedObj, field);
133
- if(value !== undefined && value !== null) {
134
- set(maskedObj, field, (`${options.maskWith}`.repeat(value.toString().length)))
135
- }
145
+ const value = get(maskedObj, field);
146
+ if(value === undefined || value === null) {
147
+ continue;
148
+ }
149
+ if(typeof(value) == 'string' && options.maxMaskedCharactersStr != -1 && options.maxMaskedCharactersStr < value.length) {
150
+ set(maskedObj, field, (`${options.maskWith}`.repeat(options.maxMaskedCharactersStr)))
151
+ } else {
152
+ set(maskedObj, field, (`${options.maskWith}`.repeat(value.toString().length)))
153
+ }
136
154
  }
137
155
  } catch(ex) { continue; }
138
156
  }
@@ -140,6 +158,7 @@ class MaskData {
140
158
  }
141
159
 
142
160
  static maskString(str, options) {
161
+ if(!str) return str;
143
162
  str = str + '';
144
163
  if(options) {
145
164
  options = MaskHelper.mapWithDefaultValues(options, defaultStringMaskOptions);
@@ -149,6 +149,9 @@ class MaskHelper {
149
149
  if((!options.maskWith) || options.maskWith.toString().length <= 0) {
150
150
  options.maskWith = '*';
151
151
  }
152
+ if(!options.maxMaskedCharactersStr || typeof(options.maxMaskedCharactersStr) !== 'number' || options.maxMaskedCharactersStr <= 0) {
153
+ options.maxMaskedCharactersStr = -1;
154
+ }
152
155
  if(reasons.length > 0) {
153
156
  const badOptions = new BadOption('JSON mask configuration', reasons);
154
157
  badOptions.throwBadOptionException();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maskdata",
3
- "version": "1.1.6",
3
+ "version": "1.1.10",
4
4
  "description": "Node module to mask various kinds of data in the required formats",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -8,7 +8,8 @@
8
8
  "url": "https://github.com/Sumukha1496/maskdata"
9
9
  },
10
10
  "scripts": {
11
- "test": "node test.js"
11
+ "test": "node test.js && mocha tests/test*.js --timeout 5000 --exit",
12
+ "coverage": "nyc --reporter=lcov --report-dir coverage/ mocha --timeout 5000 --reporter mocha-junit-reporter --reporter-options mochaFile=coverage/junit.xml --exit"
12
13
  },
13
14
  "keywords": [
14
15
  "mask",
@@ -30,7 +31,12 @@
30
31
  "author": "sumukha.s<sumukha.hs369@gmail.com>",
31
32
  "license": "MIT",
32
33
  "dependencies": {
33
- "lodash.get": "^4.4.2",
34
- "lodash.set": "^4.3.2"
34
+ "lodash": "^4.17.21"
35
+ },
36
+ "devDependencies": {
37
+ "chai": "^4.3.6",
38
+ "mocha": "^9.2.2",
39
+ "mocha-junit-reporter": "^2.0.2",
40
+ "nyc": "^15.1.0"
35
41
  }
36
42
  }
package/test.js CHANGED
@@ -53,15 +53,14 @@ console.log(`shortEmail after masking: ${MaskData.maskEmail2(shortEmail, emailMa
53
53
  console.log("========================================");
54
54
 
55
55
  const jsonMaskOptions = {
56
- fields: ['password', 'firstName']
56
+ fields: ['password', 'firstName'],
57
+ maxMaskedCharactersStr: 3
57
58
  };
58
-
59
59
  const json = {
60
60
  password: "PasswordValue",
61
61
  firstName: "FIRST_NAME",
62
62
  lastName: "LAST_NAME"
63
63
  };
64
-
65
64
  console.log(`Unmasked object: `);
66
65
  console.log(JSON.stringify(json));
67
66
  console.log(`Object after masking:`);
@@ -69,21 +68,18 @@ console.log(JSON.stringify(MaskData.maskJSONFields(json, jsonMaskOptions)));
69
68
  console.log("========================================");
70
69
  console.log(JSON.stringify(json));
71
70
  console.log("========================================");
72
-
73
71
  const jsonMaskOptions2 = {
74
72
  fields : ['level1.level2.level3.field3', 'level1.level2.field2', 'level1.field1', 'value1', 'level1.level2.level3.field4[0].Hello', 'level1.level2.level3.field4[2]']
75
73
  };
76
-
77
74
  const maskAllFieldsOptions = {
78
75
  fields : ['level1.level2.field3[*].Hello', 'level1.level2.level3.*']
79
76
  };
80
-
81
77
  const nestedAllObject = {
82
78
  level1: {
83
79
  field1: "field1Value",
84
80
  level2: {
85
81
  field2: "field2Value",
86
- field3: [ { Hello: "Hello", Hi: "one" }, { Hello: "Hello again" } ],
82
+ field3: [ { Hello: "HelloValue", Hi: "one" }, { Hello: "Hello again" } ],
87
83
  level3: {
88
84
  field4: "field4Value",
89
85
  field5: "field5Value"
@@ -92,13 +88,11 @@ const nestedAllObject = {
92
88
  },
93
89
  value1: "value"
94
90
  };
95
-
96
91
  console.log(`Unmasked nested object: `);
97
92
  console.log(JSON.stringify(nestedAllObject));
98
93
  console.log(`Nested Object after masking all:`);
99
94
  console.log(JSON.stringify(MaskData.maskJSONFields(nestedAllObject, maskAllFieldsOptions)));
100
95
  console.log("========================================");
101
-
102
96
  const nestedObject = {
103
97
  level1: {
104
98
  field1: "field1",
@@ -112,7 +106,6 @@ const nestedObject = {
112
106
  },
113
107
  value1: "value"
114
108
  };
115
-
116
109
  console.log(`Unmasked nested object: `);
117
110
  console.log(JSON.stringify(nestedObject));
118
111
  console.log(`Nested Object after masking:`);
@@ -185,18 +178,14 @@ const nestedJson = {
185
178
  },
186
179
  value1: "value"
187
180
  };
188
-
189
181
  const field = 'level1.level2.level3.field4[0].Hello';
190
-
191
182
  console.log("========================================");
192
183
  console.log(`${field}: ${MaskData.getInnerProperty(nestedJson, field)}`);
193
-
194
184
  const input = {
195
185
  name: "John",
196
186
  age: 33,
197
187
  married: true
198
188
  }
199
-
200
189
  console.log("Before replacing: " + JSON.stringify(input));
201
190
  console.log("========================================");
202
191
  let afterReplacing = MaskData.replaceValue(input, 'age', 99);
@@ -213,4 +202,4 @@ const maskCardOptions = {
213
202
 
214
203
  const obj = { cardNumber: "123456789123" ,abc: "abceew" ,efg: "121212" } ;
215
204
  const result = MaskData.replaceValue(obj, 'cardNumber', MaskData.maskCard(obj['cardNumber'], maskCardOptions));
216
- console.log(result);
205
+ console.log(result);
@@ -0,0 +1,161 @@
1
+ 'use strict';
2
+ const maskData = require('../index');
3
+ const expect = require('chai').expect;
4
+
5
+ describe('Masking card numbers', function() {
6
+
7
+ describe('Mask with default options', function() {
8
+
9
+ // default options are this - let tests fail when defaults change
10
+ // const defaultCardMaskOptions = {
11
+ // maskWith: '*',
12
+ // unmaskedStartDigits: 4,
13
+ // unmaskedEndDigits: 1
14
+ // };
15
+
16
+ let testData = [
17
+ {
18
+ title: 'test default string',
19
+ input: '1234-5678-1234-5678',
20
+ output: '1234-****-****-***8'
21
+ },
22
+ {
23
+ title: 'test string length equal than start + end',
24
+ input: '123-4',
25
+ output: '123-4'
26
+ },
27
+ {
28
+ title: 'test shorter string than start + end',
29
+ input: '1234',
30
+ output: '1234'
31
+ }
32
+ ]
33
+
34
+ testData.forEach(({title, input, output}) => {
35
+ it(`default mask - ${title}`, function() {
36
+ const masked = maskData.maskCard(input);
37
+ expect(masked).to.equal(output, 'masked output does not match expected value');
38
+ });
39
+ })
40
+ });
41
+
42
+ describe('Mask with custom options', function() {
43
+
44
+ const maskOptions = {
45
+ maskWith: 'x',
46
+ unmaskedStartDigits: 2, // use different numbers for start / end to check correct masking
47
+ unmaskedEndDigits: 3
48
+ };
49
+
50
+ let testData = [
51
+ {
52
+ title: 'test default string',
53
+ input: '1234-5678-1234-5678',
54
+ output: '12xx-xxxx-xxxx-x678'
55
+ },
56
+ {
57
+ title: 'test string length equal than start + end',
58
+ input: '12-345',
59
+ output: '12-345'
60
+ },
61
+ {
62
+ title: 'test shorter string than start + end',
63
+ input: '1234',
64
+ output: '1234'
65
+ }
66
+ ]
67
+
68
+ testData.forEach(({title, input, output}) => {
69
+ it(`custom mask - ${title}`, function() {
70
+ const masked = maskData.maskCard(input, maskOptions);
71
+ expect(masked).to.equal(output, 'masked output does not match expected value');
72
+ });
73
+ });
74
+
75
+ it('test with negative unmaskedStartDigits', function() {
76
+ const negativeStartOption = {
77
+ maskWith: 'x',
78
+ unmaskedStartDigits: -2, // internal set to 0
79
+ unmaskedEndDigits: 3
80
+ }
81
+ const input = '1234-5678-1234-5678'
82
+ const output = 'xxxx-xxxx-xxxx-x678'
83
+ const masked = maskData.maskCard(input, negativeStartOption);
84
+ expect(masked).to.equal(output, 'masked output does not match expected value');
85
+ });
86
+
87
+ it('test with negative unmaskedEndDigits', function() {
88
+ const negativeEndOption = {
89
+ maskWith: 'x',
90
+ unmaskedStartDigits: 2,
91
+ unmaskedEndDigits: -3 // internal set to 0
92
+ }
93
+ const input = '1234-5678-1234-5678'
94
+ const output = '12xx-xxxx-xxxx-xxxx'
95
+ const masked = maskData.maskCard(input, negativeEndOption);
96
+ expect(masked).to.equal(output, 'masked output does not match expected value');
97
+ });
98
+ });
99
+
100
+ describe('Mask with special input strings', function() {
101
+
102
+ const maskOptions = {
103
+ maskWith: "x",
104
+ unmaskedStartDigits: 2,
105
+ unmaskedEndDigits: 3
106
+ };
107
+
108
+ describe('Mask with special input - input will generate an output', function() {
109
+
110
+ // first set with input generating an masked output
111
+ let testData = [
112
+ {
113
+ title: 'test with string length zero',
114
+ input: '',
115
+ output: ''
116
+ },
117
+ {
118
+ title: 'test input null',
119
+ input: null,
120
+ output: null
121
+ }
122
+ ]
123
+
124
+ testData.forEach(({title, input, output}) => {
125
+ it(`special input - ${title}`, function() {
126
+ const masked = maskData.maskPassword(input, maskOptions);
127
+ expect(masked).to.equal(output, 'masked output does not match expected value');
128
+ });
129
+ });
130
+ });
131
+
132
+ describe('Mask with special input - input shall throw error', function() {
133
+
134
+ // set with input generating an error / exception
135
+ let testData = [
136
+ {
137
+ title: 'test input as number',
138
+ input: 12,
139
+ output: '12'
140
+ },
141
+ {
142
+ title: 'test input as array',
143
+ input: ['12'],
144
+ output: '12'
145
+ },
146
+ {
147
+ title: 'test input as object',
148
+ input: {a: 'b', x: 'y'},
149
+ output: '12'
150
+ }
151
+ ]
152
+
153
+ testData.forEach(({title, input, output}) => {
154
+ it(`special input - ${title}`, function() {
155
+ const masked = maskData.maskPassword(input, maskOptions);
156
+ expect(masked).to.equal(output, 'masked output does not match expected value');
157
+ });
158
+ });
159
+ });
160
+ });
161
+ });
@@ -0,0 +1,235 @@
1
+ 'use strict';
2
+ const maskData = require('../index');
3
+ const BadOption = require('../lib/ExceptionsHandler/BadOption');
4
+ const expect = require('chai').expect;
5
+
6
+ describe('Masking email addresses', function() {
7
+
8
+ describe('Mask with default options', function() {
9
+
10
+ // default options are this - let tests fail when defaults change
11
+ // const defaultCardMaskOptions = {
12
+ // maskWith: '*',
13
+ // unmaskedStartCharactersBeforeAt: 3,
14
+ // unmaskedEndCharactersAfterAt: 2
15
+ // maskAtTheRate: false
16
+ // };
17
+
18
+ let testData = [
19
+ {
20
+ title: 'test standard address',
21
+ input: 'testuser@dummy.org',
22
+ output: 'tes*****@*******rg'
23
+ },
24
+ {
25
+ title: 'test user part shorter than chars before',
26
+ input: 'we@dummy.org',
27
+ output: 'we@*******rg'
28
+ },
29
+ {
30
+ title: 'test user part equal to chars before',
31
+ input: 'you@dummy.org',
32
+ output: 'you@*******rg'
33
+ },
34
+ {
35
+ title: 'test no user part and domain only',
36
+ input: '@dummy.org',
37
+ output: '@*******rg'
38
+ },
39
+ {
40
+ title: 'test no domain part and user only',
41
+ input: 'user@',
42
+ output: 'use*@'
43
+ },
44
+ {
45
+ title: 'test domain part shorter than chars after',
46
+ input: 'user@x',
47
+ output: 'use*@x'
48
+ }
49
+ ]
50
+
51
+ testData.forEach(({title, input, output}) => {
52
+ it(`default mask - ${title}`, function() {
53
+ const masked = maskData.maskEmail2(input);
54
+ expect(masked).to.equal(output, 'masked output does not match expected value');
55
+ });
56
+ })
57
+ });
58
+
59
+ describe('Mask with custom options (char, before, after)', function() {
60
+
61
+ const maskOptions = {
62
+ maskWith: 'x',
63
+ unmaskedStartCharactersBeforeAt: 4,
64
+ unmaskedEndCharactersAfterAt: 6, // full tld and part of sld
65
+ maskAtTheRate: false
66
+ };
67
+
68
+ let testData = [
69
+ {
70
+ title: 'test default string',
71
+ input: 'testuser@dummy.org',
72
+ output: 'testxxxx@xxxmy.org'
73
+ }
74
+ ]
75
+
76
+ testData.forEach(({title, input, output}) => {
77
+ it(`default mask - ${title}`, function() {
78
+ const masked = maskData.maskEmail2(input, maskOptions);
79
+ expect(masked).to.equal(output, 'masked output does not match expected value');
80
+ });
81
+ })
82
+ });
83
+
84
+ describe('Mask with custom options (maskAtTheRate)', function() {
85
+
86
+ const maskOptions = {
87
+ maskWith: '*',
88
+ unmaskedStartCharactersBeforeAt: 4,
89
+ unmaskedEndCharactersAfterAt: 6, // full tld and part of sld
90
+ maskAtTheRate: true
91
+ };
92
+
93
+ let testData = [
94
+ {
95
+ title: 'test default string',
96
+ input: 'testuser@dummy.org',
97
+ output: 'test********my.org'
98
+ },
99
+ {
100
+ title: 'test user and domain part to short',
101
+ input: 'we@a.de',
102
+ output: 'we*a.de' // before and after are shorter than visible, only @ replaced
103
+ },
104
+ {
105
+ title: 'test user part to short',
106
+ input: 'we@dummy.org',
107
+ output: 'we****my.org' // before and after are shorter than visible, only @ replaced
108
+ },
109
+ {
110
+ title: 'test domain part to short',
111
+ input: 'testuser@a.de',
112
+ output: 'test*****a.de' // before and after are shorter than visible, only @ replaced
113
+ }
114
+ ]
115
+
116
+ testData.forEach(({title, input, output}) => {
117
+ it(`custom mask - ${title}`, function() {
118
+ const masked = maskData.maskEmail2(input, maskOptions);
119
+ expect(masked).to.equal(output, 'masked output does not match expected value');
120
+ });
121
+ });
122
+
123
+ it('custom mask - negative start digits', function() {
124
+ const negativeStartOptions = {
125
+ maskWith: '*',
126
+ unmaskedStartCharactersBeforeAt: -1, // replaced with 0 internally
127
+ unmaskedEndCharactersAfterAt: 1,
128
+ maskAtTheRate: false
129
+ };
130
+ const input = 'test@dummy.de';
131
+ const output = '****@*******e';
132
+ const masked = maskData.maskEmail2(input, negativeStartOptions);
133
+ expect(masked).to.equal(output, 'masked output does not match expected value');
134
+ });
135
+
136
+ it('custom mask - negative end digits', function() {
137
+ const negativeEndOptions = {
138
+ maskWith: '*',
139
+ unmaskedStartCharactersBeforeAt: 1, // replaced with 0 internally
140
+ unmaskedEndCharactersAfterAt: -1,
141
+ maskAtTheRate: false
142
+ };
143
+ const input = 'test@dummy.de';
144
+ const output = 't***@********';
145
+ const masked = maskData.maskEmail2(input, negativeEndOptions);
146
+ expect(masked).to.equal(output, 'masked output does not match expected value');
147
+ });
148
+ });
149
+
150
+ describe('test error cases for email addresses', function() {
151
+
152
+ let testData = [
153
+ {
154
+ title: 'test no email address at all',
155
+ input: 'user-dummy.org',
156
+ output: 'use*@x'
157
+ }
158
+ ]
159
+
160
+ testData.forEach(({title, input, output}) => {
161
+ it(`default mask - ${title}`, function() {
162
+ try {
163
+ const masked = maskData.maskEmail2(input);
164
+ expect.fail('maskEmail2 should throw error');
165
+ }
166
+ catch(e) {
167
+ expect(e).to.be.a('string').and.match(/Bad config.*Email must.*@/);
168
+ }
169
+ });
170
+ })
171
+ });
172
+
173
+ describe('Mask with special input strings', function() {
174
+
175
+ const maskOptions = {
176
+ maskWith: "x",
177
+ unmaskedStartCharactersBeforeAt: 1,
178
+ unmaskedEndCharactersAfterAt: 1,
179
+ maskAtTheRate: false
180
+ };
181
+
182
+ describe('Mask with special input - input will generate an output', function() {
183
+
184
+ // first set with input generating an masked output
185
+ let testData = [
186
+ {
187
+ title: 'test with string length zero',
188
+ input: '',
189
+ output: ''
190
+ },
191
+ {
192
+ title: 'test input null',
193
+ input: null,
194
+ output: null
195
+ }
196
+ ]
197
+
198
+ testData.forEach(({title, input, output}) => {
199
+ it(`special input - ${title}`, function() {
200
+ const masked = maskData.maskPassword(input, maskOptions);
201
+ expect(masked).to.equal(output, 'masked output does not match expected value');
202
+ });
203
+ });
204
+ });
205
+
206
+ describe('Mask with special input - input shall throw error', function() {
207
+
208
+ // set with input generating an error / exception
209
+ let testData = [
210
+ {
211
+ title: 'test input as number',
212
+ input: 12,
213
+ output: '12'
214
+ },
215
+ {
216
+ title: 'test input as array',
217
+ input: ['12'],
218
+ output: '12'
219
+ },
220
+ {
221
+ title: 'test input as object',
222
+ input: {a: 'b', x: 'y'},
223
+ output: '12'
224
+ }
225
+ ]
226
+
227
+ testData.forEach(({title, input, output}) => {
228
+ it(`special input - ${title}`, function() {
229
+ const masked = maskData.maskPassword(input, maskOptions);
230
+ expect(masked).to.equal(output, 'masked output does not match expected value');
231
+ });
232
+ });
233
+ });
234
+ });
235
+ });
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+ const maskData = require('../index');
3
+ const expect = require('chai').expect;
4
+
5
+ describe('Masking JSON fields', function() {
6
+
7
+ // TODO - migrate all tests from test.js into mocha tests to fail on error
8
+ // TODO - extend tests
9
+ });
@@ -0,0 +1,183 @@
1
+ 'use strict';
2
+ const maskData = require('../index');
3
+ const expect = require('chai').expect;
4
+
5
+ describe('Masking password', function() {
6
+
7
+ describe('Mask with default options', function() {
8
+
9
+ // default options are this - let tests fail when defaults change
10
+ // const defaultPasswordMaskOptions = {
11
+ // maskWith: "*",
12
+ // maxMaskedCharacters: 16,
13
+ // unmaskedStartCharacters: 0,
14
+ // unmaskedEndCharacters: 0
15
+ // };
16
+
17
+ let testData = [
18
+ {
19
+ title: 'test default string',
20
+ input: '1234-5678-1234-5678',
21
+ output: '****************' // equal maxMaskedChars
22
+ },
23
+ {
24
+ title: 'test string shorter than maxMasked',
25
+ input: '123-4',
26
+ output: '*****'
27
+ },
28
+ {
29
+ title: 'test longer than maxMasked',
30
+ input: '1234567890abcdefghijkl',
31
+ output: '****************' // equal maxMaskedChars
32
+ }
33
+ ]
34
+
35
+ testData.forEach(({title, input, output}) => {
36
+ it(`default mask - ${title}`, function() {
37
+ const masked = maskData.maskPassword(input);
38
+ expect(masked).to.equal(output, 'masked output does not match expected value');
39
+ });
40
+ })
41
+ });
42
+
43
+ describe('Mask with custom options - maskWith, maxMaskedChars', function() {
44
+
45
+ const maskOptions = {
46
+ maskWith: "x",
47
+ maxMaskedCharacters: 6,
48
+ unmaskedStartCharacters: 1, // use different numbers for start / end to check correct masking
49
+ unmaskedEndCharacters: 2
50
+ };
51
+
52
+ let testData = [
53
+ {
54
+ title: 'test default string',
55
+ input: '1234-5678-1234-5678',
56
+ output: '1xxx78'
57
+ },
58
+ {
59
+ title: 'test string length equal than start + end',
60
+ input: '12-345',
61
+ output: '1xxx45'
62
+ },
63
+ {
64
+ title: 'test shorter string than start + end',
65
+ input: '12',
66
+ output: '12'
67
+ }
68
+ ]
69
+
70
+ testData.forEach(({title, input, output}) => {
71
+ it(`custom mask - ${title}`, function() {
72
+ const masked = maskData.maskPassword(input, maskOptions);
73
+ expect(masked).to.equal(output, 'masked output does not match expected value');
74
+ });
75
+ });
76
+
77
+ it('custom mask - test with negative unmaskedStartDigits', function() {
78
+ const negativeStartOption = {
79
+ maskWith: 'x',
80
+ maxMaskedCharacters: 6,
81
+ unmaskedStartCharacters: -2, // internal set to 0
82
+ unmaskedEndCharacters: 2
83
+ }
84
+ const input = '1234-5678-1234-5678'
85
+ const output = 'xxxx78'
86
+ const masked = maskData.maskPassword(input, negativeStartOption);
87
+ expect(masked).to.equal(output, 'masked output does not match expected value');
88
+ });
89
+
90
+ it('custom mask - test with maxMasked shorter than start + end', function() {
91
+ // TODO - should this throw an error? there is no masking at all
92
+ const shortMaxCharsOption = {
93
+ maskWith: 'x',
94
+ maxMaskedCharacters: 6,
95
+ unmaskedStartCharacters: 4, // 4+4=8 > 6
96
+ unmaskedEndCharacters: 4
97
+ }
98
+ const input = '1234-5678-1234-5678'
99
+ const output = '123478'
100
+ const masked = maskData.maskPassword(input, shortMaxCharsOption);
101
+ expect(masked).to.equal(output, 'masked output does not match expected value');
102
+ expect.fail('Shall this test fail because options prevent full masking?')
103
+ });
104
+
105
+ it('custom mask - test with maxMasked equal than start + end', function() {
106
+ // TODO - should this throw an error? there is no masking at all
107
+ const shortMaxCharsOption = {
108
+ maskWith: 'x',
109
+ maxMaskedCharacters: 8,
110
+ unmaskedStartCharacters: 4, // 4+4=8
111
+ unmaskedEndCharacters: 4
112
+ }
113
+ const input = '1234-abcd-efgh-5678'
114
+ const output = '12345678'
115
+ const masked = maskData.maskPassword(input, shortMaxCharsOption);
116
+ expect(masked).to.equal(output, 'masked output does not match expected value');
117
+ expect.fail('Shall this test fail because options prevent full masking?')
118
+ });
119
+ });
120
+
121
+ describe('Mask with special input strings', function() {
122
+
123
+ const maskOptions = {
124
+ maskWith: "x",
125
+ maxMaskedCharacters: 6,
126
+ unmaskedStartCharacters: 2,
127
+ unmaskedEndCharacters: 2
128
+ };
129
+
130
+ describe('Mask with special input - input will generate an output', function() {
131
+
132
+ // first set with input generating an masked output
133
+ let testData = [
134
+ {
135
+ title: 'test with string length zero',
136
+ input: '',
137
+ output: ''
138
+ },
139
+ {
140
+ title: 'test input null',
141
+ input: null,
142
+ output: null
143
+ }
144
+ ]
145
+
146
+ testData.forEach(({title, input, output}) => {
147
+ it(`special input - ${title}`, function() {
148
+ const masked = maskData.maskPassword(input, maskOptions);
149
+ expect(masked).to.equal(output, 'masked output does not match expected value');
150
+ });
151
+ });
152
+ });
153
+
154
+ describe('Mask with special input - input shall throw error', function() {
155
+
156
+ // set with input generating an error / exception
157
+ let testData = [
158
+ {
159
+ title: 'test input as number',
160
+ input: 12,
161
+ output: '12'
162
+ },
163
+ {
164
+ title: 'test input as array',
165
+ input: ['12'],
166
+ output: '12'
167
+ },
168
+ {
169
+ title: 'test input as object',
170
+ input: {a: 'b', x: 'y'},
171
+ output: '12'
172
+ }
173
+ ]
174
+
175
+ testData.forEach(({title, input, output}) => {
176
+ it(`special input - ${title}`, function() {
177
+ const masked = maskData.maskPassword(input, maskOptions);
178
+ expect(masked).to.equal(output, 'masked output does not match expected value');
179
+ });
180
+ });
181
+ });
182
+ });
183
+ });
@@ -0,0 +1,161 @@
1
+ 'use strict';
2
+ const maskData = require('../index');
3
+ const expect = require('chai').expect;
4
+
5
+ describe('Masking phone numbers', function() {
6
+
7
+ describe('Mask with default options', function() {
8
+
9
+ // default options are this - let tests fail when defaults change
10
+ // const phoneMaskOptions = {
11
+ // maskWith: "*",
12
+ // unmaskedStartDigits: 4,
13
+ // unmaskedEndDigits: 1
14
+ // };
15
+
16
+ let testData = [
17
+ {
18
+ title: 'test default number',
19
+ input: '1234-56781234',
20
+ output: '1234********4'
21
+ },
22
+ {
23
+ title: 'test number length equal than start + end',
24
+ input: '12345',
25
+ output: '12345'
26
+ },
27
+ {
28
+ title: 'test shorter number than start + end',
29
+ input: '1234',
30
+ output: '1234'
31
+ }
32
+ ]
33
+
34
+ testData.forEach(({title, input, output}) => {
35
+ it(`default mask - ${title}`, function() {
36
+ const masked = maskData.maskPhone(input);
37
+ expect(masked).to.equal(output, 'masked output does not match expected value');
38
+ });
39
+ })
40
+ });
41
+
42
+ describe('Mask with custom options', function() {
43
+
44
+ const maskOptions = {
45
+ maskWith: 'x',
46
+ unmaskedStartDigits: 2, // use different numbers for start / end to check correct masking
47
+ unmaskedEndDigits: 3
48
+ };
49
+
50
+ let testData = [
51
+ {
52
+ title: 'test default string',
53
+ input: '1234-5678-1234-5678',
54
+ output: '12xxxxxxxxxxxxxx678'
55
+ },
56
+ {
57
+ title: 'test number length equal than start + end',
58
+ input: '12-34',
59
+ output: '12-34'
60
+ },
61
+ {
62
+ title: 'test shorter number than start + end',
63
+ input: '1234',
64
+ output: '1234'
65
+ }
66
+ ]
67
+
68
+ testData.forEach(({title, input, output}) => {
69
+ it(`custom mask - ${title}`, function() {
70
+ const masked = maskData.maskPhone(input, maskOptions);
71
+ expect(masked).to.equal(output, 'masked output does not match expected value');
72
+ });
73
+ });
74
+
75
+ it('test with negative unmaskedStartDigits', function() {
76
+ const negativeStartOption = {
77
+ maskWith: 'x',
78
+ unmaskedStartDigits: -2, // internal set to 0
79
+ unmaskedEndDigits: 3
80
+ }
81
+ const input = '1234-5678'
82
+ const output = 'xxxxxx678'
83
+ const masked = maskData.maskPhone(input, negativeStartOption);
84
+ expect(masked).to.equal(output, 'masked output does not match expected value');
85
+ });
86
+
87
+ it('test with negative unmaskedEndDigits', function() {
88
+ const negativeEndOption = {
89
+ maskWith: 'x',
90
+ unmaskedStartDigits: 2,
91
+ unmaskedEndDigits: -3 // internal set to 0
92
+ }
93
+ const input = '1234-5678'
94
+ const output = '12xxxxxxx'
95
+ const masked = maskData.maskPhone(input, negativeEndOption);
96
+ expect(masked).to.equal(output, 'masked output does not match expected value');
97
+ });
98
+ });
99
+
100
+ describe('Mask with special input strings', function() {
101
+
102
+ const maskOptions = {
103
+ maskWith: "x",
104
+ unmaskedStartDigits: 2,
105
+ unmaskedEndDigits: 2
106
+ };
107
+
108
+ describe('Mask with special input - input will generate an output', function() {
109
+
110
+ // first set with input generating an masked output
111
+ let testData = [
112
+ {
113
+ title: 'test with string length zero',
114
+ input: '',
115
+ output: ''
116
+ },
117
+ {
118
+ title: 'test input null',
119
+ input: null,
120
+ output: null
121
+ },
122
+ {
123
+ title: 'test input as number',
124
+ input: 1234567,
125
+ output: 'xx' // TODO - ok to convert number into string? Or should it fail?
126
+ },
127
+ ]
128
+
129
+ testData.forEach(({title, input, output}) => {
130
+ it(`special input - ${title}`, function() {
131
+ const masked = maskData.maskPhone(input, maskOptions);
132
+ expect(masked).to.equal(output, 'masked output does not match expected value');
133
+ });
134
+ });
135
+ });
136
+
137
+ describe('Mask with special input - input shall throw error', function() {
138
+
139
+ // set with input generating an error / exception
140
+ let testData = [
141
+ {
142
+ title: 'test input as array',
143
+ input: ['12', '34'],
144
+ output: 'xx'
145
+ },
146
+ {
147
+ title: 'test input as object',
148
+ input: {a: 'b', x: 'y'},
149
+ output: '12'
150
+ }
151
+ ]
152
+
153
+ testData.forEach(({title, input, output}) => {
154
+ it(`special input - ${title}`, function() {
155
+ const masked = maskData.maskPhone(input, maskOptions);
156
+ expect(masked).to.equal(output, 'masked output does not match expected value');
157
+ });
158
+ });
159
+ });
160
+ });
161
+ });
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+ const maskData = require('../index');
3
+ const expect = require('chai').expect;
4
+
5
+ describe('Test replaceValue', function() {
6
+
7
+ // TODO - migrate all tests from test.js into mocha tests to fail on error
8
+ // TODO - extend tests
9
+ });
@@ -0,0 +1,258 @@
1
+ 'use strict';
2
+ const maskData = require('../index');
3
+ const expect = require('chai').expect;
4
+
5
+ describe('Masking strings', function() {
6
+
7
+ // common strings without output - used as base for all tests below
8
+ let testData = [
9
+ {
10
+ title: 'test default string',
11
+ input: 'This is a testingAPI String',
12
+ output: ''
13
+ },
14
+ {
15
+ title: 'test string without space',
16
+ input: 'thisisastring',
17
+ output: ''
18
+ },
19
+ {
20
+ title: 'test string with newline',
21
+ input: 'this is a\nstring with newline',
22
+ output: ''
23
+ }
24
+ ]
25
+
26
+ describe('Mask with default options', function() {
27
+
28
+ // default options are this - let tests fail when defaults change
29
+ // const defaultStringMaskOptions = {
30
+ // maskWith: "*",
31
+ // maskOnlyFirstOccurance: false,
32
+ // values: [],
33
+ // maskAll: false,
34
+ // maskSpace: true
35
+ // };
36
+
37
+ // no values -> no masking
38
+ const localTestData = JSON.parse(JSON.stringify(testData));
39
+ localTestData[0].output = localTestData[0].input;
40
+ localTestData[1].output = localTestData[1].input;
41
+ localTestData[2].output = localTestData[2].input;
42
+
43
+ localTestData.forEach(({title, input, output}) => {
44
+ it(`default mask - ${title}`, function() {
45
+ const masked = maskData.maskString(input);
46
+ expect(masked).to.equal(output, 'masked output does not match expected value');
47
+ });
48
+ })
49
+ });
50
+
51
+ describe('Mask with custom options - maskWith, maskSpace', function() {
52
+
53
+ // only maskWith and maskSpace set
54
+ const maskOptions = {
55
+ maskWith: "?",
56
+ maskOnlyFirstOccurance: false,
57
+ values: [],
58
+ maskAll: false,
59
+ maskSpace: true
60
+ };
61
+
62
+ // no values -> no masking
63
+ const localTestData = JSON.parse(JSON.stringify(testData));
64
+ localTestData[0].output = localTestData[0].input;
65
+ localTestData[1].output = localTestData[1].input;
66
+ localTestData[2].output = localTestData[2].input;
67
+
68
+ localTestData.forEach(({title, input, output}) => {
69
+ it(`custom mask - ${title}`, function() {
70
+ const masked = maskData.maskString(input, maskOptions);
71
+ expect(masked).to.equal(output, 'masked output does not match expected value');
72
+ });
73
+ });
74
+ });
75
+
76
+ describe('Mask with custom options - maskWith, maskAll, no spaces', function() {
77
+
78
+ // only maskWith, maskAll set
79
+ const maskOptions = {
80
+ maskWith: "?",
81
+ maskOnlyFirstOccurance: false,
82
+ values: [],
83
+ maskAll: true,
84
+ maskSpace: false
85
+ };
86
+
87
+ const localTestData = JSON.parse(JSON.stringify(testData));
88
+ localTestData[0].output = '???? ?? ? ?????????? ??????';
89
+ localTestData[1].output = '?'.repeat(localTestData[1].input.length); // no space here
90
+ localTestData[2].output = '???? ?? ???????? ???? ???????';
91
+
92
+ localTestData.forEach(({title, input, output}) => {
93
+ it(`custom mask - ${title}`, function() {
94
+ const masked = maskData.maskString(input, maskOptions);
95
+ expect(masked).to.equal(output, 'masked output does not match expected value');
96
+ });
97
+ });
98
+ });
99
+
100
+ describe('Mask with custom options - maskWith multiple chars, maskAll, maskSpace', function() {
101
+
102
+ // only maskWith, maskAll set
103
+ const maskChar = "??"; // mask every char in input string with two chars in output
104
+ const maskOptions = {
105
+ maskWith: maskChar,
106
+ maskOnlyFirstOccurance: false,
107
+ values: [],
108
+ maskAll: true,
109
+ maskSpace: true
110
+ };
111
+
112
+ const localTestData = JSON.parse(JSON.stringify(testData));
113
+ localTestData[0].output = maskChar.repeat(localTestData[0].input.length);
114
+ localTestData[1].output = maskChar.repeat(localTestData[1].input.length);
115
+ localTestData[2].output = maskChar.repeat(localTestData[2].input.length);
116
+
117
+ localTestData.forEach(({title, input, output}) => {
118
+ it(`custom mask - ${title}`, function() {
119
+ const masked = maskData.maskString(input, maskOptions);
120
+ expect(masked).to.equal(output, 'masked output does not match expected value');
121
+ });
122
+ });
123
+ });
124
+
125
+ describe('Mask with custom options - values and maskAll set, no spaces', function() {
126
+
127
+ const maskOptions = {
128
+ maskWith: "x",
129
+ maskOnlyFirstOccurance: false,
130
+ values: ['is', 'ing'],
131
+ maskAll: true,
132
+ maskSpace: false
133
+ };
134
+
135
+ // expected: values ignored and all masked, space remains
136
+ const localTestData = JSON.parse(JSON.stringify(testData));
137
+ localTestData[0].output = 'xxxx xx x xxxxxxxxxx xxxxxx';
138
+ localTestData[1].output = 'x'.repeat(localTestData[1].input.length); // no spaces here
139
+ localTestData[2].output = 'xxxx xx xxxxxxxx xxxx xxxxxxx';
140
+
141
+ localTestData.forEach(({title, input, output}) => {
142
+ it(`custom mask - ${title}`, function() {
143
+ const masked = maskData.maskString(input, maskOptions);
144
+ expect(masked).to.equal(output, 'masked output does not match expected value');
145
+ });
146
+ });
147
+ });
148
+
149
+ describe('Mask with custom options - values and all occurrences', function() {
150
+
151
+ const maskOptions = {
152
+ maskWith: "*",
153
+ maskOnlyFirstOccurance: false,
154
+ values: ['is', 'ing'],
155
+ maskAll: false,
156
+ maskSpace: true
157
+ };
158
+
159
+ const localTestData = JSON.parse(JSON.stringify(testData));
160
+ localTestData[0].output = 'Th** ** a test***API Str***';
161
+ localTestData[1].output = 'th****astr***';
162
+ localTestData[2].output = 'th** ** a\nstr*** with newline';
163
+
164
+ localTestData.forEach(({title, input, output}) => {
165
+ it(`custom mask - ${title}`, function() {
166
+ const masked = maskData.maskString(input, maskOptions);
167
+ expect(masked).to.equal(output, 'masked output does not match expected value');
168
+ });
169
+ });
170
+ });
171
+
172
+ describe('Mask with custom options - values and first occureances', function() {
173
+
174
+ const maskOptions = {
175
+ maskWith: "*",
176
+ maskOnlyFirstOccurance: true,
177
+ values: ['is', 'ing'],
178
+ maskAll: false,
179
+ maskSpace: true
180
+ };
181
+
182
+ const localTestData = JSON.parse(JSON.stringify(testData));
183
+ localTestData[0].output = 'Th** is a test***API String';
184
+ localTestData[1].output = 'th**isastr***';
185
+ localTestData[2].output = 'th** is a\nstr*** with newline';
186
+
187
+ localTestData.forEach(({title, input, output}) => {
188
+ it(`custom mask - ${title}`, function() {
189
+ const masked = maskData.maskString(input, maskOptions);
190
+ expect(masked).to.equal(output, 'masked output does not match expected value');
191
+ });
192
+ });
193
+ });
194
+
195
+ describe('Mask with special input strings', function() {
196
+
197
+ const maskOptions = {
198
+ maskWith: "x",
199
+ maskOnlyFirstOccurance: true,
200
+ values: [],
201
+ maskAll: true,
202
+ maskSpace: true
203
+ };
204
+
205
+ describe('Mask with special input - input will generate an output', function() {
206
+
207
+ // first set with input generating an masked output
208
+ let testData = [
209
+ {
210
+ title: 'test with string length zero',
211
+ input: '',
212
+ output: ''
213
+ },
214
+ {
215
+ title: 'test input null',
216
+ input: null,
217
+ output: null
218
+ },
219
+ {
220
+ title: 'test input as number',
221
+ input: 12,
222
+ output: 'xx' // TODO - ok to convert number into string? Or should it fail?
223
+ },
224
+ ]
225
+
226
+ testData.forEach(({title, input, output}) => {
227
+ it(`special input - ${title}`, function() {
228
+ const masked = maskData.maskString(input, maskOptions);
229
+ expect(masked).to.equal(output, 'masked output does not match expected value');
230
+ });
231
+ });
232
+ });
233
+
234
+ describe('Mask with special input - input shall throw error', function() {
235
+
236
+ // set with input generating an error / exception
237
+ let testData = [
238
+ {
239
+ title: 'test input as array',
240
+ input: ['12', 'ab'],
241
+ output: 'xx' // mask method seems to join all strings in the array into on big string and masks it?
242
+ },
243
+ {
244
+ title: 'test input as object',
245
+ input: {a: 'b', x: 'y'},
246
+ output: '12' // json stringified and masked?
247
+ }
248
+ ]
249
+
250
+ testData.forEach(({title, input, output}) => {
251
+ it(`special input - ${title}`, function() {
252
+ const masked = maskData.maskString(input, maskOptions);
253
+ expect(masked).to.equal(output, 'masked output does not match expected value');
254
+ });
255
+ });
256
+ });
257
+ });
258
+ });