1
0
mirror of https://github.com/go-acme/lego.git synced 2024-12-18 07:24:59 +02:00
lego/providers/dns/dreamhost/dreamhost_test.go
Ludovic Fernandez 42941ccea6
Refactor the core of the lib (#700)
- Packages
- Isolate code used by the CLI into the package `cmd`
- (experimental) Add e2e tests for HTTP01, TLS-ALPN-01 and DNS-01, use [Pebble](https://github.com/letsencrypt/pebble) and [challtestsrv](https://github.com/letsencrypt/boulder/tree/master/test/challtestsrv) 
- Support non-ascii domain name (punnycode)
- Check all challenges in a predictable order
- No more global exported variables
- Archive revoked certificates
- Fixes revocation for subdomains and non-ascii domains
- Disable pending authorizations
- use pointer for RemoteError/ProblemDetails
- Poll authz URL instead of challenge URL
- The ability for a DNS provider to solve the challenge sequentially
- Check all nameservers in a predictable order
- Option to disable the complete propagation Requirement
- CLI, support for renew with CSR
- CLI, add SAN on renew
- Add command to list certificates.
- Logs every iteration of waiting for the propagation
- update DNSimple client
- update github.com/miekg/dns
2018-12-06 22:50:17 +01:00

197 lines
4.7 KiB
Go

package dreamhost
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var envTest = tester.NewEnvTest("DREAMHOST_API_KEY").
WithDomain("DREAMHOST_TEST_DOMAIN")
const (
fakeAPIKey = "asdf1234"
fakeChallengeToken = "foobar"
fakeKeyAuth = "w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI"
)
func setupTest() (*DNSProvider, *http.ServeMux, func()) {
handler := http.NewServeMux()
server := httptest.NewServer(handler)
config := NewDefaultConfig()
config.APIKey = fakeAPIKey
config.BaseURL = server.URL
provider, err := NewDNSProviderConfig(config)
if err != nil {
panic(err)
}
return provider, handler, server.Close
}
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
desc string
envVars map[string]string
expected string
}{
{
desc: "success",
envVars: map[string]string{
"DREAMHOST_API_KEY": "123",
},
},
{
desc: "missing API key",
envVars: map[string]string{
"DREAMHOST_API_KEY": "",
},
expected: "dreamhost: some credentials information are missing: DREAMHOST_API_KEY",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
if len(test.expected) == 0 {
assert.NoError(t, err)
assert.NotNil(t, p)
} else {
require.EqualError(t, err, test.expected)
}
})
}
}
func TestNewDNSProviderConfig(t *testing.T) {
testCases := []struct {
desc string
apiKey string
expected string
}{
{
desc: "success",
apiKey: "123",
},
{
desc: "missing credentials",
expected: "dreamhost: credentials missing",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
config := NewDefaultConfig()
config.APIKey = test.apiKey
p, err := NewDNSProviderConfig(config)
if len(test.expected) == 0 {
assert.NoError(t, err)
assert.NotNil(t, p)
} else {
require.EqualError(t, err, test.expected)
}
})
}
}
func TestDNSProvider_Present(t *testing.T) {
provider, mux, tearDown := setupTest()
defer tearDown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "method")
q := r.URL.Query()
assert.Equal(t, q.Get("key"), fakeAPIKey)
assert.Equal(t, q.Get("cmd"), "dns-add_record")
assert.Equal(t, q.Get("format"), "json")
assert.Equal(t, q.Get("record"), "_acme-challenge.example.com")
assert.Equal(t, q.Get("value"), fakeKeyAuth)
assert.Equal(t, q.Get("comment"), "Managed+By+lego")
_, err := fmt.Fprintf(w, `{"data":"record_added","result":"success"}`)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
err := provider.Present("example.com", "", fakeChallengeToken)
require.NoError(t, err)
}
func TestDNSProvider_PresentFailed(t *testing.T) {
provider, mux, tearDown := setupTest()
defer tearDown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "method")
_, err := fmt.Fprintf(w, `{"data":"record_already_exists_remove_first","result":"error"}`)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
err := provider.Present("example.com", "", fakeChallengeToken)
require.EqualError(t, err, "dreamhost: add TXT record failed: record_already_exists_remove_first")
}
func TestDNSProvider_Cleanup(t *testing.T) {
provider, mux, tearDown := setupTest()
defer tearDown()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "method")
q := r.URL.Query()
assert.Equal(t, q.Get("key"), fakeAPIKey, "key mismatch")
assert.Equal(t, q.Get("cmd"), "dns-remove_record", "cmd mismatch")
assert.Equal(t, q.Get("format"), "json")
assert.Equal(t, q.Get("record"), "_acme-challenge.example.com")
assert.Equal(t, q.Get("value"), fakeKeyAuth, "value mismatch")
assert.Equal(t, q.Get("comment"), "Managed+By+lego")
_, err := fmt.Fprintf(w, `{"data":"record_removed","result":"success"}`)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
err := provider.CleanUp("example.com", "", fakeChallengeToken)
require.NoError(t, err, "failed to remove TXT record")
}
func TestLivePresentAndCleanUp(t *testing.T) {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
time.Sleep(1 * time.Second)
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}