-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathrun_test.go
More file actions
265 lines (247 loc) · 8.97 KB
/
run_test.go
File metadata and controls
265 lines (247 loc) · 8.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2022-2026 Salesforce, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package platform
import (
"context"
"testing"
"github.com/slackapi/slack-cli/internal/cmdutil"
"github.com/slackapi/slack-cli/internal/hooks"
"github.com/slackapi/slack-cli/internal/pkg/platform"
"github.com/slackapi/slack-cli/internal/prompts"
"github.com/slackapi/slack-cli/internal/shared"
"github.com/slackapi/slack-cli/internal/shared/types"
"github.com/slackapi/slack-cli/internal/slackcontext"
"github.com/slackapi/slack-cli/internal/slackerror"
"github.com/slackapi/slack-cli/internal/style"
"github.com/slackapi/slack-cli/test/testutil"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
// Setup a mock for the package
type RunCmdMock struct {
mock.Mock
}
func (m *RunCmdMock) RunRunCommand(clients *shared.ClientFactory, cmd *cobra.Command, args []string) error {
m.Called()
return nil
}
type RunPkgMock struct {
mock.Mock
}
func (m *RunPkgMock) Run(ctx context.Context, clients *shared.ClientFactory, runArgs platform.RunArgs) (types.InstallState, error) {
args := m.Called(ctx, clients, runArgs)
return types.InstallSuccess, args.Error(0)
}
func TestRunCommand_Flags(t *testing.T) {
tests := map[string]struct {
cmdArgs []string
appFlag string
tokenFlag string
selectedAppAuth prompts.SelectedApp
selectedAppErr error
expectedRunArgs platform.RunArgs
expectedErr error
}{
"New app with org auth": {
cmdArgs: []string{"--" + cmdutil.OrgGrantWorkspaceFlag, "T123", "--hide-triggers"},
selectedAppAuth: prompts.SelectedApp{
App: types.NewApp(),
Auth: types.SlackAuth{IsEnterpriseInstall: true},
},
expectedRunArgs: platform.RunArgs{
Activity: true,
ActivityLevel: "info",
Auth: types.SlackAuth{IsEnterpriseInstall: true},
App: types.NewApp(),
Cleanup: false,
ShowTriggers: false,
OrgGrantWorkspaceID: "T123", // Flag passed through
},
expectedErr: nil,
},
"Uninstalled org app": {
cmdArgs: []string{"--" + cmdutil.OrgGrantWorkspaceFlag, "T123"},
selectedAppAuth: prompts.SelectedApp{
App: types.App{InstallStatus: types.AppStatusUninstalled, TeamID: "E123", EnterpriseID: "E123"},
Auth: types.SlackAuth{IsEnterpriseInstall: true},
},
expectedRunArgs: platform.RunArgs{
Activity: true,
ActivityLevel: "info",
Auth: types.SlackAuth{IsEnterpriseInstall: true},
App: types.App{InstallStatus: types.AppStatusUninstalled, TeamID: "E123", EnterpriseID: "E123"},
Cleanup: false,
ShowTriggers: true,
OrgGrantWorkspaceID: "T123", // Flag passed through
},
expectedErr: nil,
},
"Installed org app; can't set different workspace grant": {
cmdArgs: []string{"--" + cmdutil.OrgGrantWorkspaceFlag, "T123"},
selectedAppAuth: prompts.SelectedApp{
App: types.App{
AppID: "A123",
InstallStatus: types.AppStatusInstalled,
TeamID: "E123",
EnterpriseGrants: []types.EnterpriseGrant{{WorkspaceID: "T0"}}},
Auth: types.SlackAuth{IsEnterpriseInstall: true},
},
expectedErr: slackerror.New(slackerror.ErrOrgGrantExists).
WithMessage("A different org workspace grant already exists for installed app 'A123'\n Workspace Grant: T0"),
},
"Installed org app; can pass same workspace grant": {
cmdArgs: []string{"--" + cmdutil.OrgGrantWorkspaceFlag, "T123"},
selectedAppAuth: prompts.SelectedApp{
App: types.App{
InstallStatus: types.AppStatusInstalled,
TeamID: "E123",
EnterpriseGrants: []types.EnterpriseGrant{{WorkspaceID: "T123"}}},
Auth: types.SlackAuth{IsEnterpriseInstall: true},
},
expectedRunArgs: platform.RunArgs{
Activity: true,
ActivityLevel: "info",
Auth: types.SlackAuth{IsEnterpriseInstall: true},
App: types.App{
InstallStatus: types.AppStatusInstalled,
TeamID: "E123",
EnterpriseGrants: []types.EnterpriseGrant{{WorkspaceID: "T123"}}},
Cleanup: false,
ShowTriggers: true,
OrgGrantWorkspaceID: "T123", // Flag passed through
},
},
"Standalone workspace app": {
cmdArgs: []string{"--" + cmdutil.OrgGrantWorkspaceFlag, "T123"},
selectedAppAuth: prompts.SelectedApp{
App: types.App{
IsDev: true,
InstallStatus: types.AppStatusUninstalled,
TeamID: "T123",
},
Auth: types.SlackAuth{IsEnterpriseInstall: true},
},
expectedRunArgs: platform.RunArgs{
Activity: true,
ActivityLevel: "info",
Auth: types.SlackAuth{IsEnterpriseInstall: true},
App: types.App{
IsDev: true,
InstallStatus: types.AppStatusUninstalled,
TeamID: "T123",
},
Cleanup: false,
ShowTriggers: true,
OrgGrantWorkspaceID: "", // Flag not passed through
},
expectedErr: nil,
},
"Set the selected app to development when selecting with flags": {
appFlag: "A123",
tokenFlag: "T123",
selectedAppAuth: prompts.SelectedApp{
App: types.App{AppID: "A123"},
Auth: types.SlackAuth{TeamID: "T123"},
},
selectedAppErr: slackerror.New(slackerror.ErrDeployedAppNotSupported),
expectedRunArgs: platform.RunArgs{
Activity: true,
ActivityLevel: "info",
Auth: types.SlackAuth{TeamID: "T123"},
App: types.App{
AppID: "A123",
IsDev: true,
},
Cleanup: false,
ShowTriggers: true,
},
},
"Error if interrupted during app selection": {
selectedAppErr: slackerror.New(slackerror.ErrProcessInterrupted),
expectedRunArgs: platform.RunArgs{
Activity: true,
ActivityLevel: "info",
Cleanup: false,
ShowTriggers: true,
},
expectedErr: slackerror.New(slackerror.ErrProcessInterrupted),
},
"Error if no apps are available when using a remote manifest source": {
selectedAppErr: slackerror.New(slackerror.ErrMissingOptions),
expectedErr: slackerror.New(slackerror.ErrAppNotFound).
WithMessage("No apps are available for selection").
WithRemediation(
"Create a new app on app settings: %s\nThen add the app to this project with %s",
style.LinkText("https://api.slack.com/apps"),
style.Commandf("app link", false),
).
WithDetails(slackerror.ErrorDetails{
slackerror.ErrorDetail{
Code: slackerror.ErrProjectConfigManifestSource,
Message: "App manifests for this project are sourced from app settings",
},
}),
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())
clientsMock := shared.NewClientsMock()
clientsMock.IO.On("IsTTY").Return(true)
clientsMock.IO.AddDefaultMocks()
clients := shared.NewClientFactory(clientsMock.MockClientFactory(), func(clients *shared.ClientFactory) {
clients.SDKConfig = hooks.NewSDKConfigMock()
clients.Config.AppFlag = tc.appFlag
clients.Config.TokenFlag = tc.tokenFlag
})
appSelectMock := prompts.NewAppSelectMock()
appSelectMock.On("AppSelectPrompt", mock.Anything, mock.Anything, prompts.ShowLocalOnly, prompts.ShowAllApps).Return(tc.selectedAppAuth, tc.selectedAppErr)
runAppSelectPromptFunc = appSelectMock.AppSelectPrompt
runPkgMock := new(RunPkgMock)
runFunc = runPkgMock.Run
runPkgMock.On("Run", mock.Anything, mock.Anything, mock.Anything).Return(nil)
cmd := NewRunCommand(clients)
testutil.MockCmdIO(clients.IO, cmd)
cmd.SetArgs(tc.cmdArgs)
// Execute
err := cmd.ExecuteContext(ctx)
// Check args passed into the run function
if tc.expectedErr == nil {
assert.NoError(t, err)
runPkgMock.AssertCalled(t, "Run", mock.Anything, mock.Anything,
tc.expectedRunArgs,
)
} else {
assert.Equal(t, tc.expectedErr, slackerror.ToSlackError(err))
}
})
}
}
func TestRunCommand_Help(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())
clientsMock := shared.NewClientsMock()
clientsMock.AddDefaultMocks()
clients := shared.NewClientFactory(clientsMock.MockClientFactory())
cmd := NewRunCommand(clients)
testutil.MockCmdIO(clients.IO, cmd)
cmd.SetArgs([]string{"--help"})
runPkgMock := new(RunPkgMock)
runFunc = runPkgMock.Run
runPkgMock.On("Run", mock.Anything, mock.Anything, mock.Anything).Return(nil)
err := cmd.ExecuteContext(ctx)
assert.NoError(t, err)
runPkgMock.AssertNotCalled(t, "Run")
assert.Contains(t, clientsMock.GetStdoutOutput(), "activity level to display (default \"info\")")
}