Implementing Slash Commands Using Amazon Lambda Functions – Writing Tests

In a couple of earlier posts, I’ve described how to set up a simple (insecure) AWS Lambda function to handle a request from a Slack slash command using AWS Lambda functions and the AWS API Gateway, as well as how to encrypt the Slack token that can be used by the micro-service to check that the request has come from a known Slack channel. In this post, I’ll show how to define a simple test event that allows you to test the operation of a Lambda function.

The function I want to test initially is one that simply parses an HTTP POST message from a Slack slash command. When the slash command is issued, a callback is raised that POSTs a payload with the following structure to the Lambda function:

token=SOME_TOKEN
team_id=T0001
team_domain=example
channel_id=C123456789
channel_name=test
user_id=U123456789
user_name=TestUser
command=/testcommand
text="some sort of text string"
response_url=https://hooks.slack.com/commands/1234/5678

The Slack documentation describes how this data will be sent to your URL as a HTTP POST with a content-type header set as application/x-www-form-urlencoded, which is to say that it will be passed in the body in a encoded URL form:

token=SOME_TOKEN&team_id=T0001&team_domain=example&... etc.

The Lambda function test event is created from the Lambda function control panel:

Lambda_Management_Console_test1

The test event needs to contain an example of the POSTed information that the Lambda function expects and can handle:

Lambda_Management_Console_test

For example:
{
"body":"token=ACTUAL_TOKEN _GIVES_THE_SECRET_AWAY_OOP&text=Who+are+the+members+of+the+Defence+Committee&command=/simpletest&user_name=testuser&channel_name=testChannel"
}

I suppose a dummy test token could be used in the test string and provided with limited access to the Lambda function routines?

Saving and running the text function provides a report showing either the output from a successful execution of the function, or an error message…:

Lambda_Management_Console_and_Inbox

Of course, if you don’t create a test event that faithfully resembles the content of a message sent from the service that triggered the Lambda function (in this case, an application/x-www-form-urlencoded POST event raised by the Slack slash command), you’ll either be testing against the wrong thing or getting a false response from the test.