From 8c3943094d6a9013b99eee21c3fe0f3171c83ec5 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Tue, 7 Jul 2020 11:42:56 +1000 Subject: [PATCH] Add tests --- js/views/conversation_view.js | 11 +- ts/test/util/blockedNumberController_test.ts | 176 +++++++++++++++++++ ts/util/blockedNumberController.ts | 10 +- 3 files changed, 190 insertions(+), 7 deletions(-) create mode 100644 ts/test/util/blockedNumberController_test.ts diff --git a/js/views/conversation_view.js b/js/views/conversation_view.js index 21eff707c..d139cbed6 100644 --- a/js/views/conversation_view.js +++ b/js/views/conversation_view.js @@ -10,6 +10,7 @@ textsecure, Whisper, ConversationController, + BlockedNumberController, */ // eslint-disable-next-line func-names @@ -1910,11 +1911,17 @@ toastOptions.id = 'clockOutOfSync'; } } - if (this.model.isPrivate() && storage.isBlocked(this.model.id)) { + if ( + this.model.isPrivate() && + BlockedNumberController.isBlocked(this.model.id) + ) { toastOptions.title = i18n('unblockToSend'); toastOptions.id = 'unblockToSend'; } - if (!this.model.isPrivate() && storage.isGroupBlocked(this.model.id)) { + if ( + !this.model.isPrivate() && + BlockedNumberController.isGroupBlocked(this.model.id) + ) { toastOptions.title = i18n('unblockGroupToSend'); toastOptions.id = 'unblockGroupToSend'; } diff --git a/ts/test/util/blockedNumberController_test.ts b/ts/test/util/blockedNumberController_test.ts new file mode 100644 index 000000000..79d952d4d --- /dev/null +++ b/ts/test/util/blockedNumberController_test.ts @@ -0,0 +1,176 @@ +import { expect } from 'chai'; +import * as crypto from 'crypto'; +import Sinon, * as sinon from 'sinon'; +import { BlockedNumberController } from '../../util/blockedNumberController'; +import { TestUtils } from '../test-utils'; +import { PubKey } from '../../session/types'; +import { MultiDeviceProtocol } from '../../session/protocols'; + +describe('BlockedNumberController', () => { + const sandbox = sinon.createSandbox(); + let memoryDB: { [key: string]: any }; + beforeEach(() => { + memoryDB = {}; + + TestUtils.stubData('createOrUpdateItem').callsFake(data => { + memoryDB[data.id] = data.value; + }); + + TestUtils.stubData('getItemById').callsFake(id => { + if (!memoryDB[id]) { + return undefined; + } + const value = memoryDB[id]; + return { + id, + value, + }; + }); + + BlockedNumberController.reset(); + }); + + afterEach(() => { + sandbox.restore(); + TestUtils.restoreStubs(); + }); + + describe('load', async () => { + it('should load data from the database', async () => { + const normal = TestUtils.generateFakePubKey(); + const group = TestUtils.generateFakePubKey(); + memoryDB.blocked = [normal.key]; + memoryDB['blocked-groups'] = [group.key]; + await BlockedNumberController.load(); + + const blockedNumbers = BlockedNumberController.getBlockedNumbers(); + const blockedGroups = BlockedNumberController.getBlockedGroups(); + + expect(blockedNumbers).to.have.lengthOf(1); + expect(blockedNumbers).to.include(normal.key); + expect(blockedGroups).to.have.lengthOf(1); + expect(blockedGroups).to.include(group.key); + }); + + it('should return empty if nothing in the db exists', async () => { + await BlockedNumberController.load(); + const blockedNumbers = BlockedNumberController.getBlockedNumbers(); + const blockedGroups = BlockedNumberController.getBlockedGroups(); + + expect(blockedNumbers).to.be.empty; + expect(blockedGroups).to.be.empty; + }); + }); + + describe('block', async () => { + it('should block all linked devices of a user', async () => { + const pubKey = TestUtils.generateFakePubKey(); + const linkedDevice = TestUtils.generateFakePubKey(); + sandbox + .stub(MultiDeviceProtocol, 'getAllDevices') + .resolves([pubKey, linkedDevice]); + + await BlockedNumberController.block(linkedDevice); + + const blockedNumbers = BlockedNumberController.getBlockedNumbers(); + expect(blockedNumbers).to.have.lengthOf(2); + expect(blockedNumbers).to.have.same.members([ + pubKey.key, + linkedDevice.key, + ]); + expect(BlockedNumberController.getBlockedGroups()).to.be.empty; + }); + }); + + describe('unblock', async () => { + it('should unblock all linked device of a user', async () => { + const pubKey = TestUtils.generateFakePubKey(); + const linkedDevice = TestUtils.generateFakePubKey(); + memoryDB.blocked = [pubKey.key, linkedDevice.key]; + sandbox + .stub(MultiDeviceProtocol, 'getAllDevices') + .resolves([pubKey, linkedDevice]); + + await BlockedNumberController.unblock(linkedDevice); + + const blockedNumbers = BlockedNumberController.getBlockedNumbers(); + expect(blockedNumbers).to.be.empty; + }); + + it('should only unblock if a device was blocked', async () => { + const pubKey = TestUtils.generateFakePubKey(); + const another = TestUtils.generateFakePubKey(); + memoryDB.blocked = [pubKey.key, another.key]; + sandbox.stub(MultiDeviceProtocol, 'getAllDevices').resolves([pubKey]); + + await BlockedNumberController.unblock(pubKey); + + const blockedNumbers = BlockedNumberController.getBlockedNumbers(); + expect(blockedNumbers).to.have.lengthOf(1); + expect(blockedNumbers).to.include(another.key); + }); + }); + + describe('blockGroup', async () => { + it('should block a group', async () => { + const group = TestUtils.generateFakePubKey(); + + await BlockedNumberController.blockGroup(group); + + const blockedGroups = BlockedNumberController.getBlockedGroups(); + expect(blockedGroups).to.have.lengthOf(1); + expect(blockedGroups).to.include(group.key); + expect(BlockedNumberController.getBlockedNumbers()).to.be.empty; + }); + }); + + describe('unblockGroup', async () => { + it('should unblock a group', async () => { + const group = TestUtils.generateFakePubKey(); + const another = TestUtils.generateFakePubKey(); + memoryDB['blocked-groups'] = [group.key, another.key]; + + await BlockedNumberController.unblockGroup(group); + + const blockedGroups = BlockedNumberController.getBlockedGroups(); + expect(blockedGroups).to.have.lengthOf(1); + expect(blockedGroups).to.include(another.key); + }); + }); + + describe('isBlocked', async () => { + it('should return the correct value', async () => { + const pubKey = TestUtils.generateFakePubKey(); + const groupPubKey = TestUtils.generateFakePubKey(); + memoryDB.blocked = [pubKey.key]; + memoryDB['blocked-groups'] = [groupPubKey.key]; + await BlockedNumberController.load(); + expect(BlockedNumberController.isBlocked(pubKey.key)).to.equal( + true, + 'Expected isBlocked to return true for user pubkey' + ); + expect(BlockedNumberController.isBlocked(groupPubKey.key)).to.equal( + false, + 'Expected isBlocked to return false for a group pubkey' + ); + }); + }); + + describe('isGroupBlocked', async () => { + it('should return the correct value', async () => { + const pubKey = TestUtils.generateFakePubKey(); + const groupPubKey = TestUtils.generateFakePubKey(); + memoryDB.blocked = [pubKey.key]; + memoryDB['blocked-groups'] = [groupPubKey.key]; + await BlockedNumberController.load(); + expect(BlockedNumberController.isGroupBlocked(pubKey.key)).to.equal( + false, + 'Expected isGroupBlocked to return false for user pubkey' + ); + expect(BlockedNumberController.isGroupBlocked(groupPubKey.key)).to.equal( + true, + 'Expected isGroupBlocked to return true for a group pubkey' + ); + }); + }); +}); diff --git a/ts/util/blockedNumberController.ts b/ts/util/blockedNumberController.ts index cdfa17134..74edd88e2 100644 --- a/ts/util/blockedNumberController.ts +++ b/ts/util/blockedNumberController.ts @@ -74,12 +74,12 @@ export class BlockedNumberController { await this.saveToDB(BLOCKED_GROUPS_ID, this.blockedGroups); } - public static getBlockedNumbers(): Set { - return new Set(this.blockedNumbers); + public static getBlockedNumbers(): Array { + return [...this.blockedNumbers]; } - public static getBlockedGroups(): Set { - return new Set(this.blockedGroups); + public static getBlockedGroups(): Array { + return [...this.blockedGroups]; } // ---- DB @@ -92,7 +92,7 @@ export class BlockedNumberController { } } - public static async reset() { + public static reset() { this.loaded = false; this.blockedNumbers = new Set(); this.blockedGroups = new Set();