mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2026-09-09 18:03:21 +00:00
migrate js-api tests from Jest to Node.js native test runner (#12104)
This commit is contained in:
@@ -16,8 +16,10 @@
|
||||
*/
|
||||
|
||||
import assert from 'assert';
|
||||
import { resetGlobalMockAgent } from './mockObjects/base.mock';
|
||||
import { AlfrescoApi, ContentAuth } from '../src';
|
||||
import { EcmAuthMock as AuthEcmMock } from '../test/mockObjects';
|
||||
import { describe, it, beforeEach, afterEach } from 'node:test';
|
||||
|
||||
describe('Ecm Auth test', () => {
|
||||
const hostEcm = 'https://127.0.0.1:8080';
|
||||
@@ -42,148 +44,156 @@ describe('Ecm Auth test', () => {
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
resetGlobalMockAgent();
|
||||
});
|
||||
|
||||
it('should remember username on login', () => {
|
||||
const auth = new ContentAuth({}, alfrescoJsApi);
|
||||
auth.login('johndoe', 'password');
|
||||
authEcmMock.get201Response();
|
||||
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 auth = new ContentAuth({}, alfrescoJsApi);
|
||||
|
||||
authEcmMock.get201Response();
|
||||
|
||||
auth.login('johndoe', 'password');
|
||||
auth.login('johndoe', 'password').catch(() => {});
|
||||
assert.equal(auth.authentications.basicAuth.username, 'johndoe');
|
||||
|
||||
authEcmMock.get204ResponseLogout();
|
||||
|
||||
auth.logout().then(() => {
|
||||
assert.equal(auth.authentications.basicAuth.username, null);
|
||||
done();
|
||||
});
|
||||
await auth.logout();
|
||||
assert.equal(auth.authentications.basicAuth.username, null);
|
||||
});
|
||||
|
||||
describe('With Authentication', () => {
|
||||
it('login should return the Ticket if all is ok', (done) => {
|
||||
it('login should return the Ticket if all is ok', async () => {
|
||||
authEcmMock.get201Response();
|
||||
|
||||
contentAuth.login('admin', 'admin').then((data) => {
|
||||
assert.equal(data, 'TICKET_4479f4d3bb155195879bfbb8d5206f433488a1b1');
|
||||
done();
|
||||
});
|
||||
const data = await contentAuth.login('admin', 'admin');
|
||||
assert.equal(data, 'TICKET_4479f4d3bb155195879bfbb8d5206f433488a1b1');
|
||||
});
|
||||
|
||||
it('login password should be removed after login', (done) => {
|
||||
it('login password should be removed after login', async () => {
|
||||
authEcmMock.get201Response();
|
||||
|
||||
contentAuth.login('admin', 'admin').then(() => {
|
||||
assert.notEqual(contentAuth.authentications.basicAuth.password, 'admin');
|
||||
done();
|
||||
});
|
||||
await contentAuth.login('admin', 'admin');
|
||||
assert.notEqual(contentAuth.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 () => {
|
||||
authEcmMock.get201Response();
|
||||
|
||||
contentAuth.login('admin', 'admin').then(() => {
|
||||
assert.equal(contentAuth.isLoggedIn(), true);
|
||||
done();
|
||||
});
|
||||
await contentAuth.login('admin', 'admin');
|
||||
assert.equal(contentAuth.isLoggedIn(), true);
|
||||
});
|
||||
|
||||
it('isLoggedIn should return false if the host change', (done) => {
|
||||
it('isLoggedIn should return false if the host change', async () => {
|
||||
authEcmMock.get201Response();
|
||||
|
||||
contentAuth.login('admin', 'admin').then(() => {
|
||||
assert.equal(contentAuth.isLoggedIn(), true);
|
||||
contentAuth.changeHost();
|
||||
assert.equal(contentAuth.isLoggedIn(), false);
|
||||
done();
|
||||
});
|
||||
await contentAuth.login('admin', 'admin');
|
||||
assert.equal(contentAuth.isLoggedIn(), true);
|
||||
contentAuth.changeHost();
|
||||
assert.equal(contentAuth.isLoggedIn(), false);
|
||||
});
|
||||
|
||||
it('isLoggedIn should return false if the api is logged out', (done) => {
|
||||
it('isLoggedIn should return false if the api is logged out', async () => {
|
||||
authEcmMock.get201Response();
|
||||
|
||||
contentAuth.login('admin', 'admin');
|
||||
await contentAuth.login('admin', 'admin');
|
||||
|
||||
authEcmMock.get204ResponseLogout();
|
||||
|
||||
contentAuth.logout().then(() => {
|
||||
assert.equal(contentAuth.isLoggedIn(), false);
|
||||
done();
|
||||
});
|
||||
await contentAuth.logout();
|
||||
assert.equal(contentAuth.isLoggedIn(), false);
|
||||
});
|
||||
|
||||
it('login should return an error if wrong credential are used 403 the login fails', (done) => {
|
||||
it('login should return an error if wrong credential are used 403 the login fails', async () => {
|
||||
authEcmMock.get403Response();
|
||||
|
||||
contentAuth.login('wrong', 'name').then(
|
||||
() => {},
|
||||
(error: any) => {
|
||||
assert.equal(error.status, 403);
|
||||
done();
|
||||
}
|
||||
);
|
||||
try {
|
||||
await contentAuth.login('wrong', 'name');
|
||||
} catch (error: any) {
|
||||
assert.equal(error.status, 403);
|
||||
}
|
||||
});
|
||||
|
||||
it('login should return an error if wrong credential are used 400 userId and/or password are/is not provided', (done) => {
|
||||
it('login should return an error if wrong credential are used 400 userId and/or password are/is not provided', async () => {
|
||||
authEcmMock.get400Response();
|
||||
|
||||
contentAuth.login(null, null).then(
|
||||
() => {},
|
||||
(error) => {
|
||||
assert.equal(error.status, 400);
|
||||
done();
|
||||
}
|
||||
);
|
||||
try {
|
||||
await contentAuth.login(null, null);
|
||||
} catch (error: any) {
|
||||
assert.equal(error.status, 400);
|
||||
}
|
||||
});
|
||||
|
||||
describe('Events ', () => {
|
||||
it('login should fire an event if is unauthorized 401', (done) => {
|
||||
it('login should fire an event if is unauthorized 401', async () => {
|
||||
authEcmMock.get401Response();
|
||||
|
||||
let unauthorizedEventFired = false;
|
||||
const loginPromise: any = contentAuth.login('wrong', 'name');
|
||||
loginPromise.catch(() => {});
|
||||
|
||||
loginPromise.on('unauthorized', () => {
|
||||
done();
|
||||
unauthorizedEventFired = true;
|
||||
});
|
||||
|
||||
await loginPromise.catch(() => {});
|
||||
assert.equal(unauthorizedEventFired, true, 'Unauthorized event should have fired');
|
||||
});
|
||||
|
||||
it('login should fire an event if is forbidden 403', (done) => {
|
||||
it('login should fire an event if is forbidden 403', async () => {
|
||||
authEcmMock.get403Response();
|
||||
|
||||
let forbiddenEventFired = false;
|
||||
const loginPromise: any = contentAuth.login('wrong', 'name');
|
||||
|
||||
loginPromise.catch(() => {});
|
||||
|
||||
loginPromise.on('forbidden', () => {
|
||||
done();
|
||||
forbiddenEventFired = true;
|
||||
});
|
||||
|
||||
await loginPromise.catch(() => {});
|
||||
assert.equal(forbiddenEventFired, true, 'Forbidden event should have fired');
|
||||
});
|
||||
|
||||
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 () => {
|
||||
authEcmMock.get201Response();
|
||||
|
||||
let successEventFired = false;
|
||||
const loginPromise: any = contentAuth.login('admin', 'admin');
|
||||
|
||||
loginPromise.catch(() => {});
|
||||
|
||||
loginPromise.on('success', () => {
|
||||
done();
|
||||
successEventFired = true;
|
||||
});
|
||||
|
||||
await loginPromise.catch(() => {});
|
||||
assert.equal(successEventFired, true, 'Success event should have fired');
|
||||
});
|
||||
|
||||
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 () => {
|
||||
authEcmMock.get201Response();
|
||||
contentAuth.login('admin', 'admin');
|
||||
contentAuth.login('admin', 'admin').catch(() => {});
|
||||
authEcmMock.get204ResponseLogout();
|
||||
|
||||
let logoutEventFired = false;
|
||||
(contentAuth.logout() as any).on('logout', () => {
|
||||
done();
|
||||
logoutEventFired = true;
|
||||
});
|
||||
(contentAuth.logout() as any).catch(() => {});
|
||||
|
||||
await new Promise<void>((resolve) => {
|
||||
setTimeout(() => resolve(), 100);
|
||||
});
|
||||
assert.equal(logoutEventFired, true, 'Logout event should have fired');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -204,32 +214,26 @@ describe('Ecm Auth test', () => {
|
||||
});
|
||||
|
||||
describe('Logout Api', () => {
|
||||
beforeEach((done) => {
|
||||
beforeEach(async () => {
|
||||
authEcmMock.get201Response('TICKET_22d7a5a83d78b9cc9666ec4e412475e5455b33bd');
|
||||
|
||||
contentAuth.login('admin', 'admin').then(() => {
|
||||
done();
|
||||
});
|
||||
await contentAuth.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 () => {
|
||||
authEcmMock.get204ResponseLogout();
|
||||
|
||||
contentAuth.logout().then(() => {
|
||||
assert.equal(contentAuth.config.ticket, undefined);
|
||||
done();
|
||||
});
|
||||
await contentAuth.logout();
|
||||
assert.equal(contentAuth.config.ticket, undefined);
|
||||
});
|
||||
|
||||
it('Logout should be rejected if the Ticket is already expired', (done) => {
|
||||
it('Logout should be rejected if the Ticket is already expired', async () => {
|
||||
authEcmMock.get404ResponseLogout();
|
||||
contentAuth.logout().then(
|
||||
() => {},
|
||||
(error) => {
|
||||
assert.equal(error.error.toString(), 'Error: Not Found');
|
||||
done();
|
||||
}
|
||||
);
|
||||
try {
|
||||
await contentAuth.logout();
|
||||
} catch (error: any) {
|
||||
assert.equal(error.error.toString(), 'Error: Not Found');
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user