Skip to content

Testing APIs: using basic authentication #16

@lvanvugt

Description

@lvanvugt

In chapter 13 is shown how to test your own APIs making use of the trick MS is using with respect to authentication as "Including the authentication in tests would be quite cumbersome" as I state in the following section (taken from chapter 13):

Authenticating the client

The next challenge is the authentication of the client as the service being called upon will require the client’s credentials. Including the authentication in tests would be quite cumbersome, and therefore the Microsoft Business Central team has chosen the practical approach, focusing on testing the API business logic and not validating the authentication. As such API tests are run in a database with no user and user-related data. In order to achieve this, the following needs to be done:

  1. Set up your Business Central environment to use Windows authentication.
  2. Remove all user and user-related data from the database.
  3. Deploy your extension(s) with your application and test code.
  4. Run the tests with standard test runner Test Runner - Isol. Disabled (130451).

Note that with this approach, we ignore testing the authorization to the server.

Trade-off

The trade-off of using Windows authentication is that it is not (easily) possible to get your API tests run in a pipeline as most probably the various parts of the pipeline are not run/triggered in your domain.

Alternative trick

Triggered by my Dutch Dynamics Community presentation, and later my Areopa webinar, Automated testing of your Business Central API Arend-Jan Kauffmann - who else - suggested to use the following trick allowing the usage of basic authentication. In this we are assuming we're using a docker container.

  1. When building the docker container for testing the APIs the user and password are known
  2. Adjust the script in such a way that it:
    a. adds an extension which introduces a table in BC - let's call it Test Setup - with a field in which the password can be stored
    b. populates that field with the password
  3. Design your API tests to make use of this password (combined with the user)

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions