2019-03-11 15:54:35 +00:00
|
|
|
package registration // import "github.com/xenolf/lego/registration"
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/xenolf/lego/acme"
|
|
|
|
"github.com/xenolf/lego/acme/api"
|
|
|
|
"github.com/xenolf/lego/platform/tester"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRegistrar_ResolveAccountByKey(t *testing.T) {
|
|
|
|
mux, apiURL, tearDown := tester.SetupFakeAPI()
|
|
|
|
defer tearDown()
|
|
|
|
|
2019-02-06 17:15:53 +00:00
|
|
|
mux.HandleFunc("/account", func(w http.ResponseWriter, _ *http.Request) {
|
2018-12-06 21:50:17 +00:00
|
|
|
w.Header().Set("Location", apiURL+"/account_recovery")
|
|
|
|
_, err := w.Write([]byte("{}"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-02-06 17:15:53 +00:00
|
|
|
mux.HandleFunc("/account_recovery", func(w http.ResponseWriter, _ *http.Request) {
|
2018-12-06 21:50:17 +00:00
|
|
|
err := tester.WriteJSONResponse(w, acme.Account{
|
|
|
|
Status: "valid",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
key, err := rsa.GenerateKey(rand.Reader, 512)
|
|
|
|
require.NoError(t, err, "Could not generate test key")
|
|
|
|
|
|
|
|
user := mockUser{
|
|
|
|
email: "test@test.com",
|
|
|
|
regres: &Resource{},
|
|
|
|
privatekey: key,
|
|
|
|
}
|
|
|
|
|
|
|
|
core, err := api.New(http.DefaultClient, "lego-test", apiURL+"/dir", "", key)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
registrar := NewRegistrar(core, user)
|
|
|
|
|
|
|
|
res, err := registrar.ResolveAccountByKey()
|
|
|
|
require.NoError(t, err, "Unexpected error resolving account by key")
|
|
|
|
|
|
|
|
assert.Equal(t, "valid", res.Body.Status, "Unexpected account status")
|
|
|
|
}
|