|
| 1 | +package ingestionapikey |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stackvista/stackstate-cli/generated/stackstate_api" |
| 7 | + "github.com/stackvista/stackstate-cli/internal/di" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestIngestApiKeyGenerate(t *testing.T) { |
| 12 | + cli := di.NewMockDeps(t) |
| 13 | + cmd := CreateCommand(&cli.Deps) |
| 14 | + |
| 15 | + cli.MockClient.ApiMocks.IngestionApiKeyApi.GenerateIngestionApiKeyResponse.Result = stackstate_api.GeneratedIngestionApiKeyResponse{ |
| 16 | + Name: "test-token", |
| 17 | + ApiKey: "test-token-key", |
| 18 | + Expiration: int64p(1590105600000), |
| 19 | + Description: stringp("test-token-description"), |
| 20 | + } |
| 21 | + |
| 22 | + di.ExecuteCommandWithContextUnsafe(&cli.Deps, cmd, "--name", "test-token", "--description", "test-token-description", "--expiration", "2020-05-22") |
| 23 | + |
| 24 | + checkCreateCall(t, cli.MockClient.ApiMocks.IngestionApiKeyApi.GenerateIngestionApiKeyCalls, "test-token", stringp("test-token-description"), int64p(1590105600000)) |
| 25 | + assert.Equal(t, []string{"Ingestion API Key generated: \x1b[37mtest-token-key\x1b[0m\n"}, *cli.MockPrinter.SuccessCalls) |
| 26 | +} |
| 27 | + |
| 28 | +func TestIngestApiKeyGenerateOnlyRequriedFlags(t *testing.T) { |
| 29 | + cli := di.NewMockDeps(t) |
| 30 | + cmd := CreateCommand(&cli.Deps) |
| 31 | + |
| 32 | + cli.MockClient.ApiMocks.IngestionApiKeyApi.GenerateIngestionApiKeyResponse.Result = stackstate_api.GeneratedIngestionApiKeyResponse{ |
| 33 | + Name: "test-token2", |
| 34 | + ApiKey: "test-token2-key", |
| 35 | + } |
| 36 | + |
| 37 | + di.ExecuteCommandWithContextUnsafe(&cli.Deps, cmd, "--name", "test-token2") |
| 38 | + |
| 39 | + checkCreateCall(t, cli.MockClient.ApiMocks.IngestionApiKeyApi.GenerateIngestionApiKeyCalls, "test-token2", nil, nil) |
| 40 | + assert.Equal(t, []string{"Ingestion API Key generated: \x1b[37mtest-token2-key\x1b[0m\n"}, *cli.MockPrinter.SuccessCalls) |
| 41 | +} |
| 42 | + |
| 43 | +func TestIngestApiKeyGenerateJSON(t *testing.T) { |
| 44 | + cli := di.NewMockDeps(t) |
| 45 | + cmd := CreateCommand(&cli.Deps) |
| 46 | + |
| 47 | + r := &stackstate_api.GeneratedIngestionApiKeyResponse{ |
| 48 | + Name: "test-token", |
| 49 | + ApiKey: "test-token-key", |
| 50 | + Expiration: int64p(1590105600000), |
| 51 | + Description: stringp("test-token-description"), |
| 52 | + } |
| 53 | + |
| 54 | + cli.MockClient.ApiMocks.IngestionApiKeyApi.GenerateIngestionApiKeyResponse.Result = *r |
| 55 | + |
| 56 | + di.ExecuteCommandWithContextUnsafe(&cli.Deps, cmd, "--name", "test-token", "--description", "test-token-description", "--expiration", "2020-05-22", "-o", "json") |
| 57 | + |
| 58 | + checkCreateCall(t, cli.MockClient.ApiMocks.IngestionApiKeyApi.GenerateIngestionApiKeyCalls, "test-token", stringp("test-token-description"), int64p(1590105600000)) |
| 59 | + assert.Equal(t, |
| 60 | + []map[string]interface{}{{ |
| 61 | + "ingestion-api-key": r, |
| 62 | + }}, |
| 63 | + *cli.MockPrinter.PrintJsonCalls, |
| 64 | + ) |
| 65 | + assert.False(t, cli.MockPrinter.HasNonJsonCalls) |
| 66 | +} |
| 67 | + |
| 68 | +func int64p(i int64) *int64 { |
| 69 | + return &i |
| 70 | +} |
| 71 | + |
| 72 | +func stringp(i string) *string { |
| 73 | + return &i |
| 74 | +} |
| 75 | + |
| 76 | +func checkCreateCall(t *testing.T, calls *[]stackstate_api.GenerateIngestionApiKeyCall, name string, description *string, expiration *int64) { |
| 77 | + assert.Len(t, *calls, 1) |
| 78 | + |
| 79 | + call := (*calls)[0] |
| 80 | + assert.Equal(t, name, call.PgenerateIngestionApiKeyRequest.Name) |
| 81 | + |
| 82 | + if description != nil { |
| 83 | + assert.Equal(t, *description, *call.PgenerateIngestionApiKeyRequest.Description) |
| 84 | + } else { |
| 85 | + assert.Nil(t, call.PgenerateIngestionApiKeyRequest.Description) |
| 86 | + } |
| 87 | + |
| 88 | + if expiration != nil { |
| 89 | + assert.Equal(t, *expiration, *call.PgenerateIngestionApiKeyRequest.Expiration) |
| 90 | + } else { |
| 91 | + assert.Nil(t, call.PgenerateIngestionApiKeyRequest.Expiration) |
| 92 | + } |
| 93 | +} |
0 commit comments