Browse Source

Add function for fetching order list from account

master
Isaac 2 years ago
parent
commit
7e790a024a
  1. 14
      account.go
  2. 64
      account_test.go
  3. 7
      acme.go
  4. 40
      utility_test.go

14
account.go

@ -3,6 +3,7 @@ package acme
import (
"crypto"
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
@ -112,3 +113,16 @@ func (c Client) DeactivateAccount(account Account) (Account, error) {
return account, err
}
// FetchOrderList fetches a list of orders from the account url provided in the account Orders field
func (c Client) FetchOrderList(account Account) (OrderList, error) {
orderList := OrderList{}
if account.Orders == "" {
return orderList, errors.New("no order list for account")
}
_, err := c.post(account.Orders, account.URL, account.PrivateKey, "", &orderList, http.StatusOK)
return orderList, err
}

64
account_test.go

@ -3,6 +3,7 @@ package acme
import (
"crypto"
"reflect"
"strings"
"testing"
)
@ -142,3 +143,66 @@ func TestClient_DeactivateAccount(t *testing.T) {
t.Fatalf("expected account deactivated, got: %s", account.Status)
}
}
func TestClient_FetchOrderList(t *testing.T) {
if testClientMeta.Software == clientBoulder {
t.Skip("boulder doesnt support orders list: https://github.com/letsencrypt/boulder/issues/3335")
return
}
tests := []struct {
pre func(acct *Account) bool
post func(*testing.T, Account, OrderList)
expectsError bool
errorStr string
}{
{
pre: func(acct *Account) bool {
acct.Orders = ""
return false
},
expectsError: true,
errorStr: "no order",
},
{
pre: func(acct *Account) bool {
*acct, _, _ = makeOrderFinalised(t, nil)
return true
},
post: func(st *testing.T, account Account, list OrderList) {
if len(list.Orders) != 1 {
st.Fatalf("expected 1 orders, got: %d", len(list.Orders))
}
},
expectsError: false,
},
}
for i, ct := range tests {
acct := makeAccount(t)
if ct.pre != nil {
update := ct.pre(&acct)
if update {
var err error
acct, err = testClient.UpdateAccount(acct)
if err != nil {
panic(err)
}
}
}
list, err := testClient.FetchOrderList(acct)
if ct.expectsError && err == nil {
t.Errorf("order list test %d expected error, got none", i)
}
if !ct.expectsError && err != nil {
t.Errorf("order list test %d expected no error, got: %v", i, err)
}
if err != nil && ct.errorStr != "" && !strings.Contains(err.Error(), ct.errorStr) {
t.Errorf("order list test %d error doesnt contain %q: %s", i, ct.errorStr, err.Error())
}
if ct.post != nil {
ct.post(t, acct, list)
}
}
}

7
acme.go

@ -8,6 +8,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
"time"
@ -218,6 +219,12 @@ func (c Client) post(requestURL, keyID string, privateKey crypto.Signer, payload
return resp, err
}
if _, b := os.LookupEnv("ACME_DEBUG_POST"); b {
fmt.Println()
fmt.Println(string(body))
fmt.Println()
}
if len(body) > 0 && out != nil {
if err := json.Unmarshal(body, out); err != nil {
return resp, fmt.Errorf("acme: error parsing response: %v - %s", err, string(body))

40
utility_test.go

@ -18,8 +18,20 @@ import (
"time"
)
type clientMeta struct {
Software string
RootCert string
Options []OptionFunc
}
const (
clientBoulder = "boulder"
clientPebble = "pebble"
)
var (
testClient Client
testClient Client
testClientMeta clientMeta
)
func init() {
@ -41,20 +53,30 @@ func init() {
return
}
directories := []string{
"https://localhost:14000/dir", // pebble
"https://localhost:4431/directory", // boulder https
"http://localhost:4001/directory", // boulder non-https
directories := map[string]clientMeta{
"https://localhost:14000/dir": {
Software: clientPebble,
RootCert: "https://localhost:15000/roots/0",
Options: []OptionFunc{WithInsecureSkipVerify()},
},
"https://localhost:4431/directory": {
Software: clientBoulder,
Options: []OptionFunc{WithInsecureSkipVerify()},
},
"http://localhost:4001/directory": {
Software: clientBoulder,
},
}
for _, d := range directories {
testClient, err = NewClient(d, WithInsecureSkipVerify())
for k, v := range directories {
testClient, err = NewClient(k, v.Options...)
if err != nil {
log.Printf("error creating client for %s - %v", d, err)
log.Printf("error creating client for %s - %v", k, err)
continue
}
testClientMeta = v
log.Printf("using directory at: %s", d)
log.Printf("using %s directory at: %s", v.Software, k)
return
}

Loading…
Cancel
Save