Refactor certificate output parsing and add unit tests for parseCertOutput function
This commit is contained in:
@@ -70,3 +70,53 @@ func TestEnsureCredentials_EmptyToken(t *testing.T) {
|
||||
t.Error("expected error for empty token")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCertOutput(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
output string
|
||||
wildcard bool
|
||||
issuer string
|
||||
}{
|
||||
{
|
||||
name: "wildcard cert (no spaces around =)",
|
||||
output: "subject=CN=*.cloud.payne.io\nnotAfter=Sep 4 23:22:30 2026 GMT\nissuer=C=US, O=Let's Encrypt, CN=YR1\n",
|
||||
wildcard: true,
|
||||
issuer: "C=US, O=Let's Encrypt, CN=YR1",
|
||||
},
|
||||
{
|
||||
name: "wildcard cert (spaces around =)",
|
||||
output: "subject=CN = *.example.com\nnotAfter=Jan 15 12:00:00 2027 GMT\nissuer=CN=R3\n",
|
||||
wildcard: true,
|
||||
issuer: "CN=R3",
|
||||
},
|
||||
{
|
||||
name: "single domain cert",
|
||||
output: "subject=CN=example.com\nnotAfter=Jan 15 12:00:00 2027 GMT\nissuer=CN=R3\n",
|
||||
wildcard: false,
|
||||
issuer: "CN=R3",
|
||||
},
|
||||
{
|
||||
name: "single domain with subdomain",
|
||||
output: "subject=CN=app.example.com\nnotAfter=Jan 15 12:00:00 2027 GMT\nissuer=CN=R3\n",
|
||||
wildcard: false,
|
||||
issuer: "CN=R3",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
status := &CertStatus{}
|
||||
parseCertOutput(tt.output, status)
|
||||
if status.Wildcard != tt.wildcard {
|
||||
t.Errorf("Wildcard = %v, want %v", status.Wildcard, tt.wildcard)
|
||||
}
|
||||
if status.IssuerCN != tt.issuer {
|
||||
t.Errorf("IssuerCN = %q, want %q", status.IssuerCN, tt.issuer)
|
||||
}
|
||||
if status.Expiry.IsZero() {
|
||||
t.Error("Expiry should be parsed")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user