Skip to content

Commit

Permalink
review: 3
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Oct 11, 2023
1 parent 9394dd5 commit 7061dd5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 152 deletions.
117 changes: 1 addition & 116 deletions providers/dns/liquidweb/liquidweb_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,123 +29,8 @@ func setupTest(t *testing.T) (*DNSProvider, *http.ServeMux) {
return provider, mux
}

// func TestDNSProvider_Present(t *testing.T) {
// provider, mux := setupTest(t)
//
// mux.HandleFunc("/v1/Network/DNS/Record/create", func(w http.ResponseWriter, r *http.Request) {
// assert.Equal(t, http.MethodPost, r.Method)
//
// username, password, ok := r.BasicAuth()
// assert.Equal(t, "blars", username)
// assert.Equal(t, "tacoman", password)
// assert.True(t, ok)
//
// reqBody, err := io.ReadAll(r.Body)
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
//
// expectedReqBody := `
// {
// "params": {
// "name": "_acme-challenge.tacoman.com",
// "rdata": "\"47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU\"",
// "ttl": 300,
// "type": "TXT",
// "zone": "tacoman.com"
// }
// }`
// assert.JSONEq(t, expectedReqBody, string(reqBody))
//
// w.WriteHeader(http.StatusOK)
// _, err = fmt.Fprintf(w, `{
// "type": "TXT",
// "name": "_acme-challenge.tacoman.com",
// "rdata": "\"47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU\"",
// "ttl": 300,
// "id": 1234567,
// "prio": null
// }`)
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// })
//
// err := provider.Present("tacoman.com", "", "")
// require.NoError(t, err)
// }
//
// func TestDNSProvider_CleanUp(t *testing.T) {
// provider, mux := setupTest(t)
//
// mux.HandleFunc("/v1/Network/DNS/Record/delete", func(w http.ResponseWriter, r *http.Request) {
// assert.Equal(t, http.MethodPost, r.Method)
//
// username, password, ok := r.BasicAuth()
// assert.Equal(t, "blars", username)
// assert.Equal(t, "tacoman", password)
// assert.True(t, ok)
//
// _, err := fmt.Fprintf(w, `{"deleted": "123"}`)
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// })
//
// provider.recordIDs["123"] = 1234567
//
// err := provider.CleanUp("tacoman.com.", "123", "")
// require.NoError(t, err, "fail to remove TXT record")
// }

func TestDNSProvider_Present(t *testing.T) {
envTest.Apply(map[string]string{
EnvUsername: "blars",
EnvPassword: "tacoman",
EnvURL: mockAPIServer(t),
EnvZone: "tacoman.com", // this needs to be removed from test?
})

defer envTest.ClearEnv()

provider, err := NewDNSProvider()
require.NoError(t, err)

err = provider.Present("tacoman.com", "", "")
require.NoError(t, err)
}

func TestDNSProvider_CleanUp(t *testing.T) {
envTest.Apply(map[string]string{
EnvUsername: "blars",
EnvPassword: "tacoman",
EnvURL: mockAPIServer(t, network.DNSRecord{
Name: "_acme-challenge.tacoman.com",
RData: "123d==",
Type: "TXT",
TTL: 300,
ID: 1234567,
ZoneID: 42,
}),
EnvZone: "tacoman.com", // this needs to be removed from test?
})

defer envTest.ClearEnv()

provider, err := NewDNSProvider()
require.NoError(t, err)

provider.recordIDs["123d=="] = 1234567

err = provider.CleanUp("tacoman.com.", "123d==", "")
require.NoError(t, err, "fail to remove TXT record")
}

// FIXME

Check failure on line 32 in providers/dns/liquidweb/liquidweb_integration_test.go

View workflow job for this annotation

GitHub Actions / Main Process

todoCommentWithoutDetail: may want to add detail/assignee to this TODO/FIXME/BUG comment (gocritic)
func TestIntegration(t *testing.T) {
func TestFoo(t *testing.T) {
testCases := []struct {
desc string
envVars map[string]string
Expand Down
52 changes: 48 additions & 4 deletions providers/dns/liquidweb/liquidweb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/go-acme/lego/v4/platform/tester"
"github.com/liquidweb/liquidweb-go/network"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -143,9 +144,54 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
}

func TestLivePresent(t *testing.T) {
defer envTest.ClearEnv()
func TestDNSProvider_Present(t *testing.T) {
serverURL := mockAPIServer(t)

defer envTest.RestoreEnv()
envTest.ClearEnv()

envTest.Apply(map[string]string{
EnvUsername: "blars",
EnvPassword: "tacoman",
EnvURL: serverURL,
})

provider, err := NewDNSProvider()
require.NoError(t, err)

err = provider.Present("tacoman.com", "", "")
require.NoError(t, err)
}

func TestDNSProvider_CleanUp(t *testing.T) {
serverURL := mockAPIServer(t, network.DNSRecord{
Name: "_acme-challenge.tacoman.com",
RData: "123d==",
Type: "TXT",
TTL: 300,
ID: 1234567,
ZoneID: 42,
})

defer envTest.RestoreEnv()
envTest.ClearEnv()

envTest.Apply(map[string]string{
EnvUsername: "blars",
EnvPassword: "tacoman",
EnvURL: serverURL,
})

provider, err := NewDNSProvider()
require.NoError(t, err)

provider.recordIDs["123d=="] = 1234567

err = provider.CleanUp("tacoman.com.", "123d==", "")
require.NoError(t, err, "fail to remove TXT record")
}

func TestLivePresent(t *testing.T) {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
Expand All @@ -160,8 +206,6 @@ func TestLivePresent(t *testing.T) {
}

func TestLiveCleanUp(t *testing.T) {
defer envTest.ClearEnv()

if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
Expand Down
65 changes: 33 additions & 32 deletions providers/dns/liquidweb/servermock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,57 @@ import (
"github.com/liquidweb/liquidweb-go/types"
)

func requireBasicAuth(child http.Handler) func(http.ResponseWriter, *http.Request) {
func mockAPIServer(t *testing.T, initRecs ...network.DNSRecord) string {
t.Helper()

recs := make(map[int]network.DNSRecord)

for _, rec := range initRecs {
recs[int(rec.ID)] = rec
}

mux := http.NewServeMux()
mux.Handle("/v1/Network/DNS/Record/delete", mockAPIDelete(recs))
mux.Handle("/v1/Network/DNS/Record/create", mockAPICreate(recs))
mux.Handle("/v1/Network/DNS/Zone/list", mockAPIListZones())
mux.Handle("/bleed/Network/DNS/Record/delete", mockAPIDelete(recs))
mux.Handle("/bleed/Network/DNS/Record/create", mockAPICreate(recs))
mux.Handle("/bleed/Network/DNS/Zone/list", mockAPIListZones())

server := httptest.NewServer(requireBasicAuth(requireJSON(mux)))
t.Cleanup(server.Close)

return server.URL
}

func requireBasicAuth(next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
if ok && username == "blars" && password == "tacoman" {
child.ServeHTTP(w, r)
next.ServeHTTP(w, r)
return
}

http.Error(w, "invalid auth", http.StatusForbidden)
}
}

func requireJSON(child http.Handler) func(http.ResponseWriter, *http.Request) {
func requireJSON(next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
buf := &bytes.Buffer{}

_, err := buf.ReadFrom(r.Body)
if err != nil {
http.Error(w, "malformed request - json required", http.StatusBadRequest)
return
}

r.Body = io.NopCloser(buf)
child.ServeHTTP(w, r)
next.ServeHTTP(w, r)
}
}

func mockAPICreate(recs map[int]network.DNSRecord) func(http.ResponseWriter, *http.Request) {
func mockAPICreate(recs map[int]network.DNSRecord) http.HandlerFunc {
_, mockAPIServerZones := makeMockZones()
return func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
Expand Down Expand Up @@ -73,7 +99,7 @@ func mockAPICreate(recs map[int]network.DNSRecord) func(http.ResponseWriter, *ht
}
}

func mockAPIDelete(recs map[int]network.DNSRecord) func(http.ResponseWriter, *http.Request) {
func mockAPIDelete(recs map[int]network.DNSRecord) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
Expand Down Expand Up @@ -107,7 +133,7 @@ func mockAPIDelete(recs map[int]network.DNSRecord) func(http.ResponseWriter, *ht
}
}

func mockAPIListZones() func(http.ResponseWriter, *http.Request) {
func mockAPIListZones() http.HandlerFunc {
mockZones, mockAPIServerZones := makeMockZones()
return func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
Expand Down Expand Up @@ -147,28 +173,3 @@ func mockAPIListZones() func(http.ResponseWriter, *http.Request) {
http.Error(w, "", http.StatusInternalServerError)
}
}

func mockAPIServer(t *testing.T, initRecs ...network.DNSRecord) string {
t.Helper()

recs := make(map[int]network.DNSRecord)

for _, rec := range initRecs {
recs[int(rec.ID)] = rec
}

mux := http.NewServeMux()

mux.HandleFunc("/v1/Network/DNS/Record/delete", mockAPIDelete(recs))
mux.HandleFunc("/v1/Network/DNS/Record/create", mockAPICreate(recs))
mux.HandleFunc("/v1/Network/DNS/Zone/list", mockAPIListZones())
mux.HandleFunc("/bleed/Network/DNS/Record/delete", mockAPIDelete(recs))
mux.HandleFunc("/bleed/Network/DNS/Record/create", mockAPICreate(recs))
mux.HandleFunc("/bleed/Network/DNS/Zone/list", mockAPIListZones())
handler := http.HandlerFunc(requireJSON(mux))
handler = http.HandlerFunc(requireBasicAuth(handler))

server := httptest.NewServer(handler)
t.Cleanup(server.Close)
return server.URL
}

0 comments on commit 7061dd5

Please sign in to comment.