Skip to content

Commit

Permalink
Merge pull request #16960 from ckeditor/ck/token-refresh-warn-test-fa…
Browse files Browse the repository at this point in the history
…ilure

Tests (cloud-services): Ensured all tested `Tokens` are destroyed correctly to avoid unexpected `Token` refresh warnings while running tests.
oleq authored Aug 22, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 0154d4c + ebb44e6 commit eda3866
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
@@ -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 => {
@@ -79,6 +81,8 @@ describe( 'Token', () => {
token.init();

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

token.destroy();
} );

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

token.destroy();

done();
} );

@@ -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 } );
@@ -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 } );
@@ -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 } );
@@ -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 } );
@@ -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 } );
@@ -237,6 +253,8 @@ describe( 'Token', () => {
.then( newToken => {
expect( newToken.value ).to.equal( tokenValue );

token.destroy();

done();
} );

@@ -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();
} );

@@ -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();
} );

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

token.destroy();

done();
} );

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

token.destroy();

done();
} );

Original file line number Diff line number Diff line change
@@ -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' );
Original file line number Diff line number Diff line change
@@ -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', () => {

0 comments on commit eda3866

Please sign in to comment.