Skip to content

Commit

Permalink
better test template
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <[email protected]>
  • Loading branch information
AustinAbro321 committed Nov 5, 2024
1 parent 0c0d4f9 commit 08a5ba6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/internal/packager/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ func debugPrintTemplateMap(ctx context.Context, templateMap map[string]*variable
func getSanitizedTemplateMap(templateMap map[string]*variables.TextTemplate) map[string]string {
sanitizedMap := make(map[string]string, len(templateMap))
for key, template := range templateMap {
if template.Sensitive {
if template == nil {
sanitizedMap[key] = ""
} else if template.Sensitive {
sanitizedMap[key] = "**sanitized**"
} else {
sanitizedMap[key] = template.Value
Expand Down
78 changes: 63 additions & 15 deletions src/internal/packager/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,70 @@ import (

func TestGetSanitizedTemplateMap(t *testing.T) {
t.Parallel()
input := map[string]*variables.TextTemplate{
"###ZARF_GIT_AUTH_PULL###": {Sensitive: true, Value: "secret1"},
"###ZARF_GIT_AUTH_PUSH###": {Sensitive: true, Value: "secret2"},
"###ZARF_REGISTRY###": {Sensitive: false, Value: "127.0.0.1:31999"},
"###ZARF_GIT_PUSH###": {Sensitive: false, Value: "zarf-git-user"},
"###ZARF_GIT_PULL###": {Sensitive: false, Value: "zarf-git-read-user"},
}

expected := map[string]string{
"###ZARF_GIT_AUTH_PULL###": "**sanitized**",
"###ZARF_GIT_AUTH_PUSH###": "**sanitized**",
"###ZARF_GIT_PULL###": "zarf-git-read-user",
"###ZARF_GIT_PUSH###": "zarf-git-user",
"###ZARF_REGISTRY###": "127.0.0.1:31999",
tests := []struct {
name string
input map[string]*variables.TextTemplate
expected map[string]string
}{
{
name: "Sensitive entry",
input: map[string]*variables.TextTemplate{
"###SENSITIVE###": {Sensitive: true, Value: "secret"},
},
expected: map[string]string{
"###SENSITIVE###": "**sanitized**",
},
},
{
name: "Non-sensitive entries",
input: map[string]*variables.TextTemplate{
"###VARIABLE###": {Sensitive: false, Value: "value"},
},
expected: map[string]string{
"###VARIABLE###": "value",
},
},
{
name: "Sensitive and non-sensitive entries",
input: map[string]*variables.TextTemplate{
"###ZARF_GIT_AUTH_PULL###": {Sensitive: true, Value: "secret1"},
"###ZARF_GIT_AUTH_PUSH###": {Sensitive: true, Value: "secret2"},
"###ZARF_GIT_PUSH###": {Sensitive: false, Value: "zarf-git-user"},
"###ZARF_GIT_PULL###": {Sensitive: false, Value: "zarf-git-read-user"},
},
expected: map[string]string{
"###ZARF_GIT_AUTH_PULL###": "**sanitized**",
"###ZARF_GIT_AUTH_PUSH###": "**sanitized**",
"###ZARF_GIT_PULL###": "zarf-git-read-user",
"###ZARF_GIT_PUSH###": "zarf-git-user",
},
},
{
name: "Nil map",
input: nil,
expected: map[string]string{},
},
{
name: "Empty map",
input: map[string]*variables.TextTemplate{},
expected: map[string]string{},
},
{
name: "Map with nil value",
input: map[string]*variables.TextTemplate{
"###ZARF_GIT_AUTH_PULL###": nil,
},
expected: map[string]string{
"###ZARF_GIT_AUTH_PULL###": "",
},
},
}

output := getSanitizedTemplateMap(input)
require.Equal(t, expected, output)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := getSanitizedTemplateMap(test.input)
require.Equal(t, test.expected, output)
})
}
}

0 comments on commit 08a5ba6

Please sign in to comment.