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 TestNewAccessResponse(t *testing.T) { ctrl := gomock.NewController(t) handler := internal.NewMockTokenEndpointHandler(ctrl) defer ctrl.Finish() f := &Fosite{} for k, c := range []struct { handlers TokenEndpointHandlers mock func() expectErr error expect AccessResponder }{ { mock: func() {}, handlers: TokenEndpointHandlers{}, expectErr: ErrUnsupportedGrantType, }, { mock: func() { handler.EXPECT().HandleTokenEndpointRequest(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(ErrServerError) }, handlers: TokenEndpointHandlers{"a": handler}, expectErr: ErrServerError, }, { mock: func() { handler.EXPECT().HandleTokenEndpointRequest(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) }, handlers: TokenEndpointHandlers{"a": handler}, expectErr: ErrUnsupportedGrantType, }, { mock: func() { handler.EXPECT().HandleTokenEndpointRequest(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Do(func(_ context.Context, _ *http.Request, _ AccessRequester, resp AccessResponder) { resp.SetAccessToken("foo") }).Return(nil) }, handlers: TokenEndpointHandlers{"a": handler}, expectErr: ErrUnsupportedGrantType, }, { mock: func() { handler.EXPECT().HandleTokenEndpointRequest(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Do(func(_ context.Context, _ *http.Request, _ AccessRequester, resp AccessResponder) { resp.SetAccessToken("foo") resp.SetTokenType("bar") }).Return(nil) }, handlers: TokenEndpointHandlers{"a": handler}, expect: &AccessResponse{ Extra: map[string]interface{}{}, AccessToken: "foo", TokenType: "bar", }, }, } { f.TokenEndpointHandlers = c.handlers c.mock() ar, err := f.NewAccessResponse(nil, nil, nil) assert.True(t, errors.Is(c.expectErr, err), "%d", k) assert.Equal(t, ar, c.expect) 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) } }