chore: test authorization parameters (#25498)

This commit is contained in:
andig 2025-11-23 13:33:13 +01:00 • committed by GitHub
parent 99c5f8cc3f
commit 694816512e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 39 additions and 1 deletions

View file

@ -1,10 +1,14 @@
package templates
import (
"context"
"errors"
"maps"
"slices"
"testing"
"github.com/evcc-io/evcc/plugin/auth"
"github.com/evcc-io/evcc/util"
"go.yaml.in/yaml/v4"
)
@ -34,6 +38,35 @@ func test(t *testing.T, tmpl Template, values map[string]any, cb func(values map
cb(values)
}
func testAuth(other map[string]any) error {
if len(other) == 0 {
return nil
}
var cc struct {
Type string
Params []string
}
if err := util.DecodeOther(other, &cc); err != nil {
return err
}
params := make(map[string]any)
for _, p := range cc.Params {
params[p] = "foo"
}
_, err := auth.NewFromConfig(context.TODO(), cc.Type, params)
// ConfigError indicates invalid parameters in mapstructure decode
if ce := new(util.ConfigError); errors.As(err, &ce) {
return err
}
return nil
}
func TestClass(t *testing.T, class Class, instantiate func(t *testing.T, values map[string]any)) {
t.Parallel()
@ -60,6 +93,11 @@ func TestClass(t *testing.T, class Class, instantiate func(t *testing.T, values
// https://github.com/evcc-io/evcc/pull/10272 - override example IP (192.0.2.2)
values["host"] = "localhost"
// test auth configuration
if err := testAuth(tmpl.TemplateDefinition.Auth); err != nil {
t.Error("authorization:", err)
}
usages := tmpl.Usages()
if len(usages) == 0 {
t.Run(tmpl.Template, func(t *testing.T) {

View file

@ -26,7 +26,7 @@ var acceptable = []string{
"unexpected status: 401",
"discussions/17501", // Tesla
"login failed: code not found", // Polestar
"empty instance type- check for missing usage", // Merces
"empty instance type- check for missing usage", // Mercedes
}
func TestTemplates(t *testing.T) {