github.com/kiali/kiali@v1.84.0/business/authentication/openshift_auth_controller_test.go (about) 1 package authentication_test 2 3 import ( 4 "context" 5 "encoding/json" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 osoauth_v1 "github.com/openshift/api/oauth/v1" 11 "github.com/stretchr/testify/require" 12 meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 13 "k8s.io/client-go/rest" 14 15 "github.com/kiali/kiali/business" 16 "github.com/kiali/kiali/business/authentication" 17 "github.com/kiali/kiali/config" 18 "github.com/kiali/kiali/kubernetes" 19 "github.com/kiali/kiali/kubernetes/kubetest" 20 ) 21 22 func fakeOAuthMetadataServer(t *testing.T) *httptest.Server { 23 t.Helper() 24 // This is known after we create the server. 25 // Probably another way of doing this but this works too. 26 addr := "" 27 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 28 oAuthResponse := &business.OAuthAuthorizationServer{ 29 AuthorizationEndpoint: addr + "/oauth/authorize", 30 Issuer: addr, 31 TokenEndpoint: addr + "/oauth/token", 32 } 33 b, err := json.Marshal(oAuthResponse) 34 if err != nil { 35 panic("unable to marshal json response for fake oAuthMetadataServer") 36 } 37 _, _ = w.Write(b) 38 })) 39 addr = server.URL 40 t.Cleanup(server.Close) 41 return server 42 } 43 44 func TestNewOpenshiftAuthService(t *testing.T) { 45 require := require.New(t) 46 conf := config.NewConfig() 47 oAuthClient := &osoauth_v1.OAuthClient{ 48 ObjectMeta: meta_v1.ObjectMeta{ 49 Name: "kiali-istio-system", 50 }, 51 RedirectURIs: []string{"http://localhost:20001/kiali"}, 52 } 53 54 metadataServer := fakeOAuthMetadataServer(t) 55 persistor := authentication.NewCookieSessionPersistor(conf) 56 client := kubetest.NewFakeK8sClient(oAuthClient) 57 client.KubeClusterInfo.ClientConfig = &rest.Config{Host: metadataServer.URL} 58 clients := map[string]kubernetes.ClientInterface{conf.KubernetesConfig.ClusterName: client} 59 clientFactory := kubetest.NewFakeClientFactory(conf, clients) 60 61 oAuthService, err := business.NewOpenshiftOAuthService(context.TODO(), conf, clients, clientFactory) 62 require.NoError(err) 63 64 _, err = authentication.NewOpenshiftAuthController(persistor, oAuthService, conf) 65 require.NoError(err) 66 }