Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func NewCommand(clients *shared.ClientFactory) *cobra.Command {
"",
"Token resolution (in priority order):",
" 1. --token flag Explicit token value",
" 2. --app flag Install app and use bot token (in project)",
" 2. --app flag Install app and use bot token (requires a project)",
" 3. SLACK_BOT_TOKEN env var Bot token (set during slack deploy)",
" 4. SLACK_USER_TOKEN env var User token",
" 5. App prompt (in project) Select installed app or \"No app\"",
Expand Down Expand Up @@ -283,16 +283,19 @@ func resolveToken(ctx context.Context, clients *shared.ClientFactory) (string, e
}

if clients.Config.AppFlag != "" {
if sdkConfigExists, _ := clients.SDKConfig.Exists(); sdkConfigExists {
selected, err := prompts.AppSelectPrompt(ctx, clients, prompts.ShowAllEnvironments, prompts.ShowInstalledAppsOnly)
if err != nil {
return "", err
}
if selected.App.AppID != "" {
token, err := installAndGetBotToken(ctx, clients, selected)
if err == nil && token != "" {
return token, nil
}
if _, err := clients.SDKConfig.Exists(); err != nil {
return "", slackerror.New(slackerror.ErrInvalidSlackProjectDirectory).
WithMessage("The --app flag needs a Slack project directory").
WithRemediation("Run the command in a Slack project directory, or pass a token with --token")
}
selected, err := prompts.AppSelectPrompt(ctx, clients, prompts.ShowAllEnvironments, prompts.ShowInstalledAppsOnly)
if err != nil {
return "", err
}
if selected.App.AppID != "" {
token, err := installAndGetBotToken(ctx, clients, selected)
if err == nil && token != "" {
return token, nil
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions cmd/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ import (
"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/test/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

func Test_NewCommand(t *testing.T) {
Expand Down Expand Up @@ -569,6 +571,21 @@ func Test_resolveToken_AppFlag_Local(t *testing.T) {
assert.Equal(t, "xoxb-local-bot-token", token)
}

func Test_resolveToken_AppFlag_NoProject(t *testing.T) {
t.Setenv("SLACK_BOT_TOKEN", "xoxb-env-bot-token")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 praise: Setting SLACK_BOT_TOKEN and asserting the prompt never runs pins the exact fallback this fixes, so the test fails on main for the right reason.


ctx := slackcontext.MockContext(t.Context())
clientsMock := shared.NewClientsMock()
clientsMock.Config.AppFlag = "A111"
clients := shared.NewClientFactory(clientsMock.MockClientFactory())

token, err := resolveToken(ctx, clients)
require.Error(t, err)
assert.Equal(t, slackerror.ErrInvalidSlackProjectDirectory, slackerror.ToSlackError(err).Code)
assert.Empty(t, token)
clientsMock.IO.AssertNotCalled(t, "SelectPrompt", mock.Anything, mock.Anything, mock.Anything, mock.Anything)
}

func Test_resolveToken_AppFlag_NotFound(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())
clientsMock := shared.NewClientsMock()
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/commands/slack_api.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading