Testing
You can test your API KEY authentication using the simpleAuthApiClient
japa plugin to authenticate
when making HTTP requests to your API.
tests/bootstrap.ts
import { simpleAuthApiClient } from '@eienjs/adonisjs-simple-auth/plugins/api_client'; export const plugins: Config['plugins'] = [ assert(), simpleAuthApiClient(app), pluginAdonisJS(app), ];
That’s all. Now, you can authenticate requests using the withApiKey
method.
tests/functional/health_checks.spec.ts
import { test } from '@japa/runner'; test.group('Health checks', () => { const endpoint = 'api.health'; test('server is healthy', async ({ client, route }) => { const response = await client.get(route(endpoint)).withApiKey(); if (response.status() === 400) { const { data } = response.body(); console.info(data); } response.assertOk(); response.assertBodyContains({ status: 'success', data: { isHealthy: true, }, }); }); test('unauthorized access', async ({ client, route }) => { const response = await client.get(route(endpoint)); response.assertUnauthorized(); response.assertBodyContains({ errors: [ { message: 'Unauthorized access', }, ], }); }); });