|
| 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 | + "testing" |
| 20 | + |
| 21 | + "github.com/slackapi/slack-cli/internal/app" |
| 22 | + "github.com/slackapi/slack-cli/internal/config" |
| 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/internal/slackerror" |
| 27 | + "github.com/slackapi/slack-cli/internal/slacktrace" |
| 28 | + "github.com/slackapi/slack-cli/test/testutil" |
| 29 | + "github.com/spf13/cobra" |
| 30 | + "github.com/stretchr/testify/mock" |
| 31 | +) |
| 32 | + |
| 33 | +func Test_App_SettingsCommand(t *testing.T) { |
| 34 | + testutil.TableTestCommand(t, testutil.CommandTests{ |
| 35 | + "requires a valid project directory": { |
| 36 | + ExpectedError: slackerror.New(slackerror.ErrInvalidAppDirectory), |
| 37 | + }, |
| 38 | + "errors for rosi applications": { |
| 39 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 40 | + cf.SDKConfig.WorkingDirectory = "." |
| 41 | + projectConfigMock := config.NewProjectConfigMock() |
| 42 | + projectConfigMock.On( |
| 43 | + "GetManifestSource", |
| 44 | + mock.Anything, |
| 45 | + ).Return( |
| 46 | + config.ManifestSourceLocal, |
| 47 | + nil, |
| 48 | + ) |
| 49 | + cm.Config.ProjectConfig = projectConfigMock |
| 50 | + manifestMock := &app.ManifestMockObject{} |
| 51 | + manifestMock.On( |
| 52 | + "GetManifestLocal", |
| 53 | + mock.Anything, |
| 54 | + mock.Anything, |
| 55 | + mock.Anything, |
| 56 | + ).Return( |
| 57 | + types.SlackYaml{ |
| 58 | + AppManifest: types.AppManifest{ |
| 59 | + Settings: &types.AppSettings{FunctionRuntime: types.SlackHosted}, |
| 60 | + }, |
| 61 | + }, |
| 62 | + nil, |
| 63 | + ) |
| 64 | + cm.AppClient.Manifest = manifestMock |
| 65 | + }, |
| 66 | + ExpectedError: slackerror.New(slackerror.ErrAppHosted), |
| 67 | + }, |
| 68 | + "opens a rosi application with the force flag": { |
| 69 | + CmdArgs: []string{"--force"}, |
| 70 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 71 | + cf.SDKConfig.WorkingDirectory = "." |
| 72 | + projectConfigMock := config.NewProjectConfigMock() |
| 73 | + projectConfigMock.On( |
| 74 | + "GetManifestSource", |
| 75 | + mock.Anything, |
| 76 | + ).Return( |
| 77 | + config.ManifestSourceLocal, |
| 78 | + nil, |
| 79 | + ) |
| 80 | + cm.Config.ProjectConfig = projectConfigMock |
| 81 | + manifestMock := &app.ManifestMockObject{} |
| 82 | + manifestMock.On( |
| 83 | + "GetManifestLocal", |
| 84 | + mock.Anything, |
| 85 | + mock.Anything, |
| 86 | + mock.Anything, |
| 87 | + ).Return( |
| 88 | + types.SlackYaml{ |
| 89 | + AppManifest: types.AppManifest{ |
| 90 | + Settings: &types.AppSettings{FunctionRuntime: types.SlackHosted}, |
| 91 | + }, |
| 92 | + }, |
| 93 | + nil, |
| 94 | + ) |
| 95 | + cm.AppClient.Manifest = manifestMock |
| 96 | + appSelectMock := prompts.NewAppSelectMock() |
| 97 | + appSelectMock.On( |
| 98 | + "AppSelectPrompt", |
| 99 | + ).Return( |
| 100 | + prompts.SelectedApp{App: types.App{AppID: "A0101010101"}}, |
| 101 | + nil, |
| 102 | + ) |
| 103 | + settingsAppSelectPromptFunc = appSelectMock.AppSelectPrompt |
| 104 | + }, |
| 105 | + ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
| 106 | + expectedURL := "https://api.slack.com/apps/A0101010101" |
| 107 | + cm.Browser.AssertCalled(t, "OpenURL", expectedURL) |
| 108 | + cm.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.AppSettingsStart, mock.Anything) |
| 109 | + cm.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.AppSettingsSuccess, []string{expectedURL}) |
| 110 | + }, |
| 111 | + }, |
| 112 | + "requires an existing application": { |
| 113 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 114 | + cf.SDKConfig.WorkingDirectory = "." |
| 115 | + projectConfigMock := config.NewProjectConfigMock() |
| 116 | + projectConfigMock.On( |
| 117 | + "GetManifestSource", |
| 118 | + mock.Anything, |
| 119 | + ).Return( |
| 120 | + config.ManifestSourceRemote, |
| 121 | + nil, |
| 122 | + ) |
| 123 | + cm.Config.ProjectConfig = projectConfigMock |
| 124 | + appSelectMock := prompts.NewAppSelectMock() |
| 125 | + appSelectMock.On( |
| 126 | + "AppSelectPrompt", |
| 127 | + ).Return( |
| 128 | + prompts.SelectedApp{}, |
| 129 | + slackerror.New(slackerror.ErrInstallationRequired), |
| 130 | + ) |
| 131 | + settingsAppSelectPromptFunc = appSelectMock.AppSelectPrompt |
| 132 | + }, |
| 133 | + ExpectedError: slackerror.New(slackerror.ErrInstallationRequired), |
| 134 | + }, |
| 135 | + "opens the url to app settings of an app in production": { |
| 136 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 137 | + cf.SDKConfig.WorkingDirectory = "." |
| 138 | + projectConfigMock := config.NewProjectConfigMock() |
| 139 | + projectConfigMock.On( |
| 140 | + "GetManifestSource", |
| 141 | + mock.Anything, |
| 142 | + ).Return( |
| 143 | + config.ManifestSourceRemote, |
| 144 | + nil, |
| 145 | + ) |
| 146 | + cm.Config.ProjectConfig = projectConfigMock |
| 147 | + appSelectMock := prompts.NewAppSelectMock() |
| 148 | + appSelectMock.On( |
| 149 | + "AppSelectPrompt", |
| 150 | + ).Return( |
| 151 | + prompts.SelectedApp{App: types.App{AppID: "A0123456789"}}, |
| 152 | + nil, |
| 153 | + ) |
| 154 | + settingsAppSelectPromptFunc = appSelectMock.AppSelectPrompt |
| 155 | + }, |
| 156 | + ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
| 157 | + expectedURL := "https://api.slack.com/apps/A0123456789" |
| 158 | + cm.Browser.AssertCalled(t, "OpenURL", expectedURL) |
| 159 | + cm.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.AppSettingsStart, mock.Anything) |
| 160 | + cm.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.AppSettingsSuccess, []string{expectedURL}) |
| 161 | + }, |
| 162 | + }, |
| 163 | + "opens the url to app settings of an app in development": { |
| 164 | + Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) { |
| 165 | + host := "https://dev1234.slack.com" |
| 166 | + cf.SDKConfig.WorkingDirectory = "." |
| 167 | + projectConfigMock := config.NewProjectConfigMock() |
| 168 | + projectConfigMock.On( |
| 169 | + "GetManifestSource", |
| 170 | + mock.Anything, |
| 171 | + ).Return( |
| 172 | + config.ManifestSourceRemote, |
| 173 | + nil, |
| 174 | + ) |
| 175 | + cm.Config.ProjectConfig = projectConfigMock |
| 176 | + appSelectMock := prompts.NewAppSelectMock() |
| 177 | + appSelectMock.On( |
| 178 | + "AppSelectPrompt", |
| 179 | + ).Return( |
| 180 | + prompts.SelectedApp{ |
| 181 | + App: types.App{AppID: "A0123456789"}, |
| 182 | + Auth: types.SlackAuth{APIHost: &host}, |
| 183 | + }, |
| 184 | + nil, |
| 185 | + ) |
| 186 | + settingsAppSelectPromptFunc = appSelectMock.AppSelectPrompt |
| 187 | + cm.API.On("Host").Return(host) // SetHost is implemented in AppSelectPrompt |
| 188 | + }, |
| 189 | + ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) { |
| 190 | + expectedURL := "https://api.dev1234.slack.com/apps/A0123456789" |
| 191 | + cm.Browser.AssertCalled(t, "OpenURL", expectedURL) |
| 192 | + cm.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.AppSettingsStart, mock.Anything) |
| 193 | + cm.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.AppSettingsSuccess, []string{expectedURL}) |
| 194 | + }, |
| 195 | + }, |
| 196 | + }, func(cf *shared.ClientFactory) *cobra.Command { |
| 197 | + return NewSettingsCommand(cf) |
| 198 | + }) |
| 199 | +} |
0 commit comments