Fix tests.

This commit is contained in:
Mariano Cano 2019-12-18 14:44:08 -08:00
parent a6deea7d8d
commit eeabf5ba4c
3 changed files with 16 additions and 12 deletions

View file

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"testing" "testing"
"github.com/smallstep/certificates/errs"
) )
func TestError_MarshalJSON(t *testing.T) { func TestError_MarshalJSON(t *testing.T) {
@ -22,7 +24,7 @@ func TestError_MarshalJSON(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
e := &Error{ e := &errs.Error{
Status: tt.fields.Status, Status: tt.fields.Status,
Err: tt.fields.Err, Err: tt.fields.Err,
} }
@ -45,15 +47,15 @@ func TestError_UnmarshalJSON(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
args args args args
expected *Error expected *errs.Error
wantErr bool wantErr bool
}{ }{
{"ok", args{[]byte(`{"status":400,"message":"bad request"}`)}, &Error{Status: 400, Err: fmt.Errorf("bad request")}, false}, {"ok", args{[]byte(`{"status":400,"message":"bad request"}`)}, &errs.Error{Status: 400, Err: fmt.Errorf("bad request")}, false},
{"fail", args{[]byte(`{"status":"400","message":"bad request"}`)}, &Error{}, true}, {"fail", args{[]byte(`{"status":"400","message":"bad request"}`)}, &errs.Error{}, true},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
e := new(Error) e := new(errs.Error)
if err := e.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr { if err := e.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr {
t.Errorf("Error.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("Error.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
} }

View file

@ -16,18 +16,19 @@ import (
"github.com/smallstep/assert" "github.com/smallstep/assert"
"github.com/smallstep/certificates/authority" "github.com/smallstep/certificates/authority"
"github.com/smallstep/certificates/authority/provisioner" "github.com/smallstep/certificates/authority/provisioner"
"github.com/smallstep/certificates/errs"
"github.com/smallstep/certificates/logging" "github.com/smallstep/certificates/logging"
) )
func TestRevokeRequestValidate(t *testing.T) { func TestRevokeRequestValidate(t *testing.T) {
type test struct { type test struct {
rr *RevokeRequest rr *RevokeRequest
err *Error err *errs.Error
} }
tests := map[string]test{ tests := map[string]test{
"error/missing serial": { "error/missing serial": {
rr: &RevokeRequest{}, rr: &RevokeRequest{},
err: &Error{Err: errors.New("missing serial"), Status: http.StatusBadRequest}, err: &errs.Error{Err: errors.New("missing serial"), Status: http.StatusBadRequest},
}, },
"error/bad reasonCode": { "error/bad reasonCode": {
rr: &RevokeRequest{ rr: &RevokeRequest{
@ -35,7 +36,7 @@ func TestRevokeRequestValidate(t *testing.T) {
ReasonCode: 15, ReasonCode: 15,
Passive: true, Passive: true,
}, },
err: &Error{Err: errors.New("reasonCode out of bounds"), Status: http.StatusBadRequest}, err: &errs.Error{Err: errors.New("reasonCode out of bounds"), Status: http.StatusBadRequest},
}, },
"error/non-passive not implemented": { "error/non-passive not implemented": {
rr: &RevokeRequest{ rr: &RevokeRequest{
@ -43,7 +44,7 @@ func TestRevokeRequestValidate(t *testing.T) {
ReasonCode: 8, ReasonCode: 8,
Passive: false, Passive: false,
}, },
err: &Error{Err: errors.New("non-passive revocation not implemented"), Status: http.StatusNotImplemented}, err: &errs.Error{Err: errors.New("non-passive revocation not implemented"), Status: http.StatusNotImplemented},
}, },
"ok": { "ok": {
rr: &RevokeRequest{ rr: &RevokeRequest{
@ -57,7 +58,7 @@ func TestRevokeRequestValidate(t *testing.T) {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
if err := tc.rr.Validate(); err != nil { if err := tc.rr.Validate(); err != nil {
switch v := err.(type) { switch v := err.(type) {
case *Error: case *errs.Error:
assert.HasPrefix(t, v.Error(), tc.err.Error()) assert.HasPrefix(t, v.Error(), tc.err.Error())
assert.Equals(t, v.StatusCode(), tc.err.Status) assert.Equals(t, v.StatusCode(), tc.err.Status)
default: default:
@ -189,7 +190,7 @@ func Test_caHandler_Revoke(t *testing.T) {
return nil, nil return nil, nil
}, },
revoke: func(ctx context.Context, opts *authority.RevokeOptions) error { revoke: func(ctx context.Context, opts *authority.RevokeOptions) error {
return InternalServerError(errors.New("force")) return errs.InternalServerError(errors.New("force"))
}, },
}, },
} }

View file

@ -9,6 +9,7 @@ import (
"testing" "testing"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/smallstep/certificates/errs"
"github.com/smallstep/certificates/logging" "github.com/smallstep/certificates/logging"
) )
@ -108,7 +109,7 @@ func TestReadJSON(t *testing.T) {
t.Errorf("ReadJSON() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("ReadJSON() error = %v, wantErr %v", err, tt.wantErr)
} }
if tt.wantErr { if tt.wantErr {
e, ok := err.(*Error) e, ok := err.(*errs.Error)
if ok { if ok {
if code := e.StatusCode(); code != 400 { if code := e.StatusCode(); code != 400 {
t.Errorf("error.StatusCode() = %v, wants 400", code) t.Errorf("error.StatusCode() = %v, wants 400", code)