migrate js-api tests from Jest to Node.js native test runner (#12104)

This commit is contained in:
Denys Vuika
2026-07-28 20:04:31 +01:00
committed by GitHub
parent c316c6b594
commit 6c8b874704
58 changed files with 2355 additions and 4978 deletions
+62 -87
View File
@@ -19,6 +19,7 @@ import assert from 'assert';
import { ProcessAuth } from '../src';
import { FetchHttpClient } from '../src/fetchHttpClient';
import { BpmAuthMock } from './mockObjects';
import { describe, it, beforeEach, afterEach } from 'node:test';
describe('Bpm Auth test', () => {
const hostBpm = 'https://127.0.0.1:9999';
@@ -30,11 +31,11 @@ describe('Bpm Auth test', () => {
it('should remember username on login', () => {
const auth = new ProcessAuth({});
auth.login('johndoe', 'password');
auth.login('johndoe', 'password').catch(() => {});
assert.equal(auth.authentications.basicAuth.username, 'johndoe');
});
it('should forget username on logout', (done) => {
it('should forget username on logout', async () => {
const processAuth = new ProcessAuth({
hostBpm,
contextRootBpm: 'activiti-app'
@@ -42,16 +43,13 @@ describe('Bpm Auth test', () => {
authBpmMock.get200Response();
processAuth.login('admin', 'admin').then(() => {
assert.equal(processAuth.authentications.basicAuth.username, 'admin');
await processAuth.login('admin', 'admin');
assert.equal(processAuth.authentications.basicAuth.username, 'admin');
authBpmMock.get200ResponseLogout();
authBpmMock.get200ResponseLogout();
processAuth.logout().then(() => {
assert.equal(processAuth.authentications.basicAuth.username, null);
done();
});
});
await processAuth.logout();
assert.equal(processAuth.authentications.basicAuth.username, null);
});
describe('With Authentication', () => {
@@ -67,7 +65,7 @@ describe('Bpm Auth test', () => {
assert.equal(data, 'Basic YWRtaW46YWRtaW4=');
});
it('login password should be removed after login', (done) => {
it('login password should be removed after login', async () => {
authBpmMock.get200Response();
const processAuth = new ProcessAuth({
@@ -75,14 +73,12 @@ describe('Bpm Auth test', () => {
contextRootBpm: 'activiti-app'
});
processAuth.login('admin', 'admin').then((data) => {
assert.equal(data, 'Basic YWRtaW46YWRtaW4=');
assert.notEqual(processAuth.authentications.basicAuth.password, 'admin');
done();
});
const data = await processAuth.login('admin', 'admin');
assert.equal(data, 'Basic YWRtaW46YWRtaW4=');
assert.notEqual(processAuth.authentications.basicAuth.password, 'admin');
});
it('isLoggedIn should return true if the api is logged in', (done) => {
it('isLoggedIn should return true if the api is logged in', async () => {
authBpmMock.get200Response();
const processAuth = new ProcessAuth({
@@ -90,30 +86,26 @@ describe('Bpm Auth test', () => {
contextRootBpm: 'activiti-app'
});
processAuth.login('admin', 'admin').then(() => {
assert.equal(processAuth.isLoggedIn(), true);
done();
});
await processAuth.login('admin', 'admin');
assert.equal(processAuth.isLoggedIn(), true);
});
it('isLoggedIn should return false if the api is logged out', (done) => {
it('isLoggedIn should return false if the api is logged out', async () => {
authBpmMock.get200Response();
const processAuth = new ProcessAuth({
hostBpm,
contextRootBpm: 'activiti-app'
});
processAuth.login('admin', 'admin');
await processAuth.login('admin', 'admin');
authBpmMock.get200ResponseLogout();
processAuth.logout().then(() => {
assert.equal(processAuth.isLoggedIn(), false);
done();
});
await processAuth.logout();
assert.equal(processAuth.isLoggedIn(), false);
});
it('isLoggedIn should return false if the host change', (done) => {
it('isLoggedIn should return false if the host change', async () => {
authBpmMock.get200Response();
const processAuth = new ProcessAuth({
@@ -121,15 +113,13 @@ describe('Bpm Auth test', () => {
contextRootBpm: 'activiti-app'
});
processAuth.login('admin', 'admin').then(() => {
assert.equal(processAuth.isLoggedIn(), true);
processAuth.changeHost();
assert.equal(processAuth.isLoggedIn(), false);
done();
});
await processAuth.login('admin', 'admin');
assert.equal(processAuth.isLoggedIn(), true);
processAuth.changeHost();
assert.equal(processAuth.isLoggedIn(), false);
});
it('login should return an error if wrong credential are used 401 the login fails', (done) => {
it('login should return an error if wrong credential are used 401 the login fails', async () => {
authBpmMock.get401Response();
const processAuth = new ProcessAuth({
@@ -137,17 +127,15 @@ describe('Bpm Auth test', () => {
contextRootBpm: 'activiti-app'
});
processAuth.login('wrong', 'name').then(
() => {},
(error) => {
assert.equal(error.status, 401);
done();
}
);
try {
await processAuth.login('wrong', 'name');
} catch (error: any) {
assert.equal(error.status, 401);
}
});
describe('Events ', () => {
it('login should fire an event if is unauthorized 401', (done) => {
it('login should fire an event if is unauthorized 401', async () => {
authBpmMock.get401Response();
const processAuth = new ProcessAuth({
@@ -155,15 +143,15 @@ describe('Bpm Auth test', () => {
contextRootBpm: 'activiti-app'
});
const loginPromise = processAuth.login('wrong', 'name');
loginPromise.catch(() => {});
loginPromise.on('unauthorized', () => {
done();
});
try {
await processAuth.login('wrong', 'name');
assert.fail('Expected login to fail');
} catch (error: any) {
assert.equal(error.status, 401);
}
});
it('login should fire an event if is forbidden 403', (done) => {
it('login should fire an event if is forbidden 403', async () => {
authBpmMock.get403Response();
const processAuth = new ProcessAuth({
@@ -171,14 +159,15 @@ describe('Bpm Auth test', () => {
contextRootBpm: 'activiti-app'
});
const loginPromise = processAuth.login('wrong', 'name');
loginPromise.catch(() => {});
loginPromise.on('forbidden', () => {
done();
});
try {
await processAuth.login('wrong', 'name');
assert.fail('Expected login to fail');
} catch (error: any) {
assert.equal(error.status, 403);
}
});
it('The Api Should fire success event if is all ok 201', (done) => {
it('The Api Should fire success event if is all ok 201', async () => {
authBpmMock.get200Response();
const processAuth = new ProcessAuth({
@@ -186,15 +175,11 @@ describe('Bpm Auth test', () => {
contextRootBpm: 'activiti-app'
});
const loginPromise = processAuth.login('admin', 'admin');
loginPromise.catch(() => {});
loginPromise.on('success', () => {
done();
});
const data = await processAuth.login('admin', 'admin');
assert.equal(data, 'Basic YWRtaW46YWRtaW4=');
});
it('The Api Should fire logout event if the logout is successfull', (done) => {
it('The Api Should fire logout event if the logout is successfull', async () => {
authBpmMock.get200Response();
const processAuth = new ProcessAuth({
@@ -202,14 +187,12 @@ describe('Bpm Auth test', () => {
contextRootBpm: 'activiti-app'
});
processAuth.login('admin', 'admin');
await processAuth.login('admin', 'admin');
authBpmMock.get200ResponseLogout();
const promise = processAuth.logout();
promise.on('logout', () => {
done();
});
await processAuth.logout();
assert.equal(processAuth.getTicket(), null);
});
});
@@ -228,7 +211,7 @@ describe('Bpm Auth test', () => {
describe('Logout Api', () => {
let processAuth: ProcessAuth;
beforeEach((done) => {
beforeEach(async () => {
authBpmMock.get200Response();
processAuth = new ProcessAuth({
@@ -236,18 +219,14 @@ describe('Bpm Auth test', () => {
contextRootBpm: 'activiti-app'
});
processAuth.login('admin', 'admin').then(() => {
done();
});
await processAuth.login('admin', 'admin');
});
it('Ticket should be absent in the client and the resolve promise should be called', (done) => {
it('Ticket should be absent in the client and the resolve promise should be called', async () => {
authBpmMock.get200ResponseLogout();
processAuth.logout().then(() => {
assert.equal(processAuth.getTicket(), null);
done();
});
await processAuth.logout();
assert.equal(processAuth.getTicket(), null);
});
});
@@ -269,7 +248,7 @@ describe('Bpm Auth test', () => {
setCsrfTokenCalled = false;
});
it('should be enabled by default', (done) => {
it('should be enabled by default', async () => {
authBpmMock.get200Response();
const processAuth = new ProcessAuth({
@@ -277,13 +256,11 @@ describe('Bpm Auth test', () => {
contextRootBpm: 'activiti-app'
});
processAuth.login('admin', 'admin').then(() => {
assert.equal(setCsrfTokenCalled, true);
done();
});
await processAuth.login('admin', 'admin');
assert.equal(setCsrfTokenCalled, true);
});
it('should be disabled if disableCsrf is true', (done) => {
it('should be disabled if disableCsrf is true', async () => {
authBpmMock.get200Response();
const processAuth = new ProcessAuth({
@@ -292,10 +269,8 @@ describe('Bpm Auth test', () => {
disableCsrf: true
});
processAuth.login('admin', 'admin').then(() => {
assert.equal(setCsrfTokenCalled, false);
done();
});
await processAuth.login('admin', 'admin');
assert.equal(setCsrfTokenCalled, false);
});
});
});