Testing the Auth0 Management API with a machine-to-machine token

Most apps on Auth0 call its Management API from the backend: create a user on invite, block one on offboarding, assign a role on upgrade. The token that does this is powerful, and the scopes on it decide how powerful. A test collection checks both that the calls work and that the token can't do more than it should.

Step 1: Application and environment

In a development tenant, Applications ▸ Create Application ▸ Machine to Machine, authorize it for the Auth0 Management API, and grant only read:users, create:users, update:users and read:roles.

VariableValue
auth0Domainyour-tenant.eu.auth0.com
auth0ClientIdthe application's client id
auth0ClientSecretits secret, marked as secret

The token request is the first request of the collection, so it can also check what the token may do:

HTTP
POST https://{{auth0Domain}}/oauth/token
Content-Type: application/json

{
  "grant_type": "client_credentials",
  "client_id": "{{auth0ClientId}}",
  "client_secret": "{{auth0ClientSecret}}",
  "audience": "https://{{auth0Domain}}/api/v2/"
}

The audience is required: without it Auth0 refuses the request with access_denied.

Step 2: Check the token's scopes

The answer lists the granted scopes next to the token:

JavaScript
const t = pm.response.json();
pm.environment.set('auth0Token', t.access_token);
pm.test('token is not over-scoped', () => {
  pm.expect(t.scope).to.include('read:users');
  pm.expect(t.scope).to.not.include('delete:users');
  pm.expect(t.scope).to.not.include('update:clients');
});

Set the collection's Authorization to Bearer token with {{auth0Token}}, and give this request No auth. Tokens last 24 hours by default, so fetching one at the start of each run is enough. More on reading tokens in testing JWT expiry and claims.

Step 3: Find a user by email

HTTP
GET https://{{auth0Domain}}/api/v2/users?q=email:"[email protected]"&search_engine=v3
JavaScript
const users = pm.response.json();
pm.test('search works', () => pm.expect(users).to.be.an('array'));
if (users.length) pm.environment.set('userId', users[0].user_id);

User search is eventually consistent: a user created a moment ago may not be found yet. For lookups right after a create, use GET /api/v2/users-by-email?email=…, which isn't.

Step 4: Create a user

HTTP
POST https://{{auth0Domain}}/api/v2/users
Content-Type: application/json

{
  "connection": "Username-Password-Authentication",
  "email": "api-test+{{$timestamp}}@example.com",
  "password": "{{testUserPassword}}",
  "email_verified": true,
  "app_metadata": {"plan": "pro"}
}
JavaScript
const u = pm.response.json();
pm.test('user created', () => {
  pm.expect(pm.response.code).to.equal(201);
  pm.expect(u.app_metadata.plan).to.equal('pro');
});
pm.environment.set('userId', u.user_id);

A password that fails the connection's password policy answers 400 with PasswordStrengthError. A duplicate email answers 409. Both are worth a negative test, because your invite flow has to show something sensible for them.

Step 5: The call it must not make

HTTP
DELETE https://{{auth0Domain}}/api/v2/users/{{userId}}
JavaScript
pm.test('token cannot delete users', () => {
  pm.expect(pm.response.code).to.equal(403);
  pm.expect(pm.response.json().errorCode).to.equal('insufficient_scope');
});

This test fails the day someone "temporarily" adds delete:users to the application and forgets. Clean up test users with a separate, more privileged collection, or block them with PATCH {"blocked": true}.

Rate limits

The Management API is rate-limited per tenant, with x-ratelimit-limit, x-ratelimit-remaining and x-ratelimit-reset on every response, and 429 when you go over. Development tenants have low limits, so use --delay in bulk runs.