github.com/confluentinc/cli@v1.100.0/test/api-key_test.go (about) 1 package test 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 8 "github.com/confluentinc/cli/internal/pkg/config" 9 "github.com/confluentinc/cli/internal/pkg/config/load" 10 v3 "github.com/confluentinc/cli/internal/pkg/config/v3" 11 "github.com/confluentinc/cli/internal/pkg/log" 12 ) 13 14 func (s *CLITestSuite) TestAPIKey() { 15 // TODO: add --config flag to all commands or ENVVAR instead of using standard config file location 16 tests := []CLITest{ 17 {args: "api-key create --resource lkc-bob", login: "default", fixture: "apikey/1.golden"}, // MYKEY3 18 {args: "api-key list --resource lkc-bob", fixture: "apikey/2.golden"}, 19 {args: "api-key list --resource lkc-abc", fixture: "apikey/3.golden"}, 20 {args: "api-key update MYKEY1 --description first-key", fixture: "apikey/40.golden"}, 21 {args: "api-key list --resource lkc-bob", fixture: "apikey/41.golden"}, 22 23 // list json and yaml output 24 {args: "api-key list", fixture: "apikey/28.golden"}, 25 {args: "api-key list -o json", fixture: "apikey/29.golden"}, 26 {args: "api-key list -o yaml", fixture: "apikey/30.golden"}, 27 28 // create api key for kafka cluster 29 {args: "api-key list --resource lkc-cool1", fixture: "apikey/4.golden"}, 30 {args: "api-key create --description my-cool-app --resource lkc-cool1", fixture: "apikey/5.golden"}, // MYKEY4 31 {args: "api-key list --resource lkc-cool1", fixture: "apikey/6.golden"}, 32 33 // create api key for other kafka cluster 34 {args: "api-key create --description my-other-app --resource lkc-other1", fixture: "apikey/7.golden"}, // MYKEY5 35 {args: "api-key list --resource lkc-cool1", fixture: "apikey/6.golden"}, 36 {args: "api-key list --resource lkc-other1", fixture: "apikey/8.golden"}, 37 38 // create api key for ksql cluster 39 {args: "api-key create --description my-ksql-app --resource lksqlc-ksql1", fixture: "apikey/9.golden"}, // MYKEY6 40 {args: "api-key list --resource lkc-cool1", fixture: "apikey/6.golden"}, 41 {args: "api-key list --resource lksqlc-ksql1", fixture: "apikey/10.golden"}, 42 43 // create api key for schema registry cluster 44 {args: "api-key create --resource lsrc-1", fixture: "apikey/20.golden"}, // MYKEY7 45 {args: "api-key list --resource lsrc-1", fixture: "apikey/21.golden"}, 46 47 // create cloud api key 48 {args: "api-key create --resource cloud", fixture: "apikey/34.golden"}, // MYKEY8 49 {args: "api-key list --resource cloud", fixture: "apikey/35.golden"}, 50 51 // use an api key for kafka cluster 52 {args: "api-key use MYKEY4 --resource lkc-cool1", fixture: "apikey/45.golden"}, 53 {args: "api-key list --resource lkc-cool1", fixture: "apikey/11.golden"}, 54 55 // use an api key for other kafka cluster 56 {args: "api-key use MYKEY5 --resource lkc-other1", fixture: "apikey/46.golden"}, 57 {args: "api-key list --resource lkc-cool1", fixture: "apikey/11.golden"}, 58 {args: "api-key list --resource lkc-other1", fixture: "apikey/12.golden"}, 59 60 // delete api key that is in use 61 {args: "api-key delete MYKEY5", fixture: "apikey/42.golden"}, 62 {args: "api-key list --resource lkc-other1", fixture: "apikey/43.golden"}, 63 64 // store an api-key for kafka cluster 65 {args: "api-key store UIAPIKEY100 @test/fixtures/input/UIAPISECRET100.txt --resource lkc-cool1", fixture: "apikey/47.golden"}, 66 {args: "api-key list --resource lkc-cool1", fixture: "apikey/11.golden"}, 67 68 // store an api-key for other kafka cluster 69 {args: "api-key store UIAPIKEY101 @test/fixtures/input/UIAPISECRET101.txt --resource lkc-other1", fixture: "apikey/48.golden"}, 70 {args: "api-key list --resource lkc-cool1", fixture: "apikey/11.golden"}, 71 {args: "api-key list --resource lkc-other1", fixture: "apikey/44.golden"}, 72 73 // store exists already error 74 {args: "api-key store UIAPIKEY101 @test/fixtures/input/UIAPISECRET101.txt --resource lkc-other1", fixture: "apikey/override-error.golden", wantErrCode: 1}, 75 76 // store an api-key for ksql cluster (not yet supported) 77 //{args: "api-key store UIAPIKEY103 UIAPISECRET103 --resource lksqlc-ksql1", fixture: "empty.golden"}, 78 //{args: "api-key list --resource lksqlc-ksql1", fixture: "apikey/10.golden"}, 79 // TODO: change test back once api-key store and use command allows for non kafka clusters 80 {args: "api-key store UIAPIKEY103 UIAPISECRET103 --resource lksqlc-ksql1", fixture: "apikey/36.golden", wantErrCode: 1}, 81 {args: "api-key use UIAPIKEY103 --resource lksqlc-ksql1", fixture: "apikey/36.golden", wantErrCode: 1}, 82 83 // list all api-keys 84 {args: "api-key list", fixture: "apikey/22.golden"}, 85 86 // list api-keys belonging to currently logged in user 87 {args: "api-key list --current-user", fixture: "apikey/23.golden"}, 88 89 // create api-key for a service account 90 {args: "api-key create --resource lkc-cool1 --service-account 99", fixture: "apikey/24.golden"}, 91 {args: "api-key list --current-user", fixture: "apikey/23.golden"}, 92 {args: "api-key list", fixture: "apikey/25.golden"}, 93 {args: "api-key list --service-account 99", fixture: "apikey/26.golden"}, 94 {args: "api-key list --resource lkc-cool1", fixture: "apikey/27.golden"}, 95 {args: "api-key list --resource lkc-cool1 --service-account 99", fixture: "apikey/26.golden"}, 96 97 // create json yaml output 98 {args: "api-key create --description human-output --resource lkc-other1", fixture: "apikey/31.golden"}, 99 {args: "api-key create --description json-output --resource lkc-other1 -o json", fixture: "apikey/32.golden"}, 100 {args: "api-key create --description yaml-output --resource lkc-other1 -o yaml", fixture: "apikey/33.golden"}, 101 102 // store: error handling 103 {name: "error if storing unknown api key", args: "api-key store UNKNOWN @test/fixtures/input/UIAPISECRET100.txt --resource lkc-cool1", fixture: "apikey/15.golden"}, 104 {name: "error if storing api key with existing secret", args: "api-key store UIAPIKEY100 NEWSECRET --resource lkc-cool1", fixture: "apikey/16.golden"}, 105 {name: "succeed if forced to overwrite existing secret", args: "api-key store -f UIAPIKEY100 NEWSECRET --resource lkc-cool1", fixture: "apikey/49.golden", 106 wantFunc: func(t *testing.T) { 107 logger := log.New() 108 cfg := v3.New(&config.Params{ 109 CLIName: "ccloud", 110 MetricSink: nil, 111 Logger: logger, 112 }) 113 cfg, err := load.LoadAndMigrate(cfg) 114 require.NoError(t, err) 115 ctx := cfg.Context() 116 require.NotNil(t, ctx) 117 kcc := ctx.KafkaClusterContext.GetKafkaClusterConfig("lkc-cool1") 118 pair := kcc.APIKeys["UIAPIKEY100"] 119 require.NotNil(t, pair) 120 require.Equal(t, "NEWSECRET", pair.Secret) 121 }}, 122 123 // use: error handling 124 {name: "error if using non-existent api-key", args: "api-key use UNKNOWN --resource lkc-cool1", fixture: "apikey/17.golden"}, 125 {name: "error if using api-key for wrong cluster", args: "api-key use MYKEY2 --resource lkc-cool1", fixture: "apikey/18.golden"}, 126 {name: "error if using api-key without existing secret", args: "api-key use UIAPIKEY103 --resource lkc-cool1", fixture: "apikey/19.golden"}, 127 128 // more errors 129 {args: "api-key use UIAPIKEY103", fixture: "apikey/37.golden", wantErrCode: 1}, 130 {args: "api-key create", fixture: "apikey/38.golden", wantErrCode: 1}, 131 {args: "api-key use UIAPIKEY103 --resource lkc-unknown", fixture: "apikey/resource-unknown-error.golden", wantErrCode: 1}, 132 {args: "api-key create --resource lkc-unknown", fixture: "apikey/resource-unknown-error.golden", wantErrCode: 1}, 133 } 134 135 resetConfiguration(s.T(), "ccloud") 136 kafkaURL := serveKafkaAPI(s.T()).URL 137 loginURL := serve(s.T(), kafkaURL).URL 138 139 for _, tt := range tests { 140 tt.workflow = true 141 s.runCcloudTest(tt, loginURL) 142 } 143 }