|
1 | 1 | package azion |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "os" |
| 9 | + "path/filepath" |
4 | 10 | "testing" |
5 | 11 |
|
| 12 | + "github.com/aziontech/azionapi-go-sdk/idns" |
6 | 13 | "github.com/go-acme/lego/v4/platform/tester" |
| 14 | + "github.com/stretchr/testify/assert" |
7 | 15 | "github.com/stretchr/testify/require" |
8 | 16 | ) |
9 | 17 |
|
@@ -113,3 +121,129 @@ func TestLiveCleanUp(t *testing.T) { |
113 | 121 | err = provider.CleanUp(envTest.GetDomain(), "", "123d==") |
114 | 122 | require.NoError(t, err) |
115 | 123 | } |
| 124 | + |
| 125 | +func TestDNSProvider_findZone(t *testing.T) { |
| 126 | + provider, mux := setupTest(t) |
| 127 | + mux.HandleFunc("GET /intelligent_dns", writeFixtureHandler("zones.json")) |
| 128 | + |
| 129 | + testCases := []struct { |
| 130 | + desc string |
| 131 | + fqdn string |
| 132 | + expected *idns.Zone |
| 133 | + }{ |
| 134 | + { |
| 135 | + desc: "apex", |
| 136 | + fqdn: "example.com.", |
| 137 | + expected: &idns.Zone{ |
| 138 | + Id: idns.PtrInt32(1), |
| 139 | + Domain: idns.PtrString("example.com"), |
| 140 | + }, |
| 141 | + }, |
| 142 | + { |
| 143 | + desc: "sub domain", |
| 144 | + fqdn: "sub.example.com.", |
| 145 | + expected: &idns.Zone{ |
| 146 | + Id: idns.PtrInt32(2), |
| 147 | + Domain: idns.PtrString("sub.example.com"), |
| 148 | + }, |
| 149 | + }, |
| 150 | + { |
| 151 | + desc: "long sub domain", |
| 152 | + fqdn: "_acme-challenge.api.sub.example.com.", |
| 153 | + expected: &idns.Zone{ |
| 154 | + Id: idns.PtrInt32(2), |
| 155 | + Domain: idns.PtrString("sub.example.com"), |
| 156 | + }, |
| 157 | + }, |
| 158 | + { |
| 159 | + desc: "long sub domain, apex", |
| 160 | + fqdn: "_acme-challenge.test.example.com.", |
| 161 | + expected: &idns.Zone{ |
| 162 | + Id: idns.PtrInt32(1), |
| 163 | + Domain: idns.PtrString("example.com"), |
| 164 | + }, |
| 165 | + }, |
| 166 | + } |
| 167 | + |
| 168 | + for _, test := range testCases { |
| 169 | + t.Run(test.desc, func(t *testing.T) { |
| 170 | + zone, err := provider.findZone(context.Background(), test.fqdn) |
| 171 | + require.NoError(t, err) |
| 172 | + |
| 173 | + assert.Equal(t, test.expected, zone) |
| 174 | + }) |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +func TestDNSProvider_findZone_error(t *testing.T) { |
| 179 | + testCases := []struct { |
| 180 | + desc string |
| 181 | + fqdn string |
| 182 | + response string |
| 183 | + expected string |
| 184 | + }{ |
| 185 | + { |
| 186 | + desc: "no parent zone found", |
| 187 | + fqdn: "_acme-challenge.example.org.", |
| 188 | + response: "zones.json", |
| 189 | + expected: `zone not found (fqdn: "_acme-challenge.example.org.")`, |
| 190 | + }, |
| 191 | + { |
| 192 | + desc: "empty zones list", |
| 193 | + fqdn: "example.com.", |
| 194 | + response: "zones_empty.json", |
| 195 | + expected: `zone not found (fqdn: "example.com.")`, |
| 196 | + }, |
| 197 | + } |
| 198 | + |
| 199 | + for _, test := range testCases { |
| 200 | + t.Run(test.desc, func(t *testing.T) { |
| 201 | + provider, mux := setupTest(t) |
| 202 | + mux.HandleFunc("GET /intelligent_dns", writeFixtureHandler(test.response)) |
| 203 | + |
| 204 | + zone, err := provider.findZone(context.Background(), test.fqdn) |
| 205 | + require.EqualError(t, err, test.expected) |
| 206 | + |
| 207 | + assert.Nil(t, zone) |
| 208 | + }) |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +func setupTest(t *testing.T) (*DNSProvider, *http.ServeMux) { |
| 213 | + t.Helper() |
| 214 | + |
| 215 | + mux := http.NewServeMux() |
| 216 | + server := httptest.NewServer(mux) |
| 217 | + |
| 218 | + config := NewDefaultConfig() |
| 219 | + config.PersonalToken = "secret" |
| 220 | + |
| 221 | + provider, err := NewDNSProviderConfig(config) |
| 222 | + require.NoError(t, err) |
| 223 | + |
| 224 | + clientConfig := provider.client.GetConfig() |
| 225 | + clientConfig.HTTPClient = server.Client() |
| 226 | + clientConfig.Servers = idns.ServerConfigurations{ |
| 227 | + { |
| 228 | + URL: server.URL, |
| 229 | + Description: "Production", |
| 230 | + }, |
| 231 | + } |
| 232 | + |
| 233 | + return provider, mux |
| 234 | +} |
| 235 | + |
| 236 | +func writeFixtureHandler(filename string) http.HandlerFunc { |
| 237 | + return func(rw http.ResponseWriter, req *http.Request) { |
| 238 | + rw.Header().Set("Content-Type", "application/json") |
| 239 | + |
| 240 | + file, err := os.Open(filepath.Join("fixtures", filename)) |
| 241 | + if err != nil { |
| 242 | + http.Error(rw, err.Error(), http.StatusInternalServerError) |
| 243 | + return |
| 244 | + } |
| 245 | + defer func() { _ = file.Close() }() |
| 246 | + |
| 247 | + _, _ = io.Copy(rw, file) |
| 248 | + } |
| 249 | +} |
0 commit comments