2019-03-11 18:56:48 +02:00
|
|
|
package registration
|
2018-12-06 23:50:17 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/acme"
|
|
|
|
"github.com/go-acme/lego/v4/acme/api"
|
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2018-12-06 23:50:17 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRegistrar_ResolveAccountByKey(t *testing.T) {
|
2021-11-02 01:52:38 +02:00
|
|
|
mux, apiURL := tester.SetupFakeAPI(t)
|
2018-12-06 23:50:17 +02:00
|
|
|
|
2019-02-06 19:15:53 +02:00
|
|
|
mux.HandleFunc("/account", func(w http.ResponseWriter, _ *http.Request) {
|
2020-05-26 19:06:38 +02:00
|
|
|
w.Header().Set("Location", apiURL+"/account")
|
2018-12-06 23:50:17 +02: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")
|
|
|
|
}
|