github.com/argoproj/argo-cd/v3@v3.2.1/cmd/argocd/commands/login_test.go (about) 1 package commands 2 3 import ( 4 "io" 5 "os" 6 "testing" 7 8 utilio "github.com/argoproj/argo-cd/v3/util/io" 9 10 "github.com/golang-jwt/jwt/v5" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func captureStdout(callback func()) (string, error) { 15 oldStdout := os.Stdout 16 oldStderr := os.Stderr 17 r, w, err := os.Pipe() 18 if err != nil { 19 return "", err 20 } 21 os.Stdout = w 22 defer func() { 23 os.Stdout = oldStdout 24 os.Stderr = oldStderr 25 }() 26 27 callback() 28 utilio.Close(w) 29 30 data, err := io.ReadAll(r) 31 if err != nil { 32 return "", err 33 } 34 return string(data), err 35 } 36 37 func Test_userDisplayName_email(t *testing.T) { 38 claims := jwt.MapClaims{"iss": "qux", "sub": "foo", "email": "firstname.lastname@example.com", "groups": []string{"baz"}} 39 actualName := userDisplayName(claims) 40 expectedName := "firstname.lastname@example.com" 41 assert.Equal(t, expectedName, actualName) 42 } 43 44 func Test_userDisplayName_name(t *testing.T) { 45 claims := jwt.MapClaims{"iss": "qux", "sub": "foo", "name": "Firstname Lastname", "groups": []string{"baz"}} 46 actualName := userDisplayName(claims) 47 expectedName := "Firstname Lastname" 48 assert.Equal(t, expectedName, actualName) 49 } 50 51 func Test_userDisplayName_sub(t *testing.T) { 52 claims := jwt.MapClaims{"iss": "qux", "sub": "foo", "groups": []string{"baz"}} 53 actualName := userDisplayName(claims) 54 expectedName := "foo" 55 assert.Equal(t, expectedName, actualName) 56 } 57 58 func Test_userDisplayName_federatedClaims(t *testing.T) { 59 claims := jwt.MapClaims{ 60 "iss": "qux", 61 "sub": "foo", 62 "groups": []string{"baz"}, 63 "federated_claims": map[string]any{ 64 "connector_id": "dex", 65 "user_id": "ldap-123", 66 }, 67 } 68 actualName := userDisplayName(claims) 69 expectedName := "ldap-123" 70 assert.Equal(t, expectedName, actualName) 71 } 72 73 func Test_ssoAuthFlow_ssoLaunchBrowser_false(t *testing.T) { 74 out, _ := captureStdout(func() { 75 ssoAuthFlow("http://test-sso-browser-flow.com", false) 76 }) 77 78 assert.Contains(t, out, "To authenticate, copy-and-paste the following URL into your preferred browser: http://test-sso-browser-flow.com") 79 }