func TestNewRevocationRequest(t *testing.T) { ctrl := gomock.NewController(t) store := internal.NewMockStorage(ctrl) client := internal.NewMockClient(ctrl) handler := internal.NewMockRevocationHandler(ctrl) hasher := internal.NewMockHasher(ctrl) defer ctrl.Finish() fosite := &Fosite{Store: store, Hasher: hasher} for k, c := range []struct { header http.Header form url.Values mock func() method string expectErr error expect *AccessRequest handlers RevocationHandlers }{ { header: http.Header{}, expectErr: ErrInvalidRequest, method: "GET", mock: func() {}, }, { header: http.Header{}, expectErr: ErrInvalidRequest, method: "POST", mock: func() {}, }, { header: http.Header{}, method: "POST", form: url.Values{ "token": {"foo"}, }, mock: func() {}, expectErr: ErrInvalidRequest, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "token": {"foo"}, }, expectErr: ErrInvalidClient, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(nil, errors.New("")) }, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "token": {"foo"}, }, expectErr: ErrInvalidClient, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(client, nil) client.EXPECT().GetHashedSecret().Return([]byte("foo")) hasher.EXPECT().Compare(gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(errors.New("")) }, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "token": {"foo"}, }, expectErr: nil, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(client, nil) client.EXPECT().GetHashedSecret().Return([]byte("foo")) hasher.EXPECT().Compare(gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(nil) handler.EXPECT().RevokeToken(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) }, handlers: RevocationHandlers{handler}, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "token": {"foo"}, "token_type_hint": {"access_token"}, }, expectErr: nil, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(client, nil) client.EXPECT().GetHashedSecret().Return([]byte("foo")) hasher.EXPECT().Compare(gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(nil) handler.EXPECT().RevokeToken(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) }, handlers: RevocationHandlers{handler}, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "token": {"foo"}, "token_type_hint": {"refresh_token"}, }, expectErr: nil, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(client, nil) client.EXPECT().GetHashedSecret().Return([]byte("foo")) hasher.EXPECT().Compare(gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(nil) handler.EXPECT().RevokeToken(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) }, handlers: RevocationHandlers{handler}, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "token": {"foo"}, "token_type_hint": {"bar"}, }, expectErr: nil, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(client, nil) client.EXPECT().GetHashedSecret().Return([]byte("foo")) hasher.EXPECT().Compare(gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(nil) handler.EXPECT().RevokeToken(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) }, handlers: RevocationHandlers{handler}, }, } { r := &http.Request{ Header: c.header, PostForm: c.form, Form: c.form, Method: c.method, } c.mock() ctx := NewContext() fosite.RevocationHandlers = c.handlers err := fosite.NewRevocationRequest(ctx, r) assert.True(t, errors.Cause(err) == c.expectErr, "%d\nwant: %s \ngot: %s", k, c.expectErr, err) if err != nil { t.Logf("Error occured: %v", err) } t.Logf("Passed test case %d", k) } }
func TestNewAccessRequest(t *testing.T) { ctrl := gomock.NewController(t) store := internal.NewMockStorage(ctrl) client := internal.NewMockClient(ctrl) handler := internal.NewMockTokenEndpointHandler(ctrl) hasher := internal.NewMockHasher(ctrl) defer ctrl.Finish() fosite := &Fosite{Store: store, Hasher: hasher} for k, c := range []struct { header http.Header form url.Values mock func() method string expectErr error expect *AccessRequest handlers TokenEndpointHandlers }{ { header: http.Header{}, expectErr: ErrInvalidRequest, method: "POST", mock: func() {}, }, { header: http.Header{}, method: "POST", form: url.Values{ "grant_type": {"foo"}, }, mock: func() {}, expectErr: ErrInvalidRequest, }, { header: http.Header{}, method: "POST", form: url.Values{ "grant_type": {"foo"}, "client_id": {"foo"}, }, expectErr: ErrInvalidRequest, mock: func() {}, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "grant_type": {"foo"}, }, expectErr: ErrInvalidClient, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(nil, errors.New("")) }, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "GET", form: url.Values{ "grant_type": {"foo"}, }, expectErr: ErrInvalidRequest, mock: func() {}, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "grant_type": {"foo"}, }, expectErr: ErrInvalidClient, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(nil, errors.New("")) }, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "grant_type": {"foo"}, }, expectErr: ErrInvalidClient, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(client, nil) client.EXPECT().GetHashedSecret().Return([]byte("foo")) hasher.EXPECT().Compare(gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(errors.New("")) }, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "grant_type": {"foo"}, }, expectErr: ErrServerError, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(client, nil) client.EXPECT().GetHashedSecret().Return([]byte("foo")) hasher.EXPECT().Compare(gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(nil) handler.EXPECT().ValidateTokenEndpointRequest(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(ErrServerError) }, handlers: TokenEndpointHandlers{"a": handler}, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "grant_type": {"foo"}, }, expectErr: ErrUnsupportedGrantType, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(client, nil) client.EXPECT().GetHashedSecret().Return([]byte("foo")) hasher.EXPECT().Compare(gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(nil) handler.EXPECT().ValidateTokenEndpointRequest(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) }, handlers: TokenEndpointHandlers{"a": handler}, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "grant_type": {"foo"}, }, expectErr: ErrUnsupportedGrantType, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(client, nil) client.EXPECT().GetHashedSecret().Return([]byte("foo")) hasher.EXPECT().Compare(gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(nil) handler.EXPECT().ValidateTokenEndpointRequest(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Do(func(_ context.Context, _ *http.Request, a AccessRequester, _ interface{}) { a.SetGrantTypeHandled("bar") }).Return(nil) }, handlers: TokenEndpointHandlers{"a": handler}, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "grant_type": {"foo"}, }, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(client, nil) client.EXPECT().GetHashedSecret().Return([]byte("foo")) hasher.EXPECT().Compare(gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(nil) handler.EXPECT().ValidateTokenEndpointRequest(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Do(func(_ context.Context, _ *http.Request, a AccessRequester, _ interface{}) { a.SetGrantTypeHandled("foo") a.SetScopes([]string{"asdfasdf"}) }).Return(nil) }, handlers: TokenEndpointHandlers{"a": handler}, expectErr: ErrInvalidScope, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "grant_type": {"foo"}, }, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(client, nil) client.EXPECT().GetHashedSecret().Return([]byte("foo")) hasher.EXPECT().Compare(gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(nil) handler.EXPECT().ValidateTokenEndpointRequest(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Do(func(_ context.Context, _ *http.Request, a AccessRequester, _ interface{}) { a.SetGrantTypeHandled("foo") a.SetScopes([]string{DefaultRequiredScopeName}) }).Return(nil) }, handlers: TokenEndpointHandlers{"a": handler}, expect: &AccessRequest{ GrantType: "foo", HandledGrantType: []string{"foo"}, Client: client, }, }, } { r := &http.Request{ Header: c.header, PostForm: c.form, Form: c.form, Method: c.method, } c.mock() ctx := NewContext() fosite.TokenEndpointHandlers = c.handlers ar, err := fosite.NewAccessRequest(ctx, r, &struct{}{}) is := errors.Is(c.expectErr, err) assert.True(t, is, "%d\nwant: %s \ngot: %s", k, c.expectErr, err) if !is { t.Logf("Error occured: %s", err.(*errors.Error).ErrorStack()) } if err == nil { pkg.AssertObjectKeysEqual(t, c.expect, ar, "GrantType", "Client") assert.NotNil(t, ar.GetRequestedAt()) } t.Logf("Passed test case %d", k) } }
func TestNewAccessRequest(t *testing.T) { ctrl := gomock.NewController(t) store := internal.NewMockStorage(ctrl) client := internal.NewMockClient(ctrl) handler := internal.NewMockTokenEndpointHandler(ctrl) hasher := internal.NewMockHasher(ctrl) defer ctrl.Finish() fosite := &Fosite{Store: store, Hasher: hasher} for k, c := range []struct { header http.Header form url.Values mock func() method string expectErr error expect *AccessRequest handlers TokenEndpointHandlers }{ { header: http.Header{}, expectErr: ErrInvalidRequest, method: "POST", mock: func() {}, }, { header: http.Header{}, method: "POST", form: url.Values{ "grant_type": {"foo"}, }, mock: func() {}, expectErr: ErrInvalidRequest, }, { header: http.Header{}, method: "POST", form: url.Values{ "grant_type": {"foo"}, "client_id": {"foo"}, }, expectErr: ErrInvalidRequest, mock: func() {}, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "grant_type": {"foo"}, }, expectErr: ErrInvalidClient, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(nil, errors.New("")) }, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "GET", form: url.Values{ "grant_type": {"foo"}, }, expectErr: ErrInvalidRequest, mock: func() {}, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "grant_type": {"foo"}, }, expectErr: ErrInvalidClient, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(nil, errors.New("")) }, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "grant_type": {"foo"}, }, expectErr: ErrInvalidClient, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(client, nil) client.EXPECT().IsPublic().Return(false) client.EXPECT().GetHashedSecret().Return([]byte("foo")) hasher.EXPECT().Compare(gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(errors.New("")) }, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "grant_type": {"foo"}, }, expectErr: ErrServerError, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(client, nil) client.EXPECT().IsPublic().Return(false) client.EXPECT().GetHashedSecret().Return([]byte("foo")) hasher.EXPECT().Compare(gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(nil) handler.EXPECT().HandleTokenEndpointRequest(gomock.Any(), gomock.Any(), gomock.Any()).Return(ErrServerError) }, handlers: TokenEndpointHandlers{handler}, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "grant_type": {"foo"}, }, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(client, nil) client.EXPECT().IsPublic().Return(false) client.EXPECT().GetHashedSecret().Return([]byte("foo")) hasher.EXPECT().Compare(gomock.Eq([]byte("foo")), gomock.Eq([]byte("bar"))).Return(nil) handler.EXPECT().HandleTokenEndpointRequest(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) }, handlers: TokenEndpointHandlers{handler}, expect: &AccessRequest{ GrantTypes: Arguments{"foo"}, Request: Request{ Client: client, }, }, }, { header: http.Header{ "Authorization": {basicAuth("foo", "bar")}, }, method: "POST", form: url.Values{ "grant_type": {"foo"}, }, mock: func() { store.EXPECT().GetClient(gomock.Eq("foo")).Return(client, nil) client.EXPECT().IsPublic().Return(true) handler.EXPECT().HandleTokenEndpointRequest(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) }, handlers: TokenEndpointHandlers{handler}, expect: &AccessRequest{ GrantTypes: Arguments{"foo"}, Request: Request{ Client: client, }, }, }, } { r := &http.Request{ Header: c.header, PostForm: c.form, Form: c.form, Method: c.method, } c.mock() ctx := NewContext() fosite.TokenEndpointHandlers = c.handlers ar, err := fosite.NewAccessRequest(ctx, r, new(DefaultSession)) assert.True(t, errors.Cause(err) == c.expectErr, "%d\nwant: %s \ngot: %s", k, c.expectErr, err) if err != nil { t.Logf("Error occured: %v", err) } else { pkg.AssertObjectKeysEqual(t, c.expect, ar, "GrantTypes", "Client") assert.NotNil(t, ar.GetRequestedAt()) } t.Logf("Passed test case %d", k) } }