1
0
Fork 0
mirror of https://code.forgejo.org/actions/ovh-dns-update synced 2025-03-15 06:46:59 +01:00
ovh-dns-update/main_test.go
2023-08-18 16:20:01 +02:00

79 lines
2.2 KiB
Go

// SPDX-FileCopyrightText: 2023 Olivier Charvin
// SPDX-License-Identifier: CC0-1.0
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/sethvargo/go-githubactions"
)
func TestRun(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
default:
msg := fmt.Sprintf("unexpected request on %s", r.URL.Path)
http.Error(w, msg, http.StatusBadRequest)
t.Error(msg)
case "/auth/time":
case "/domain/zone/FAKEVALUE-INPUT_DOMAIN/record/FAKEVALUE-INPUT_RECORD-ID":
buf, err := io.ReadAll(r.Body)
if err != nil {
msg := fmt.Sprintf("could not read request body: %v", err)
http.Error(w, msg, http.StatusBadRequest)
t.Error(msg)
}
if string(buf) != `{"subDomain":"FAKEVALUE-INPUT_SUBDOMAIN","target":"\"FAKEVALUE-INPUT_VALUE\""}` {
msg := fmt.Sprintf("unexpected body: %s", string(buf))
http.Error(w, msg, http.StatusBadRequest)
t.Error(msg)
}
}
}))
t.Cleanup(s.Close)
action := githubactions.New(githubactions.WithGetenv(func(key string) string {
switch key {
case "INPUT_OVH-ENDPOINT":
return "http://" + s.Listener.Addr().String()
}
return "FAKEVALUE-" + key
}))
err := run(action)
if err != nil {
t.Fatal(err)
}
// for action testing (see .forgejo/workflows/pr.yml)
if os.Getenv("ACTION_TESTING") == "1" {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
default:
msg := fmt.Sprintf("unexpected request on %s", r.URL.Path)
http.Error(w, msg, http.StatusBadRequest)
t.Error(msg)
case "/auth/time":
case "/domain/zone/example.org/record/12345":
buf, err := io.ReadAll(r.Body)
if err != nil {
msg := fmt.Sprintf("could not read request body: %v", err)
http.Error(w, msg, http.StatusBadRequest)
t.Error(msg)
}
if string(buf) != `{"subDomain":"_release","target":"\"v=1.42\""}` {
msg := fmt.Sprintf("unexpected body: %s", string(buf))
http.Error(w, msg, http.StatusBadRequest)
t.Error(msg)
}
}
}))
t.Cleanup(s.Close)
githubactions.SetOutput("OVH_ENDPOINT", "http://"+s.Listener.Addr().String())
select {}
}
}