Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvallejo committed Nov 3, 2017
1 parent b366e91 commit df1e76b
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pkg/cmd/util/tokencmd/request_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const (
// See IETF Draft:
// https://tools.ietf.org/html/draft-ietf-oauth-discovery-04#section-2
// Copied from pkg/cmd/server/origin/nonapiserver.go
oauthMetadataEndpoint = "/.well-known/oauth-authorization-server"
OauthMetadataEndpoint = "/.well-known/oauth-authorization-server"

// openShiftCLIClientID is the name of the CLI OAuth client, copied from pkg/oauth/apiserver/auth.go
openShiftCLIClientID = "openshift-challenging-client"
Expand Down Expand Up @@ -108,7 +108,7 @@ func (o *RequestTokenOptions) SetDefaultOsinConfig() error {
if err != nil {
return err
}
resp, err := request(rt, strings.TrimRight(o.ClientConfig.Host, "/")+oauthMetadataEndpoint, nil)
resp, err := request(rt, strings.TrimRight(o.ClientConfig.Host, "/")+OauthMetadataEndpoint, nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/util/tokencmd/request_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ func TestSetDefaultOsinConfig(t *testing.T) {
t.Errorf("%s: Expected GET, got %s", tc.name, req.Method)
return
}
if req.URL.Path != oauthMetadataEndpoint {
if req.URL.Path != OauthMetadataEndpoint {
t.Errorf("%s: Expected metadata endpoint, got %s", tc.name, req.URL.Path)
return
}
Expand Down
100 changes: 100 additions & 0 deletions pkg/oc/cli/cmd/login/loginoptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@ package login

import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"regexp"
"strings"
"testing"

"github.com/spf13/pflag"

"github.com/MakeNowJust/heredoc"

"github.com/openshift/origin/pkg/cmd/util/clientcmd"
"github.com/openshift/origin/pkg/cmd/util/tokencmd"
"github.com/openshift/origin/pkg/oauth/util"
"github.com/openshift/origin/pkg/oc/cli/config"

kapierrs "k8s.io/apimachinery/pkg/api/errors"
restclient "k8s.io/client-go/rest"
kclientcmd "k8s.io/client-go/tools/clientcmd"
kclientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

Expand Down Expand Up @@ -256,6 +266,96 @@ func TestDialToHTTPServer(t *testing.T) {
}
}

type oauthMetadataResponse struct {
metadata *util.OauthAuthorizationServerMetadata
}

func (r *oauthMetadataResponse) Serialize() string {
b, err := json.Marshal(r.metadata)
if err != nil {
return ""
}

return string(b)
}

func TestPreserveErrTypeAuthInfo(t *testing.T) {
invoked := make(chan struct{}, 2)
metadataResponse := &oauthMetadataResponse{}

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
invoked <- struct{}{}

if r.URL.Path == tokencmd.OauthMetadataEndpoint {
w.WriteHeader(http.StatusOK)
io.WriteString(w, metadataResponse.Serialize())
return
}
w.WriteHeader(http.StatusUnauthorized)
}))

metadataResponse.metadata = &util.OauthAuthorizationServerMetadata{
Issuer: server.URL,
AuthorizationEndpoint: server.URL + "/oauth/authorize",
TokenEndpoint: server.URL + "/oauth/token",
CodeChallengeMethodsSupported: []string{"plain", "S256"},
}
defer server.Close()

clientConfig := &restclient.Config{
Host: server.URL,
}

options := newLoginOptions(server.URL, "test", "test", false)
options.Config = clientConfig

err := options.gatherAuthInfo()
if err == nil {
t.Fatalf("expecting unauthorized error when gathering authinfo")
}

if !kapierrs.IsUnauthorized(err) {
t.Fatalf("expecting error of type metav1.StatusReasonUnauthorized, but got %v", reflect.TypeOf(err))
}
}

func newLoginOptions(server string, username string, password string, insecure bool) *LoginOptions {
flagset := pflag.NewFlagSet("test-flags", pflag.ContinueOnError)
flags := []string{}
clientConfig := defaultClientConfig(flagset)
flagset.Parse(flags)

startingConfig, _ := clientConfig.RawConfig()

loginOptions := &LoginOptions{
Server: server,
StartingKubeConfig: &startingConfig,
Username: username,
Password: password,
InsecureTLS: insecure,

Out: ioutil.Discard,
ErrOut: ioutil.Discard,
}

return loginOptions
}

func defaultClientConfig(flags *pflag.FlagSet) kclientcmd.ClientConfig {
loadingRules := &kclientcmd.ClientConfigLoadingRules{ExplicitPath: ""}

flags.StringVar(&loadingRules.ExplicitPath, config.OpenShiftConfigFlagName, "", "Path to the config file to use for CLI requests.")

overrides := &kclientcmd.ConfigOverrides{}
overrideFlags := kclientcmd.RecommendedConfigOverrideFlags("")
overrideFlags.ContextOverrideFlags.Namespace.ShortName = "n"
kclientcmd.BindOverrideFlags(overrides, flags, overrideFlags)

clientConfig := kclientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides)

return clientConfig
}

func TestDialToHTTPSServer(t *testing.T) {
invoked := make(chan struct{}, 1)
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit df1e76b

Please sign in to comment.