Unit testing auth/local/register API

System Information
  • Strapi Version: 4.5.6
  • Operating System: Ubuntu 22.04.1 LTS
  • Database: PostgreSQL
  • Node Version: v18.12.1
  • NPM Version: 9.2.0
  • Yarn Version: 1.22.19

Following strapi documentation, I’m trying to do a unit test of the user registration API, but I am getting a 400 error.

import assert from 'assert';
import { backend } from '../testlib/api.js';

const user_profile = {
	username: 'Strapi user',
	email: 'user@strapi.io',
	password: 'strapiPassword',
};

describe('Test register user', () => {
	it('should be able to add user', done => {
		backend('auth/local/register', 'post', user_profile).then(response => {
			// Handle success.
			console.log(response.data.error);
			let data = response.data.data;
			expect(data).to.exist;
			assert.equal(data.user, user_profile.username);
			expect(data.jwt).to.exist;
			done()
		}).catch(error => {
			// Handle error.
			console.log('An error occurred:', error.message);
			done();
		});
	});
});

The testlib/api.js is a module calling axios to strapi:

import axios from 'axios';
import dotenv from 'dotenv';
dotenv.config();

const Base = process.env.VITE_STRAPI_BASE||'http://localhost:1337/api/';
const config = {
	headers: {
		Authorization: `Bearer ${process.env.VITE_AUTH_TOKEN}`,
		'Content-Type': 'application/json'
	}
};

export function backend (api, method, value) {
	try {
		return axios({
			method: method,
			url: `${Base}${api}`,
			data: value?JSON.stringify({data:value}):undefined,
			...config
		}).then(response => {
			return response.data;
		}).catch(err => {
			return Promise.reject(err);
		})
	} catch (err) {
		return Promise.reject(err);
	}
};

The particular API function is accessible to the public role - I did double check that it is in the Strapi admin panel.

Any advice / thoughts would be much appreciated!