Skip to content

Commit 4098541

Browse files
authored
test: mock context in TableTestCommand (#20)
* refactor: Add slackcontext package to DRY context management * refactor: Revert changes to unrelated error codes * test: mock context in TableTestCommand
1 parent b82e4c3 commit 4098541

34 files changed

+219
-210
lines changed

cmd/app/add_test.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ func TestAppAddCommandPreRun(t *testing.T) {
7676
testutil.TableTestCommand(t, testutil.CommandTests{
7777
"errors if not run in a project directory": {
7878
ExpectedError: slackerror.New(slackerror.ErrInvalidAppDirectory),
79-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
79+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
8080
},
8181
},
8282
"proceeds if run in a project directory": {
8383
ExpectedError: nil,
84-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
84+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
8585
cf.SDKConfig.WorkingDirectory = "."
8686
},
8787
},
@@ -100,8 +100,7 @@ func TestAppAddCommandPreRun(t *testing.T) {
100100
Message: "Cannot install apps with manifests sourced from app settings",
101101
},
102102
}),
103-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
104-
ctx := context.Background()
103+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
105104
cf.SDKConfig.WorkingDirectory = "."
106105
cm.AddDefaultMocks()
107106
cm.Config.ExperimentsFlag = append(cm.Config.ExperimentsFlag, experiment.BoltFrameworks)
@@ -113,8 +112,7 @@ func TestAppAddCommandPreRun(t *testing.T) {
113112
},
114113
"proceeds if manifest.source is local with the bolt experiment": {
115114
ExpectedError: nil,
116-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
117-
ctx := context.Background()
115+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
118116
cf.SDKConfig.WorkingDirectory = "."
119117
cm.AddDefaultMocks()
120118
cm.Config.ExperimentsFlag = append(cm.Config.ExperimentsFlag, experiment.BoltFrameworks)
@@ -137,7 +135,7 @@ func TestAppAddCommand(t *testing.T) {
137135
"adds a new deployed app": {
138136
CmdArgs: []string{},
139137
ExpectedOutputs: []string{"Creating app manifest", "Installing"},
140-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
138+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
141139
prepareAddMocks(t, cf, cm)
142140

143141
// Mock TeamSelector prompt to return "team1"
@@ -209,7 +207,7 @@ func TestAppAddCommand(t *testing.T) {
209207
"updates an existing deployed app": {
210208
CmdArgs: []string{},
211209
ExpectedOutputs: []string{"Updated app manifest"},
212-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
210+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
213211
prepareAddMocks(t, cf, cm)
214212

215213
// Mock TeamSelector prompt to return "team1"
@@ -291,7 +289,7 @@ func TestAppAddCommand(t *testing.T) {
291289
"errors if authentication for the team is missing": {
292290
CmdArgs: []string{},
293291
ExpectedError: slackerror.New(slackerror.ErrCredentialsNotFound),
294-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
292+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
295293
prepareAddMocks(t, cf, cm)
296294
appSelectMock := prompts.NewAppSelectMock()
297295
teamAppSelectPromptFunc = appSelectMock.TeamAppSelectPrompt
@@ -301,7 +299,7 @@ func TestAppAddCommand(t *testing.T) {
301299
"adds a new deployed app to an org with a workspace grant": {
302300
CmdArgs: []string{"--" + cmdutil.OrgGrantWorkspaceFlag, "T123"},
303301
ExpectedOutputs: []string{"Creating app manifest", "Installing"},
304-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
302+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
305303
prepareAddMocks(t, cf, cm)
306304
// Select workspace
307305
appSelectMock := prompts.NewAppSelectMock()
@@ -361,7 +359,7 @@ func TestAppAddCommand(t *testing.T) {
361359
"When admin approval request is pending, outputs instructions": {
362360
CmdArgs: []string{"--" + cmdutil.OrgGrantWorkspaceFlag, "T123"},
363361
ExpectedOutputs: []string{"Creating app manifest", "Installing", "Your request to install the app is pending", "complete installation by re-running"},
364-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
362+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
365363
prepareAddMocks(t, cf, cm)
366364
// Select workspace
367365
appSelectMock := prompts.NewAppSelectMock()
@@ -417,7 +415,7 @@ func TestAppAddCommand(t *testing.T) {
417415
"When admin approval request is cancelled, outputs instructions": {
418416
CmdArgs: []string{"--" + cmdutil.OrgGrantWorkspaceFlag, "T123"},
419417
ExpectedOutputs: []string{"Creating app manifest", "Installing", "Your request to install the app has been cancelled"},
420-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
418+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
421419
prepareAddMocks(t, cf, cm)
422420
// Select workspace
423421
appSelectMock := prompts.NewAppSelectMock()

cmd/app/delete_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package app
1616

1717
import (
18+
"context"
1819
"fmt"
1920
"testing"
2021

@@ -49,7 +50,7 @@ func TestAppsDeleteCommand(t *testing.T) {
4950
testutil.TableTestCommand(t, testutil.CommandTests{
5051
"happy path; delete the deployed app": {
5152
CmdArgs: []string{},
52-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
53+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
5354
prepareCommonDeleteMocks(t, cf, cm)
5455
// Mock App Selection
5556
appSelectMock := prompts.NewAppSelectMock()
@@ -83,7 +84,7 @@ func TestAppsDeleteCommand(t *testing.T) {
8384
},
8485
"happy path; delete the local app": {
8586
CmdArgs: []string{},
86-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
87+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
8788
prepareCommonDeleteMocks(t, cf, cm)
8889
// Mock App Selection
8990
appSelectMock := prompts.NewAppSelectMock()
@@ -116,7 +117,7 @@ func TestAppsDeleteCommand(t *testing.T) {
116117
},
117118
"sad path; deleting the deployed app fails": {
118119
CmdArgs: []string{},
119-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
120+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
120121
prepareCommonDeleteMocks(t, cf, cm)
121122
// Mock App Selection
122123
appSelectMock := prompts.NewAppSelectMock()
@@ -143,7 +144,7 @@ func TestAppsDeleteCommand(t *testing.T) {
143144
"errors if authentication for the team is missing": {
144145
CmdArgs: []string{},
145146
ExpectedError: slackerror.New(slackerror.ErrCredentialsNotFound),
146-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
147+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
147148
prepareCommonDeleteMocks(t, cf, cm)
148149
cm.ApiInterface.On("ValidateSession", mock.Anything, mock.Anything).Return(api.AuthSession{}, nil)
149150
appSelectMock := prompts.NewAppSelectMock()

cmd/app/link_test.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var mockLinkSlackAuth2 = types.SlackAuth{
5454
func Test_Apps_Link(t *testing.T) {
5555
testutil.TableTestCommand(t, testutil.CommandTests{
5656
"saves information about the provided deployed app": {
57-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
57+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
5858
cm.AuthInterface.On("Auths", mock.Anything).Return([]types.SlackAuth{
5959
mockLinkSlackAuth2,
6060
mockLinkSlackAuth1,
@@ -111,7 +111,7 @@ func Test_Apps_Link(t *testing.T) {
111111
},
112112
},
113113
"saves information about the provided local app": {
114-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
114+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
115115
cm.AuthInterface.On("Auths", mock.Anything).Return([]types.SlackAuth{
116116
mockLinkSlackAuth2,
117117
mockLinkSlackAuth1,
@@ -170,8 +170,7 @@ func Test_Apps_Link(t *testing.T) {
170170
},
171171
},
172172
"avoids overwriting an app saved in json without confirmation": {
173-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
174-
ctx := context.Background()
173+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
175174
cm.AuthInterface.On("Auths", mock.Anything).Return([]types.SlackAuth{
176175
mockLinkSlackAuth1,
177176
mockLinkSlackAuth2,
@@ -240,8 +239,7 @@ func Test_Apps_Link(t *testing.T) {
240239
WithRemediation("Remove the app from this project or try again with --force"),
241240
},
242241
"avoids overwriting a matching app id for the team without confirmation": {
243-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
244-
ctx := context.Background()
242+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
245243
cm.AuthInterface.On("Auths", mock.Anything).Return([]types.SlackAuth{
246244
mockLinkSlackAuth1,
247245
mockLinkSlackAuth2,
@@ -316,8 +314,7 @@ func Test_Apps_Link(t *testing.T) {
316314
WithRemediation("Remove the app from this project or try again with --force"),
317315
},
318316
"completes overwriting an app saved in json with confirmation": {
319-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
320-
ctx := context.Background()
317+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
321318
cm.AuthInterface.On("Auths", mock.Anything).Return([]types.SlackAuth{
322319
mockLinkSlackAuth1,
323320
mockLinkSlackAuth2,
@@ -385,7 +382,7 @@ func Test_Apps_Link(t *testing.T) {
385382
},
386383
},
387384
"refuses to write an app with app id not existing upstream": {
388-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
385+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
389386
cm.AuthInterface.On("Auths", mock.Anything).Return([]types.SlackAuth{
390387
mockLinkSlackAuth1,
391388
mockLinkSlackAuth2,
@@ -433,7 +430,7 @@ func Test_Apps_Link(t *testing.T) {
433430
ExpectedError: slackerror.New(slackerror.ErrAppNotFound),
434431
},
435432
"accept manifest source prompt and saves information about the provided deployed app": {
436-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
433+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
437434
cm.AuthInterface.On("Auths", mock.Anything).Return([]types.SlackAuth{
438435
mockLinkSlackAuth2,
439436
mockLinkSlackAuth1,
@@ -506,7 +503,7 @@ func Test_Apps_Link(t *testing.T) {
506503
},
507504
},
508505
"decline manifest source prompt should not link app": {
509-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
506+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
510507
cm.AddDefaultMocks()
511508
setupAppLinkCommandMocks(t, cm, cf)
512509
// Set manifest source to project to trigger confirmation prompt

cmd/app/uninstall_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestAppsUninstall(t *testing.T) {
4747

4848
testutil.TableTestCommand(t, testutil.CommandTests{
4949
"Successfully uninstall": {
50-
Setup: func(t *testing.T, clientsMock *shared.ClientsMock, clients *shared.ClientFactory) {
50+
Setup: func(t *testing.T, ctx context.Context, clientsMock *shared.ClientsMock, clients *shared.ClientFactory) {
5151
prepareCommonUninstallMocks(clients, clientsMock)
5252
clientsMock.ApiInterface.On("UninstallApp", mock.Anything, mock.Anything, fakeAppID, fakeAppTeamID).
5353
Return(nil).Once()
@@ -57,7 +57,7 @@ func TestAppsUninstall(t *testing.T) {
5757
},
5858
},
5959
"Successfully uninstall with a get-manifest hook error": {
60-
Setup: func(t *testing.T, clientsMock *shared.ClientsMock, clients *shared.ClientFactory) {
60+
Setup: func(t *testing.T, ctx context.Context, clientsMock *shared.ClientsMock, clients *shared.ClientFactory) {
6161
prepareCommonUninstallMocks(clients, clientsMock)
6262
clientsMock.ApiInterface.On("UninstallApp", mock.Anything, mock.Anything, fakeAppID, fakeAppTeamID).
6363
Return(nil).Once()
@@ -72,7 +72,7 @@ func TestAppsUninstall(t *testing.T) {
7272
},
7373
"Fail to uninstall due to API error": {
7474
ExpectedError: slackerror.New("something went wrong"),
75-
Setup: func(t *testing.T, clientsMock *shared.ClientsMock, clients *shared.ClientFactory) {
75+
Setup: func(t *testing.T, ctx context.Context, clientsMock *shared.ClientsMock, clients *shared.ClientFactory) {
7676
prepareCommonUninstallMocks(clients, clientsMock)
7777
clientsMock.ApiInterface.On("UninstallApp", mock.Anything, mock.Anything, fakeAppID, fakeAppTeamID).
7878
Return(slackerror.New("something went wrong")).Once()
@@ -81,7 +81,7 @@ func TestAppsUninstall(t *testing.T) {
8181
"errors if authentication for the team is missing": {
8282
CmdArgs: []string{},
8383
ExpectedError: slackerror.New(slackerror.ErrCredentialsNotFound),
84-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
84+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
8585
prepareCommonUninstallMocks(cf, cm)
8686
appSelectMock := prompts.NewAppSelectMock()
8787
uninstallAppSelectPromptFunc = appSelectMock.AppSelectPrompt

cmd/auth/login_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package auth
1616

1717
import (
18+
"context"
1819
"testing"
1920

2021
"github.com/slackapi/slack-cli/internal/api"
@@ -52,7 +53,7 @@ func TestLoginCommmand(t *testing.T) {
5253
"deprecated auth flag is noted in outputs": {
5354
CmdArgs: []string{"--auth", "xoxp-example"},
5455
ExpectedOutputs: []string{deprecatedUserTokenMessage},
55-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
56+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
5657
cm.ApiInterface.On("ValidateSession", mock.Anything, mock.Anything).Return(api.AuthSession{
5758
UserID: &mockOrgAuth.UserID,
5859
TeamID: &mockOrgAuth.TeamID,
@@ -79,7 +80,7 @@ func TestLoginCommmand(t *testing.T) {
7980
"suggests creating a new app if not in a project": {
8081
CmdArgs: []string{"--ticket=example", "--challenge=tictactoe"},
8182
ExpectedStdoutOutputs: []string{"Get started by creating a new app"},
82-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
83+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
8384
cm.ApiInterface.On(
8485
"ExchangeAuthTicket",
8586
mock.Anything,
@@ -109,7 +110,7 @@ func TestLoginCommmand(t *testing.T) {
109110
"suggests listing existing apps from the project": {
110111
CmdArgs: []string{"--ticket", "example", "--challenge", "tictactoe"},
111112
ExpectedStdoutOutputs: []string{"Review existing installations of the app"},
112-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
113+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
113114
cm.ApiInterface.On(
114115
"ExchangeAuthTicket",
115116
mock.Anything,
@@ -139,7 +140,7 @@ func TestLoginCommmand(t *testing.T) {
139140
},
140141
"happy path login with prompt flow should pass challenge code to ExchangeAuthTicket API": {
141142
CmdArgs: []string{},
142-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
143+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
143144
cm.ApiInterface.On("GenerateAuthTicket", mock.Anything, mock.Anything, mock.Anything).Return(api.GenerateAuthTicketResult{}, nil)
144145
cm.IO.On("InputPrompt", mock.Anything, "Enter challenge code", iostreams.InputPromptConfig{
145146
Required: true,
@@ -162,7 +163,7 @@ func TestLoginCommmand(t *testing.T) {
162163
},
163164
"should explode if ExchangeAuthTicket API fails": {
164165
CmdArgs: []string{},
165-
Setup: func(t *testing.T, cm *shared.ClientsMock, cf *shared.ClientFactory) {
166+
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
166167
cm.ApiInterface.On("GenerateAuthTicket", mock.Anything, mock.Anything, mock.Anything).Return(api.GenerateAuthTicketResult{}, nil)
167168
cm.IO.On("InputPrompt", mock.Anything, "Enter challenge code", iostreams.InputPromptConfig{
168169
Required: true,

0 commit comments

Comments
 (0)