2023-02-26 23:53:57 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2023-02-26 23:53:57 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setupTest(t *testing.T, filename string) *Client {
|
|
|
|
t.Helper()
|
2023-05-05 07:49:38 +00:00
|
|
|
|
2023-02-26 23:53:57 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
t.Cleanup(server.Close)
|
|
|
|
|
|
|
|
serverURL, err := url.Parse(server.URL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
client := NewClient(serverURL, "user", "secret")
|
|
|
|
client.HTTPClient = server.Client()
|
|
|
|
|
|
|
|
mux.HandleFunc("/enterprise/control/agent.php", func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != http.MethodPost {
|
|
|
|
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
login := req.Header.Get("Http_auth_login")
|
|
|
|
if login != "user" {
|
|
|
|
http.Error(rw, fmt.Sprintf("invalid login: %s", login), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
password := req.Header.Get("Http_auth_passwd")
|
|
|
|
if password != "secret" {
|
|
|
|
http.Error(rw, fmt.Sprintf("invalid password: %s", password), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Open(filepath.Join("fixtures", filename))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() { _ = file.Close() }()
|
|
|
|
|
|
|
|
_, err = io.Copy(rw, file)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_GetSite(t *testing.T) {
|
|
|
|
client := setupTest(t, "get-site.xml")
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
siteID, err := client.GetSite(context.Background(), "example.com")
|
2023-02-26 23:53:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 82, siteID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_GetSite_error(t *testing.T) {
|
|
|
|
client := setupTest(t, "get-site-error.xml")
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
siteID, err := client.GetSite(context.Background(), "example.com")
|
2023-02-26 23:53:57 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 0, siteID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_GetSite_system_error(t *testing.T) {
|
|
|
|
client := setupTest(t, "global-error.xml")
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
siteID, err := client.GetSite(context.Background(), "example.com")
|
2023-02-26 23:53:57 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 0, siteID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_AddRecord(t *testing.T) {
|
|
|
|
client := setupTest(t, "add-record.xml")
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
recordID, err := client.AddRecord(context.Background(), 123, "_acme-challenge.example.com", "txtTXTtxt")
|
2023-02-26 23:53:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 4537, recordID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_AddRecord_error(t *testing.T) {
|
|
|
|
client := setupTest(t, "add-record-error.xml")
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
recordID, err := client.AddRecord(context.Background(), 123, "_acme-challenge.example.com", "txtTXTtxt")
|
|
|
|
require.ErrorAs(t, err, new(RecResult))
|
2023-02-26 23:53:57 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 0, recordID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_AddRecord_system_error(t *testing.T) {
|
|
|
|
client := setupTest(t, "global-error.xml")
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
recordID, err := client.AddRecord(context.Background(), 123, "_acme-challenge.example.com", "txtTXTtxt")
|
|
|
|
require.ErrorAs(t, err, new(*System))
|
2023-02-26 23:53:57 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 0, recordID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_DeleteRecord(t *testing.T) {
|
|
|
|
client := setupTest(t, "delete-record.xml")
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
recordID, err := client.DeleteRecord(context.Background(), 4537)
|
2023-02-26 23:53:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 4537, recordID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_DeleteRecord_error(t *testing.T) {
|
|
|
|
client := setupTest(t, "delete-record-error.xml")
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
recordID, err := client.DeleteRecord(context.Background(), 4537)
|
|
|
|
require.ErrorAs(t, err, new(RecResult))
|
2023-02-26 23:53:57 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 0, recordID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_DeleteRecord_system_error(t *testing.T) {
|
|
|
|
client := setupTest(t, "global-error.xml")
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
recordID, err := client.DeleteRecord(context.Background(), 4537)
|
|
|
|
require.ErrorAs(t, err, new(*System))
|
2023-02-26 23:53:57 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 0, recordID)
|
|
|
|
}
|