diff --git a/ts/test/session/unit/crypto/MessageEncrypter_test.ts b/ts/test/session/unit/crypto/MessageEncrypter_test.ts index c3957226c..b92c0efd1 100644 --- a/ts/test/session/unit/crypto/MessageEncrypter_test.ts +++ b/ts/test/session/unit/crypto/MessageEncrypter_test.ts @@ -1,6 +1,6 @@ import chai, { expect } from 'chai'; import * as crypto from 'crypto'; -import * as sinon from 'sinon'; +import Sinon, * as sinon from 'sinon'; import { concatUInt8Array, getSodiumRenderer, MessageEncrypter } from '../../../../session/crypto'; import { EncryptionType } from '../../../../session/types/EncryptionType'; import { TestUtils } from '../../../test-utils'; @@ -17,7 +17,6 @@ chai.use(chaiBytes); // tslint:disable-next-line: max-func-body-length describe('MessageEncrypter', () => { - const sandbox = sinon.createSandbox(); const ourNumber = '0123456789abcdef'; const ourUserEd25516Keypair = { pubKey: '37e1631b002de498caf7c5c1712718bde7f257c6dadeed0c21abf5e939e6c309', @@ -98,13 +97,12 @@ describe('MessageEncrypter', () => { }; beforeEach(() => { - sandbox.stub(UserUtils, 'getOurPubKeyStrFromCache').returns(ourNumber); - sandbox.stub(UserUtils, 'getUserED25519KeyPair').resolves(ourUserEd25516Keypair); + Sinon.stub(UserUtils, 'getOurPubKeyStrFromCache').returns(ourNumber); + Sinon.stub(UserUtils, 'getUserED25519KeyPair').resolves(ourUserEd25516Keypair); }); afterEach(() => { - sandbox.restore(); - TestUtils.restoreStubs(); + Sinon.restore(); }); describe('EncryptionType', () => { @@ -153,16 +151,12 @@ describe('MessageEncrypter', () => { // tslint:disable-next-line: max-func-body-length describe('Session Protocol', () => { - let sandboxSessionProtocol: sinon.SinonSandbox; - beforeEach(() => { - sandboxSessionProtocol = sinon.createSandbox(); - - sandboxSessionProtocol.stub(UserUtils, 'getIdentityKeyPair').resolves(ourIdentityKeypair); + Sinon.stub(UserUtils, 'getIdentityKeyPair').resolves(ourIdentityKeypair); }); afterEach(() => { - sandboxSessionProtocol.restore(); + Sinon.restore(); }); it('should pass the padded message body to encrypt', async () => { @@ -180,7 +174,7 @@ describe('MessageEncrypter', () => { const keypair = await UserUtils.getUserED25519KeyPair(); const recipient = TestUtils.generateFakePubKey(); const sodium = await getSodiumRenderer(); - const cryptoSignDetachedSpy = sandboxSessionProtocol.spy(sodium, 'crypto_sign_detached'); + const cryptoSignDetachedSpy = Sinon.spy(sodium, 'crypto_sign_detached'); const plainText = '123456'; const plainTextBytes = new Uint8Array(StringUtils.encode(plainText, 'utf8')); const userED25519PubKeyBytes = new Uint8Array( diff --git a/ts/test/session/unit/onion/GuardNodes_test.ts b/ts/test/session/unit/onion/GuardNodes_test.ts index aeafd6c96..e4afd8d21 100644 --- a/ts/test/session/unit/onion/GuardNodes_test.ts +++ b/ts/test/session/unit/onion/GuardNodes_test.ts @@ -1,7 +1,7 @@ // tslint:disable: no-implicit-dependencies max-func-body-length no-unused-expression import chai from 'chai'; -import * as sinon from 'sinon'; +import Sinon, * as sinon from 'sinon'; import _ from 'lodash'; import { describe } from 'mocha'; @@ -34,8 +34,6 @@ const fakeSnodePool: Array = [ // tslint:disable-next-line: max-func-body-length describe('GuardNodes', () => { - // Initialize new stubbed cache - const sandbox = sinon.createSandbox(); let getSnodePoolFromDBOrFetchFromSeed: sinon.SinonStub; let fetchFromSeedWithRetriesAndWriteToDb: sinon.SinonStub; describe('selectGuardNodes', () => { @@ -51,22 +49,23 @@ describe('GuardNodes', () => { }); afterEach(() => { - TestUtils.restoreStubs(); - sandbox.restore(); + Sinon.restore(); }); it('does not fetch from seed if we got 12 or more snodes in the db', async () => { - sandbox.stub(Data, 'getSnodePoolFromDb').resolves(fakeSnodePool); - - getSnodePoolFromDBOrFetchFromSeed = sandbox - .stub(SnodePool, 'getSnodePoolFromDBOrFetchFromSeed') - .callThrough(); - fetchFromSeedWithRetriesAndWriteToDb = sandbox - .stub(SnodePool, 'TEST_fetchFromSeedWithRetriesAndWriteToDb') - .resolves(); - const testGuardNode = sandbox.stub(OnionPaths, 'TEST_testGuardNode').resolves(true); - - sandbox.stub(Data, 'updateGuardNodes').resolves(); + Sinon.stub(Data, 'getSnodePoolFromDb').resolves(fakeSnodePool); + + getSnodePoolFromDBOrFetchFromSeed = Sinon.stub( + SnodePool, + 'getSnodePoolFromDBOrFetchFromSeed' + ).callThrough(); + fetchFromSeedWithRetriesAndWriteToDb = Sinon.stub( + SnodePool, + 'TEST_fetchFromSeedWithRetriesAndWriteToDb' + ).resolves(); + const testGuardNode = Sinon.stub(OnionPaths, 'TEST_testGuardNode').resolves(true); + + Sinon.stub(Data, 'updateGuardNodes').resolves(); // run the command const fetchedGuardNodes = await OnionPaths.selectGuardNodes(); @@ -89,17 +88,19 @@ describe('GuardNodes', () => { }); it('throws an error if we got enough snodes in the db but none test passes', async () => { - sandbox.stub(Data, 'getSnodePoolFromDb').resolves(fakeSnodePool); - - getSnodePoolFromDBOrFetchFromSeed = sandbox - .stub(SnodePool, 'getSnodePoolFromDBOrFetchFromSeed') - .callThrough(); - fetchFromSeedWithRetriesAndWriteToDb = sandbox - .stub(SnodePool, 'TEST_fetchFromSeedWithRetriesAndWriteToDb') - .resolves(); - const testGuardNode = sandbox.stub(OnionPaths, 'TEST_testGuardNode').resolves(false); - - sandbox.stub(Data, 'updateGuardNodes').resolves(); + Sinon.stub(Data, 'getSnodePoolFromDb').resolves(fakeSnodePool); + + getSnodePoolFromDBOrFetchFromSeed = Sinon.stub( + SnodePool, + 'getSnodePoolFromDBOrFetchFromSeed' + ).callThrough(); + fetchFromSeedWithRetriesAndWriteToDb = Sinon.stub( + SnodePool, + 'TEST_fetchFromSeedWithRetriesAndWriteToDb' + ).resolves(); + const testGuardNode = Sinon.stub(OnionPaths, 'TEST_testGuardNode').resolves(false); + + Sinon.stub(Data, 'updateGuardNodes').resolves(); // run the command let throwedError: string | undefined; try { @@ -125,17 +126,19 @@ describe('GuardNodes', () => { it('throws an error if we have to fetch from seed, fetch from seed enough snode but we still fail', async () => { const invalidSndodePool = fakeSnodePool.slice(0, 11); - sandbox.stub(Data, 'getSnodePoolFromDb').resolves(invalidSndodePool); + Sinon.stub(Data, 'getSnodePoolFromDb').resolves(invalidSndodePool); TestUtils.stubWindow('getSeedNodeList', () => [{ url: 'whatever' }]); - getSnodePoolFromDBOrFetchFromSeed = sandbox - .stub(SnodePool, 'getSnodePoolFromDBOrFetchFromSeed') - .callThrough(); - fetchFromSeedWithRetriesAndWriteToDb = sandbox - .stub(SeedNodeAPI, 'fetchSnodePoolFromSeedNodeWithRetries') - .resolves(fakeSnodePool); + getSnodePoolFromDBOrFetchFromSeed = Sinon.stub( + SnodePool, + 'getSnodePoolFromDBOrFetchFromSeed' + ).callThrough(); + fetchFromSeedWithRetriesAndWriteToDb = Sinon.stub( + SeedNodeAPI, + 'fetchSnodePoolFromSeedNodeWithRetries' + ).resolves(fakeSnodePool); - sandbox.stub(Data, 'updateGuardNodes').resolves(); + Sinon.stub(Data, 'updateGuardNodes').resolves(); // run the command let throwedError: string | undefined; try { @@ -149,18 +152,20 @@ describe('GuardNodes', () => { it('returns valid guardnode if we have to fetch from seed, fetch from seed enough snodes but guard node tests passes', async () => { const invalidSndodePool = fakeSnodePool.slice(0, 11); - sandbox.stub(Data, 'getSnodePoolFromDb').resolves(invalidSndodePool); + Sinon.stub(Data, 'getSnodePoolFromDb').resolves(invalidSndodePool); TestUtils.stubWindow('getSeedNodeList', () => [{ url: 'whatever' }]); - const testGuardNode = sandbox.stub(OnionPaths, 'TEST_testGuardNode').resolves(true); - - getSnodePoolFromDBOrFetchFromSeed = sandbox - .stub(SnodePool, 'getSnodePoolFromDBOrFetchFromSeed') - .callThrough(); - fetchFromSeedWithRetriesAndWriteToDb = sandbox - .stub(SeedNodeAPI, 'fetchSnodePoolFromSeedNodeWithRetries') - .resolves(fakeSnodePool); - - sandbox.stub(Data, 'updateGuardNodes').resolves(); + const testGuardNode = Sinon.stub(OnionPaths, 'TEST_testGuardNode').resolves(true); + + getSnodePoolFromDBOrFetchFromSeed = Sinon.stub( + SnodePool, + 'getSnodePoolFromDBOrFetchFromSeed' + ).callThrough(); + fetchFromSeedWithRetriesAndWriteToDb = Sinon.stub( + SeedNodeAPI, + 'fetchSnodePoolFromSeedNodeWithRetries' + ).resolves(fakeSnodePool); + + Sinon.stub(Data, 'updateGuardNodes').resolves(); // run the command const guardNodes = await OnionPaths.selectGuardNodes(); @@ -170,17 +175,19 @@ describe('GuardNodes', () => { it('throws if we have to fetch from seed, fetch from seed but not have enough fetched snodes', async () => { const invalidSndodePool = fakeSnodePool.slice(0, 11); - sandbox.stub(Data, 'getSnodePoolFromDb').resolves(invalidSndodePool); + Sinon.stub(Data, 'getSnodePoolFromDb').resolves(invalidSndodePool); TestUtils.stubWindow('getSeedNodeList', () => [{ url: 'whatever' }]); - getSnodePoolFromDBOrFetchFromSeed = sandbox - .stub(SnodePool, 'getSnodePoolFromDBOrFetchFromSeed') - .callThrough(); - fetchFromSeedWithRetriesAndWriteToDb = sandbox - .stub(SeedNodeAPI, 'fetchSnodePoolFromSeedNodeWithRetries') - .resolves(invalidSndodePool); + getSnodePoolFromDBOrFetchFromSeed = Sinon.stub( + SnodePool, + 'getSnodePoolFromDBOrFetchFromSeed' + ).callThrough(); + fetchFromSeedWithRetriesAndWriteToDb = Sinon.stub( + SeedNodeAPI, + 'fetchSnodePoolFromSeedNodeWithRetries' + ).resolves(invalidSndodePool); - sandbox.stub(Data, 'updateGuardNodes').resolves(); + Sinon.stub(Data, 'updateGuardNodes').resolves(); // run the command let throwedError: string | undefined; try { diff --git a/ts/test/session/unit/onion/OnionErrors_test.ts b/ts/test/session/unit/onion/OnionErrors_test.ts index f8ba87dd7..ae81dde32 100644 --- a/ts/test/session/unit/onion/OnionErrors_test.ts +++ b/ts/test/session/unit/onion/OnionErrors_test.ts @@ -1,7 +1,7 @@ // tslint:disable: no-implicit-dependencies max-func-body-length no-unused-expression import chai from 'chai'; -import * as sinon from 'sinon'; +import Sinon, * as sinon from 'sinon'; import { describe } from 'mocha'; import { TestUtils } from '../../../test-utils'; @@ -45,7 +45,6 @@ const getFakeResponseOnDestination = (statusCode?: number, body?: string) => { // tslint:disable-next-line: max-func-body-length describe('OnionPathsErrors', () => { // Initialize new stubbed cache - const sandbox = sinon.createSandbox(); let updateSwarmSpy: sinon.SinonStub; let dropSnodeFromSwarmIfNeededSpy: sinon.SinonSpy; let dropSnodeFromSnodePool: sinon.SinonSpy; @@ -84,30 +83,29 @@ describe('OnionPathsErrors', () => { associatedWith = TestUtils.generateFakePubKey().key; fakeSwarmForAssociatedWith = otherNodesPubkeys.slice(0, 6); // Stubs - sandbox.stub(OnionPaths, 'selectGuardNodes').resolves(guardNodesArray); - sandbox.stub(SNodeAPI.SNodeAPI, 'TEST_getSnodePoolFromSnode').resolves(guardNodesArray); + Sinon.stub(OnionPaths, 'selectGuardNodes').resolves(guardNodesArray); + Sinon.stub(SNodeAPI.SNodeAPI, 'TEST_getSnodePoolFromSnode').resolves(guardNodesArray); TestUtils.stubData('getGuardNodes').resolves([ guardPubkeys[0], guardPubkeys[1], guardPubkeys[2], ]); TestUtils.stubWindow('getSeedNodeList', () => ['seednode1']); - sandbox.stub(SeedNodeAPI, 'fetchSnodePoolFromSeedNodeWithRetries').resolves(fakeSnodePool); - sandbox.stub(Data, 'getSwarmNodesForPubkey').resolves(fakeSwarmForAssociatedWith); - updateGuardNodesStub = sandbox.stub(Data, 'updateGuardNodes').resolves(); + Sinon.stub(SeedNodeAPI, 'fetchSnodePoolFromSeedNodeWithRetries').resolves(fakeSnodePool); + Sinon.stub(Data, 'getSwarmNodesForPubkey').resolves(fakeSwarmForAssociatedWith); + updateGuardNodesStub = Sinon.stub(Data, 'updateGuardNodes').resolves(); // those are still doing what they do, but we spy on their executation - updateSwarmSpy = sandbox.stub(Data, 'updateSwarmNodesForPubkey').resolves(); - sandbox - .stub(DataItem, 'getItemById') + updateSwarmSpy = Sinon.stub(Data, 'updateSwarmNodesForPubkey').resolves(); + Sinon.stub(DataItem, 'getItemById') .withArgs(Data.SNODE_POOL_ITEM_ID) .resolves({ id: Data.SNODE_POOL_ITEM_ID, value: '' }); - sandbox.stub(DataItem, 'createOrUpdateItem').resolves(); - dropSnodeFromSnodePool = sandbox.spy(SNodeAPI.SnodePool, 'dropSnodeFromSnodePool'); - dropSnodeFromSwarmIfNeededSpy = sandbox.spy(SNodeAPI.SnodePool, 'dropSnodeFromSwarmIfNeeded'); - dropSnodeFromPathSpy = sandbox.spy(OnionPaths, 'dropSnodeFromPath'); - incrementBadPathCountOrDropSpy = sandbox.spy(OnionPaths, 'incrementBadPathCountOrDrop'); - incrementBadSnodeCountOrDropSpy = sandbox.spy(SNodeAPI.Onions, 'incrementBadSnodeCountOrDrop'); + Sinon.stub(DataItem, 'createOrUpdateItem').resolves(); + dropSnodeFromSnodePool = Sinon.spy(SNodeAPI.SnodePool, 'dropSnodeFromSnodePool'); + dropSnodeFromSwarmIfNeededSpy = Sinon.spy(SNodeAPI.SnodePool, 'dropSnodeFromSwarmIfNeeded'); + dropSnodeFromPathSpy = Sinon.spy(OnionPaths, 'dropSnodeFromPath'); + incrementBadPathCountOrDropSpy = Sinon.spy(OnionPaths, 'incrementBadPathCountOrDrop'); + incrementBadSnodeCountOrDropSpy = Sinon.spy(SNodeAPI.Onions, 'incrementBadSnodeCountOrDrop'); OnionPaths.clearTestOnionPath(); @@ -116,16 +114,16 @@ describe('OnionPathsErrors', () => { await OnionPaths.getOnionPath({}); oldOnionPaths = OnionPaths.TEST_getTestOnionPath(); - sandbox - .stub(SNodeAPI.Onions, 'decodeOnionResult') - .callsFake((_symkey: ArrayBuffer, plaintext: string) => - Promise.resolve({ plaintext, ciphertextBuffer: new Uint8Array() }) - ); + Sinon.stub( + SNodeAPI.Onions, + 'decodeOnionResult' + ).callsFake((_symkey: ArrayBuffer, plaintext: string) => + Promise.resolve({ plaintext, ciphertextBuffer: new Uint8Array() }) + ); }); afterEach(() => { - TestUtils.restoreStubs(); - sandbox.restore(); + Sinon.restore(); }); describe('processOnionResponse', () => { diff --git a/ts/test/session/unit/onion/OnionPaths_test.ts b/ts/test/session/unit/onion/OnionPaths_test.ts index 5bf1ae6f6..3595cd3a6 100644 --- a/ts/test/session/unit/onion/OnionPaths_test.ts +++ b/ts/test/session/unit/onion/OnionPaths_test.ts @@ -1,7 +1,7 @@ // tslint:disable: no-implicit-dependencies max-func-body-length no-unused-expression import chai from 'chai'; -import * as sinon from 'sinon'; +import Sinon from 'sinon'; import _ from 'lodash'; import { describe } from 'mocha'; @@ -42,7 +42,6 @@ const fakeGuardNodesFromDB: Array = fakeGuardNodesEd25519.map(ed // tslint:disable-next-line: max-func-body-length describe('OnionPaths', () => { // Initialize new stubbed cache - const sandbox = sinon.createSandbox(); let oldOnionPaths: Array>; describe('dropSnodeFromPath', () => { @@ -50,9 +49,9 @@ describe('OnionPaths', () => { // Utils Stubs OnionPaths.clearTestOnionPath(); - sandbox.stub(OnionPaths, 'selectGuardNodes').resolves(fakeGuardNodes); - sandbox.stub(SNodeAPI.SNodeAPI, 'TEST_getSnodePoolFromSnode').resolves(fakeGuardNodes); - sandbox.stub(Data, 'getSnodePoolFromDb').resolves(fakeSnodePool); + Sinon.stub(OnionPaths, 'selectGuardNodes').resolves(fakeGuardNodes); + Sinon.stub(SNodeAPI.SNodeAPI, 'TEST_getSnodePoolFromSnode').resolves(fakeGuardNodes); + Sinon.stub(Data, 'getSnodePoolFromDb').resolves(fakeSnodePool); TestUtils.stubData('getGuardNodes').resolves(fakeGuardNodesFromDB); TestUtils.stubData('createOrUpdateItem').resolves(); @@ -61,7 +60,7 @@ describe('OnionPaths', () => { TestUtils.stubWindowLog(); - sandbox.stub(SeedNodeAPI, 'fetchSnodePoolFromSeedNodeWithRetries').resolves(fakeSnodePool); + Sinon.stub(SeedNodeAPI, 'fetchSnodePoolFromSeedNodeWithRetries').resolves(fakeSnodePool); SNodeAPI.Onions.resetSnodeFailureCount(); OnionPaths.resetPathFailureCount(); // get a copy of what old ones look like @@ -75,8 +74,7 @@ describe('OnionPaths', () => { }); afterEach(() => { - TestUtils.restoreStubs(); - sandbox.restore(); + Sinon.restore(); }); describe('with valid snode pool', () => { it('rebuilds after removing last snode on path', async () => { diff --git a/ts/test/session/unit/onion/SeedNodeAPI_test.ts b/ts/test/session/unit/onion/SeedNodeAPI_test.ts index 9e7968556..528cb30c7 100644 --- a/ts/test/session/unit/onion/SeedNodeAPI_test.ts +++ b/ts/test/session/unit/onion/SeedNodeAPI_test.ts @@ -1,7 +1,7 @@ // tslint:disable: no-implicit-dependencies max-func-body-length no-unused-expression import chai from 'chai'; -import * as sinon from 'sinon'; +import Sinon from 'sinon'; import _ from 'lodash'; import { describe } from 'mocha'; @@ -44,7 +44,6 @@ const fakeSnodePoolFromSeedNode: Array = fakeSnodePool.map(m => { // tslint:disable-next-line: max-func-body-length describe('SeedNodeAPI', () => { // Initialize new stubbed cache - const sandbox = sinon.createSandbox(); describe('getSnodeListFromSeednode', () => { beforeEach(() => { @@ -59,19 +58,20 @@ describe('SeedNodeAPI', () => { }); afterEach(() => { - TestUtils.restoreStubs(); - sandbox.restore(); + Sinon.restore(); }); it('if the cached snode pool has less than 12 snodes, trigger a fetch from the seed nodes with retries', async () => { - const TEST_fetchSnodePoolFromSeedNodeRetryable = sandbox - .stub(SeedNodeAPI, 'TEST_fetchSnodePoolFromSeedNodeRetryable') + const TEST_fetchSnodePoolFromSeedNodeRetryable = Sinon.stub( + SeedNodeAPI, + 'TEST_fetchSnodePoolFromSeedNodeRetryable' + ) .onFirstCall() .throws() .onSecondCall() .resolves(fakeSnodePoolFromSeedNode); - sandbox.stub(SeedNodeAPI, 'getMinTimeout').returns(20); + Sinon.stub(SeedNodeAPI, 'getMinTimeout').returns(20); // run the command const fetched = await SeedNodeAPI.fetchSnodePoolFromSeedNodeWithRetries([ diff --git a/ts/test/session/unit/onion/SnodePoolUpdate_test.ts b/ts/test/session/unit/onion/SnodePoolUpdate_test.ts index e13c3e819..4adb85685 100644 --- a/ts/test/session/unit/onion/SnodePoolUpdate_test.ts +++ b/ts/test/session/unit/onion/SnodePoolUpdate_test.ts @@ -1,7 +1,7 @@ // tslint:disable: no-implicit-dependencies max-func-body-length no-unused-expression import chai from 'chai'; -import * as sinon from 'sinon'; +import Sinon, * as sinon from 'sinon'; import _ from 'lodash'; import { describe } from 'mocha'; @@ -33,9 +33,6 @@ const fakeSnodePool: Array = [ // tslint:disable-next-line: max-func-body-length describe('OnionPaths', () => { - // Initialize new stubbed cache - const sandbox = sinon.createSandbox(); - describe('getSnodePoolFromDBOrFetchFromSeed', () => { let getSnodePoolFromDb: sinon.SinonStub; let fetchFromSeedWithRetriesAndWriteToDb: sinon.SinonStub; @@ -54,12 +51,11 @@ describe('OnionPaths', () => { }); afterEach(() => { - TestUtils.restoreStubs(); - sandbox.restore(); + Sinon.restore(); }); it('if the cached snode pool has at least 12 snodes, just return it without fetching from seed', async () => { - getSnodePoolFromDb = sandbox.stub(Data, 'getSnodePoolFromDb').resolves(fakeSnodePool); - fetchFromSeedWithRetriesAndWriteToDb = sandbox.stub( + getSnodePoolFromDb = Sinon.stub(Data, 'getSnodePoolFromDb').resolves(fakeSnodePool); + fetchFromSeedWithRetriesAndWriteToDb = Sinon.stub( SnodePool, 'TEST_fetchFromSeedWithRetriesAndWriteToDb' ); @@ -74,15 +70,17 @@ describe('OnionPaths', () => { it('if the cached snode pool 12 or less snodes, trigger a fetch from the seed nodes', async () => { const length12 = fakeSnodePool.slice(0, 12); expect(length12.length).to.eq(12); - getSnodePoolFromDb = sandbox.stub(Data, 'getSnodePoolFromDb').resolves(length12); - - sandbox.stub(Data, 'updateSnodePoolOnDb').resolves(); - fetchFromSeedWithRetriesAndWriteToDb = sandbox - .stub(SnodePool, 'TEST_fetchFromSeedWithRetriesAndWriteToDb') - .callThrough(); - fetchSnodePoolFromSeedNodeWithRetries = sandbox - .stub(SeedNodeAPI, 'fetchSnodePoolFromSeedNodeWithRetries') - .resolves(fakeSnodePool); + getSnodePoolFromDb = Sinon.stub(Data, 'getSnodePoolFromDb').resolves(length12); + + Sinon.stub(Data, 'updateSnodePoolOnDb').resolves(); + fetchFromSeedWithRetriesAndWriteToDb = Sinon.stub( + SnodePool, + 'TEST_fetchFromSeedWithRetriesAndWriteToDb' + ).callThrough(); + fetchSnodePoolFromSeedNodeWithRetries = Sinon.stub( + SeedNodeAPI, + 'fetchSnodePoolFromSeedNodeWithRetries' + ).resolves(fakeSnodePool); // run the command const fetched = await SnodePool.getSnodePoolFromDBOrFetchFromSeed(); diff --git a/ts/test/session/unit/receiver/opengroup/deduplicate_test.ts b/ts/test/session/unit/receiver/opengroup/deduplicate_test.ts index c5023a316..25c67d821 100644 --- a/ts/test/session/unit/receiver/opengroup/deduplicate_test.ts +++ b/ts/test/session/unit/receiver/opengroup/deduplicate_test.ts @@ -2,6 +2,7 @@ import chai from 'chai'; import { describe } from 'mocha'; +import Sinon from 'sinon'; import { filterDuplicatesFromDbAndIncoming } from '../../../../../session/apis/open_group_api/opengroupV2/SogsFilterDuplicate'; import { TestUtils } from '../../../../test-utils'; @@ -16,7 +17,7 @@ describe('filterDuplicatesFromDbAndIncoming', () => { }); afterEach(() => { - TestUtils.restoreStubs(); + Sinon.restore(); }); it('no duplicates', async () => { diff --git a/ts/test/session/unit/receiving/ClosedGroupUpdates_test.ts b/ts/test/session/unit/receiving/ClosedGroupUpdates_test.ts index b991cbb72..efc96bcd5 100644 --- a/ts/test/session/unit/receiving/ClosedGroupUpdates_test.ts +++ b/ts/test/session/unit/receiving/ClosedGroupUpdates_test.ts @@ -9,8 +9,6 @@ chai.use(chaiAsPromised as any); // tslint:disable-next-line: max-func-body-length describe('ClosedGroupUpdates', () => { //FIXME AUDRIC TODO - // Initialize new stubbed cache - // const sandbox = sinon.createSandbox(); // const ourDevice = TestUtils.generateFakePubKey(); // const ourNumber = ourDevice.key; // const groupId = TestUtils.generateFakePubKey().key; @@ -22,7 +20,6 @@ describe('ClosedGroupUpdates', () => { // sandbox.stub(UserUtils, 'getCurrentDevicePubKey').resolves(ourNumber); // }); // afterEach(() => { - // TestUtils.restoreStubs(); // sandbox.restore(); // }); // describe('handleClosedGroupControlMessage', () => { diff --git a/ts/test/session/unit/receiving/ConfigurationMessage_test.ts b/ts/test/session/unit/receiving/ConfigurationMessage_test.ts index fa7b16091..1c36d7b7a 100644 --- a/ts/test/session/unit/receiving/ConfigurationMessage_test.ts +++ b/ts/test/session/unit/receiving/ConfigurationMessage_test.ts @@ -7,7 +7,7 @@ import { ConfigurationMessage } from '../../../../session/messages/outgoing/cont import { UserUtils } from '../../../../session/utils'; import { TestUtils } from '../../../test-utils'; -import Sinon, * as sinon from 'sinon'; +import Sinon from 'sinon'; import * as cache from '../../../../receiver/cache'; import * as data from '../../../../../ts/data/data'; import { EnvelopePlus } from '../../../../receiver/types'; @@ -20,7 +20,6 @@ chai.should(); const { expect } = chai; describe('ConfigurationMessage_receiving', () => { - const sandbox = sinon.createSandbox(); let createOrUpdateStub: Sinon.SinonStub; let getItemByIdStub: Sinon.SinonStub; let sender: string; @@ -29,7 +28,7 @@ describe('ConfigurationMessage_receiving', () => { let config: ConfigurationMessage; beforeEach(() => { - sandbox.stub(cache, 'removeFromCache').resolves(); + Sinon.stub(cache, 'removeFromCache').resolves(); sender = TestUtils.generateFakePubKey().key; config = new ConfigurationMessage({ activeOpenGroups: [], @@ -42,17 +41,16 @@ describe('ConfigurationMessage_receiving', () => { }); afterEach(() => { - TestUtils.restoreStubs(); - sandbox.restore(); + Sinon.restore(); }); it('should not be processed if we do not have a pubkey', async () => { - sandbox.stub(UserUtils, 'getOurPubKeyStrFromCache').resolves(undefined); + Sinon.stub(UserUtils, 'getOurPubKeyStrFromCache').resolves(undefined); envelope = TestUtils.generateEnvelopePlus(sender); const proto = config.contentProto(); - createOrUpdateStub = sandbox.stub(data, 'createOrUpdateItem').resolves(); - getItemByIdStub = sandbox.stub(data, 'getItemById').resolves(); + createOrUpdateStub = Sinon.stub(data, 'createOrUpdateItem').resolves(); + getItemByIdStub = Sinon.stub(data, 'getItemById').resolves(); await handleConfigurationMessage( envelope, proto.configurationMessage as SignalService.ConfigurationMessage @@ -65,7 +63,7 @@ describe('ConfigurationMessage_receiving', () => { const ourNumber = TestUtils.generateFakePubKey().key; beforeEach(() => { - sandbox.stub(UserUtils, 'getOurPubKeyStrFromCache').resolves(ourNumber); + Sinon.stub(UserUtils, 'getOurPubKeyStrFromCache').resolves(ourNumber); }); it('should not be processed if the message is not coming from our number', async () => { @@ -73,8 +71,8 @@ describe('ConfigurationMessage_receiving', () => { // sender !== ourNumber envelope = TestUtils.generateEnvelopePlus(sender); - createOrUpdateStub = sandbox.stub(data, 'createOrUpdateItem').resolves(); - getItemByIdStub = sandbox.stub(data, 'getItemById').resolves(); + createOrUpdateStub = Sinon.stub(data, 'createOrUpdateItem').resolves(); + getItemByIdStub = Sinon.stub(data, 'getItemById').resolves(); await handleConfigurationMessage( envelope, proto.configurationMessage as SignalService.ConfigurationMessage diff --git a/ts/test/session/unit/sending/MessageQueue_test.ts b/ts/test/session/unit/sending/MessageQueue_test.ts index 2699a71b9..c0da8db74 100644 --- a/ts/test/session/unit/sending/MessageQueue_test.ts +++ b/ts/test/session/unit/sending/MessageQueue_test.ts @@ -1,7 +1,7 @@ // tslint:disable: no-implicit-dependencies max-func-body-length no-unused-expression import chai from 'chai'; -import * as sinon from 'sinon'; +import Sinon, * as sinon from 'sinon'; import { describe } from 'mocha'; import { randomBytes } from 'crypto'; import * as Data from '../../../../../ts/data/data'; @@ -26,7 +26,6 @@ const { expect } = chai; // tslint:disable-next-line: max-func-body-length describe('MessageQueue', () => { // Initialize new stubbed cache - const sandbox = sinon.createSandbox(); const ourDevice = TestUtils.generateFakePubKey(); const ourNumber = ourDevice.key; @@ -47,19 +46,22 @@ describe('MessageQueue', () => { beforeEach(() => { // Utils Stubs - sandbox.stub(UserUtils, 'getOurPubKeyStrFromCache').returns(ourNumber); + Sinon.stub(UserUtils, 'getOurPubKeyStrFromCache').returns(ourNumber); // Message Sender Stubs - sendStub = sandbox.stub(MessageSender, 'send'); - messageSentHandlerFailedStub = sandbox - .stub(MessageSentHandler as any, 'handleMessageSentFailure') - .resolves(); - messageSentHandlerSuccessStub = sandbox - .stub(MessageSentHandler as any, 'handleMessageSentSuccess') - .resolves(); - messageSentPublicHandlerSuccessStub = sandbox - .stub(MessageSentHandler as any, 'handlePublicMessageSentSuccess') - .resolves(); + sendStub = Sinon.stub(MessageSender, 'send'); + messageSentHandlerFailedStub = Sinon.stub( + MessageSentHandler as any, + 'handleMessageSentFailure' + ).resolves(); + messageSentHandlerSuccessStub = Sinon.stub( + MessageSentHandler as any, + 'handleMessageSentSuccess' + ).resolves(); + messageSentPublicHandlerSuccessStub = Sinon.stub( + MessageSentHandler as any, + 'handlePublicMessageSentSuccess' + ).resolves(); // Init Queue pendingMessageCache = new PendingMessageCacheStub(); @@ -68,8 +70,7 @@ describe('MessageQueue', () => { }); afterEach(() => { - TestUtils.restoreStubs(); - sandbox.restore(); + Sinon.restore(); }); describe('processPending', () => { @@ -117,12 +118,12 @@ describe('MessageQueue', () => { describe('events', () => { it('should send a success event if message was sent', done => { - sandbox.stub(Data, 'getMessageById').resolves(); + Sinon.stub(Data, 'getMessageById').resolves(); const message = TestUtils.generateVisibleMessage(); sendStub.resolves({ effectiveTimestamp: Date.now(), wrappedEnvelope: randomBytes(10) }); const device = TestUtils.generateFakePubKey(); - sandbox.stub(MessageSender, 'getMinRetryTimeout').returns(10); + Sinon.stub(MessageSender, 'getMinRetryTimeout').returns(10); const waitForMessageSentEvent = async () => new Promise(resolve => { resolve(); @@ -173,7 +174,7 @@ describe('MessageQueue', () => { describe('sendToPubKey', () => { it('should send the message to the device', async () => { const device = TestUtils.generateFakePubKey(); - const stub = sandbox.stub(messageQueueStub as any, 'process').resolves(); + const stub = Sinon.stub(messageQueueStub as any, 'process').resolves(); const message = TestUtils.generateVisibleMessage(); await messageQueueStub.sendToPubKey(device, message); @@ -195,9 +196,9 @@ describe('MessageQueue', () => { describe('closed groups', () => { it('can send to closed group', async () => { const members = TestUtils.generateFakePubKeys(4).map(p => new PubKey(p.key)); - sandbox.stub(GroupUtils, 'getGroupMembers').returns(members); + Sinon.stub(GroupUtils, 'getGroupMembers').returns(members); - const send = sandbox.stub(messageQueueStub, 'sendToPubKey').resolves(); + const send = Sinon.stub(messageQueueStub, 'sendToPubKey').resolves(); const message = TestUtils.generateClosedGroupMessage(); await messageQueueStub.sendToGroup(message); @@ -213,9 +214,9 @@ describe('MessageQueue', () => { describe('open groupsv2', () => { let sendToOpenGroupV2Stub: sinon.SinonStub; beforeEach(() => { - sendToOpenGroupV2Stub = sandbox - .stub(MessageSender, 'sendToOpenGroupV2') - .resolves(TestUtils.generateOpenGroupMessageV2()); + sendToOpenGroupV2Stub = Sinon.stub(MessageSender, 'sendToOpenGroupV2').resolves( + TestUtils.generateOpenGroupMessageV2() + ); }); it('can send to open group', async () => { diff --git a/ts/test/session/unit/sending/MessageSender_test.ts b/ts/test/session/unit/sending/MessageSender_test.ts index f347014b3..614c2954a 100644 --- a/ts/test/session/unit/sending/MessageSender_test.ts +++ b/ts/test/session/unit/sending/MessageSender_test.ts @@ -1,6 +1,6 @@ import { expect } from 'chai'; import * as crypto from 'crypto'; -import * as sinon from 'sinon'; +import Sinon, * as sinon from 'sinon'; import { MessageSender } from '../../../../session/sending'; import { TestUtils } from '../../../test-utils'; import { MessageEncrypter } from '../../../../session/crypto'; @@ -14,11 +14,8 @@ import { SNodeAPI } from '../../../../session/apis/snode_api'; import _ from 'lodash'; describe('MessageSender', () => { - const sandbox = sinon.createSandbox(); - afterEach(() => { - sandbox.restore(); - TestUtils.restoreStubs(); + sinon.restore(); }); beforeEach(() => { @@ -32,16 +29,16 @@ describe('MessageSender', () => { let encryptStub: sinon.SinonStub<[PubKey, Uint8Array, EncryptionType]>; beforeEach(() => { - sessionMessageAPISendStub = sandbox.stub(MessageSender, 'TEST_sendMessageToSnode').resolves(); + sessionMessageAPISendStub = Sinon.stub(MessageSender, 'TEST_sendMessageToSnode').resolves(); - sandbox.stub(Data, 'getMessageById').resolves(); + Sinon.stub(Data, 'getMessageById').resolves(); - encryptStub = sandbox.stub(MessageEncrypter, 'encrypt').resolves({ + encryptStub = Sinon.stub(MessageEncrypter, 'encrypt').resolves({ envelopeType: SignalService.Envelope.Type.SESSION_MESSAGE, cipherText: crypto.randomBytes(10), }); - sandbox.stub(UserUtils, 'getOurPubKeyStrFromCache').returns(ourNumber); + Sinon.stub(UserUtils, 'getOurPubKeyStrFromCache').returns(ourNumber); }); describe('retry', () => { @@ -117,7 +114,7 @@ describe('MessageSender', () => { const visibleMessage = TestUtils.generateVisibleMessage(); const rawMessage = await MessageUtils.toRawMessage(device, visibleMessage); const offset = 200000; - sandbox.stub(SNodeAPI, 'getLatestTimestampOffset').returns(offset); + Sinon.stub(SNodeAPI, 'getLatestTimestampOffset').returns(offset); await MessageSender.send(rawMessage, 3, 10); const data = sessionMessageAPISendStub.getCall(0).args[1]; @@ -188,20 +185,19 @@ describe('MessageSender', () => { }); describe('sendToOpenGroupV2', () => { - const sandbox2 = sinon.createSandbox(); let postMessageRetryableStub: sinon.SinonStub; beforeEach(() => { - sandbox - .stub(UserUtils, 'getOurPubKeyStrFromCache') - .resolves(TestUtils.generateFakePubKey().key); + Sinon.stub(UserUtils, 'getOurPubKeyStrFromCache').resolves( + TestUtils.generateFakePubKey().key + ); - postMessageRetryableStub = sandbox - .stub(ApiV2, 'postMessageRetryable') - .resolves(TestUtils.generateOpenGroupMessageV2()); + postMessageRetryableStub = Sinon.stub(ApiV2, 'postMessageRetryable').resolves( + TestUtils.generateOpenGroupMessageV2() + ); }); afterEach(() => { - sandbox2.restore(); + Sinon.restore(); }); it('should call postMessageRetryableStub', async () => { @@ -217,7 +213,7 @@ describe('MessageSender', () => { const roomInfos = TestUtils.generateOpenGroupV2RoomInfos(); postMessageRetryableStub.throws('whate'); - sandbox2.stub(ApiV2, 'getMinTimeout').returns(2); + Sinon.stub(ApiV2, 'getMinTimeout').returns(2); postMessageRetryableStub.onThirdCall().resolves(); await MessageSender.sendToOpenGroupV2(message, roomInfos); @@ -227,7 +223,7 @@ describe('MessageSender', () => { it('should not retry more than 3 postMessageRetryableStub ', async () => { const message = TestUtils.generateOpenGroupVisibleMessage(); const roomInfos = TestUtils.generateOpenGroupV2RoomInfos(); - sandbox2.stub(ApiV2, 'getMinTimeout').returns(2); + Sinon.stub(ApiV2, 'getMinTimeout').returns(2); postMessageRetryableStub.throws('fake error'); postMessageRetryableStub.onCall(4).resolves(); try { diff --git a/ts/test/session/unit/sending/PendingMessageCache_test.ts b/ts/test/session/unit/sending/PendingMessageCache_test.ts index c975597ab..941c34fc7 100644 --- a/ts/test/session/unit/sending/PendingMessageCache_test.ts +++ b/ts/test/session/unit/sending/PendingMessageCache_test.ts @@ -1,6 +1,6 @@ // tslint:disable: no-implicit-dependencies max-func-body-length no-unused-expression import { expect } from 'chai'; -import * as sinon from 'sinon'; +import Sinon from 'sinon'; import * as _ from 'lodash'; import { MessageUtils } from '../../../../session/utils'; import { TestUtils } from '../../../../test/test-utils'; @@ -13,7 +13,6 @@ interface StorageItem { } describe('PendingMessageCache', () => { - const sandbox = sinon.createSandbox(); // Initialize new stubbed cache let data: StorageItem; let pendingMessageCacheStub: PendingMessageCache; @@ -42,8 +41,7 @@ describe('PendingMessageCache', () => { }); afterEach(() => { - sandbox.restore(); - TestUtils.restoreStubs(); + Sinon.restore(); }); it('can initialize cache', async () => { diff --git a/ts/test/session/unit/swarm_polling/SwarmPolling_test.ts b/ts/test/session/unit/swarm_polling/SwarmPolling_test.ts index 84b8e5bcf..a15c2a5a8 100644 --- a/ts/test/session/unit/swarm_polling/SwarmPolling_test.ts +++ b/ts/test/session/unit/swarm_polling/SwarmPolling_test.ts @@ -30,7 +30,6 @@ const { expect } = chai; // tslint:disable-next-line: max-func-body-length describe('SwarmPolling', () => { // Initialize new stubbed cache - const sandbox = sinon.createSandbox(); const ourPubkey = TestUtils.generateFakePubKey(); const ourNumber = ourPubkey.key; @@ -42,16 +41,16 @@ describe('SwarmPolling', () => { let clock: Sinon.SinonFakeTimers; beforeEach(async () => { // Utils Stubs - sandbox.stub(UserUtils, 'getOurPubKeyStrFromCache').returns(ourNumber); + Sinon.stub(UserUtils, 'getOurPubKeyStrFromCache').returns(ourNumber); - sandbox.stub(Data, 'getAllConversations').resolves(new ConversationCollection()); - sandbox.stub(DataItem, 'getItemById').resolves(); - sandbox.stub(Data, 'saveConversation').resolves(); - sandbox.stub(Data, 'getSwarmNodesForPubkey').resolves(); - sandbox.stub(Data, 'getLastHashBySnode').resolves(); + Sinon.stub(Data, 'getAllConversations').resolves(new ConversationCollection()); + Sinon.stub(DataItem, 'getItemById').resolves(); + Sinon.stub(Data, 'saveConversation').resolves(); + Sinon.stub(Data, 'getSwarmNodesForPubkey').resolves(); + Sinon.stub(Data, 'getLastHashBySnode').resolves(); - sandbox.stub(SnodePool, 'getSwarmFor').resolves(generateFakeSnodes(5)); - sandbox.stub(SNodeAPI, 'retrieveNextMessages').resolves([]); + Sinon.stub(SnodePool, 'getSwarmFor').resolves(generateFakeSnodes(5)); + Sinon.stub(SNodeAPI, 'retrieveNextMessages').resolves([]); TestUtils.stubWindow('inboxStore', undefined); TestUtils.stubWindow('getGlobalOnlineStatus', () => true); TestUtils.stubWindowLog(); @@ -62,14 +61,13 @@ describe('SwarmPolling', () => { swarmPolling = getSwarmPollingInstance(); swarmPolling.TEST_reset(); - TEST_pollOnceForKeySpy = sandbox.spy(swarmPolling, 'TEST_pollOnceForKey'); + TEST_pollOnceForKeySpy = Sinon.spy(swarmPolling, 'TEST_pollOnceForKey'); clock = sinon.useFakeTimers(Date.now()); }); afterEach(() => { - TestUtils.restoreStubs(); - sandbox.restore(); + Sinon.restore(); getConversationController().reset(); clock.restore(); }); diff --git a/ts/test/session/unit/utils/Messages_test.ts b/ts/test/session/unit/utils/Messages_test.ts index 274101863..e320f2bfc 100644 --- a/ts/test/session/unit/utils/Messages_test.ts +++ b/ts/test/session/unit/utils/Messages_test.ts @@ -1,7 +1,6 @@ // tslint:disable: no-implicit-dependencies import chai from 'chai'; -import * as sinon from 'sinon'; import { TestUtils } from '../../../test-utils'; import { MessageUtils, UserUtils } from '../../../../session/utils'; import { EncryptionType, PubKey } from '../../../../session/types'; @@ -17,14 +16,13 @@ import { ClosedGroupEncryptionPairMessage } from '../../../../session/messages/o import { ClosedGroupNameChangeMessage } from '../../../../session/messages/outgoing/controlMessage/group/ClosedGroupNameChangeMessage'; import { ClosedGroupNewMessage } from '../../../../session/messages/outgoing/controlMessage/group/ClosedGroupNewMessage'; import { ClosedGroupRemovedMembersMessage } from '../../../../session/messages/outgoing/controlMessage/group/ClosedGroupRemovedMembersMessage'; +import Sinon from 'sinon'; const { expect } = chai; describe('Message Utils', () => { - const sandbox = sinon.createSandbox(); - afterEach(() => { - sandbox.restore(); + Sinon.restore(); }); // tslint:disable-next-line: max-func-body-length @@ -236,13 +234,13 @@ describe('Message Utils', () => { beforeEach(() => { // convos = []; - sandbox.stub(UserUtils, 'getOurPubKeyStrFromCache').resolves(ourNumber); - sandbox.stub(UserUtils, 'getOurPubKeyFromCache').resolves(PubKey.cast(ourNumber)); + Sinon.stub(UserUtils, 'getOurPubKeyStrFromCache').resolves(ourNumber); + Sinon.stub(UserUtils, 'getOurPubKeyFromCache').resolves(PubKey.cast(ourNumber)); }); beforeEach(() => { // convos = []; - sandbox.restore(); + Sinon.restore(); }); // it('filter out non active open groups', async () => { diff --git a/ts/test/session/unit/utils/Promise_test.ts b/ts/test/session/unit/utils/Promise_test.ts index 7762dc9a0..e0d577ff4 100644 --- a/ts/test/session/unit/utils/Promise_test.ts +++ b/ts/test/session/unit/utils/Promise_test.ts @@ -1,7 +1,7 @@ // tslint:disable: no-implicit-dependencies max-func-body-length no-unused-expression import chai from 'chai'; -import * as sinon from 'sinon'; +import Sinon, * as sinon from 'sinon'; import { PromiseUtils } from '../../../../session/utils'; @@ -20,7 +20,6 @@ chai.should(); const { expect } = chai; describe('Promise Utils', () => { - const sandbox = sinon.createSandbox(); let pollSpy: sinon.SinonSpy< [ (done: (arg: any) => void) => Promise | void, @@ -38,20 +37,20 @@ describe('Promise Utils', () => { >; beforeEach(() => { - pollSpy = sandbox.spy(PromiseUtils, 'poll'); - waitForTaskSpy = sandbox.spy(PromiseUtils, 'waitForTask'); - waitUntilSpy = sandbox.spy(PromiseUtils, 'waitUntil'); + pollSpy = Sinon.spy(PromiseUtils, 'poll'); + waitForTaskSpy = Sinon.spy(PromiseUtils, 'waitForTask'); + waitUntilSpy = Sinon.spy(PromiseUtils, 'waitUntil'); TestUtils.stubWindowLog(); }); afterEach(() => { - sandbox.restore(); + Sinon.restore(); }); describe('poll', () => { it('will call done on finished', async () => { // completionSpy will be called on done - const completionSpy = sandbox.spy(); + const completionSpy = Sinon.spy(); // tslint:disable-next-line: mocha-unneeded-done const task = (done: any) => { @@ -68,7 +67,7 @@ describe('Promise Utils', () => { it('can timeout a task', () => { // completionSpy will be called on done - const completionSpy = sandbox.spy(); + const completionSpy = Sinon.spy(); const task = (_done: any) => undefined; const promise = PromiseUtils.poll(task, { timeoutMs: 1, interval: 10 }); @@ -83,7 +82,7 @@ describe('Promise Utils', () => { const timeout = 3000; const interval = 3; - const recurrenceSpy = sandbox.spy(); + const recurrenceSpy = Sinon.spy(); const task = (done: any) => { recurrenceSpy(); @@ -104,7 +103,7 @@ describe('Promise Utils', () => { describe('waitForTask', () => { it('can wait for a task', async () => { // completionSpy will be called on done - const completionSpy = sandbox.spy(); + const completionSpy = Sinon.spy(); // tslint:disable-next-line: mocha-unneeded-done const task = (done: any) => { @@ -121,7 +120,7 @@ describe('Promise Utils', () => { it('can timeout a task', () => { // completionSpy will be called on done - const completionSpy = sandbox.spy(); + const completionSpy = Sinon.spy(); const task = async (_done: any) => undefined; const promise = PromiseUtils.waitForTask(task, 1); diff --git a/ts/test/session/unit/utils/SyncUtils_test.ts b/ts/test/session/unit/utils/SyncUtils_test.ts index 4383cf167..df306ba92 100644 --- a/ts/test/session/unit/utils/SyncUtils_test.ts +++ b/ts/test/session/unit/utils/SyncUtils_test.ts @@ -1,17 +1,13 @@ // tslint:disable: no-implicit-dependencies import chai from 'chai'; -import * as sinon from 'sinon'; -import { restoreStubs } from '../../../test-utils/utils'; import chaiAsPromised from 'chai-as-promised'; +import Sinon from 'sinon'; chai.use(chaiAsPromised as any); describe('SyncUtils', () => { - const sandbox = sinon.createSandbox(); - afterEach(() => { - sandbox.restore(); - restoreStubs(); + Sinon.restore(); }); describe('syncConfigurationIfNeeded', () => { diff --git a/ts/test/test-utils/utils/stubbing.ts b/ts/test/test-utils/utils/stubbing.ts index c467f122c..34440afbf 100644 --- a/ts/test/test-utils/utils/stubbing.ts +++ b/ts/test/test-utils/utils/stubbing.ts @@ -1,8 +1,9 @@ -import * as sinon from 'sinon'; +import Sinon from 'sinon'; import * as DataShape from '../../../../ts/data/data'; +import * as utilWorker from '../../../webworker/workers/util_worker_interface'; + const globalAny: any = global; -const sandbox = sinon.createSandbox(); // We have to do this in a weird way because Data uses module.exports // which doesn't play well with sinon or ImportMock @@ -18,20 +19,29 @@ type DataFunction = typeof DataShape; * Please call `restoreStubs()` or `stub.restore()` to restore original functionality. */ export function stubData(fn: K): sinon.SinonStub { - return sandbox.stub(Data, fn); + return Sinon.stub(Data, fn); } export function stubDataItem(fn: K): sinon.SinonStub { - return sandbox.stub(DataItem, fn); + return Sinon.stub(DataItem, fn); +} + +export function stubUtilWorker(fnName: string, returnedValue: any): sinon.SinonStub { + return Sinon.stub(utilWorker, 'callUtilsWorker') + .withArgs(fnName) + .resolves(returnedValue); +} +export function stubCreateObjectUrl() { + (global as any).URL = {}; + (global as any).URL.createObjectURL = function() { + return `${Date.now()}:${Math.floor(Math.random() * 1000)}`; + }; } type WindowValue = Partial | undefined; /** - * Stub a window object. - * - * Note: This uses a custom sandbox. - * Please call `restoreStubs()` or `stub.restore()` to restore original functionality. + * Stub a window object */ export function stubWindow(fn: K, value: WindowValue) { // tslint:disable-next-line: no-typeof-undefined @@ -55,11 +65,6 @@ export function stubWindow(fn: K, value: WindowValue) }; } -export function restoreStubs() { - globalAny.window = undefined; - sandbox.restore(); -} - export const stubWindowLog = () => { stubWindow('log', { // tslint:disable: no-void-expression diff --git a/ts/test/types/Settings_test.ts b/ts/test/types/Settings_test.ts index 714b1342e..8743efff9 100644 --- a/ts/test/types/Settings_test.ts +++ b/ts/test/types/Settings_test.ts @@ -5,16 +5,14 @@ import { assert } from 'chai'; import * as Settings from '../../../ts/types/Settings'; describe('Settings', () => { - const sandbox = Sinon.createSandbox(); - describe('isAudioNotificationSupported', () => { context('on macOS', () => { beforeEach(() => { - sandbox.stub(process, 'platform').value('darwin'); + Sinon.stub(process, 'platform').value('darwin'); }); afterEach(() => { - sandbox.restore(); + Sinon.restore(); }); it('should return true', () => { @@ -25,12 +23,12 @@ describe('Settings', () => { context('on Windows', () => { context('version 7', () => { beforeEach(() => { - sandbox.stub(process, 'platform').value('win32'); - sandbox.stub(os, 'release').returns('7.0.0'); + Sinon.stub(process, 'platform').value('win32'); + Sinon.stub(os, 'release').returns('7.0.0'); }); afterEach(() => { - sandbox.restore(); + Sinon.restore(); }); it('should return false', () => { @@ -40,12 +38,12 @@ describe('Settings', () => { context('version 8+', () => { beforeEach(() => { - sandbox.stub(process, 'platform').value('win32'); - sandbox.stub(os, 'release').returns('8.0.0'); + Sinon.stub(process, 'platform').value('win32'); + Sinon.stub(os, 'release').returns('8.0.0'); }); afterEach(() => { - sandbox.restore(); + Sinon.restore(); }); it('should return true', () => { @@ -56,11 +54,11 @@ describe('Settings', () => { context('on Linux', () => { beforeEach(() => { - sandbox.stub(process, 'platform').value('linux'); + Sinon.stub(process, 'platform').value('linux'); }); afterEach(() => { - sandbox.restore(); + Sinon.restore(); }); it('should return false', () => { @@ -72,11 +70,11 @@ describe('Settings', () => { describe('isNotificationGroupingSupported', () => { context('on macOS', () => { beforeEach(() => { - sandbox.stub(process, 'platform').value('darwin'); + Sinon.stub(process, 'platform').value('darwin'); }); afterEach(() => { - sandbox.restore(); + Sinon.restore(); }); it('should return true', () => { @@ -87,12 +85,12 @@ describe('Settings', () => { context('on Windows', () => { context('version 7', () => { beforeEach(() => { - sandbox.stub(process, 'platform').value('win32'); - sandbox.stub(os, 'release').returns('7.0.0'); + Sinon.stub(process, 'platform').value('win32'); + Sinon.stub(os, 'release').returns('7.0.0'); }); afterEach(() => { - sandbox.restore(); + Sinon.restore(); }); it('should return false', () => { @@ -102,12 +100,12 @@ describe('Settings', () => { context('version 8+', () => { beforeEach(() => { - sandbox.stub(process, 'platform').value('win32'); - sandbox.stub(os, 'release').returns('8.0.0'); + Sinon.stub(process, 'platform').value('win32'); + Sinon.stub(os, 'release').returns('8.0.0'); }); afterEach(() => { - sandbox.restore(); + Sinon.restore(); }); it('should return true', () => { @@ -118,11 +116,11 @@ describe('Settings', () => { context('on Linux', () => { beforeEach(() => { - sandbox.stub(process, 'platform').value('linux'); + Sinon.stub(process, 'platform').value('linux'); }); afterEach(() => { - sandbox.restore(); + Sinon.restore(); }); it('should return true', () => { @@ -133,11 +131,11 @@ describe('Settings', () => { describe('isHideMenuBarSupported', () => { context('on macOS', () => { beforeEach(() => { - sandbox.stub(process, 'platform').value('darwin'); + Sinon.stub(process, 'platform').value('darwin'); }); afterEach(() => { - sandbox.restore(); + Sinon.restore(); }); it('should return false', () => { @@ -148,12 +146,12 @@ describe('Settings', () => { context('on Windows', () => { context('version 7', () => { beforeEach(() => { - sandbox.stub(process, 'platform').value('win32'); - sandbox.stub(os, 'release').returns('7.0.0'); + Sinon.stub(process, 'platform').value('win32'); + Sinon.stub(os, 'release').returns('7.0.0'); }); afterEach(() => { - sandbox.restore(); + Sinon.restore(); }); it('should return true', () => { @@ -163,12 +161,12 @@ describe('Settings', () => { context('version 8+', () => { beforeEach(() => { - sandbox.stub(process, 'platform').value('win32'); - sandbox.stub(os, 'release').returns('8.0.0'); + Sinon.stub(process, 'platform').value('win32'); + Sinon.stub(os, 'release').returns('8.0.0'); }); afterEach(() => { - sandbox.restore(); + Sinon.restore(); }); it('should return true', () => { @@ -179,11 +177,11 @@ describe('Settings', () => { context('on Linux', () => { beforeEach(() => { - sandbox.stub(process, 'platform').value('linux'); + Sinon.stub(process, 'platform').value('linux'); }); afterEach(() => { - sandbox.restore(); + Sinon.restore(); }); it('should return true', () => { diff --git a/ts/test/util/blockedNumberController_test.ts b/ts/test/util/blockedNumberController_test.ts index 01b483af6..aa3366aee 100644 --- a/ts/test/util/blockedNumberController_test.ts +++ b/ts/test/util/blockedNumberController_test.ts @@ -1,15 +1,14 @@ // tslint:disable: no-implicit-dependencies max-func-body-length no-unused-expression import { expect } from 'chai'; -import * as sinon from 'sinon'; import { BlockedNumberController } from '../../util/blockedNumberController'; import { TestUtils } from '../test-utils'; import { PubKey } from '../../session/types'; import { UserUtils } from '../../session/utils'; +import Sinon from 'sinon'; // tslint:disable-next-line: max-func-body-length describe('BlockedNumberController', () => { - const sandbox = sinon.createSandbox(); let memoryDB: { [key: string]: any }; beforeEach(() => { memoryDB = {}; @@ -33,8 +32,7 @@ describe('BlockedNumberController', () => { }); afterEach(() => { - sandbox.restore(); - TestUtils.restoreStubs(); + Sinon.restore(); }); describe('load', () => { @@ -168,7 +166,7 @@ describe('BlockedNumberController', () => { let ourDevice: PubKey; beforeEach(() => { ourDevice = TestUtils.generateFakePubKey(); - sandbox.stub(UserUtils, 'getOurPubKeyStrFromCache').returns(ourDevice.key); + Sinon.stub(UserUtils, 'getOurPubKeyStrFromCache').returns(ourDevice.key); }); it('should return false for our device', async () => { const isBlocked = await BlockedNumberController.isBlockedAsync(ourDevice); diff --git a/ts/types/MessageAttachment.ts b/ts/types/MessageAttachment.ts index caecff69c..22f750911 100644 --- a/ts/types/MessageAttachment.ts +++ b/ts/types/MessageAttachment.ts @@ -16,17 +16,6 @@ import { // tslint:disable: prefer-object-spread -// FIXME audric -// upgrade: exports._mapAttachments(autoOrientJPEGAttachment), -// upgrade: exports._mapAttachments(replaceUnicodeOrderOverrides), -// upgrade: _mapAttachments(migrateDataToFileSystem), -// upgrade: ._mapQuotedAttachments(migrateDataToFileSystem), -// upgrade: initializeAttachmentMetadata, -// upgrade: initializeAttachmentMetadata, -// upgrade: _mapAttachments(captureDimensionsAndScreenshot), -// upgrade: _mapAttachments(replaceUnicodeV2), -// upgrade: _mapPreviewAttachments(migrateDataToFileSystem), - // I think this is only used on the renderer side, but how?! export const deleteExternalMessageFiles = async (message: {