1package hyperone
2
3import (
4	"testing"
5
6	"github.com/go-acme/lego/v4/platform/tester"
7	"github.com/stretchr/testify/require"
8)
9
10const envDomain = envNamespace + "DOMAIN"
11
12var envTest = tester.NewEnvTest(EnvPassportLocation, EnvAPIUrl, EnvLocationID).
13	WithDomain(envDomain)
14
15func TestNewDNSProvider(t *testing.T) {
16	testCases := []struct {
17		desc     string
18		envVars  map[string]string
19		expected string
20	}{
21		{
22			desc: "success",
23			envVars: map[string]string{
24				EnvPassportLocation: "./internal/fixtures/validPassport.json",
25				EnvAPIUrl:           "",
26				EnvLocationID:       "",
27			},
28		},
29		{
30			desc: "invalid passport",
31			envVars: map[string]string{
32				EnvPassportLocation: "./internal/fixtures/invalidPassport.json",
33				EnvAPIUrl:           "",
34				EnvLocationID:       "",
35			},
36			expected: "hyperone: passport file validation failed: private key is missing",
37		},
38		{
39			desc: "non existing passport",
40			envVars: map[string]string{
41				EnvPassportLocation: "./internal/fixtures/non-existing.json",
42				EnvAPIUrl:           "",
43				EnvLocationID:       "",
44			},
45			expected: "hyperone: failed to open passport file: open ./internal/fixtures/non-existing.json:",
46		},
47	}
48
49	for _, test := range testCases {
50		t.Run(test.desc, func(t *testing.T) {
51			defer envTest.RestoreEnv()
52			envTest.ClearEnv()
53
54			envTest.Apply(test.envVars)
55
56			p, err := NewDNSProvider()
57
58			if test.expected == "" {
59				require.NoError(t, err)
60				require.NotNil(t, p)
61				require.NotNil(t, p.config)
62			} else {
63				require.Error(t, err)
64				require.Contains(t, err.Error(), test.expected)
65			}
66		})
67	}
68}
69
70func TestNewDNSProviderConfig(t *testing.T) {
71	testCases := []struct {
72		desc             string
73		passportLocation string
74		apiEndpoint      string
75		locationID       string
76		expected         string
77	}{
78		{
79			desc:             "success",
80			passportLocation: "./internal/fixtures/validPassport.json",
81			apiEndpoint:      "",
82			locationID:       "",
83		},
84		{
85			desc:             "invalid passport",
86			passportLocation: "./internal/fixtures/invalidPassport.json",
87			apiEndpoint:      "",
88			locationID:       "",
89			expected:         "hyperone: passport file validation failed: private key is missing",
90		},
91		{
92			desc:             "non existing passport",
93			passportLocation: "./internal/fixtures/non-existing.json",
94			apiEndpoint:      "",
95			locationID:       "",
96			expected:         "hyperone: failed to open passport file: open ./internal/fixtures/non-existing.json:",
97		},
98	}
99
100	for _, test := range testCases {
101		t.Run(test.desc, func(t *testing.T) {
102			config := NewDefaultConfig()
103			config.PassportLocation = test.passportLocation
104			config.APIEndpoint = test.apiEndpoint
105			config.LocationID = test.locationID
106
107			p, err := NewDNSProviderConfig(config)
108
109			if test.expected == "" {
110				require.NoError(t, err)
111				require.NotNil(t, p)
112				require.NotNil(t, p.config)
113			} else {
114				require.Error(t, err)
115				require.Contains(t, err.Error(), test.expected)
116			}
117		})
118	}
119}
120
121func TestLivePresent(t *testing.T) {
122	if !envTest.IsLiveTest() {
123		t.Skip("skipping live test")
124	}
125
126	envTest.RestoreEnv()
127	provider, err := NewDNSProvider()
128	require.NoError(t, err)
129
130	err = provider.Present(envTest.GetDomain(), "", "123d==")
131	require.NoError(t, err)
132}
133
134func TestLiveCleanUp(t *testing.T) {
135	if !envTest.IsLiveTest() {
136		t.Skip("skipping live test")
137	}
138
139	envTest.RestoreEnv()
140	provider, err := NewDNSProvider()
141	require.NoError(t, err)
142
143	err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
144	require.NoError(t, err)
145}
146