Skip to content

Commit

Permalink
feat(xmlmask): add multi mask test
Browse files Browse the repository at this point in the history
  • Loading branch information
Chao-Ma5566 committed Jan 3, 2024
1 parent 4ebcaff commit cbfb7f4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
3 changes: 1 addition & 2 deletions pkg/xml/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ func (engine MaskEngine) Mask(e model.Entry, context ...model.Dictionary) (model
}
}
}

// Mask remove return *remove as value to element/attribute
for k := range m {
// remove element/attribute return *remove as value
if _, ok := newList[k]; !ok {
newList[k] = "*remove"
}
Expand Down
37 changes: 37 additions & 0 deletions pkg/xml/xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,40 @@ func TestMaskShouldAddInjectParentValueToTargetAttribute(t *testing.T) {
)
assert.Nil(t, err, "error should be nil")
}

func TestMaskWithMultiMasks(t *testing.T) {
masking := []model.Masking{
{
Selector: model.SelectorType{Jsonpath: "@author"},
Mask: model.MaskType{Template: " "},
},
{
Selector: model.SelectorType{Jsonpath: "date"},
Mask: model.MaskType{Template: " "},
},
}
config := model.XMLType{
XPath: "note",
InjectParent: "title",
Masking: masking,
}
maskingConfig := model.Masking{Mask: model.MaskType{XML: config}}
factoryConfig := model.MaskFactoryConfiguration{Masking: maskingConfig, Seed: 42}
mask, present, err := Factory(factoryConfig)
assert.Nil(t, err, "error should be nil")
assert.True(t, present, "should be true")
data := model.NewDictionary().
With("title", "my blog note").
With("content", `<note author="John Doe"><date>10/10/2023</date>This is a note of my blog....</note>`).
Pack()
masked, err := mask.Mask("content", data)

assert.Equal(t,
model.NewDictionary().
With("title", "my blog note").
With("content", `<note author=" my blog note"><date> my blog note</date>This is a note of my blog....</note>`).
Unordered(),
masked.(model.Dictionary).Unordered(),
)
assert.Nil(t, err, "error should be nil")
}
13 changes: 13 additions & 0 deletions test/masking_xml_mask.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: "1"
seed: 42
masking:
- selector:
jsonpath: "content"
mask:
xml:
xpath: "note"
masking:
- selector:
jsonpath: "@author"
mask:
template: "New Author Name"
28 changes: 28 additions & 0 deletions test/suites/masking_xml.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: XML masking features
testcases:
- name: replace target attribute value
steps:
- script: rm -f masking.yml
- script: |-
cat > masking.yml <<EOF
version: "1"
seed: 42
masking:
- selector:
jsonpath: "content"
mask:
xml:
xpath: "note"
injectParent: "title"
masking:
- selector:
jsonpath: "@author"
mask:
template: "New Author Name"
EOF
- script: |-
echo '{"title": "my blog note","content": "<note author='John Doe'><date>10/10/2023</date>This is a note of my blog....</note>"}' | pimo
assertions:
- result.code ShouldEqual 0
- result.systemout ShouldEqual {"title":"my blog note","content":"<note author='New Author Name'><date>10/10/2023</date>This is a note of my blog....</note>"}
- result.systemerr ShouldBeEmpty

0 comments on commit cbfb7f4

Please sign in to comment.