diff --git a/internal/api/app_test.go b/internal/api/app_test.go index e7b65eb..2dd6206 100644 --- a/internal/api/app_test.go +++ b/internal/api/app_test.go @@ -450,8 +450,7 @@ func TestClient_DeleteApp(t *testing.T) { } func TestClient_DeveloperAppInstall_RequestAppApproval(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { app types.App orgGrantWorkspaceID string teamID string @@ -459,33 +458,30 @@ func TestClient_DeveloperAppInstall_RequestAppApproval(t *testing.T) { wantErr bool errMessage string }{ - { - name: `Standalone workspace, AAA is requested \ - (workspace ID passed into apps.approvals.requests.create)`, + `Standalone workspace, AAA is requested \ + (workspace ID passed into apps.approvals.requests.create)`: { app: types.App{AppID: "A1234", TeamID: "T1234"}, orgGrantWorkspaceID: "", teamID: "T1234", requestJSON: `{"app":"A1234","reason":"This request has been automatically generated according to project environment settings.","team_id":"T1234"}`, }, - { - name: `User tried to install to a single workspace in an org, AAA is requested \ - (workspace ID passed into apps.approvals.requests.create)`, + `User tried to install to a single workspace in an org, AAA is requested \ + (workspace ID passed into apps.approvals.requests.create)`: { app: types.App{AppID: "A1234", EnterpriseID: "E1234", TeamID: "E1234"}, orgGrantWorkspaceID: "T1234", teamID: "T1234", requestJSON: `{"app":"A1234","reason":"This request has been automatically generated according to project environment settings.","team_id":"T1234"}`, }, - { - name: `User tried to install to all workspaces in an org, AAA is requested \ - (no team_id passed into apps.approvals.requests.create so it will default to creating a request for the auth team ie. the org)`, + `User tried to install to all workspaces in an org, AAA is requested \ + (no team_id passed into apps.approvals.requests.create so it will default to creating a request for the auth team ie. the org)`: { app: types.App{AppID: "A1234", EnterpriseID: "E1234", TeamID: "E1234"}, orgGrantWorkspaceID: "all", teamID: "E1234", requestJSON: `{"app":"A1234","reason":"This request has been automatically generated according to project environment settings."}`, }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { ctx := slackcontext.MockContext(t.Context()) // prepare diff --git a/internal/api/debug_test.go b/internal/api/debug_test.go index d166bde..14c23a4 100644 --- a/internal/api/debug_test.go +++ b/internal/api/debug_test.go @@ -24,89 +24,73 @@ import ( func Test_RedactPII(t *testing.T) { home, _ := os.UserHomeDir() - tests := []struct { - name string + tests := map[string]struct { text string expected string }{ - { - name: "Simple case", + "Simple case": { text: "hello world", expected: "hello world", }, - { - name: "Preserve the word XOXP", + "Preserve the word XOXP": { text: "This is an XOXP token", expected: "This is an XOXP token", }, - { - name: "Redact actual XOXP token", + "Redact actual XOXP token": { text: `{"ok":true,"token":"xoxe.xoxp-123","refresh_token":"xoxe-1-123","team_id":"T0123","user_id":"U0123", "xxtoken":"123"}`, expected: `{"ok":true,"token":"...","refresh_token":"...","team_id":"T0123","user_id":"U0123", "xxtoken":"..."}`, }, - { - name: "Redact home directory", + "Redact home directory": { text: "found authorizations at " + home + "/.slack/credentials.json reading", expected: `found authorizations at .../.slack/credentials.json reading`, }, - { - name: "Redact username with single quotes", + "Redact username with single quotes": { text: `'user':'username'`, expected: `'user':"..."`, }, - { - name: "Redact username with double quotes", + "Redact username with double quotes": { text: `"user":"username"`, expected: `"user":"..."`, }, - { - name: "Redact username with no quotes", + "Redact username with no quotes": { text: `user:username`, expected: `user:username`, }, - { - name: "Redact username in http response", + "Redact username in http response": { text: `{"ok":true,"token":"xoxe.xoxp-123","refresh_token":"xoxe-1-123","team_id":"T0123","user_id":"U0123", "xxtoken":"123", "user":"username"}`, expected: `{"ok":true,"token":"...","refresh_token":"...","team_id":"T0123","user_id":"U0123", "xxtoken":"...", "user":"..."}`, }, - { - name: "Preserve the word XOXE", + "Preserve the word XOXE": { text: "This is an XOXE token", expected: "This is an XOXE token", }, - { - name: "Redact actual token in HTTP request", + "Redact actual token in HTTP request": { text: "HTTP Request Body:refresh_token=xoxe-1", expected: `HTTP Request Body:refresh_token=...`, }, - { - name: "Display Trace ID in log", + "Display Trace ID in log": { text: "TraceID: 123", expected: `TraceID: 123`, }, - { - name: "Display Team ID in log", + "Display Team ID in log": { text: "TeamID: T123", expected: `TeamID: T123`, }, - { - name: "Display User ID in log", + "Display User ID in log": { text: "UserID: U123", expected: `UserID: U123`, }, - { - name: "Display Slack-CLI version in log", + "Display Slack-CLI version in log": { text: "Slack-CLI Version: v1.10.0", expected: `Slack-CLI Version: v1.10.0`, }, - { - name: "Display user's OS in log", + "Display user's OS in log": { text: "Operating System (OS): darwin", expected: `Operating System (OS): darwin`, }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { redacted := goutils.RedactPII(tc.text) require.Equal(t, redacted, tc.expected) }) diff --git a/internal/app/app_client_test.go b/internal/app/app_client_test.go index 805e39a..de582b1 100644 --- a/internal/app/app_client_test.go +++ b/internal/app/app_client_test.go @@ -692,46 +692,48 @@ func TestAppClient_CleanupAppsJSONFiles(t *testing.T) { }, } - for _, tc := range tests { - ac, _, _, pathToAppsJSON, pathToDevAppsJSON, teardown := setup(t) - defer teardown(t) - ctx := slackcontext.MockContext(t.Context()) - - err := afero.WriteFile(ac.fs, pathToAppsJSON, tc.appsJSON, 0600) - require.NoError(t, err) - err = afero.WriteFile(ac.fs, pathToDevAppsJSON, tc.devAppsJSON, 0600) - require.NoError(t, err) - - _, err = ac.fs.Stat(pathToAppsJSON) - require.NoError(t, err, "failed to access the apps.json file") - deployedApps, _, err := ac.GetDeployedAll(ctx) - require.NoError(t, err) - - _, err = ac.fs.Stat(pathToDevAppsJSON) - require.NoError(t, err, "failed to access the apps.dev.json file") - localApps, err := ac.GetLocalAll(ctx) - require.NoError(t, err) - - ac.CleanUp() - - dotSlackFolder := filepath.Dir(pathToAppsJSON) - _, err = ac.fs.Stat(dotSlackFolder) - require.NoError(t, err, "failed to access the .slack directory") - - appsJSON, err := afero.ReadFile(ac.fs, pathToAppsJSON) - if len(deployedApps) == 0 { - require.ErrorIs(t, err, os.ErrNotExist, "apps.json was not deleted") - } else { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + ac, _, _, pathToAppsJSON, pathToDevAppsJSON, teardown := setup(t) + defer teardown(t) + ctx := slackcontext.MockContext(t.Context()) + + err := afero.WriteFile(ac.fs, pathToAppsJSON, tc.appsJSON, 0600) + require.NoError(t, err) + err = afero.WriteFile(ac.fs, pathToDevAppsJSON, tc.devAppsJSON, 0600) + require.NoError(t, err) + + _, err = ac.fs.Stat(pathToAppsJSON) require.NoError(t, err, "failed to access the apps.json file") - assert.Equal(t, appsJSONExample, appsJSON) - } + deployedApps, _, err := ac.GetDeployedAll(ctx) + require.NoError(t, err) - devAppsJSON, err := afero.ReadFile(ac.fs, pathToDevAppsJSON) - if len(localApps) == 0 { - require.ErrorIs(t, err, os.ErrNotExist, "apps.dev.json was not deleted") - } else { + _, err = ac.fs.Stat(pathToDevAppsJSON) require.NoError(t, err, "failed to access the apps.dev.json file") - assert.Equal(t, devAppsJSONExample, devAppsJSON) - } + localApps, err := ac.GetLocalAll(ctx) + require.NoError(t, err) + + ac.CleanUp() + + dotSlackFolder := filepath.Dir(pathToAppsJSON) + _, err = ac.fs.Stat(dotSlackFolder) + require.NoError(t, err, "failed to access the .slack directory") + + appsJSON, err := afero.ReadFile(ac.fs, pathToAppsJSON) + if len(deployedApps) == 0 { + require.ErrorIs(t, err, os.ErrNotExist, "apps.json was not deleted") + } else { + require.NoError(t, err, "failed to access the apps.json file") + assert.Equal(t, appsJSONExample, appsJSON) + } + + devAppsJSON, err := afero.ReadFile(ac.fs, pathToDevAppsJSON) + if len(localApps) == 0 { + require.ErrorIs(t, err, os.ErrNotExist, "apps.dev.json was not deleted") + } else { + require.NoError(t, err, "failed to access the apps.dev.json file") + assert.Equal(t, devAppsJSONExample, devAppsJSON) + } + }) } } diff --git a/internal/app/app_test.go b/internal/app/app_test.go index bc2b6bd..916a887 100644 --- a/internal/app/app_test.go +++ b/internal/app/app_test.go @@ -128,39 +128,34 @@ func Test_App_UpdateDefaultProjectFiles(t *testing.T) { } func Test_RegexReplaceAppNameInManifest(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { src []byte appName string expectedSrc []byte }{ - { - name: "manifest.json is validate", + "manifest.json is validate": { src: testdata.ManifestJSON, appName: "vibrant-butterfly-1234", expectedSrc: testdata.ManifestJSONAppName, }, - { - name: "manifest.js is validate", + "manifest.js is validate": { src: testdata.ManifestJS, appName: "vibrant-butterfly-1234", expectedSrc: testdata.ManifestJSAppName, }, - { - name: "manifest.ts is validate", + "manifest.ts is validate": { src: testdata.ManifestTS, appName: "vibrant-butterfly-1234", expectedSrc: testdata.ManifestTSAppName, }, - { - name: "manifest.ts with sdk is validate", + "manifest.ts with sdk is validate": { src: testdata.ManifestSDKTS, appName: "vibrant-butterfly-1234", expectedSrc: testdata.ManifestSDKTSAppName, }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { actualSrc := regexReplaceAppNameInManifest(tc.src, tc.appName) require.Equal(t, tc.expectedSrc, actualSrc) }) diff --git a/internal/goutils/strings_test.go b/internal/goutils/strings_test.go index dba9a2e..236af8b 100644 --- a/internal/goutils/strings_test.go +++ b/internal/goutils/strings_test.go @@ -144,224 +144,181 @@ func Test_addLogWhenValExist(t *testing.T) { } func Test_RedactPII(t *testing.T) { home, _ := os.UserHomeDir() - tests := []struct { - name string + tests := map[string]struct { text string expected string }{ - { - name: "Simple case", + "Simple case": { text: "hello world", expected: "hello world", }, - { - name: "Preserve the word XOXP", + "Preserve the word XOXP": { text: "This is an XOXP token", expected: "This is an XOXP token", }, - { - name: "Redact actual XOXP token", + "Redact actual XOXP token": { text: `{"ok":true,"token":"xoxe.xoxp-123","refresh_token":"xoxe-1-123","team_id":"T0123","user_id":"U0123", "xxtoken":"123"}`, expected: `{"ok":true,"token":"...","refresh_token":"...","team_id":"T0123","user_id":"U0123", "xxtoken":"..."}`, }, - { - name: "Redact home directory", + "Redact home directory": { text: "found authorizations at " + home + "/.slack/credentials.json reading", expected: `found authorizations at .../.slack/credentials.json reading`, }, - { - name: "Redact username with single quotes", + "Redact username with single quotes": { text: `'user':'username'`, expected: `'user':"..."`, }, - { - name: "Redact username with double quotes", + "Redact username with double quotes": { text: `"user":"username"`, expected: `"user":"..."`, }, - { - name: "Don't redact username with no quotes", + "Don't redact username with no quotes": { text: `user:username`, expected: `user:username`, }, - { - name: "Redact username in http response", + "Redact username in http response": { text: `{"ok":true,"token":"xoxe.xoxp-123","refresh_token":"xoxe-1-123","team_id":"T0123","user_id":"U0123", "xxtoken":"123", "user":"username"}`, expected: `{"ok":true,"token":"...","refresh_token":"...","team_id":"T0123","user_id":"U0123", "xxtoken":"...", "user":"..."}`, }, - { - name: "Preserve the word XOXE", + "Preserve the word XOXE": { text: "This is an XOXE token", expected: "This is an XOXE token", }, - { - name: "Redact actual token in HTTP request", + "Redact actual token in HTTP request": { text: "HTTP Request Body:refresh_token=xoxe-1", expected: `HTTP Request Body:refresh_token=...`, }, - { - name: "App Token (xapp) as JSON value", + "App Token (xapp) as JSON value": { text: `{"ok":true,"api_access_tokens":{"app_level":"xapp-1-A000-1111-ABCD"}}`, expected: `{"ok":true,"api_access_tokens":{"app_level":"..."}}`, }, - { - name: "App Token (xapp) as open text", + "App Token (xapp) as open text": { text: `Logging app token xapp-1-A000-1111-ABCD in the output`, expected: `Logging app token ... in the output`, }, - { - name: "Bot Token (xoxb) as JSON value", + "Bot Token (xoxb) as JSON value": { text: `{"ok":true,"api_access_tokens":{"bot":"xoxb-1111-2222-ABCD"}}`, expected: `{"ok":true,"api_access_tokens":{"bot":"..."}}`, }, - { - name: "Bot Token (xoxb) as text", + "Bot Token (xoxb) as text": { text: `Logging bot token xoxb-1111-2222-ABCD in the output`, expected: `Logging bot token ... in the output`, }, - { - name: "User Token (xoxp) as JSON value", + "User Token (xoxp) as JSON value": { text: `{"ok":true,"api_access_tokens":{"user":"xoxp-1111-2222-ABCD"}}`, expected: `{"ok":true,"api_access_tokens":{"user":"..."}}`, }, - { - name: "User Token (xoxp) as text", + "User Token (xoxp) as text": { text: `Logging user token xoxp-1111-2222-ABCD in the output`, expected: `Logging user token ... in the output`, }, - { - name: "Refresh Token (xoxe) as JSON value", + "Refresh Token (xoxe) as JSON value": { text: `{"ok":true,"refresh_token":"xoxe-1-A000-1111-ABCD}`, expected: `{"ok":true,"refresh_token":"..."}`, }, - { - name: "Refresh Token (xoxe) as text", + "Refresh Token (xoxe) as text": { text: `Logging user token xoxe-1-A000-1111-ABCD in the output`, expected: `Logging user token ... in the output`, }, - { - name: "Display Trace ID in log", + "Display Trace ID in log": { text: "TraceID: 123", expected: `TraceID: 123`, }, - { - name: "Display Team ID in log", + "Display Team ID in log": { text: "TeamID: T123", expected: `TeamID: T123`, }, - { - name: "Display User ID in log", + "Display User ID in log": { text: "UserID: U123", expected: `UserID: U123`, }, - { - name: "Display Slack-CLI version in log", + "Display Slack-CLI version in log": { text: "Slack-CLI Version: v1.10.0", expected: `Slack-CLI Version: v1.10.0`, }, - { - name: "Display user's OS in log", + "Display user's OS in log": { text: "Operating System (OS): darwin", expected: `Operating System (OS): darwin`, }, - { - name: "Escape oauth_authorize_url", + "Escape oauth_authorize_url": { text: `"oauth_authorize_url":"www.fake.com"`, expected: `"oauth_authorize_url":"..."`, }, - { - name: "Escape provider_key", + "Escape provider_key": { text: `"provider_key":"provider_key"`, expected: `"provider_key":"..."`, }, - { - name: "Escape authorizations", + "Escape authorizations": { text: `"authorizations":"authorizations"`, expected: `"authorizations":"..."`, }, - { - name: "Escape authorization_url", + "Escape authorization_url": { text: `"authorization_url":"authorization_url"`, expected: `"authorization_url":"..."`, }, - { - name: "Escape secret", + "Escape secret": { text: `"secret":"secret"`, expected: `"secret":"..."`, }, - { - name: "Escape secret with prefix", + "Escape secret with prefix": { text: `"client_secret":"secret"`, expected: `"client_secret":"..."`, }, - { - name: "Escape client_id", + "Escape client_id": { text: `"client_id":"client_id"`, expected: `"client_id":"..."`, }, - { - name: "Escape variables", + "Escape variables": { text: `"variables":[{"foo":"bar", "hello":"world"}]`, expected: `"variables":"..."`, }, - { - name: "Escape sensitive data from mock HTTP response", + "Escape sensitive data from mock HTTP response": { text: `{"ok":true,"app_id":"A123","credentials":{"client_id":"123","client_secret":"123","verification_token":"123","signing_secret":"123"},"oauth_authorize_url":"123":\/\/slack.com\/oauth\/v2\/authorize?client_id=123&scope=commands,chat:write"}`, expected: `{"ok":true,"app_id":"A123","credentials":{"client_id":"...","client_secret":"...","verification_token":"...","signing_secret":"..."},"oauth_authorize_url":"...":\/\/slack.com\/oauth\/v2\/authorize?client_id=...&scope=commands,chat:write"}`, }, - { - name: "Escape from `Command` for external-auth add-secret", + "Escape from `Command` for external-auth add-secret": { text: `slack external-auth add-secret --provider google --secret 123abcd`, expected: "slack external-auth add-secret ...", }, - { - name: "Escape from `Command` for var add", + "Escape from `Command` for var add": { text: `slack var add topsecret 123`, expected: `slack var add ...`, }, - { - name: "Escape from `Command` for external-auth add", + "Escape from `Command` for external-auth add": { text: `slack external-auth add topsecret 123`, expected: `slack external-auth add ...`, }, - { - name: "Escape from `Command` for var remove", + "Escape from `Command` for var remove": { text: `slack var remove topsecret 123`, expected: `slack var remove ...`, }, - { - name: "Escape from `Command` for env add", + "Escape from `Command` for env add": { text: `slack env add topsecret 123`, expected: `slack env add ...`, }, - { - name: "Escape from `Command` for env remove", + "Escape from `Command` for env remove": { text: `slack env remove topsecret 123`, expected: `slack env remove ...`, }, - { - name: "Escape from `Command` for vars add", + "Escape from `Command` for vars add": { text: `slack vars add topsecret 123`, expected: `slack vars add ...`, }, - { - name: "Escape from `Command` for vars remove", + "Escape from `Command` for vars remove": { text: `slack vars remove topsecret 123`, expected: `slack vars remove ...`, }, - { - name: "Escape from `Command` for variables add", + "Escape from `Command` for variables add": { text: `slack variables add topsecret 123`, expected: `slack variables add ...`, }, - { - name: "Escape from `Command` for variables remove", + "Escape from `Command` for variables remove": { text: `slack variables remove topsecret 123`, expected: `slack variables remove ...`, }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { redacted := RedactPII(tc.text) require.Equal(t, tc.expected, redacted) }) diff --git a/internal/pkg/platform/activity_test.go b/internal/pkg/platform/activity_test.go index 8c36090..5f064ae 100644 --- a/internal/pkg/platform/activity_test.go +++ b/internal/pkg/platform/activity_test.go @@ -32,13 +32,11 @@ import ( ) func Test_prettifyActivity(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { activity api.Activity expectedResults []string }{ - { - name: "nil payload should result in valid log without nulls", + "nil payload should result in valid log without nulls": { activity: api.Activity{ TraceID: "a123", Level: "info", @@ -54,12 +52,10 @@ func Test_prettifyActivity(t *testing.T) { "Trace=a123", }, }, - { - name: "empty activity should not contain nulls", + "empty activity should not contain nulls": { activity: api.Activity{}, }, - { - name: "unknown EventType should result in valid log without nulls", + "unknown EventType should result in valid log without nulls": { activity: api.Activity{ TraceID: "a123", Level: "info", @@ -80,8 +76,8 @@ func Test_prettifyActivity(t *testing.T) { }, }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { actualResult := prettifyActivity(tc.activity) for _, expectedResult := range tc.expectedResults { require.Contains(t, actualResult, expectedResult) diff --git a/internal/runtime/runtime_test.go b/internal/runtime/runtime_test.go index 1652a3b..9d41c0e 100644 --- a/internal/runtime/runtime_test.go +++ b/internal/runtime/runtime_test.go @@ -58,34 +58,29 @@ func Test_Runtime_New(t *testing.T) { } func Test_Runtime_NewDetectProject(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { sdkConfig hooks.SDKCLIConfig expectedRuntimeType Runtime }{ - { - name: "Deno SDK", + "Deno SDK": { sdkConfig: hooks.SDKCLIConfig{Runtime: "deno"}, expectedRuntimeType: deno.New(), }, - { - name: "Bolt for JavaScript", + "Bolt for JavaScript": { sdkConfig: hooks.SDKCLIConfig{Runtime: "node"}, expectedRuntimeType: node.New(), }, - { - name: "Bolt for Python", + "Bolt for Python": { sdkConfig: hooks.SDKCLIConfig{Runtime: "python"}, expectedRuntimeType: python.New(), }, - { - name: "Unsupported Runtime", + "Unsupported Runtime": { sdkConfig: hooks.SDKCLIConfig{Runtime: ""}, expectedRuntimeType: nil, }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { // Setup ctx := slackcontext.MockContext(t.Context()) fs := afero.NewMemMapFs()