Skip to content

Commit 1a24880

Browse files
committed
add unit test
1 parent b366e91 commit 1a24880

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

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

Lines changed: 80 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,12 +15,18 @@ import (
1215
"github.com/MakeNowJust/heredoc"
1316

1417
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
18+
"github.com/openshift/origin/pkg/oauth/util"
1519
"github.com/openshift/origin/pkg/oc/cli/config"
1620

21+
kapierrs "k8s.io/apimachinery/pkg/api/errors"
1722
restclient "k8s.io/client-go/rest"
1823
kclientcmdapi "k8s.io/client-go/tools/clientcmd/api"
1924
)
2025

26+
const (
27+
oauthMetadataEndpoint = "/.well-known/oauth-authorization-server"
28+
)
29+
2130
func TestNormalizeServerURL(t *testing.T) {
2231
testCases := []struct {
2332
originalServerURL string
@@ -256,6 +265,77 @@ func TestDialToHTTPServer(t *testing.T) {
256265
}
257266
}
258267

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

0 commit comments

Comments
 (0)