Repository navigation
fix(api): error when --app can't get a bot token instead of sending the request unauthenticated - #840
Conversation
…he request unauthenticated
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
WilliamBergamin
left a comment
There was a problem hiding this comment.
--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 🙏
| case types.InstallRequestPending: | ||
| return "", slackerror.New(slackerror.ErrAppApprovalRequestPending). | ||
| WithRemediation("Ask an admin to approve the app, or pass a token with --token") | ||
| default: |
There was a problem hiding this comment.
🧠 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.
There was a problem hiding this comment.
Added in commit fc2a7fe: cancelled now has its own message, and the table test covers each approval state. Thanks! 🙇
| if err == nil && token != "" { | ||
| return token, nil | ||
| } | ||
| return installAndGetBotToken(ctx, clients, selected) |
There was a problem hiding this comment.
🧪 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.
There was a problem hiding this comment.
Added in commit fc2a7fe as Test_resolveToken_AppSelection_InstallError. It fails when I put the old swallow-the-error form back. Great catch!
| assert.Equal(t, "xoxb-app-bot-token", token) | ||
| } | ||
|
|
||
| func Test_resolveToken_AppFlag_ApprovalNotSent(t *testing.T) { |
There was a problem hiding this comment.
🧪 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?
There was a problem hiding this comment.
Refactored in commit fc2a7fe into Test_resolveToken_AppFlag_InstallErrors, which also gained an install-error case. Much easier to read, thanks!
| // 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. |
There was a problem hiding this comment.
🪓 nitpick (non-blocking): The ErrAppHosted message already says why. Could this comment go?
| // 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. |
| if err != nil { | ||
| return "", err | ||
| } | ||
| // An install that needs admin approval returns no token and no error |
There was a problem hiding this comment.
🪓 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.
| // An install that needs admin approval returns no token and no error |
There was a problem hiding this comment.
Removed in commit fc2a7fe, and the table test now holds that contract. Thanks!
| 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 |
There was a problem hiding this comment.
🏷️ nitpick (non-blocking): Could this move into the test name, like Test_resolveToken_AppFlag_LocalSlackHostedRuntime, and the comment go?
| // A local Deno app keeps the "slack" runtime in its manifest |
There was a problem hiding this comment.
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.
|
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
left a comment
There was a problem hiding this comment.
🧪 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 💯
- : 🤔 question (non-blocking): Outside a project,
Line 286 in 0fbb2f5
--appis 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)".
|
|
||
| token, err := resolveToken(ctx, clients) | ||
| assert.Empty(t, token) | ||
| assert.True(t, slackerror.Is(err, tc.expectedErr), "expected %s, got %v", tc.expectedErr, err) |
There was a problem hiding this comment.
🧪 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?
There was a problem hiding this comment.
Added in commit 3964dda: each row now checks its message too, and swapping the cancelled and not-sent messages fails the test. Great catch!
|
Thanks again @WilliamBergamin! 🙇 The row messages are checked in 3964dda, and |
| 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) |
There was a problem hiding this comment.
💯 praise: Checking each row's message means swapping the cancelled and not-sent messages now fails the test.
|
Thanks @WilliamBergamin for the quick and careful reviews! 🙇 |
Changelog
slack api --appnow 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, soslack api auth.test --app A123printed{"ok":false,"error":"not_authed"}and exited 0.apps.developerInstallnever returns tokens for it. The local app (--app local) still works.Testing
slack api auth.test --app deployedand confirm theapp_hostederror with the--app localand--tokenremediation.slack runonce, thenslack api auth.test --app localand confirm"ok": true.slack api auth.test --app <app-id>and confirm it still returns the bot's identity.Test_resolveToken_AppFlag_*tests for hosted apps, approval states, and an empty bot token fail onmainand pass on this branch.Notes
slack apistill exits 0 for an HTTP 200 response with"ok": false. That's a separate change.Requirements