8
0

cloudservices.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document */
  6. import CloudServices from '../src/cloudservices';
  7. import Context from '@ckeditor/ckeditor5-core/src/context';
  8. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  9. import TokenMock from './_utils/tokenmock';
  10. const Token = CloudServices.Token;
  11. describe( 'CloudServices', () => {
  12. let element;
  13. beforeEach( () => {
  14. CloudServices.Token = TokenMock;
  15. element = document.createElement( 'div' );
  16. document.body.appendChild( element );
  17. } );
  18. afterEach( () => {
  19. CloudServices.Token = Token;
  20. document.body.removeChild( element );
  21. } );
  22. describe( 'init()', () => {
  23. it( 'should expose its properties based on config', () => {
  24. return Context
  25. .create( {
  26. plugins: [ CloudServices ],
  27. cloudServices: {
  28. tokenUrl: 'http://token-endpoint',
  29. additionalOption: 'some-value'
  30. }
  31. } )
  32. .then( context => {
  33. const cloudServicesPlugin = context.plugins.get( CloudServices );
  34. expect( cloudServicesPlugin ).to.be.instanceOf( CloudServices );
  35. expect( cloudServicesPlugin.tokenUrl ).to.equal( 'http://token-endpoint' );
  36. expect( cloudServicesPlugin.additionalOption ).to.equal( 'some-value' );
  37. return context.destroy();
  38. } );
  39. } );
  40. it( 'should work as an editor plugin', () => {
  41. return ClassicTestEditor
  42. .create( element, {
  43. plugins: [ CloudServices ],
  44. cloudServices: {
  45. tokenUrl: 'http://token-endpoint',
  46. additionalOption: 'some-value'
  47. }
  48. } )
  49. .then( editor => {
  50. const cloudServicesPlugin = editor.plugins.get( 'CloudServices' );
  51. expect( cloudServicesPlugin ).to.be.instanceOf( CloudServices );
  52. return editor.destroy();
  53. } );
  54. } );
  55. it( 'should be able to get by its plugin name', () => {
  56. return Context.create( { plugins: [ CloudServices ] } ).then( context => {
  57. const cloudServicesPlugin = context.plugins.get( 'CloudServices' );
  58. expect( cloudServicesPlugin ).to.be.instanceOf( CloudServices );
  59. return context.destroy();
  60. } );
  61. } );
  62. it( 'should not throw an error when no config is provided', () => {
  63. return Context.create( { plugins: [ CloudServices ] } ).then( context => context.destroy() );
  64. } );
  65. it( 'should not expose any default uploadUrl', () => {
  66. return Context.create( { plugins: [ CloudServices ] } ).then( context => {
  67. const cloudServicesPlugin = context.plugins.get( CloudServices );
  68. expect( cloudServicesPlugin.uploadUrl ).to.be.undefined;
  69. return context.destroy();
  70. } );
  71. } );
  72. it( 'should use provided uploadUrl', () => {
  73. return Context
  74. .create( {
  75. plugins: [ CloudServices ],
  76. cloudServices: {
  77. uploadUrl: 'https://some-upload-url/'
  78. }
  79. } )
  80. .then( context => {
  81. const cloudServicesPlugin = context.plugins.get( CloudServices );
  82. expect( cloudServicesPlugin.uploadUrl ).to.equal( 'https://some-upload-url/' );
  83. return context.destroy();
  84. } );
  85. } );
  86. it( 'should provide token if tokenUrl is provided', () => {
  87. CloudServices.Token.initialToken = 'initial-token';
  88. return Context
  89. .create( {
  90. plugins: [ CloudServices ],
  91. cloudServices: {
  92. tokenUrl: 'http://token-endpoint'
  93. }
  94. } )
  95. .then( context => {
  96. const cloudServicesPlugin = context.plugins.get( CloudServices );
  97. expect( cloudServicesPlugin.token.value ).to.equal( 'initial-token' );
  98. return context.destroy();
  99. } );
  100. } );
  101. it( 'should not provide token if tokenUrl is not provided', () => {
  102. CloudServices.Token.initialToken = 'initial-token';
  103. return Context.create( { plugins: [ CloudServices ] } ).then( context => {
  104. const cloudServicesPlugin = context.plugins.get( CloudServices );
  105. expect( cloudServicesPlugin.token ).to.equal( null );
  106. return context.destroy();
  107. } );
  108. } );
  109. } );
  110. describe( 'destroy()', () => {
  111. it( 'should destroy created token when tokenUrl was provided', async () => {
  112. CloudServices.Token.initialToken = 'initial-token';
  113. const context = await Context.create( {
  114. plugins: [ CloudServices ],
  115. cloudServices: {
  116. tokenUrl: 'http://token-endpoint'
  117. }
  118. } );
  119. const cloudServicesPlugin = context.plugins.get( CloudServices );
  120. const destroySpy = sinon.spy( cloudServicesPlugin.token, 'destroy' );
  121. await context.destroy();
  122. sinon.assert.calledOnce( destroySpy );
  123. } );
  124. it( 'should not crash when tokenUrl was not provided', async () => {
  125. const context = await Context.create( { plugins: [ CloudServices ] } );
  126. try {
  127. await context.destroy();
  128. } catch ( error ) {
  129. expect.fail( 'Error should not be thrown.' );
  130. }
  131. } );
  132. } );
  133. } );