Skip to content

Commit

Permalink
Tests: Ensured all tested Tokens are destroyed correctly to avoid une…
Browse files Browse the repository at this point in the history
…xpected Token refresh warnings while running tests.
  • Loading branch information
oleq committed Aug 22, 2024
1 parent 0154d4c commit ebb44e6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
48 changes: 43 additions & 5 deletions packages/ckeditor5-cloud-services/tests/token/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ describe( 'Token', () => {
const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue } );

expect( token.value ).to.equal( tokenInitValue );

token.destroy();
} );

it( 'should fire `change:value` event if the value of the token has changed', done => {
Expand All @@ -79,6 +81,8 @@ describe( 'Token', () => {
token.init();

requests[ 0 ].respond( 200, '', tokenValue );

token.destroy();
} );

it( 'should accept the callback in the constructor', () => {
Expand All @@ -98,6 +102,8 @@ describe( 'Token', () => {
.then( () => {
expect( token.value ).to.equal( tokenValue );

token.destroy();

done();
} );

Expand All @@ -111,11 +117,13 @@ describe( 'Token', () => {
return token.init()
.then( () => {
expect( token.value ).to.equal( tokenValue );

token.destroy();
} );
} );

it( 'should not refresh token if autoRefresh is disabled in options', async () => {
const clock = sinon.useFakeTimers( { toFake: [ 'setTimeout' ] } );
const clock = sinon.useFakeTimers();
const tokenInitValue = getTestTokenValue();

const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue, autoRefresh: false } );
Expand All @@ -127,10 +135,12 @@ describe( 'Token', () => {
expect( requests ).to.be.empty;

clock.restore();

token.destroy();
} );

it( 'should refresh token with the time specified in token `exp` payload property', async () => {
const clock = sinon.useFakeTimers( { toFake: [ 'setTimeout' ] } );
const clock = sinon.useFakeTimers();
const tokenInitValue = getTestTokenValue();

const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue } );
Expand All @@ -154,11 +164,13 @@ describe( 'Token', () => {

expect( requests.length ).to.equal( 5 );

token.destroy();

clock.restore();
} );

it( 'should refresh the token with the default time if getting token expiration time failed', async () => {
const clock = sinon.useFakeTimers( { toFake: [ 'setTimeout' ] } );
const clock = sinon.useFakeTimers();
const tokenValue = 'header.test.signature';

const token = new Token( 'http://token-endpoint', { initValue: tokenValue } );
Expand All @@ -173,11 +185,13 @@ describe( 'Token', () => {

expect( requests.length ).to.equal( 2 );

token.destroy();

clock.restore();
} );

it( 'should refresh the token with the default time if the token payload does not contain `exp` property', async () => {
const clock = sinon.useFakeTimers( { toFake: [ 'setTimeout' ] } );
const clock = sinon.useFakeTimers();
const tokenValue = `header.${ btoa( JSON.stringify( {} ) ) }.signature`;

const token = new Token( 'http://token-endpoint', { initValue: tokenValue } );
Expand All @@ -195,13 +209,15 @@ describe( 'Token', () => {

expect( requests.length ).to.equal( 3 );

token.destroy();

clock.restore();
} );
} );

describe( 'destroy', () => {
it( 'should stop refreshing the token', async () => {
const clock = sinon.useFakeTimers( { toFake: [ 'setTimeout', 'clearTimeout' ] } );
const clock = sinon.useFakeTimers();
const tokenInitValue = getTestTokenValue();

const token = new Token( 'http://token-endpoint', { initValue: tokenInitValue } );
Expand Down Expand Up @@ -237,6 +253,8 @@ describe( 'Token', () => {
.then( newToken => {
expect( newToken.value ).to.equal( tokenValue );

token.destroy();

done();
} );

Expand All @@ -256,6 +274,7 @@ describe( 'Token', () => {
.catch( error => {
expect( error.constructor ).to.equal( CKEditorError );
expect( error ).to.match( /token-not-in-jwt-format/ );
token.destroy();
done();
} );

Expand All @@ -274,6 +293,7 @@ describe( 'Token', () => {
.catch( error => {
expect( error.constructor ).to.equal( CKEditorError );
expect( error ).to.match( /token-not-in-jwt-format/ );
token.destroy();
done();
} );

Expand All @@ -287,6 +307,7 @@ describe( 'Token', () => {
return token.refreshToken()
.then( newToken => {
expect( newToken.value ).to.equal( tokenValue );
token.destroy();
} );
} );

Expand All @@ -302,6 +323,7 @@ describe( 'Token', () => {
}, error => {
expect( error.constructor ).to.equal( CKEditorError );
expect( error ).to.match( /token-cannot-download-new-token/ );
token.destroy();
} );
} );

Expand All @@ -316,6 +338,7 @@ describe( 'Token', () => {
throw new Error( 'Promise should be rejected' );
}, error => {
expect( error ).to.match( /Abort/ );
token.destroy();
} );
} );

Expand All @@ -330,6 +353,7 @@ describe( 'Token', () => {
throw new Error( 'Promise should be rejected' );
}, error => {
expect( error ).to.match( /Network Error/ );
token.destroy();
} );
} );

Expand All @@ -340,6 +364,7 @@ describe( 'Token', () => {
token.refreshToken()
.catch( error => {
expect( error ).to.equal( 'Custom error occurred' );
token.destroy();
} );
} );

Expand Down Expand Up @@ -369,6 +394,7 @@ describe( 'Token', () => {
throw new Error( 'Promise should fail' );
}, () => {
sinon.assert.calledWithMatch( console.warn, 'token-refresh-failed', { autoRefresh: false } );
token.destroy();
} );
} );

Expand Down Expand Up @@ -398,6 +424,8 @@ describe( 'Token', () => {

await clock.tickAsync( '05' );
expect( requests.length ).to.equal( 4 );

token.destroy();
} );
} );

Expand Down Expand Up @@ -431,6 +459,8 @@ describe( 'Token', () => {

await clock.tickAsync( '10' );
expect( requests.length ).to.equal( 4 );

token.destroy();
} );
} );

Expand All @@ -453,6 +483,8 @@ describe( 'Token', () => {

await clock.tickAsync( '10' );
expect( requests.length ).to.equal( 1 );

token.destroy();
} );
} );

Expand Down Expand Up @@ -485,6 +517,8 @@ describe( 'Token', () => {
await clock.tickAsync( '05' );

expect( requests.length ).to.equal( 6 );

token.destroy();
} );
} );
} );
Expand All @@ -498,6 +532,8 @@ describe( 'Token', () => {
.then( token => {
expect( token.value ).to.equal( tokenValue );

token.destroy();

done();
} );

Expand All @@ -511,6 +547,8 @@ describe( 'Token', () => {
.then( token => {
expect( token._options ).to.eql( { autoRefresh: true } );

token.destroy();

done();
} );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ const BASE_64_FILE = 'data:image/gif;base64,R0lGODlhCQAJAPIAAGFhYZXK/1FRUf///' +

describe( 'FileUploader', () => {
const tokenInitValue = `header.${ btoa( JSON.stringify( { exp: Date.now() + 3600000 } ) ) }.signature`;
const token = new Token( 'url', { initValue: tokenInitValue, autoRefresh: false } );

let fileUploader;
let fileUploader, token;

beforeEach( () => {
token = new Token( 'url', { initValue: tokenInitValue, autoRefresh: false } );
fileUploader = new FileUploader( BASE_64_FILE, token, API_ADDRESS );
} );

afterEach( () => {
token.destroy();
} );

describe( 'constructor()', () => {
it( 'should throw error when no fileOrData provided', () => {
expect( () => new FileUploader() ).to.throw( CKEditorError, 'fileuploader-missing-file' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@ import Token from '../../src/token/token.js';
import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils.js';

describe( 'UploadGateway', () => {
const tokenInitValue = `header.${ btoa( JSON.stringify( { exp: Date.now() + 3600000 } ) ) }.signature`;
const token = new Token( 'url', { initValue: tokenInitValue, autoRefresh: false } );
let token;

beforeEach( () => {
const tokenInitValue = `header.${ btoa( JSON.stringify( { exp: Date.now() + 3600000 } ) ) }.signature`;

token = new Token( 'url', { initValue: tokenInitValue, autoRefresh: false } );
} );

afterEach( () => {
token.destroy();
} );

describe( 'constructor()', () => {
it( 'should throw error when no token provided', () => {
Expand Down

0 comments on commit ebb44e6

Please sign in to comment.