-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathuninstall_test.go
More file actions
132 lines (116 loc) · 5.1 KB
/
uninstall_test.go
File metadata and controls
132 lines (116 loc) · 5.1 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
// Copyright 2022-2025 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 app
import (
"context"
"fmt"
"testing"
"github.com/slackapi/slack-cli/internal/api"
"github.com/slackapi/slack-cli/internal/app"
"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/slackerror"
"github.com/slackapi/slack-cli/test/testutil"
"github.com/spf13/cobra"
"github.com/stretchr/testify/mock"
)
var fakeAppID = "A1234"
var fakeAppTeamID = "T1234"
var fakeAppUserID = "U1234"
var fakeApp = types.App{
TeamDomain: "test",
AppID: fakeAppID,
TeamID: fakeAppTeamID,
UserID: fakeAppUserID,
}
var selectedProdApp = prompts.SelectedApp{Auth: types.SlackAuth{TeamDomain: "team1234"}, App: types.App{AppID: fakeAppID, TeamID: fakeAppTeamID}}
func TestAppsUninstall(t *testing.T) {
testutil.TableTestCommand(t, testutil.CommandTests{
"Successfully uninstall": {
Setup: func(t *testing.T, ctx context.Context, clientsMock *shared.ClientsMock, clients *shared.ClientFactory) {
prepareCommonUninstallMocks(ctx, clients, clientsMock)
clientsMock.API.On("UninstallApp", mock.Anything, mock.Anything, fakeAppID, fakeAppTeamID).
Return(nil).Once()
},
ExpectedStdoutOutputs: []string{
fmt.Sprintf(`Uninstalled the app "%s" from "%s"`, fakeAppID, fakeApp.TeamDomain),
},
},
"Successfully uninstall with a get-manifest hook error": {
Setup: func(t *testing.T, ctx context.Context, clientsMock *shared.ClientsMock, clients *shared.ClientFactory) {
prepareCommonUninstallMocks(ctx, clients, clientsMock)
clientsMock.API.On("UninstallApp", mock.Anything, mock.Anything, fakeAppID, fakeAppTeamID).
Return(nil).Once()
manifestMock := &app.ManifestMockObject{}
manifestMock.On("GetManifestLocal", mock.Anything, mock.Anything, mock.Anything).
Return(types.SlackYaml{}, slackerror.New(slackerror.ErrSDKHookNotFound))
clients.AppClient().Manifest = manifestMock
},
ExpectedStdoutOutputs: []string{
fmt.Sprintf(`Uninstalled the app "%s" from "%s"`, fakeAppID, fakeApp.TeamDomain),
},
},
"Fail to uninstall due to API error": {
ExpectedError: slackerror.New("something went wrong"),
Setup: func(t *testing.T, ctx context.Context, clientsMock *shared.ClientsMock, clients *shared.ClientFactory) {
prepareCommonUninstallMocks(ctx, clients, clientsMock)
clientsMock.API.On("UninstallApp", mock.Anything, mock.Anything, fakeAppID, fakeAppTeamID).
Return(slackerror.New("something went wrong")).Once()
},
},
"errors if authentication for the team is missing": {
CmdArgs: []string{},
ExpectedError: slackerror.New(slackerror.ErrCredentialsNotFound),
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
prepareCommonUninstallMocks(ctx, cf, cm)
appSelectMock := prompts.NewAppSelectMock()
uninstallAppSelectPromptFunc = appSelectMock.AppSelectPrompt
appSelectMock.On("AppSelectPrompt").Return(prompts.SelectedApp{App: fakeApp}, nil)
},
},
}, func(clients *shared.ClientFactory) *cobra.Command {
cmd := NewUninstallCommand(clients)
cmd.PreRunE = func(cmd *cobra.Command, args []string) error { return nil }
return cmd
})
}
func prepareCommonUninstallMocks(ctx context.Context, clients *shared.ClientFactory, clientsMock *shared.ClientsMock) *shared.ClientFactory {
// Mock App Selection
appSelectMock := prompts.NewAppSelectMock()
uninstallAppSelectPromptFunc = appSelectMock.AppSelectPrompt
appSelectMock.On("AppSelectPrompt").Return(selectedProdApp, nil)
// Mock API calls
clientsMock.Auth.On("ResolveAPIHost", mock.Anything, mock.Anything, mock.Anything).
Return("api host")
clientsMock.Auth.On("ResolveLogstashHost", mock.Anything, mock.Anything, mock.Anything).
Return("logstash host")
clientsMock.API.On("ValidateSession", mock.Anything, mock.Anything).Return(api.AuthSession{
TeamName: &fakeApp.TeamDomain,
TeamID: &fakeApp.TeamID,
}, nil)
clientsMock.AddDefaultMocks()
// Mock prompts
clientsMock.IO.On("ConfirmPrompt", mock.Anything, "Are you sure you want to uninstall?", mock.Anything).Return(true, nil)
// Mock AppClient calls
appClientMock := &app.AppClientMock{}
appClientMock.On("GetDeployed", mock.Anything, mock.Anything).Return(fakeApp, nil)
appClientMock.On("SaveDeployed", mock.Anything, mock.Anything).Return(nil)
clients.AppClient().AppClientInterface = appClientMock
err := clients.AppClient().SaveDeployed(ctx, fakeApp)
if err != nil {
panic("error setting up test; cant write apps.json")
}
return clients
}