|
| 1 | +// Copyright 2022-2025 Salesforce, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package app |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/slackapi/slack-cli/internal/app" |
| 23 | + "github.com/slackapi/slack-cli/internal/prompts" |
| 24 | + "github.com/slackapi/slack-cli/internal/shared" |
| 25 | + "github.com/slackapi/slack-cli/internal/shared/types" |
| 26 | + "github.com/slackapi/slack-cli/test/testutil" |
| 27 | + "github.com/spf13/cobra" |
| 28 | + "github.com/stretchr/testify/mock" |
| 29 | +) |
| 30 | + |
| 31 | +func TestAppsUnlinkCommand(t *testing.T) { |
| 32 | + testutil.TableTestCommand(t, testutil.CommandTests{ |
| 33 | + "happy path; unlink the deployed app": { |
| 34 | + CmdArgs: []string{}, |
| 35 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 36 | + prepareCommonUnlinkMocks(t, cf, cm) |
| 37 | + // Mock App Selection |
| 38 | + appSelectMock := prompts.NewAppSelectMock() |
| 39 | + unlinkAppSelectPromptFunc = appSelectMock.AppSelectPrompt |
| 40 | + appSelectMock.On("AppSelectPrompt", mock.Anything, mock.Anything, prompts.ShowAllEnvironments, prompts.ShowInstalledAndUninstalledApps).Return(prompts.SelectedApp{ |
| 41 | + Auth: types.SlackAuth{TeamDomain: fakeDeployedApp.TeamDomain}, |
| 42 | + App: fakeDeployedApp, |
| 43 | + }, nil) |
| 44 | + // Mock unlink confirmation prompt |
| 45 | + cm.IO.On("ConfirmPrompt", mock.Anything, "Are you sure you want to unlink this app?", mock.Anything).Return(true, nil) |
| 46 | + // Mock AppClient calls |
| 47 | + appClientMock := &app.AppClientMock{} |
| 48 | + appClientMock.On("Remove", mock.Anything, mock.Anything).Return(fakeDeployedApp, nil) |
| 49 | + appClientMock.On("CleanUp").Return() |
| 50 | + cf.AppClient().AppClientInterface = appClientMock |
| 51 | + }, |
| 52 | + ExpectedStdoutOutputs: []string{ |
| 53 | + fmt.Sprintf("Removed app %s from project", fakeDeployedApp.AppID), |
| 54 | + fmt.Sprintf("Team: %s", fakeDeployedApp.TeamDomain), |
| 55 | + }, |
| 56 | + }, |
| 57 | + "happy path; unlink the local app": { |
| 58 | + CmdArgs: []string{}, |
| 59 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 60 | + prepareCommonUnlinkMocks(t, cf, cm) |
| 61 | + // Mock App Selection |
| 62 | + appSelectMock := prompts.NewAppSelectMock() |
| 63 | + unlinkAppSelectPromptFunc = appSelectMock.AppSelectPrompt |
| 64 | + appSelectMock.On("AppSelectPrompt", mock.Anything, mock.Anything, prompts.ShowAllEnvironments, prompts.ShowInstalledAndUninstalledApps).Return(prompts.SelectedApp{ |
| 65 | + Auth: types.SlackAuth{TeamDomain: fakeLocalApp.TeamDomain}, |
| 66 | + App: fakeLocalApp, |
| 67 | + }, nil) |
| 68 | + // Mock unlink confirmation prompt |
| 69 | + cm.IO.On("ConfirmPrompt", mock.Anything, "Are you sure you want to unlink this app?", mock.Anything).Return(true, nil) |
| 70 | + // Mock AppClient calls |
| 71 | + appClientMock := &app.AppClientMock{} |
| 72 | + appClientMock.On("Remove", mock.Anything, mock.Anything).Return(fakeLocalApp, nil) |
| 73 | + appClientMock.On("CleanUp").Return() |
| 74 | + cf.AppClient().AppClientInterface = appClientMock |
| 75 | + }, |
| 76 | + ExpectedStdoutOutputs: []string{ |
| 77 | + fmt.Sprintf("Removed app %s from project", fakeLocalApp.AppID), |
| 78 | + fmt.Sprintf("Team: %s", fakeLocalApp.TeamDomain), |
| 79 | + }, |
| 80 | + }, |
| 81 | + "sad path; unlinking the deployed app fails": { |
| 82 | + CmdArgs: []string{}, |
| 83 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 84 | + prepareCommonUnlinkMocks(t, cf, cm) |
| 85 | + // Mock App Selection |
| 86 | + appSelectMock := prompts.NewAppSelectMock() |
| 87 | + unlinkAppSelectPromptFunc = appSelectMock.AppSelectPrompt |
| 88 | + appSelectMock.On("AppSelectPrompt", mock.Anything, mock.Anything, prompts.ShowAllEnvironments, prompts.ShowInstalledAndUninstalledApps).Return(prompts.SelectedApp{ |
| 89 | + Auth: types.SlackAuth{TeamDomain: fakeDeployedApp.TeamDomain}, |
| 90 | + App: fakeDeployedApp, |
| 91 | + }, nil) |
| 92 | + // Mock unlink confirmation prompt |
| 93 | + cm.IO.On("ConfirmPrompt", mock.Anything, "Are you sure you want to unlink this app?", mock.Anything).Return(true, nil) |
| 94 | + // Mock AppClient calls - return error |
| 95 | + appClientMock := &app.AppClientMock{} |
| 96 | + appClientMock.On("Remove", mock.Anything, mock.Anything).Return(types.App{}, fmt.Errorf("failed to remove app from project")) |
| 97 | + cf.AppClient().AppClientInterface = appClientMock |
| 98 | + }, |
| 99 | + ExpectedError: fmt.Errorf("failed to remove app from project"), |
| 100 | + }, |
| 101 | + "user cancels unlink": { |
| 102 | + CmdArgs: []string{}, |
| 103 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 104 | + prepareCommonUnlinkMocks(t, cf, cm) |
| 105 | + // Mock App Selection |
| 106 | + appSelectMock := prompts.NewAppSelectMock() |
| 107 | + unlinkAppSelectPromptFunc = appSelectMock.AppSelectPrompt |
| 108 | + appSelectMock.On("AppSelectPrompt", mock.Anything, mock.Anything, prompts.ShowAllEnvironments, prompts.ShowInstalledAndUninstalledApps).Return(prompts.SelectedApp{ |
| 109 | + Auth: types.SlackAuth{TeamDomain: fakeDeployedApp.TeamDomain}, |
| 110 | + App: fakeDeployedApp, |
| 111 | + }, nil) |
| 112 | + // Mock unlink confirmation prompt - user says no |
| 113 | + cm.IO.On("ConfirmPrompt", mock.Anything, "Are you sure you want to unlink this app?", mock.Anything).Return(false, nil) |
| 114 | + }, |
| 115 | + ExpectedStdoutOutputs: []string{ |
| 116 | + "Your app will not be unlinked", |
| 117 | + }, |
| 118 | + }, |
| 119 | + "errors if app selection fails": { |
| 120 | + CmdArgs: []string{}, |
| 121 | + ExpectedError: fmt.Errorf("failed to select app"), |
| 122 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 123 | + prepareCommonUnlinkMocks(t, cf, cm) |
| 124 | + appSelectMock := prompts.NewAppSelectMock() |
| 125 | + unlinkAppSelectPromptFunc = appSelectMock.AppSelectPrompt |
| 126 | + appSelectMock.On("AppSelectPrompt", mock.Anything, mock.Anything, prompts.ShowAllEnvironments, prompts.ShowInstalledAndUninstalledApps).Return(prompts.SelectedApp{}, fmt.Errorf("failed to select app")) |
| 127 | + }, |
| 128 | + }, |
| 129 | + }, func(cf *shared.ClientFactory) *cobra.Command { |
| 130 | + cmd := NewUnlinkCommand(cf) |
| 131 | + cmd.PreRunE = func(cmd *cobra.Command, args []string) error { return nil } |
| 132 | + return cmd |
| 133 | + }) |
| 134 | +} |
| 135 | + |
| 136 | +func prepareCommonUnlinkMocks(t *testing.T, cf *shared.ClientFactory, cm *shared.ClientsMock) { |
| 137 | + cm.AddDefaultMocks() |
| 138 | +} |
0 commit comments