Skip to content

fix(api): error when --app can't get a bot token instead of sending the request unauthenticated - #840

Merged
mwbrooks merged 9 commits into
mainfrom
mwbrooks-api-hosted-app-token
Nov 24, 2026
Merged

mwbrooks merged 9 commits into
mainfrom
mwbrooks-api-hosted-app-token

Conversation

@mwbrooks

@mwbrooks mwbrooks commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Changelog

slack api --app now errors when it can't get a bot token for the app, instead of sending the request without a token. A deployed app that runs on Slack gets a clear error, since its bot token stays on Slack.

Summary

This pull request makes slack api --app <app> fail with a clear error for developers when it can't get the app's bot token. Before, the CLI sent the request with no token, so slack api auth.test --app A123 printed {"ok":false,"error":"not_authed"} and exited 0.

  • A deployed app that runs on Slack now errors before any install, because apps.developerInstall never returns tokens for it. The local app (--app local) still works.
  • An install error, an install waiting on admin approval, or an install that returns no bot token is now returned as an error instead of dropped.
  • The CLI no longer installs the app a second time through the app prompt fallback after the first try fails.

Testing

  • In a Deno project with a deployed app, run slack api auth.test --app deployed and confirm the app_hosted error with the --app local and --token remediation.
  • In the same project, run slack run once, then slack api auth.test --app local and confirm "ok": true.
  • In a Bolt project, run slack api auth.test --app <app-id> and confirm it still returns the bot's identity.
  • The new Test_resolveToken_AppFlag_* tests for hosted apps, approval states, and an empty bot token fail on main and pass on this branch.

Notes

  • slack api still exits 0 for an HTTP 200 response with "ok": false. That's a separate change.

Requirements

@mwbrooks mwbrooks added this to the Next Release milestone Nov 24, 2026
@mwbrooks mwbrooks added bug M-T: confirmed bug report. Issues are confirmed when the reproduction steps are documented changelog Use on updates to be included in the release notes semver:patch Use on pull requests to describe the release version increment labels Nov 24, 2026
@mwbrooks mwbrooks self-assigned this Nov 24, 2026
@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.71%. Comparing base (7dd4824) to head (71b6a71).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #840   +/-   ##
=======================================
  Coverage   80.71%   80.71%           
=======================================
  Files         247      247           
  Lines       18841    18855   +14     
=======================================
+ Hits        15207    15219   +12     
- Misses       3634     3636    +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mwbrooks
mwbrooks marked this pull request as ready for review November 24, 2026 16:34
@mwbrooks
mwbrooks requested a review from a team as a code owner November 24, 2026 16:34
@mwbrooks
mwbrooks requested review from WilliamBergamin, srtaalej and zimeg and removed request for srtaalej November 24, 2026 16:35

@WilliamBergamin WilliamBergamin left a comment

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.

⚠️ Thanks @mwbrooks! Failing loudly with the --token remediation is much kinder than a silent not_authed with exit 0, and the hosted-app test checks that no install ever happens.
Left a few comments, nothing blocking 🙏

Comment thread cmd/api/api.go
case types.InstallRequestPending:
return "", slackerror.New(slackerror.ErrAppApprovalRequestPending).
WithRemediation("Ask an admin to approve the app, or pass a token with --token")
default:

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.

🧠 question (non-blocking): REQUEST_CANCELLED also lands here. Could it get its own case, or could the message name the state? Right now a cancelled request reads the same as one that was never sent, and no test covers it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added in commit fc2a7fe: cancelled now has its own message, and the table test covers each approval state. Thanks! 🙇

Comment thread cmd/api/api.go
if err == nil && token != "" {
return token, nil
}
return installAndGetBotToken(ctx, clients, selected)

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.

🧪 suggestion (non-blocking): Could a test cover an install error after picking an app from the prompt? If I put back the old swallow-the-error form here, every test still passes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added in commit fc2a7fe as Test_resolveToken_AppSelection_InstallError. It fails when I put the old swallow-the-error form back. Great catch!

Comment thread cmd/api/api_test.go Outdated
assert.Equal(t, "xoxb-app-bot-token", token)
}

func Test_resolveToken_AppFlag_ApprovalNotSent(t *testing.T) {

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.

🧪 suggestion (non-blocking): The four new tests repeat about 35 lines of the same setup. Could a table-driven test over install state, result, and expected error make each case's difference easy to spot?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Refactored in commit fc2a7fe into Test_resolveToken_AppFlag_InstallErrors, which also gained an install-error case. Much easier to read, thanks!

Comment thread cmd/api/api.go Outdated
Comment on lines +336 to +337
// A deployed Run on Slack app keeps its bot token on Slack, so installing
// it would return no token and the request would be sent unauthenticated.

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.

🪓 nitpick (non-blocking): The ErrAppHosted message already says why. Could this comment go?

Suggested change
// A deployed Run on Slack app keeps its bot token on Slack, so installing
// it would return no token and the request would be sent unauthenticated.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Removed in commit fc2a7fe, thanks!

Comment thread cmd/api/api.go Outdated
if err != nil {
return "", err
}
// An install that needs admin approval returns no token and no error

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.

🪓 nitpick (non-blocking): Same here. A test named for this case (approval needed, no token, no error) would keep this contract, and the comment could go.

Suggested change
// An install that needs admin approval returns no token and no error

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Removed in commit fc2a7fe, and the table test now holds that contract. Thanks!

Comment thread cmd/api/api_test.go Outdated
manifestMock := clientsMock.AppClient.Manifest.(*app.ManifestMockObject)
manifestMock.On("GetManifestLocal", mock.Anything, mock.Anything, mock.Anything).Return(types.SlackYaml{
AppManifest: types.AppManifest{
// A local Deno app keeps the "slack" runtime in its manifest

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.

🏷️ nitpick (non-blocking): Could this move into the test name, like Test_resolveToken_AppFlag_LocalSlackHostedRuntime, and the comment go?

Suggested change
// A local Deno app keeps the "slack" runtime in its manifest

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Renamed in commit fc2a7fe, thanks for the suggestion! 🙇

Gives a cancelled approval request its own message and drops comments the error messages and test names already carry.
@mwbrooks

Copy link
Copy Markdown
Member Author

Thanks @WilliamBergamin for the thoughtful review! 🙇 All six points are addressed in fc2a7fe. Would you mind a refresh review when you have a moment?

@WilliamBergamin WilliamBergamin left a comment

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.

🧪 Thanks @mwbrooks! The install-error table plus the prompt-path test now pin every way --app can fail, and putting back the old swallowed error fails a test.
Left two small comments, nothing blocking 💯

  • if sdkConfigExists, _ := clients.SDKConfig.Exists(); sdkConfigExists {
    : 🤔 question (non-blocking): Outside a project, --app is skipped here and the request still goes out with an env token or none. Could that be an error too, or a follow-up? The help text does say "(in project)".

Comment thread cmd/api/api_test.go

token, err := resolveToken(ctx, clients)
assert.Empty(t, token)
assert.True(t, slackerror.Is(err, tc.expectedErr), "expected %s, got %v", tc.expectedErr, err)

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.

🧪 suggestion (non-blocking): The cancelled and not-sent rows both expect ErrAppInstall, so swapping their messages still passes. Could each row also check its message, say an expectedMessage field with assert.Contains?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added in commit 3964dda: each row now checks its message too, and swapping the cancelled and not-sent messages fails the test. Great catch!

@mwbrooks

Copy link
Copy Markdown
Member Author

Thanks again @WilliamBergamin! 🙇 The row messages are checked in 3964dda, and --app outside a project is its own follow-up in #845. Would you mind a refresh review when you have a moment?

@WilliamBergamin WilliamBergamin left a comment

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.

💯 Thanks @mwbrooks, a failed --app now stops with a clear reason instead of sending an unauthenticated request, and each install state has a tested message. Nothing left from my side, and #845 is a good home for the outside-a-project case 🚀

Comment thread cmd/api/api_test.go
token, err := resolveToken(ctx, clients)
assert.Empty(t, token)
assert.True(t, slackerror.Is(err, tc.expectedErr), "expected %s, got %v", tc.expectedErr, err)
assert.Contains(t, err.Error(), tc.expectedMessage)

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: Checking each row's message means swapping the cancelled and not-sent messages now fails the test.

@mwbrooks

Copy link
Copy Markdown
Member Author

Thanks @WilliamBergamin for the quick and careful reviews! 🙇

@mwbrooks
mwbrooks enabled auto-merge (squash) November 24, 2026 19:04
@mwbrooks
mwbrooks merged commit ae3686b into main Nov 24, 2026
14 checks passed
@mwbrooks
mwbrooks deleted the mwbrooks-api-hosted-app-token branch November 24, 2026 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug M-T: confirmed bug report. Issues are confirmed when the reproduction steps are documented changelog Use on updates to be included in the release notes semver:patch Use on pull requests to describe the release version increment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants