Skip to content

Commit 19d9387

Browse files
committed
Add b64 type test
1 parent 5349b69 commit 19d9387

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

pkg/utils/base64_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/goccy/go-yaml"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestBase64String(t *testing.T) {
12+
type testStruct struct {
13+
Password Base64String `yaml:"password"`
14+
}
15+
16+
tcs := []struct {
17+
name string
18+
input []byte
19+
shouldError bool
20+
errorContains string
21+
}{
22+
{
23+
name: "happy path",
24+
// b64: hello world
25+
input: []byte(`password: "aGVsbG8gd29ybGQ="`),
26+
},
27+
{
28+
name: "invalid base64 encoding",
29+
input: []byte(`password: "notbase64"`),
30+
shouldError: true,
31+
errorContains: "failed to base64 decode",
32+
},
33+
}
34+
35+
for _, tc := range tcs {
36+
t.Run(tc.name, func(tt *testing.T) {
37+
var ts testStruct
38+
err := yaml.Unmarshal(tc.input, &ts)
39+
fmt.Printf("TS: %+v\n", ts)
40+
if tc.shouldError {
41+
assert.ErrorContains(tt, err, tc.errorContains)
42+
} else {
43+
assert.NoError(tt, err)
44+
assert.NotEmpty(tt, ts.Password)
45+
}
46+
})
47+
}
48+
}

0 commit comments

Comments
 (0)