fix tests

This commit is contained in:
Livio Amstutz 2020-03-11 09:36:01 +01:00
parent 3301981869
commit b43fa4ca38
4 changed files with 44 additions and 22 deletions

View file

@ -11,6 +11,7 @@ import (
"github.com/caos/oidc/pkg/oidc" "github.com/caos/oidc/pkg/oidc"
"github.com/caos/oidc/pkg/op" "github.com/caos/oidc/pkg/op"
"github.com/caos/oidc/pkg/op/mock" "github.com/caos/oidc/pkg/op/mock"
"github.com/caos/oidc/pkg/rp"
) )
func TestAuthorize(t *testing.T) { func TestAuthorize(t *testing.T) {
@ -70,6 +71,7 @@ func TestValidateAuthRequest(t *testing.T) {
type args struct { type args struct {
authRequest *oidc.AuthRequest authRequest *oidc.AuthRequest
storage op.Storage storage op.Storage
verifier rp.Verifier
} }
tests := []struct { tests := []struct {
name string name string
@ -82,33 +84,34 @@ func TestValidateAuthRequest(t *testing.T) {
// } // }
{ {
"scope missing fails", "scope missing fails",
args{&oidc.AuthRequest{}, nil}, args{&oidc.AuthRequest{}, nil, nil},
true, true,
}, },
{ {
"scope openid missing fails", "scope openid missing fails",
args{&oidc.AuthRequest{Scopes: []string{"profile"}}, nil}, args{&oidc.AuthRequest{Scopes: []string{"profile"}}, nil, nil},
true, true,
}, },
{ {
"response_type missing fails", "response_type missing fails",
args{&oidc.AuthRequest{Scopes: []string{"openid"}}, nil}, args{&oidc.AuthRequest{Scopes: []string{"openid"}}, nil, nil},
true, true,
}, },
{ {
"client_id missing fails", "client_id missing fails",
args{&oidc.AuthRequest{Scopes: []string{"openid"}, ResponseType: oidc.ResponseTypeCode}, nil}, args{&oidc.AuthRequest{Scopes: []string{"openid"}, ResponseType: oidc.ResponseTypeCode}, nil, nil},
true, true,
}, },
{ {
"redirect_uri missing fails", "redirect_uri missing fails",
args{&oidc.AuthRequest{Scopes: []string{"openid"}, ResponseType: oidc.ResponseTypeCode, ClientID: "client_id"}, nil}, args{&oidc.AuthRequest{Scopes: []string{"openid"}, ResponseType: oidc.ResponseTypeCode, ClientID: "client_id"}, nil, nil},
true, 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) {
if err := op.ValidateAuthRequest(nil, tt.args.authRequest, tt.args.storage); (err != nil) != tt.wantErr { _, err := op.ValidateAuthRequest(nil, tt.args.authRequest, tt.args.storage, tt.args.verifier)
if (err != nil) != tt.wantErr {
t.Errorf("ValidateAuthRequest() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("ValidateAuthRequest() error = %v, wantErr %v", err, tt.wantErr)
} }
}) })

View file

@ -6,6 +6,7 @@ package mock
import ( import (
op "github.com/caos/oidc/pkg/op" op "github.com/caos/oidc/pkg/op"
rp "github.com/caos/oidc/pkg/rp"
gomock "github.com/golang/mock/gomock" gomock "github.com/golang/mock/gomock"
schema "github.com/gorilla/schema" schema "github.com/gorilla/schema"
reflect "reflect" reflect "reflect"
@ -76,6 +77,20 @@ func (mr *MockAuthorizerMockRecorder) Encoder() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Encoder", reflect.TypeOf((*MockAuthorizer)(nil).Encoder)) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Encoder", reflect.TypeOf((*MockAuthorizer)(nil).Encoder))
} }
// IDTokenVerifier mocks base method
func (m *MockAuthorizer) IDTokenVerifier() rp.Verifier {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "IDTokenVerifier")
ret0, _ := ret[0].(rp.Verifier)
return ret0
}
// IDTokenVerifier indicates an expected call of IDTokenVerifier
func (mr *MockAuthorizerMockRecorder) IDTokenVerifier() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IDTokenVerifier", reflect.TypeOf((*MockAuthorizer)(nil).IDTokenVerifier))
}
// Issuer mocks base method // Issuer mocks base method
func (m *MockAuthorizer) Issuer() string { func (m *MockAuthorizer) Issuer() string {
m.ctrl.T.Helper() m.ctrl.T.Helper()

View file

@ -8,8 +8,9 @@ import (
"github.com/gorilla/schema" "github.com/gorilla/schema"
"gopkg.in/square/go-jose.v2" "gopkg.in/square/go-jose.v2"
oidc "github.com/caos/oidc/pkg/oidc" "github.com/caos/oidc/pkg/oidc"
"github.com/caos/oidc/pkg/op" "github.com/caos/oidc/pkg/op"
"github.com/caos/oidc/pkg/rp"
) )
func NewAuthorizer(t *testing.T) op.Authorizer { func NewAuthorizer(t *testing.T) op.Authorizer {
@ -22,6 +23,7 @@ func NewAuthorizerExpectValid(t *testing.T, wantErr bool) op.Authorizer {
ExpectEncoder(m) ExpectEncoder(m)
ExpectSigner(m, t) ExpectSigner(m, t)
ExpectStorage(m, t) ExpectStorage(m, t)
ExpectVerifier(m, t)
// ExpectErrorHandler(m, t, wantErr) // ExpectErrorHandler(m, t, wantErr)
return m return m
} }
@ -54,17 +56,19 @@ func ExpectSigner(a op.Authorizer, t *testing.T) {
}) })
} }
// func ExpectErrorHandler(a op.Authorizer, t *testing.T, wantErr bool) { func ExpectVerifier(a op.Authorizer, t *testing.T) {
// mockA := a.(*MockAuthorizer) mockA := a.(*MockAuthorizer)
// mockA.EXPECT().ErrorHandler().AnyTimes(). mockA.EXPECT().IDTokenVerifier().DoAndReturn(
// Return(func(w http.ResponseWriter, r *http.Request, authReq *oidc.AuthRequest, err error) { func() rp.Verifier {
// if wantErr { return &Verifier{}
// require.Error(t, err) })
// return }
// }
// require.NoError(t, err) type Verifier struct{}
// })
// } func (v *Verifier) Verify(ctx context.Context, accessToken, idToken string) (*oidc.IDTokenClaims, error) {
return nil, nil
}
type Sig struct{} type Sig struct{}

View file

@ -67,18 +67,18 @@ func (mr *MockStorageMockRecorder) AuthorizeClientIDSecret(arg0, arg1, arg2 inte
} }
// CreateAuthRequest mocks base method // CreateAuthRequest mocks base method
func (m *MockStorage) CreateAuthRequest(arg0 context.Context, arg1 *oidc.AuthRequest) (op.AuthRequest, error) { func (m *MockStorage) CreateAuthRequest(arg0 context.Context, arg1 *oidc.AuthRequest, arg2 string) (op.AuthRequest, error) {
m.ctrl.T.Helper() m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "CreateAuthRequest", arg0, arg1) ret := m.ctrl.Call(m, "CreateAuthRequest", arg0, arg1, arg2)
ret0, _ := ret[0].(op.AuthRequest) ret0, _ := ret[0].(op.AuthRequest)
ret1, _ := ret[1].(error) ret1, _ := ret[1].(error)
return ret0, ret1 return ret0, ret1
} }
// CreateAuthRequest indicates an expected call of CreateAuthRequest // CreateAuthRequest indicates an expected call of CreateAuthRequest
func (mr *MockStorageMockRecorder) CreateAuthRequest(arg0, arg1 interface{}) *gomock.Call { func (mr *MockStorageMockRecorder) CreateAuthRequest(arg0, arg1, arg2 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper() mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateAuthRequest", reflect.TypeOf((*MockStorage)(nil).CreateAuthRequest), arg0, arg1) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateAuthRequest", reflect.TypeOf((*MockStorage)(nil).CreateAuthRequest), arg0, arg1, arg2)
} }
// CreateToken mocks base method // CreateToken mocks base method