Skip to content

Commit

Permalink
adding tests case for ApplyTemplateOrReplace
Browse files Browse the repository at this point in the history
Signed-off-by: Javan lacerda <[email protected]>
  • Loading branch information
javanlacerda committed Jul 5, 2024
1 parent 478c37a commit 7455b87
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
6 changes: 3 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ type FulcioConfig struct {
type IssuerMetadata struct {
// Defaults contains key-value pairs that can be used for filling the templates from ExtensionTemplates
// If a key cannot be found on the token claims, the template will use the defaults
DefaultTemplateValues map[string]string
DefaultTemplateValues map[string]string `json:"DefaultTemplateValues,omitempty" yaml:"default-template-values,omitempty"`
// ExtensionTemplates contains a mapping between certificate extension and token claim
// Provide either strings following https://pkg.go.dev/text/template syntax,
// e.g "{{ .url }}/{{ .repository }}"
// or non-templated strings with token claim keys to be replaced,
// e.g "job_workflow_sha"
ExtensionTemplates certificate.Extensions
ExtensionTemplates certificate.Extensions `json:"ExtensionTemplates,omitempty" yaml:"extension-templates,omitempty"`
// Template for the Subject Alternative Name extension
// It's typically the same value as Build Signer URI
SubjectAlternativeNameTemplate string
SubjectAlternativeNameTemplate string `json:"SubjectAlternativeNameTemplate,omitempty" yaml:"subject-alternative-name-template,omitempty"`
}

type OIDCIssuer struct {
Expand Down
30 changes: 27 additions & 3 deletions pkg/identity/ciprovider/principal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,31 @@ func TestApplyTemplateOrReplace(t *testing.T) {
ExpectedResult: "https://github.com/sigstore/fulcio/actions/runs/42/attempts/1",
ExpectErr: false,
},
// Add more tests for edge cases
`Empty template`: {
Template: "{{}}",
ExpectedResult: "",
ExpectErr: true,
},
`Missing key for template`: {
Template: "{{ .foo }}",
ExpectedResult: "",
ExpectErr: true,
},
`Empty string`: {
Template: "",
ExpectedResult: "",
ExpectErr: true,
},
`Replaceable string`: {
Template: "job_workflow_ref",
ExpectedResult: "sigstore/fulcio/.github/workflows/foo.yaml@refs/heads/main",
ExpectErr: false,
},
`Missing string`: {
Template: "bar",
ExpectedResult: "",
ExpectErr: true,
},
}

for name, test := range tests {
Expand All @@ -254,9 +278,9 @@ func TestApplyTemplateOrReplace(t *testing.T) {
t.Errorf("expected result don't matches: Expected %s, received: %s",
test.ExpectedResult, res)
}
if (err == nil) == test.ExpectErr {
if (err != nil) != test.ExpectErr {
t.Errorf("should raise an error don't matches: Expected %v, received: %v",
test.ExpectErr, err)
test.ExpectErr, err != nil)
}
})
}
Expand Down

0 comments on commit 7455b87

Please sign in to comment.