1 | Whitelist = require '../lib/logasm/preprocessors/whitelist'
|
2 | _ = require 'underscore'
|
3 |
|
4 | describe 'Whitelist', ->
|
5 | processedData = null
|
6 |
|
7 | config = memo().is -> {pointers: pointers()}
|
8 | data = memo().is -> {
|
9 | field: 'secret',
|
10 | nested: {
|
11 | field: 'secret'
|
12 | }
|
13 | array: [{field: 'secret'}]
|
14 | }
|
15 | pointers = memo().is -> []
|
16 |
|
17 | context 'when pointer has trailing slash', ->
|
18 | pointers.is -> ['/field/']
|
19 |
|
20 | it 'throws error', ->
|
21 | expect(-> new Whitelist(config())).to.throw('Pointer should not contain trailing slash')
|
22 |
|
23 | context '#process', ->
|
24 | beforeEach ->
|
25 | whitelist = new Whitelist(config())
|
26 | processedData = whitelist.process(data())
|
27 |
|
28 | context 'when includes fields from default whitelist', ->
|
29 | pointers.is -> []
|
30 | data.is ->
|
31 | id: 'id'
|
32 | message: 'message',
|
33 | queue: 'queue',
|
34 | correlation_id: 'correlation_id'
|
35 |
|
36 | it 'includes fields', ->
|
37 | expect(processedData).to.eql(data())
|
38 |
|
39 | context 'with whitelisted field', ->
|
40 | pointers.is -> ['/field']
|
41 |
|
42 | it 'includes the field', ->
|
43 | expect(processedData).to.eql({
|
44 | field: 'secret',
|
45 | nested: {
|
46 | field: '******'
|
47 | }
|
48 | array: [{field: '******'}]
|
49 | })
|
50 |
|
51 | context 'with whitelisted nested field', ->
|
52 | pointers.is -> ['/nested/field']
|
53 |
|
54 | it 'includes nested field', ->
|
55 | expect(processedData).to.eql({
|
56 | field: '******',
|
57 | nested: {
|
58 | field: 'secret'
|
59 | }
|
60 | array: [{field: '******'}]
|
61 | })
|
62 |
|
63 | context 'with whitelisted array element field', ->
|
64 | pointers.is -> ['/array/0/field']
|
65 |
|
66 | it 'includes array element field', ->
|
67 | expect(processedData).to.eql({
|
68 | field: '******',
|
69 | nested: {
|
70 | field: '******'
|
71 | }
|
72 | array: [{field: 'secret'}]
|
73 | })
|
74 |
|
75 | context 'with whitelisted array element', ->
|
76 | pointers.is -> ['/array/0']
|
77 |
|
78 | it 'masks array element', ->
|
79 | expect(processedData).to.eql({
|
80 | field: '******',
|
81 | nested: {
|
82 | field: '******'
|
83 | }
|
84 | array: [{field: '******'}]
|
85 | })
|
86 |
|
87 |
|
88 | context 'with whitelisted array', ->
|
89 | pointers.is -> ['/array']
|
90 |
|
91 | it 'masks array', ->
|
92 | expect(processedData).to.eql({
|
93 | field: '******',
|
94 | nested: {
|
95 | field: '******'
|
96 | }
|
97 | array: [{field: '******'}]
|
98 | })
|
99 |
|
100 | context 'with whitelisted object', ->
|
101 | pointers.is -> ['/data']
|
102 |
|
103 | it 'masks array', ->
|
104 | expect(processedData).to.eql({
|
105 | field: '******',
|
106 | nested: {
|
107 | field: '******'
|
108 | }
|
109 | array: [{field: '******'}]
|
110 | })
|
111 |
|
112 | context 'when boolean present', ->
|
113 | data.is -> {bool: true}
|
114 |
|
115 | it 'masks it with single asteriks', ->
|
116 | expect(processedData).to.eql({bool: '*'})
|
117 |
|
118 | context 'when field has slash in the name', ->
|
119 | data.is -> {'field_with_/': 'secret'}
|
120 | pointers.is -> ['/field_with_~1']
|
121 |
|
122 | it 'does not include array', ->
|
123 | expect(processedData).to.eql({'field_with_/': 'secret'})
|
124 |
|
125 | context 'when field has tilde in the name', ->
|
126 | data.is -> {'field_with_~': 'secret'}
|
127 | pointers.is -> ['/field_with_~0']
|
128 |
|
129 | it 'does not include array', ->
|
130 | expect(processedData).to.eql({'field_with_~': 'secret'})
|