Integration Tests - Simulating Strapi Admin GUI Requests (User Actions)

Answer / Possible Solution

I think I have found an answer to the question myself.

The simplest solution I was able to come up with was to:

  1. Create the necessary Users
  • create an API user with all privileges just for test purposes
  • create an Admin Super user just for test purposes
  1. Backup/dump the database to a file (e.g., backup.sql) or save the sqlite database.

  2. Before you run the integration tests, run a command along the lines of: npm run db:setup:from:file or point to the respective sqlite database.

  3. Inside the tests where one wants to use both Super User (Admin GUI) or normal API user, use the following function to issue a token (avoid having to login via the API):

    • const apiUserToken = strapi.plugins['users-permissions'].services.jwt.issue({ id: "API_USER_ID" });
    • const superUserToken = strapi.admin.services.token.createJwtToken({ id: "SUPER_USER_ID" });
  4. Requests that are sent from the Admin GUI can be performed as well.

    const result= await post(
      '/content-manager/collection-types/application::user:user', // url/target
      { name: 'change-my-name' }, // body
      adminJwt // token
    );
    

I hope this helps others when they’re trying to write integration tests that target routes only accessible with super user privileges. @DMehaffy should I add a version of this to the documentation section on testing?