Skip to main content

Implementing Token-Based Authentication for Test Cases

A common requirement for chatbot testing is to implement token-based authentication.

  • A chatbot client knows about the authenticated user - there is a client-based mechanism for user authentication in place (Cookies ? Web-App Login?)

  • For starting a chatbot conversation, the chatbot client has to transmit some kind of user authentication token to the chatbot engine to initialize the user context in the conversation

  • The authentication token is available by calling an API

If your requirements are similar to this, head on - here are the step by step instructions how to

  • Setup Botium to query an API for an authentication token before starting a test session

  • Use this authentication token in the test cases

1. Implement Botium Hook to Retrieve API Token

In Botium, head over to the Chatbot and go to Configuration > Connector Settings
  1. Enable the Advanced Mode switch

  2. Add a new capability named CUSTOMHOOK_ONBUILD

  3. In the Capability Value field, add your custom JavaScript code to call your API for a token. In this example we are calling a public dummy API, and we are using one of the returned JSON fields as token
    module.exports = async ({ container, request }) => {
      return new Promise((resolve, reject) => {
        const requestOptions = {
          method: 'get',
          uri: 'https://jsonplaceholder.typicode.com/todos/1',
          json: true    
        }
        request(requestOptions, (err, response, body) => {
          if (err) return reject(err)
          container.pluginInstance.caps.MYTOKEN = body.title
          resolve()
        })
      })
    }
    Note: The dummy token is written to the Botium Capability MYTOKEN
  4. Click SAVE to complete your task.

2. Use the Token in a Test Case

We can use the Botium Scripting Memory to use the dummy token in our convo files.
Note: Don’t forget to enable the Scripting Memory in the Scripting settings of the Test Set. To enable Scripting Memory, go to Botium Tools & Settings > Test Sets > Test Set Configuration > Scripting.

  1. Add a Convo file in your Test Set.
  2. Here is the BotiumScript for copy and paste:
    Greet the bot
    
    #me
    /chatid $cap(MYTOKEN)
    
    #bot


This is the view from the Convo Editor:

Note: We are using the $cap Scripting Memory function to use the capability named MYTOKEN within the convo file.

Was this article helpful?

0 out of 0 found this helpful