| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695 |
- package api
- import (
- "bytes"
- "context"
- "encoding/json"
- "fmt"
- "io"
- "net/http/httptest"
- "net/url"
- "testing"
- "time"
- "github.com/go-chi/chi"
- "github.com/smallstep/assert"
- "github.com/smallstep/certificates/acme"
- "github.com/smallstep/certificates/authority/provisioner"
- "go.step.sm/crypto/jose"
- )
- var (
- defaultDisableRenewal = false
- globalProvisionerClaims = provisioner.Claims{
- MinTLSDur: &provisioner.Duration{Duration: 5 * time.Minute},
- MaxTLSDur: &provisioner.Duration{Duration: 24 * time.Hour},
- DefaultTLSDur: &provisioner.Duration{Duration: 24 * time.Hour},
- DisableRenewal: &defaultDisableRenewal,
- }
- )
- func newProv() acme.Provisioner {
- // Initialize provisioners
- p := &provisioner.ACME{
- Type: "ACME",
- Name: "test@acme-<test>provisioner.com",
- }
- if err := p.Init(provisioner.Config{Claims: globalProvisionerClaims}); err != nil {
- fmt.Printf("%v", err)
- }
- return p
- }
- func TestNewAccountRequest_Validate(t *testing.T) {
- type test struct {
- nar *NewAccountRequest
- err *acme.Error
- }
- var tests = map[string]func(t *testing.T) test{
- "fail/incompatible-input": func(t *testing.T) test {
- return test{
- nar: &NewAccountRequest{
- OnlyReturnExisting: true,
- Contact: []string{"foo", "bar"},
- },
- err: acme.NewError(acme.ErrorMalformedType, "incompatible input; onlyReturnExisting must be alone"),
- }
- },
- "fail/bad-contact": func(t *testing.T) test {
- return test{
- nar: &NewAccountRequest{
- Contact: []string{"foo", ""},
- },
- err: acme.NewError(acme.ErrorMalformedType, "contact cannot be empty string"),
- }
- },
- "ok": func(t *testing.T) test {
- return test{
- nar: &NewAccountRequest{
- Contact: []string{"foo", "bar"},
- },
- }
- },
- "ok/onlyReturnExisting": func(t *testing.T) test {
- return test{
- nar: &NewAccountRequest{
- OnlyReturnExisting: true,
- },
- }
- },
- }
- for name, run := range tests {
- tc := run(t)
- t.Run(name, func(t *testing.T) {
- if err := tc.nar.Validate(); err != nil {
- if assert.NotNil(t, err) {
- ae, ok := err.(*acme.Error)
- assert.True(t, ok)
- assert.HasPrefix(t, ae.Error(), tc.err.Error())
- assert.Equals(t, ae.StatusCode(), tc.err.StatusCode())
- assert.Equals(t, ae.Type, tc.err.Type)
- }
- } else {
- assert.Nil(t, tc.err)
- }
- })
- }
- }
- func TestUpdateAccountRequest_Validate(t *testing.T) {
- type test struct {
- uar *UpdateAccountRequest
- err *acme.Error
- }
- var tests = map[string]func(t *testing.T) test{
- "fail/incompatible-input": func(t *testing.T) test {
- return test{
- uar: &UpdateAccountRequest{
- Contact: []string{"foo", "bar"},
- Status: "foo",
- },
- err: acme.NewError(acme.ErrorMalformedType, "incompatible input; "+
- "contact and status updates are mutually exclusive"),
- }
- },
- "fail/bad-contact": func(t *testing.T) test {
- return test{
- uar: &UpdateAccountRequest{
- Contact: []string{"foo", ""},
- },
- err: acme.NewError(acme.ErrorMalformedType, "contact cannot be empty string"),
- }
- },
- "fail/bad-status": func(t *testing.T) test {
- return test{
- uar: &UpdateAccountRequest{
- Status: "foo",
- },
- err: acme.NewError(acme.ErrorMalformedType, "cannot update account "+
- "status to foo, only deactivated"),
- }
- },
- "ok/contact": func(t *testing.T) test {
- return test{
- uar: &UpdateAccountRequest{
- Contact: []string{"foo", "bar"},
- },
- }
- },
- "ok/status": func(t *testing.T) test {
- return test{
- uar: &UpdateAccountRequest{
- Status: "deactivated",
- },
- }
- },
- "ok/accept-empty": func(t *testing.T) test {
- return test{
- uar: &UpdateAccountRequest{},
- }
- },
- }
- for name, run := range tests {
- tc := run(t)
- t.Run(name, func(t *testing.T) {
- if err := tc.uar.Validate(); err != nil {
- if assert.NotNil(t, err) {
- ae, ok := err.(*acme.Error)
- assert.True(t, ok)
- assert.HasPrefix(t, ae.Error(), tc.err.Error())
- assert.Equals(t, ae.StatusCode(), tc.err.StatusCode())
- assert.Equals(t, ae.Type, tc.err.Type)
- }
- } else {
- assert.Nil(t, tc.err)
- }
- })
- }
- }
- func TestHandler_GetOrdersByAccountID(t *testing.T) {
- accID := "account-id"
- // Request with chi context
- chiCtx := chi.NewRouteContext()
- chiCtx.URLParams.Add("accID", accID)
- prov := newProv()
- provName := url.PathEscape(prov.GetName())
- baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
- u := fmt.Sprintf("http://ca.smallstep.com/acme/%s/account/%s/orders", provName, accID)
- oids := []string{"foo", "bar"}
- oidURLs := []string{
- fmt.Sprintf("%s/acme/%s/order/foo", baseURL.String(), provName),
- fmt.Sprintf("%s/acme/%s/order/bar", baseURL.String(), provName),
- }
- type test struct {
- db acme.DB
- ctx context.Context
- statusCode int
- err *acme.Error
- }
- var tests = map[string]func(t *testing.T) test{
- "fail/no-account": func(t *testing.T) test {
- return test{
- db: &acme.MockDB{},
- ctx: context.Background(),
- statusCode: 400,
- err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
- }
- },
- "fail/nil-account": func(t *testing.T) test {
- return test{
- db: &acme.MockDB{},
- ctx: context.WithValue(context.Background(), accContextKey, nil),
- statusCode: 400,
- err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
- }
- },
- "fail/account-id-mismatch": func(t *testing.T) test {
- acc := &acme.Account{ID: "foo"}
- ctx := context.WithValue(context.Background(), accContextKey, acc)
- ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
- return test{
- db: &acme.MockDB{},
- ctx: ctx,
- statusCode: 401,
- err: acme.NewError(acme.ErrorUnauthorizedType, "account ID does not match url param"),
- }
- },
- "fail/db.GetOrdersByAccountID-error": func(t *testing.T) test {
- acc := &acme.Account{ID: accID}
- ctx := context.WithValue(context.Background(), accContextKey, acc)
- ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
- return test{
- db: &acme.MockDB{
- MockError: acme.NewErrorISE("force"),
- },
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("force"),
- }
- },
- "ok": func(t *testing.T) test {
- acc := &acme.Account{ID: accID}
- ctx := context.WithValue(context.Background(), accContextKey, acc)
- ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
- ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
- ctx = context.WithValue(ctx, provisionerContextKey, prov)
- return test{
- db: &acme.MockDB{
- MockGetOrdersByAccountID: func(ctx context.Context, id string) ([]string, error) {
- assert.Equals(t, id, acc.ID)
- return oids, nil
- },
- },
- ctx: ctx,
- statusCode: 200,
- }
- },
- }
- for name, run := range tests {
- tc := run(t)
- t.Run(name, func(t *testing.T) {
- h := &Handler{db: tc.db, linker: NewLinker("dns", "acme")}
- req := httptest.NewRequest("GET", u, nil)
- req = req.WithContext(tc.ctx)
- w := httptest.NewRecorder()
- h.GetOrdersByAccountID(w, req)
- res := w.Result()
- assert.Equals(t, res.StatusCode, tc.statusCode)
- body, err := io.ReadAll(res.Body)
- res.Body.Close()
- assert.FatalError(t, err)
- if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
- var ae acme.Error
- assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
- assert.Equals(t, ae.Type, tc.err.Type)
- assert.Equals(t, ae.Detail, tc.err.Detail)
- assert.Equals(t, ae.Identifier, tc.err.Identifier)
- assert.Equals(t, ae.Subproblems, tc.err.Subproblems)
- assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
- } else {
- expB, err := json.Marshal(oidURLs)
- assert.FatalError(t, err)
- assert.Equals(t, bytes.TrimSpace(body), expB)
- assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
- }
- })
- }
- }
- func TestHandler_NewAccount(t *testing.T) {
- prov := newProv()
- escProvName := url.PathEscape(prov.GetName())
- baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
- type test struct {
- db acme.DB
- acc *acme.Account
- ctx context.Context
- statusCode int
- err *acme.Error
- }
- var tests = map[string]func(t *testing.T) test{
- "fail/no-payload": func(t *testing.T) test {
- return test{
- ctx: context.Background(),
- statusCode: 500,
- err: acme.NewErrorISE("payload expected in request context"),
- }
- },
- "fail/nil-payload": func(t *testing.T) test {
- ctx := context.WithValue(context.Background(), payloadContextKey, nil)
- return test{
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("payload expected in request context"),
- }
- },
- "fail/unmarshal-payload-error": func(t *testing.T) test {
- ctx := context.WithValue(context.Background(), payloadContextKey, &payloadInfo{})
- return test{
- ctx: ctx,
- statusCode: 400,
- err: acme.NewError(acme.ErrorMalformedType, "failed to "+
- "unmarshal new-account request payload: unexpected end of JSON input"),
- }
- },
- "fail/malformed-payload-error": func(t *testing.T) test {
- nar := &NewAccountRequest{
- Contact: []string{"foo", ""},
- }
- b, err := json.Marshal(nar)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), payloadContextKey, &payloadInfo{value: b})
- return test{
- ctx: ctx,
- statusCode: 400,
- err: acme.NewError(acme.ErrorMalformedType, "contact cannot be empty string"),
- }
- },
- "fail/no-existing-account": func(t *testing.T) test {
- nar := &NewAccountRequest{
- OnlyReturnExisting: true,
- }
- b, err := json.Marshal(nar)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), payloadContextKey, &payloadInfo{value: b})
- return test{
- ctx: ctx,
- statusCode: 400,
- err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
- }
- },
- "fail/no-jwk": func(t *testing.T) test {
- nar := &NewAccountRequest{
- Contact: []string{"foo", "bar"},
- }
- b, err := json.Marshal(nar)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), payloadContextKey, &payloadInfo{value: b})
- return test{
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("jwk expected in request context"),
- }
- },
- "fail/nil-jwk": func(t *testing.T) test {
- nar := &NewAccountRequest{
- Contact: []string{"foo", "bar"},
- }
- b, err := json.Marshal(nar)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), payloadContextKey, &payloadInfo{value: b})
- ctx = context.WithValue(ctx, jwkContextKey, nil)
- return test{
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("jwk expected in request context"),
- }
- },
- "fail/db.CreateAccount-error": func(t *testing.T) test {
- nar := &NewAccountRequest{
- Contact: []string{"foo", "bar"},
- }
- b, err := json.Marshal(nar)
- assert.FatalError(t, err)
- jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), payloadContextKey, &payloadInfo{value: b})
- ctx = context.WithValue(ctx, jwkContextKey, jwk)
- return test{
- db: &acme.MockDB{
- MockCreateAccount: func(ctx context.Context, acc *acme.Account) error {
- assert.Equals(t, acc.Contact, nar.Contact)
- assert.Equals(t, acc.Key, jwk)
- return acme.NewErrorISE("force")
- },
- },
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("force"),
- }
- },
- "ok/new-account": func(t *testing.T) test {
- nar := &NewAccountRequest{
- Contact: []string{"foo", "bar"},
- }
- b, err := json.Marshal(nar)
- assert.FatalError(t, err)
- jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), payloadContextKey, &payloadInfo{value: b})
- ctx = context.WithValue(ctx, jwkContextKey, jwk)
- ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
- ctx = context.WithValue(ctx, provisionerContextKey, prov)
- return test{
- db: &acme.MockDB{
- MockCreateAccount: func(ctx context.Context, acc *acme.Account) error {
- acc.ID = "accountID"
- assert.Equals(t, acc.Contact, nar.Contact)
- assert.Equals(t, acc.Key, jwk)
- return nil
- },
- },
- acc: &acme.Account{
- ID: "accountID",
- Key: jwk,
- Status: acme.StatusValid,
- Contact: []string{"foo", "bar"},
- OrdersURL: fmt.Sprintf("%s/acme/%s/account/accountID/orders", baseURL.String(), escProvName),
- },
- ctx: ctx,
- statusCode: 201,
- }
- },
- "ok/return-existing": func(t *testing.T) test {
- nar := &NewAccountRequest{
- OnlyReturnExisting: true,
- }
- b, err := json.Marshal(nar)
- assert.FatalError(t, err)
- jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
- assert.FatalError(t, err)
- acc := &acme.Account{
- ID: "accountID",
- Key: jwk,
- Status: acme.StatusValid,
- Contact: []string{"foo", "bar"},
- }
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
- return test{
- ctx: ctx,
- acc: acc,
- statusCode: 200,
- }
- },
- }
- for name, run := range tests {
- tc := run(t)
- t.Run(name, func(t *testing.T) {
- h := &Handler{db: tc.db, linker: NewLinker("dns", "acme")}
- req := httptest.NewRequest("GET", "/foo/bar", nil)
- req = req.WithContext(tc.ctx)
- w := httptest.NewRecorder()
- h.NewAccount(w, req)
- res := w.Result()
- assert.Equals(t, res.StatusCode, tc.statusCode)
- body, err := io.ReadAll(res.Body)
- res.Body.Close()
- assert.FatalError(t, err)
- if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
- var ae acme.Error
- assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
- assert.Equals(t, ae.Type, tc.err.Type)
- assert.Equals(t, ae.Detail, tc.err.Detail)
- assert.Equals(t, ae.Identifier, tc.err.Identifier)
- assert.Equals(t, ae.Subproblems, tc.err.Subproblems)
- assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
- } else {
- expB, err := json.Marshal(tc.acc)
- assert.FatalError(t, err)
- assert.Equals(t, bytes.TrimSpace(body), expB)
- assert.Equals(t, res.Header["Location"],
- []string{fmt.Sprintf("%s/acme/%s/account/%s", baseURL.String(),
- escProvName, "accountID")})
- assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
- }
- })
- }
- }
- func TestHandler_GetOrUpdateAccount(t *testing.T) {
- accID := "accountID"
- acc := acme.Account{
- ID: accID,
- Status: "valid",
- OrdersURL: fmt.Sprintf("https://ca.smallstep.com/acme/account/%s/orders", accID),
- }
- prov := newProv()
- escProvName := url.PathEscape(prov.GetName())
- baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
- type test struct {
- db acme.DB
- ctx context.Context
- statusCode int
- err *acme.Error
- }
- var tests = map[string]func(t *testing.T) test{
- "fail/no-account": func(t *testing.T) test {
- return test{
- ctx: context.Background(),
- statusCode: 400,
- err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
- }
- },
- "fail/nil-account": func(t *testing.T) test {
- ctx := context.WithValue(context.Background(), accContextKey, nil)
- return test{
- ctx: ctx,
- statusCode: 400,
- err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
- }
- },
- "fail/no-payload": func(t *testing.T) test {
- ctx := context.WithValue(context.Background(), accContextKey, &acc)
- return test{
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("payload expected in request context"),
- }
- },
- "fail/nil-payload": func(t *testing.T) test {
- ctx := context.WithValue(context.Background(), accContextKey, &acc)
- ctx = context.WithValue(ctx, payloadContextKey, nil)
- return test{
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("payload expected in request context"),
- }
- },
- "fail/unmarshal-payload-error": func(t *testing.T) test {
- ctx := context.WithValue(context.Background(), accContextKey, &acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{})
- return test{
- ctx: ctx,
- statusCode: 400,
- err: acme.NewError(acme.ErrorMalformedType, "failed to unmarshal new-account request payload: unexpected end of JSON input"),
- }
- },
- "fail/malformed-payload-error": func(t *testing.T) test {
- uar := &UpdateAccountRequest{
- Contact: []string{"foo", ""},
- }
- b, err := json.Marshal(uar)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), accContextKey, &acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
- return test{
- ctx: ctx,
- statusCode: 400,
- err: acme.NewError(acme.ErrorMalformedType, "contact cannot be empty string"),
- }
- },
- "fail/db.UpdateAccount-error": func(t *testing.T) test {
- uar := &UpdateAccountRequest{
- Status: "deactivated",
- }
- b, err := json.Marshal(uar)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), accContextKey, &acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
- return test{
- db: &acme.MockDB{
- MockUpdateAccount: func(ctx context.Context, upd *acme.Account) error {
- assert.Equals(t, upd.Status, acme.StatusDeactivated)
- assert.Equals(t, upd.ID, acc.ID)
- return acme.NewErrorISE("force")
- },
- },
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("force"),
- }
- },
- "ok/deactivate": func(t *testing.T) test {
- uar := &UpdateAccountRequest{
- Status: "deactivated",
- }
- b, err := json.Marshal(uar)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, &acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
- ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
- return test{
- db: &acme.MockDB{
- MockUpdateAccount: func(ctx context.Context, upd *acme.Account) error {
- assert.Equals(t, upd.Status, acme.StatusDeactivated)
- assert.Equals(t, upd.ID, acc.ID)
- return nil
- },
- },
- ctx: ctx,
- statusCode: 200,
- }
- },
- "ok/update-empty": func(t *testing.T) test {
- uar := &UpdateAccountRequest{}
- b, err := json.Marshal(uar)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, &acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
- ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
- return test{
- ctx: ctx,
- statusCode: 200,
- }
- },
- "ok/update-contacts": func(t *testing.T) test {
- uar := &UpdateAccountRequest{
- Contact: []string{"foo", "bar"},
- }
- b, err := json.Marshal(uar)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, &acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
- ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
- return test{
- db: &acme.MockDB{
- MockUpdateAccount: func(ctx context.Context, upd *acme.Account) error {
- assert.Equals(t, upd.Contact, uar.Contact)
- assert.Equals(t, upd.ID, acc.ID)
- return nil
- },
- },
- ctx: ctx,
- statusCode: 200,
- }
- },
- "ok/post-as-get": func(t *testing.T) test {
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, &acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{isPostAsGet: true})
- ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
- return test{
- ctx: ctx,
- statusCode: 200,
- }
- },
- }
- for name, run := range tests {
- tc := run(t)
- t.Run(name, func(t *testing.T) {
- h := &Handler{db: tc.db, linker: NewLinker("dns", "acme")}
- req := httptest.NewRequest("GET", "/foo/bar", nil)
- req = req.WithContext(tc.ctx)
- w := httptest.NewRecorder()
- h.GetOrUpdateAccount(w, req)
- res := w.Result()
- assert.Equals(t, res.StatusCode, tc.statusCode)
- body, err := io.ReadAll(res.Body)
- res.Body.Close()
- assert.FatalError(t, err)
- if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
- var ae acme.Error
- assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
- assert.Equals(t, ae.Type, tc.err.Type)
- assert.Equals(t, ae.Detail, tc.err.Detail)
- assert.Equals(t, ae.Identifier, tc.err.Identifier)
- assert.Equals(t, ae.Subproblems, tc.err.Subproblems)
- assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
- } else {
- expB, err := json.Marshal(acc)
- assert.FatalError(t, err)
- assert.Equals(t, bytes.TrimSpace(body), expB)
- assert.Equals(t, res.Header["Location"],
- []string{fmt.Sprintf("%s/acme/%s/account/%s", baseURL.String(),
- escProvName, accID)})
- assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
- }
- })
- }
- }
|