Slack Bolt (Python): importing bot.py in tests raises 'BoltError: `token` is invalid (auth.test result: {'ok': False, 'error': 'invalid_auth'})'; App() calls auth.test in its constructor
For tests, construct the app with App(token=..., signing_secret=..., token_verification_enabled=False), or move construction into a create_app() factory so importing bot.py has no side effects. slack_bolt.App.__init__ makes a network call to auth.test to verify the bot token. A module-level app = App(token=os.environ['SLACK_BOT_TOKEN']) therefore fails at import time with a fake token:
slack_sdk.errors.SlackApiError: The request to the Slack API failed. (url: https://slack.com/api/auth.test)
The server responded with: {'ok': False, 'error': 'invalid_auth'}
...
slack_bolt.error.BoltError: `token` is invalid (auth.test result: {'ok': False, 'error': 'invalid_auth'})Any from bot import handle_ask_command in a test triggers it. Patching SocketModeHandler.connect doesn't help, because the failure happens earlier.
With no token at all, App() raises BoltError: Either an env variable SLACK_BOT_TOKEN or token argument in the constructor is required. Verified with slack-bolt 1.30.0 in python:3.11-slim: token_verification_enabled=False constructs fine with xoxb-fake, and handlers can then be called directly with mocked ack, respond and body. Socket Mode additionally needs an app-level xapp- token for SocketModeHandler(app, os.environ['SLACK_APP_TOKEN']).