-
Notifications
You must be signed in to change notification settings - Fork 4
/
ionos_test.go
81 lines (72 loc) · 2.1 KB
/
ionos_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// some unit/explanatory tests for the IONOS DNS plugin
// (c) copyright 2021 by Jan Delgado
package ionos
import (
"fmt"
"strings"
"testing"
caddy "github.com/caddyserver/caddy/v2"
caddyfile "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/libdns/ionos"
)
func TestUnmarshalCaddyFileExtractsApiToken(t *testing.T) {
tests := []string{
"ionos token { }",
`ionos {
api_token token
}`}
for i, tc := range tests {
t.Run(fmt.Sprintf("test case %d", i), func(t *testing.T) {
// given
dispenser := caddyfile.NewTestDispenser(tc)
p := Provider{&ionos.Provider{}}
// when
err := p.UnmarshalCaddyfile(dispenser)
// then
if err != nil {
t.Errorf("UnmarshalCaddyfile failed with %v", err)
return
}
expected := "token"
actual := p.Provider.AuthAPIToken
if expected != actual {
t.Errorf("Expected AuthAPIToken to be '%s' but got '%s'", expected, actual)
}
})
}
}
func TestUnmarshalCaddyFileReportsErrorConditions(t *testing.T) {
tests := []struct{ test, expected string }{
{"ionos token invalid", "wrong argument count"},
{"ionos { }", "missing api token"},
{`ionos token { api_token token }`, "api token already set"},
{`ionos { api_token token invalid }`, "wrong argument count"},
{`ionos token { invalid token }`, "unrecognized subdirective 'invalid'"},
}
for i, tc := range tests {
t.Run(fmt.Sprintf("test case %d", i), func(t *testing.T) {
// given
dispenser := caddyfile.NewTestDispenser(tc.test)
p := Provider{&ionos.Provider{}}
// when
err := p.UnmarshalCaddyfile(dispenser)
// then
if err == nil || !strings.Contains(strings.ToLower(err.Error()), tc.expected) {
t.Errorf("expected error with '%s' but got '%s'", tc.expected, err.Error())
}
})
}
}
func TestProvisionTransformsAPIToken(t *testing.T) {
// given
expected := "{value}"
p := Provider{&ionos.Provider{}}
p.Provider.AuthAPIToken = "\\{value\\}"
// when
_ = p.Provision(caddy.Context{})
// then
actual := p.Provider.AuthAPIToken
if expected != actual {
t.Errorf("expected AuthAPIToken to be %s but got %s", expected, actual)
}
}