Trying to fix js-api unit tests

This commit is contained in:
Ehsan Rezaei
2024-11-28 11:01:56 +01:00
committed by Vito Albano
parent 3039d1612f
commit 2aa4b5951e
6 changed files with 90 additions and 125 deletions

View File

@@ -18,9 +18,6 @@
import assert from 'assert';
import { AlfrescoApi, Oauth2Auth } from '../src';
declare let window: any;
const globalAny: any = global;
describe('Oauth2 Implicit flow test', () => {
let oauth2Auth: Oauth2Auth;
let alfrescoJsApi: AlfrescoApi;
@@ -29,6 +26,26 @@ describe('Oauth2 Implicit flow test', () => {
alfrescoJsApi = new AlfrescoApi({
hostEcm: ''
});
Object.defineProperty(window, 'location', {
writable: true,
value: {
ancestorOrigins: null,
hash: '',
host: 'dummy.com',
port: '80',
protocol: 'http:',
hostname: 'dummy.com',
href: 'http://localhost/',
origin: 'dummy.com',
pathname: null,
search: null,
assign: (url: string) => {
window.location.href = url;
},
reload: null,
replace: null
}
});
});
it('should throw an error if redirectUri is not present', (done) => {
@@ -53,16 +70,7 @@ describe('Oauth2 Implicit flow test', () => {
});
it('should redirect to login if access token is not valid', (done) => {
window = globalAny.window = {
location: {
assign: (v: any) => {
window.location.href = v;
}
}
};
globalAny.document = {
getElementById: () => ''
};
document.getElementById = () => null;
oauth2Auth = new Oauth2Auth(
{
@@ -87,16 +95,7 @@ describe('Oauth2 Implicit flow test', () => {
});
it('should not loop over redirection when redirectUri contains hash and token is not valid ', (done) => {
window = globalAny.window = {
location: {
assign: (v: any) => {
window.location.href = v;
}
}
};
globalAny.document = {
getElementById: () => ''
};
document.getElementById = () => null;
oauth2Auth = new Oauth2Auth(
{
oauth2: {
@@ -124,16 +123,7 @@ describe('Oauth2 Implicit flow test', () => {
});
it('should not redirect to login if access token is valid', (done) => {
window = globalAny.window = {
location: {
assign: (v: any) => {
window.location.href = v;
}
}
};
globalAny.document = {
getElementById: () => ''
};
document.getElementById = () => null;
oauth2Auth = new Oauth2Auth(
{
oauth2: {
@@ -152,7 +142,7 @@ describe('Oauth2 Implicit flow test', () => {
oauth2Auth.isValidToken = () => true;
oauth2Auth.on('token_issued', () => {
assert.equal(window.location.url, undefined);
assert.equal(window.location.href, 'http://localhost/');
done();
});
@@ -162,27 +152,9 @@ describe('Oauth2 Implicit flow test', () => {
});
it('should set the loginFragment to redirect after the login if it is present', (done) => {
window = globalAny.window = {
location: {
assign: (v: any) => {
window.location.href = v;
}
}
};
globalAny.document = {
getElementById: () => ''
};
window.location.hash = 'asfasfasfa';
Object.defineProperty(window.location, 'hash', {
writable: true,
value: '#/redirect-path&session_state=eqfqwfqwf'
});
Object.defineProperty(window.location, 'href', {
writable: true,
value: 'https://stoca/#/redirect-path&session_state=eqfqwfqwf'
});
document.getElementById = () => null;
window.location.hash = '#/redirect-path&session_state=eqfqwfqwf';
window.location.href = 'https://stoca/#/redirect-path&session_state=eqfqwfqwf';
oauth2Auth = new Oauth2Auth(
{