feat: add posibility to use token file in hetzner config
This commit is contained in:
@@ -68,24 +68,61 @@ func TestUpdateRecordZoneNotFound(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateRecordRecordNotFound(t *testing.T) {
|
||||
func TestUpdateRecordRecordNotFoundCreates(t *testing.T) {
|
||||
domain := "new.example.com"
|
||||
ip := "1.1.1.1"
|
||||
zoneName := "example.com"
|
||||
zoneID := "zone-abc"
|
||||
postCalled := false
|
||||
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/zones":
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"zones": []map[string]string{{"id": "z", "name": "example.com"}},
|
||||
})
|
||||
case "/records":
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{"records": []map[string]string{}})
|
||||
switch {
|
||||
case r.Method == http.MethodGet && r.URL.Path == "/zones":
|
||||
// Find zone
|
||||
resp := map[string]interface{}{
|
||||
"zones": []map[string]string{{"id": zoneID, "name": zoneName}},
|
||||
}
|
||||
json.NewEncoder(w).Encode(resp)
|
||||
case r.Method == http.MethodGet && r.URL.Path == "/records":
|
||||
// Find no records
|
||||
resp := map[string]interface{}{
|
||||
"records": []map[string]string{},
|
||||
}
|
||||
json.NewEncoder(w).Encode(resp)
|
||||
case r.Method == http.MethodPost && r.URL.Path == "/records":
|
||||
// Expect creation
|
||||
postCalled = true
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
var payload map[string]interface{} // Use interface{} for mixed types (TTL is int)
|
||||
json.Unmarshal(body, &payload)
|
||||
if payload["name"] != "new" { // Name should be the subdomain part
|
||||
t.Errorf("expected create name 'new', got %s", payload["name"])
|
||||
}
|
||||
if payload["value"] != ip {
|
||||
t.Errorf("expected create value %s, got %s", ip, payload["value"])
|
||||
}
|
||||
if payload["type"] != "A" {
|
||||
t.Errorf("expected create type 'A', got %s", payload["type"])
|
||||
}
|
||||
if payload["zone_id"] != zoneID {
|
||||
t.Errorf("expected create zone_id %s, got %s", zoneID, payload["zone_id"])
|
||||
}
|
||||
// Respond with created record details (optional, but good practice)
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{"record": payload})
|
||||
default:
|
||||
http.Error(w, "not found", http.StatusNotFound)
|
||||
t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
|
||||
http.Error(w, "unexpected request", http.StatusInternalServerError)
|
||||
}
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
provider := hetzner.NewProviderWithURL("token", ts.URL)
|
||||
err := provider.UpdateRecord(context.Background(), "missing.example.com", "1.1.1.1")
|
||||
if err == nil || !strings.Contains(err.Error(), "record missing.example.com not found") {
|
||||
t.Fatalf("expected record not found error, got %v", err)
|
||||
err := provider.UpdateRecord(context.Background(), domain, ip)
|
||||
if err != nil {
|
||||
t.Fatalf("expected successful creation, but got error: %v", err)
|
||||
}
|
||||
if !postCalled {
|
||||
t.Fatalf("expected POST /records to be called for creation, but it wasn't")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user