github.com/argoproj/argo-cd@v1.8.7/test/e2e/accounts_test.go (about) 1 package e2e 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/argoproj/pkg/errors" 8 "github.com/stretchr/testify/assert" 9 "google.golang.org/grpc/codes" 10 "google.golang.org/grpc/status" 11 12 argocdclient "github.com/argoproj/argo-cd/pkg/apiclient" 13 "github.com/argoproj/argo-cd/pkg/apiclient/session" 14 . "github.com/argoproj/argo-cd/test/e2e/fixture" 15 "github.com/argoproj/argo-cd/util/io" 16 ) 17 18 func TestCreateAndUseAccount(t *testing.T) { 19 EnsureCleanState(t) 20 21 output, err := RunCli("account", "list") 22 errors.CheckError(err) 23 24 assert.Equal(t, `NAME ENABLED CAPABILITIES 25 admin true login`, output) 26 27 SetAccounts(map[string][]string{ 28 "test": {"login", "apiKey"}, 29 }) 30 31 output, err = RunCli("account", "list") 32 errors.CheckError(err) 33 34 assert.Equal(t, `NAME ENABLED CAPABILITIES 35 admin true login 36 test true login, apiKey`, output) 37 38 token, err := RunCli("account", "generate-token", "--account", "test") 39 errors.CheckError(err) 40 41 clientOpts := ArgoCDClientset.ClientOptions() 42 clientOpts.AuthToken = token 43 testAccountClientset := argocdclient.NewClientOrDie(&clientOpts) 44 45 closer, client := testAccountClientset.NewSessionClientOrDie() 46 defer io.Close(closer) 47 48 info, err := client.GetUserInfo(context.Background(), &session.GetUserInfoRequest{}) 49 assert.NoError(t, err) 50 51 assert.Equal(t, info.Username, "test") 52 } 53 54 func TestLoginBadCredentials(t *testing.T) { 55 EnsureCleanState(t) 56 57 closer, sessionClient := ArgoCDClientset.NewSessionClientOrDie() 58 defer io.Close(closer) 59 60 requests := []session.SessionCreateRequest{{ 61 Username: "user-does-not-exist", Password: "some-password", 62 }, { 63 Username: "admin", Password: "bad-password", 64 }} 65 66 for _, r := range requests { 67 _, err := sessionClient.Create(context.Background(), &r) 68 if !assert.Error(t, err) { 69 return 70 } 71 errStatus, ok := status.FromError(err) 72 if !assert.True(t, ok) { 73 return 74 } 75 assert.Equal(t, codes.Unauthenticated, errStatus.Code()) 76 assert.Equal(t, "Invalid username or password", errStatus.Message()) 77 } 78 }