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 5693551
Show file tree
Hide file tree
Showing 3 changed files with 80 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
77 changes: 77 additions & 0 deletions pkg/oc/cli/cmd/login/loginoptions_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package login

import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"regexp"
Expand All @@ -12,8 +15,11 @@ import (
"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"
kclientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
Expand Down Expand Up @@ -256,6 +262,77 @@ func TestDialToHTTPServer(t *testing.T) {
}
}

type oauthMetadataResponse struct {
metadata *util.OauthAuthorizationServerMetadata
}

func (r *oauthMetadataResponse) Serialize() ([]byte, error) {
b, err := json.Marshal(r.metadata)
if err != nil {
return []byte{}, err
}

return b, nil
}

func TestPreserveErrTypeAuthInfo(t *testing.T) {
invoked := make(chan struct{}, 2)
oauthResponse := []byte{}

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
select {
case invoked <- struct{}{}:
default:
t.Fatalf("unexpected request handled by test server: %v: %v", r.Method, r.URL)
}

if r.URL.Path == tokencmd.OauthMetadataEndpoint {
w.WriteHeader(http.StatusOK)
w.Write(oauthResponse)
return
}
w.WriteHeader(http.StatusUnauthorized)
}))
defer server.Close()

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

oauthResponse, err := metadataResponse.Serialize()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

options := &LoginOptions{
Server: server.URL,
StartingKubeConfig: &kclientcmdapi.Config{},
Username: "test",
Password: "test",
Reader: bytes.NewReader([]byte{}),

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

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

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 %T", err)
}
}

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 5693551

Please sign in to comment.