Skip to content

Commit 3cc0305

Browse files
authored
refactor(tests): convert the higher risk slice-to-map table tests (#324)
* refactor(tests): use tests/tc naming in table tests using the slice pattern * test: update map pattern table tests to use common naming pattern * test: update slice pattern table tests to map pattern (simple use-cases) * refactor(tests): cleanup formatting * refactor(tests): convert the low-risk slice-to-map table tests * test: change TODO to a regular comment * refactor(tests): convert the medium-risk slice-to-map table tests * refactor(tests): convert the medium-high risk slice-to-map table tests * refactor(tests): update test name casing * refactor(tests): convert the higher risk slice-to-map table tests * refactor(tests): fix test not wrapped in t.Run
1 parent ca9db20 commit 3cc0305

7 files changed

Lines changed: 132 additions & 207 deletions

File tree

internal/api/app_test.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -450,42 +450,38 @@ func TestClient_DeleteApp(t *testing.T) {
450450
}
451451

452452
func TestClient_DeveloperAppInstall_RequestAppApproval(t *testing.T) {
453-
tests := []struct {
454-
name string
453+
tests := map[string]struct {
455454
app types.App
456455
orgGrantWorkspaceID string
457456
teamID string
458457
requestJSON string
459458
wantErr bool
460459
errMessage string
461460
}{
462-
{
463-
name: `Standalone workspace, AAA is requested \
464-
(workspace ID passed into apps.approvals.requests.create)`,
461+
`Standalone workspace, AAA is requested \
462+
(workspace ID passed into apps.approvals.requests.create)`: {
465463
app: types.App{AppID: "A1234", TeamID: "T1234"},
466464
orgGrantWorkspaceID: "",
467465
teamID: "T1234",
468466
requestJSON: `{"app":"A1234","reason":"This request has been automatically generated according to project environment settings.","team_id":"T1234"}`,
469467
},
470-
{
471-
name: `User tried to install to a single workspace in an org, AAA is requested \
472-
(workspace ID passed into apps.approvals.requests.create)`,
468+
`User tried to install to a single workspace in an org, AAA is requested \
469+
(workspace ID passed into apps.approvals.requests.create)`: {
473470
app: types.App{AppID: "A1234", EnterpriseID: "E1234", TeamID: "E1234"},
474471
orgGrantWorkspaceID: "T1234",
475472
teamID: "T1234",
476473
requestJSON: `{"app":"A1234","reason":"This request has been automatically generated according to project environment settings.","team_id":"T1234"}`,
477474
},
478-
{
479-
name: `User tried to install to all workspaces in an org, AAA is requested \
480-
(no team_id passed into apps.approvals.requests.create so it will default to creating a request for the auth team ie. the org)`,
475+
`User tried to install to all workspaces in an org, AAA is requested \
476+
(no team_id passed into apps.approvals.requests.create so it will default to creating a request for the auth team ie. the org)`: {
481477
app: types.App{AppID: "A1234", EnterpriseID: "E1234", TeamID: "E1234"},
482478
orgGrantWorkspaceID: "all",
483479
teamID: "E1234",
484480
requestJSON: `{"app":"A1234","reason":"This request has been automatically generated according to project environment settings."}`,
485481
},
486482
}
487-
for _, tc := range tests {
488-
t.Run(tc.name, func(t *testing.T) {
483+
for name, tc := range tests {
484+
t.Run(name, func(t *testing.T) {
489485
ctx := slackcontext.MockContext(t.Context())
490486

491487
// prepare

internal/api/debug_test.go

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,89 +24,73 @@ import (
2424

2525
func Test_RedactPII(t *testing.T) {
2626
home, _ := os.UserHomeDir()
27-
tests := []struct {
28-
name string
27+
tests := map[string]struct {
2928
text string
3029
expected string
3130
}{
32-
{
33-
name: "Simple case",
31+
"Simple case": {
3432
text: "hello world",
3533
expected: "hello world",
3634
},
37-
{
38-
name: "Preserve the word XOXP",
35+
"Preserve the word XOXP": {
3936
text: "This is an XOXP token",
4037
expected: "This is an XOXP token",
4138
},
42-
{
43-
name: "Redact actual XOXP token",
39+
"Redact actual XOXP token": {
4440
text: `{"ok":true,"token":"xoxe.xoxp-123","refresh_token":"xoxe-1-123","team_id":"T0123","user_id":"U0123", "xxtoken":"123"}`,
4541
expected: `{"ok":true,"token":"...","refresh_token":"...","team_id":"T0123","user_id":"U0123", "xxtoken":"..."}`,
4642
},
47-
{
48-
name: "Redact home directory",
43+
"Redact home directory": {
4944
text: "found authorizations at " + home + "/.slack/credentials.json reading",
5045
expected: `found authorizations at .../.slack/credentials.json reading`,
5146
},
52-
{
53-
name: "Redact username with single quotes",
47+
"Redact username with single quotes": {
5448
text: `'user':'username'`,
5549
expected: `'user':"..."`,
5650
},
57-
{
58-
name: "Redact username with double quotes",
51+
"Redact username with double quotes": {
5952
text: `"user":"username"`,
6053
expected: `"user":"..."`,
6154
},
62-
{
63-
name: "Redact username with no quotes",
55+
"Redact username with no quotes": {
6456
text: `user:username`,
6557
expected: `user:username`,
6658
},
67-
{
68-
name: "Redact username in http response",
59+
"Redact username in http response": {
6960
text: `{"ok":true,"token":"xoxe.xoxp-123","refresh_token":"xoxe-1-123","team_id":"T0123","user_id":"U0123", "xxtoken":"123", "user":"username"}`,
7061
expected: `{"ok":true,"token":"...","refresh_token":"...","team_id":"T0123","user_id":"U0123", "xxtoken":"...", "user":"..."}`,
7162
},
72-
{
73-
name: "Preserve the word XOXE",
63+
"Preserve the word XOXE": {
7464
text: "This is an XOXE token",
7565
expected: "This is an XOXE token",
7666
},
77-
{
78-
name: "Redact actual token in HTTP request",
67+
"Redact actual token in HTTP request": {
7968
text: "HTTP Request Body:refresh_token=xoxe-1",
8069
expected: `HTTP Request Body:refresh_token=...`,
8170
},
82-
{
83-
name: "Display Trace ID in log",
71+
"Display Trace ID in log": {
8472
text: "TraceID: 123",
8573
expected: `TraceID: 123`,
8674
},
87-
{
88-
name: "Display Team ID in log",
75+
"Display Team ID in log": {
8976
text: "TeamID: T123",
9077
expected: `TeamID: T123`,
9178
},
92-
{
93-
name: "Display User ID in log",
79+
"Display User ID in log": {
9480
text: "UserID: U123",
9581
expected: `UserID: U123`,
9682
},
97-
{
98-
name: "Display Slack-CLI version in log",
83+
"Display Slack-CLI version in log": {
9984
text: "Slack-CLI Version: v1.10.0",
10085
expected: `Slack-CLI Version: v1.10.0`,
10186
},
102-
{
103-
name: "Display user's OS in log",
87+
"Display user's OS in log": {
10488
text: "Operating System (OS): darwin",
10589
expected: `Operating System (OS): darwin`,
10690
},
10791
}
108-
for _, tc := range tests {
109-
t.Run(tc.name, func(t *testing.T) {
92+
for name, tc := range tests {
93+
t.Run(name, func(t *testing.T) {
11094
redacted := goutils.RedactPII(tc.text)
11195
require.Equal(t, redacted, tc.expected)
11296
})

internal/app/app_client_test.go

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -692,46 +692,48 @@ func TestAppClient_CleanupAppsJSONFiles(t *testing.T) {
692692
},
693693
}
694694

695-
for _, tc := range tests {
696-
ac, _, _, pathToAppsJSON, pathToDevAppsJSON, teardown := setup(t)
697-
defer teardown(t)
698-
ctx := slackcontext.MockContext(t.Context())
699-
700-
err := afero.WriteFile(ac.fs, pathToAppsJSON, tc.appsJSON, 0600)
701-
require.NoError(t, err)
702-
err = afero.WriteFile(ac.fs, pathToDevAppsJSON, tc.devAppsJSON, 0600)
703-
require.NoError(t, err)
704-
705-
_, err = ac.fs.Stat(pathToAppsJSON)
706-
require.NoError(t, err, "failed to access the apps.json file")
707-
deployedApps, _, err := ac.GetDeployedAll(ctx)
708-
require.NoError(t, err)
709-
710-
_, err = ac.fs.Stat(pathToDevAppsJSON)
711-
require.NoError(t, err, "failed to access the apps.dev.json file")
712-
localApps, err := ac.GetLocalAll(ctx)
713-
require.NoError(t, err)
714-
715-
ac.CleanUp()
716-
717-
dotSlackFolder := filepath.Dir(pathToAppsJSON)
718-
_, err = ac.fs.Stat(dotSlackFolder)
719-
require.NoError(t, err, "failed to access the .slack directory")
720-
721-
appsJSON, err := afero.ReadFile(ac.fs, pathToAppsJSON)
722-
if len(deployedApps) == 0 {
723-
require.ErrorIs(t, err, os.ErrNotExist, "apps.json was not deleted")
724-
} else {
695+
for name, tc := range tests {
696+
t.Run(name, func(t *testing.T) {
697+
ac, _, _, pathToAppsJSON, pathToDevAppsJSON, teardown := setup(t)
698+
defer teardown(t)
699+
ctx := slackcontext.MockContext(t.Context())
700+
701+
err := afero.WriteFile(ac.fs, pathToAppsJSON, tc.appsJSON, 0600)
702+
require.NoError(t, err)
703+
err = afero.WriteFile(ac.fs, pathToDevAppsJSON, tc.devAppsJSON, 0600)
704+
require.NoError(t, err)
705+
706+
_, err = ac.fs.Stat(pathToAppsJSON)
725707
require.NoError(t, err, "failed to access the apps.json file")
726-
assert.Equal(t, appsJSONExample, appsJSON)
727-
}
708+
deployedApps, _, err := ac.GetDeployedAll(ctx)
709+
require.NoError(t, err)
728710

729-
devAppsJSON, err := afero.ReadFile(ac.fs, pathToDevAppsJSON)
730-
if len(localApps) == 0 {
731-
require.ErrorIs(t, err, os.ErrNotExist, "apps.dev.json was not deleted")
732-
} else {
711+
_, err = ac.fs.Stat(pathToDevAppsJSON)
733712
require.NoError(t, err, "failed to access the apps.dev.json file")
734-
assert.Equal(t, devAppsJSONExample, devAppsJSON)
735-
}
713+
localApps, err := ac.GetLocalAll(ctx)
714+
require.NoError(t, err)
715+
716+
ac.CleanUp()
717+
718+
dotSlackFolder := filepath.Dir(pathToAppsJSON)
719+
_, err = ac.fs.Stat(dotSlackFolder)
720+
require.NoError(t, err, "failed to access the .slack directory")
721+
722+
appsJSON, err := afero.ReadFile(ac.fs, pathToAppsJSON)
723+
if len(deployedApps) == 0 {
724+
require.ErrorIs(t, err, os.ErrNotExist, "apps.json was not deleted")
725+
} else {
726+
require.NoError(t, err, "failed to access the apps.json file")
727+
assert.Equal(t, appsJSONExample, appsJSON)
728+
}
729+
730+
devAppsJSON, err := afero.ReadFile(ac.fs, pathToDevAppsJSON)
731+
if len(localApps) == 0 {
732+
require.ErrorIs(t, err, os.ErrNotExist, "apps.dev.json was not deleted")
733+
} else {
734+
require.NoError(t, err, "failed to access the apps.dev.json file")
735+
assert.Equal(t, devAppsJSONExample, devAppsJSON)
736+
}
737+
})
736738
}
737739
}

internal/app/app_test.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,39 +128,34 @@ func Test_App_UpdateDefaultProjectFiles(t *testing.T) {
128128
}
129129

130130
func Test_RegexReplaceAppNameInManifest(t *testing.T) {
131-
tests := []struct {
132-
name string
131+
tests := map[string]struct {
133132
src []byte
134133
appName string
135134
expectedSrc []byte
136135
}{
137-
{
138-
name: "manifest.json is validate",
136+
"manifest.json is validate": {
139137
src: testdata.ManifestJSON,
140138
appName: "vibrant-butterfly-1234",
141139
expectedSrc: testdata.ManifestJSONAppName,
142140
},
143-
{
144-
name: "manifest.js is validate",
141+
"manifest.js is validate": {
145142
src: testdata.ManifestJS,
146143
appName: "vibrant-butterfly-1234",
147144
expectedSrc: testdata.ManifestJSAppName,
148145
},
149-
{
150-
name: "manifest.ts is validate",
146+
"manifest.ts is validate": {
151147
src: testdata.ManifestTS,
152148
appName: "vibrant-butterfly-1234",
153149
expectedSrc: testdata.ManifestTSAppName,
154150
},
155-
{
156-
name: "manifest.ts with sdk is validate",
151+
"manifest.ts with sdk is validate": {
157152
src: testdata.ManifestSDKTS,
158153
appName: "vibrant-butterfly-1234",
159154
expectedSrc: testdata.ManifestSDKTSAppName,
160155
},
161156
}
162-
for _, tc := range tests {
163-
t.Run(tc.name, func(t *testing.T) {
157+
for name, tc := range tests {
158+
t.Run(name, func(t *testing.T) {
164159
actualSrc := regexReplaceAppNameInManifest(tc.src, tc.appName)
165160
require.Equal(t, tc.expectedSrc, actualSrc)
166161
})

0 commit comments

Comments
 (0)