mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-04 11:19:39 -06:00
68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/coreos/go-oidc"
|
|
"github.com/owncloud/ocis/ocis-pkg/log"
|
|
"golang.org/x/oauth2"
|
|
)
|
|
|
|
func TestOpenIDConnectMiddleware(t *testing.T) {
|
|
svcCache.Invalidate(AccountsKey, "success")
|
|
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
|
|
|
m := OpenIDConnect(
|
|
Logger(log.NewLogger()),
|
|
OIDCProviderFunc(func() (OIDCProvider, error) {
|
|
return mockOP(false), nil
|
|
}),
|
|
)(next)
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "https://idp.example.com", nil)
|
|
r.Header.Set("Authorization", "Bearer sometoken")
|
|
w := httptest.NewRecorder()
|
|
m.ServeHTTP(w, r)
|
|
|
|
if w.Code != http.StatusInternalServerError {
|
|
t.Errorf("expected an internal server error")
|
|
}
|
|
}
|
|
|
|
type mockOIDCProvider struct {
|
|
UserInfoFunc func(ctx context.Context, ts oauth2.TokenSource) (*oidc.UserInfo, error)
|
|
}
|
|
|
|
// UserInfo will panic if the function has been called, but not mocked
|
|
func (m mockOIDCProvider) UserInfo(ctx context.Context, ts oauth2.TokenSource) (*oidc.UserInfo, error) {
|
|
if m.UserInfoFunc != nil {
|
|
return m.UserInfoFunc(ctx, ts)
|
|
}
|
|
|
|
panic("UserInfo was called in test but not mocked")
|
|
}
|
|
|
|
func mockOP(retErr bool) OIDCProvider {
|
|
if retErr {
|
|
return &mockOIDCProvider{
|
|
UserInfoFunc: func(ctx context.Context, ts oauth2.TokenSource) (*oidc.UserInfo, error) {
|
|
return nil, fmt.Errorf("error returned by mockOIDCProvider UserInfo")
|
|
},
|
|
}
|
|
|
|
}
|
|
return &mockOIDCProvider{
|
|
UserInfoFunc: func(ctx context.Context, ts oauth2.TokenSource) (*oidc.UserInfo, error) {
|
|
ui := &oidc.UserInfo{
|
|
// claims: private ...
|
|
}
|
|
return ui, nil
|
|
},
|
|
}
|
|
|
|
}
|