From 37b9af3f54fb252f2f349bc9eb91904a783629ba Mon Sep 17 00:00:00 2001 From: "denys.oblohin" Date: Wed, 31 Jul 2024 23:07:01 +0300 Subject: [PATCH] fix unit --- test/spec/fingerprint.js | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/test/spec/fingerprint.js b/test/spec/fingerprint.js index 31ab7776a..ad91e281f 100644 --- a/test/spec/fingerprint.js +++ b/test/spec/fingerprint.js @@ -40,7 +40,8 @@ describe('fingerprint', function() { type: 'FingerprintAvailable', fingerprint: 'ABCD' }), - origin: 'http://example.okta.com' + origin: 'http://example.okta.com', + source: test.iframe.contentWindow }); }); @@ -48,6 +49,9 @@ describe('fingerprint', function() { style: {}, parentElement: { removeChild: jest.fn() + }, + contentWindow: { + postMessage: postMessageSpy } }; @@ -61,7 +65,7 @@ describe('fingerprint', function() { jest.spyOn(document.body, 'appendChild').mockImplementation(function() { if (options.timeout) { return; } // mimic async page load with setTimeouts - if (options.sendOtherMessage) { + if (options.sendMessageFromAnotherOrigin) { setTimeout(function() { listeners.message({ data: '{"not":"forUs"}', @@ -69,15 +73,24 @@ describe('fingerprint', function() { }); }); } + if (options.sendMessageFromAnotherSource) { + setTimeout(function() { + listeners.message({ + data: '{"not":"forUs"}', + origin: 'http://example.okta.com', + source: { + postMessage: postMessageSpy + } + }); + }); + } setTimeout(function() { listeners.message({ data: options.firstMessage || JSON.stringify({ type: 'FingerprintServiceReady' }), origin: 'http://example.okta.com', - source: { - postMessage: postMessageSpy - } + source: test.iframe.contentWindow }); }); }); @@ -112,8 +125,18 @@ describe('fingerprint', function() { }); }); - it('allows non-Okta postMessages', function () { - return setup({ sendOtherMessage: true }).fingerprint() + it('ignores postMessages from another origin', function () { + return setup({ sendMessageFromAnotherOrigin: true }).fingerprint() + .catch(function(err) { + expect(err).toBeUndefined(); + }) + .then(function(fingerprint) { + expect(fingerprint).toEqual('ABCD'); + }); + }); + + it('ignores postMessages from another source', function () { + return setup({ sendMessageFromAnotherSource: true }).fingerprint() .catch(function(err) { expect(err).toBeUndefined(); })