Skip to content

Commit 5693551

Browse files
committed
add unit test
1 parent b366e91 commit 5693551

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

pkg/cmd/util/tokencmd/request_token.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const (
3030
// See IETF Draft:
3131
// https://tools.ietf.org/html/draft-ietf-oauth-discovery-04#section-2
3232
// Copied from pkg/cmd/server/origin/nonapiserver.go
33-
oauthMetadataEndpoint = "/.well-known/oauth-authorization-server"
33+
OauthMetadataEndpoint = "/.well-known/oauth-authorization-server"
3434

3535
// openShiftCLIClientID is the name of the CLI OAuth client, copied from pkg/oauth/apiserver/auth.go
3636
openShiftCLIClientID = "openshift-challenging-client"
@@ -108,7 +108,7 @@ func (o *RequestTokenOptions) SetDefaultOsinConfig() error {
108108
if err != nil {
109109
return err
110110
}
111-
resp, err := request(rt, strings.TrimRight(o.ClientConfig.Host, "/")+oauthMetadataEndpoint, nil)
111+
resp, err := request(rt, strings.TrimRight(o.ClientConfig.Host, "/")+OauthMetadataEndpoint, nil)
112112
if err != nil {
113113
return err
114114
}

pkg/cmd/util/tokencmd/request_token_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ func TestSetDefaultOsinConfig(t *testing.T) {
631631
t.Errorf("%s: Expected GET, got %s", tc.name, req.Method)
632632
return
633633
}
634-
if req.URL.Path != oauthMetadataEndpoint {
634+
if req.URL.Path != OauthMetadataEndpoint {
635635
t.Errorf("%s: Expected metadata endpoint, got %s", tc.name, req.URL.Path)
636636
return
637637
}

pkg/oc/cli/cmd/login/loginoptions_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package login
22

33
import (
4+
"bytes"
45
"crypto/tls"
6+
"encoding/json"
57
"fmt"
8+
"io/ioutil"
69
"net/http"
710
"net/http/httptest"
811
"regexp"
@@ -12,8 +15,11 @@ import (
1215
"github.com/MakeNowJust/heredoc"
1316

1417
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
18+
"github.com/openshift/origin/pkg/cmd/util/tokencmd"
19+
"github.com/openshift/origin/pkg/oauth/util"
1520
"github.com/openshift/origin/pkg/oc/cli/config"
1621

22+
kapierrs "k8s.io/apimachinery/pkg/api/errors"
1723
restclient "k8s.io/client-go/rest"
1824
kclientcmdapi "k8s.io/client-go/tools/clientcmd/api"
1925
)
@@ -256,6 +262,77 @@ func TestDialToHTTPServer(t *testing.T) {
256262
}
257263
}
258264

265+
type oauthMetadataResponse struct {
266+
metadata *util.OauthAuthorizationServerMetadata
267+
}
268+
269+
func (r *oauthMetadataResponse) Serialize() ([]byte, error) {
270+
b, err := json.Marshal(r.metadata)
271+
if err != nil {
272+
return []byte{}, err
273+
}
274+
275+
return b, nil
276+
}
277+
278+
func TestPreserveErrTypeAuthInfo(t *testing.T) {
279+
invoked := make(chan struct{}, 2)
280+
oauthResponse := []byte{}
281+
282+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
283+
select {
284+
case invoked <- struct{}{}:
285+
default:
286+
t.Fatalf("unexpected request handled by test server: %v: %v", r.Method, r.URL)
287+
}
288+
289+
if r.URL.Path == tokencmd.OauthMetadataEndpoint {
290+
w.WriteHeader(http.StatusOK)
291+
w.Write(oauthResponse)
292+
return
293+
}
294+
w.WriteHeader(http.StatusUnauthorized)
295+
}))
296+
defer server.Close()
297+
298+
metadataResponse := &oauthMetadataResponse{}
299+
metadataResponse.metadata = &util.OauthAuthorizationServerMetadata{
300+
Issuer: server.URL,
301+
AuthorizationEndpoint: server.URL + "/oauth/authorize",
302+
TokenEndpoint: server.URL + "/oauth/token",
303+
CodeChallengeMethodsSupported: []string{"plain", "S256"},
304+
}
305+
306+
oauthResponse, err := metadataResponse.Serialize()
307+
if err != nil {
308+
t.Fatalf("unexpected error: %v", err)
309+
}
310+
311+
options := &LoginOptions{
312+
Server: server.URL,
313+
StartingKubeConfig: &kclientcmdapi.Config{},
314+
Username: "test",
315+
Password: "test",
316+
Reader: bytes.NewReader([]byte{}),
317+
318+
Config: &restclient.Config{
319+
Host: server.URL,
320+
},
321+
322+
Out: ioutil.Discard,
323+
ErrOut: ioutil.Discard,
324+
}
325+
326+
err = options.gatherAuthInfo()
327+
if err == nil {
328+
t.Fatalf("expecting unauthorized error when gathering authinfo")
329+
}
330+
331+
if !kapierrs.IsUnauthorized(err) {
332+
t.Fatalf("expecting error of type metav1.StatusReasonUnauthorized, but got %T", err)
333+
}
334+
}
335+
259336
func TestDialToHTTPSServer(t *testing.T) {
260337
invoked := make(chan struct{}, 1)
261338
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)