maskdata 1.1.4 → 1.1.7

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.
@@ -0,0 +1,206 @@
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.maskEmail2(input, maskOptions);
201
+ expect(masked).to.equal(output, 'masked output does not match expected value');
202
+ });
203
+ });
204
+ });
205
+ });
206
+ });
@@ -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,149 @@
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 to 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
+ const shortMaxCharsOption = {
92
+ maskWith: 'x',
93
+ maxMaskedCharacters: 6,
94
+ unmaskedStartCharacters: 4, // 4+4=8 > 6 --> But this will give more preference to maxMaskedCharacters
95
+ unmaskedEndCharacters: 4
96
+ }
97
+ const input = '1234-5678-1234-5678'
98
+ const output = '123478'
99
+ const masked = maskData.maskPassword(input, shortMaxCharsOption);
100
+ expect(masked).to.equal(output, 'masked output does not match expected value');
101
+ });
102
+
103
+ it('custom mask - test with maxMasked equal to start + end', function() {
104
+ const shortMaxCharsOption = {
105
+ maskWith: 'x',
106
+ maxMaskedCharacters: 8,
107
+ unmaskedStartCharacters: 4, // 4+4=8
108
+ unmaskedEndCharacters: 4
109
+ }
110
+ const input = '1234-abcd-efgh-5678'
111
+ const output = '12345678'
112
+ const masked = maskData.maskPassword(input, shortMaxCharsOption);
113
+ expect(masked).to.equal(output, 'masked output does not match expected value');
114
+ });
115
+ });
116
+
117
+ describe('Mask with special input strings', function() {
118
+
119
+ const maskOptions = {
120
+ maskWith: "x",
121
+ maxMaskedCharacters: 6,
122
+ unmaskedStartCharacters: 2,
123
+ unmaskedEndCharacters: 2
124
+ };
125
+
126
+ describe('Mask with special input - input will generate an output', function() {
127
+
128
+ // first set with input generating an masked output
129
+ let testData = [
130
+ {
131
+ title: 'test with string length zero',
132
+ input: '',
133
+ output: ''
134
+ },
135
+ {
136
+ title: 'test input null',
137
+ input: null,
138
+ output: null
139
+ }
140
+ ]
141
+ testData.forEach(({title, input, output}) => {
142
+ it(`special input - ${title}`, function() {
143
+ const masked = maskData.maskPassword(input, maskOptions);
144
+ expect(masked).to.equal(output, 'masked output does not match expected value');
145
+ });
146
+ });
147
+ });
148
+ });
149
+ });
@@ -0,0 +1,156 @@
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
+
124
+ testData.forEach(({title, input, output}) => {
125
+ it(`special input - ${title}`, function() {
126
+ const masked = maskData.maskPhone(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 array',
138
+ // input: ['12', '34'],
139
+ // output: 'xx'
140
+ // },
141
+ // {
142
+ // title: 'test input as object',
143
+ // input: {a: 'b', x: 'y'},
144
+ // output: '12'
145
+ // }
146
+ // ]
147
+
148
+ // testData.forEach(({title, input, output}) => {
149
+ // it(`special input - ${title}`, function() {
150
+ // const masked = maskData.maskPhone(input, maskOptions);
151
+ // expect(masked).to.equal(output, 'masked output does not match expected value');
152
+ // });
153
+ // });
154
+ // });
155
+ });
156
+ });
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+ const maskData = require('../index');
3
+ const expect = require('chai').expect;
4
+
5
+ describe('Test Replace value', function() {
6
+
7
+ const input = {
8
+ name: "John",
9
+ age: 33,
10
+ married: true
11
+ };
12
+
13
+ const testData = [
14
+ {
15
+ title: 'replace integer inside json',
16
+ input: input,
17
+ inputField: 'age',
18
+ output: 65
19
+ },
20
+ {
21
+ title: 'replace string inside json',
22
+ input: input,
23
+ inputField: 'name',
24
+ output: "Sherlock"
25
+ }
26
+ ];
27
+ describe('Test replaceValue positive cases', function() {
28
+ testData.forEach(({title, input, inputField, output}) => {
29
+ it(`custom mask - ${title}`, function() {
30
+ const afterReplacing = maskData.replaceValue(input, inputField, output);
31
+ expect(afterReplacing[inputField]).to.equal(output, 'Replaced value same as expected value');
32
+ });
33
+ });
34
+ });
35
+ });