test: validity check for signInByLinkedDevice done

made stubs for window whisper and storage
pull/3056/head
William Grant 1 year ago
parent 7e6fb22907
commit e97ca2c7f3

@ -1,6 +1,7 @@
import { expect } from 'chai';
import Sinon from 'sinon';
import { displayNameIsValid } from '../../../../components/registration/utils';
import { getSwarmPollingInstance } from '../../../../session/apis/snode_api';
import { PubKey } from '../../../../session/types';
import {
generateMnemonic,
@ -8,16 +9,16 @@ import {
signInByLinkingDevice,
} from '../../../../util/accountManager';
import { TestUtils } from '../../../test-utils';
import { stubI18n, stubWindow, stubWindowLog } from '../../../test-utils/utils';
import { stubWindow } from '../../../test-utils/utils';
describe('Onboarding', () => {
stubWindowLog();
stubI18n();
// TODO[epic=ses-1560] set mnemonic generateMnemonic() ?
beforeEach(() => {
TestUtils.stubData('removeItemById').resolves();
TestUtils.stubWindowLog();
TestUtils.stubWindowWhisper();
TestUtils.stubStorage();
TestUtils.stubI18n();
TestUtils.stubData('createOrUpdateItem').resolves();
TestUtils.stubData('removeItemById').resolves();
stubWindow('setOpengroupPruning', () => {});
});
@ -111,6 +112,9 @@ describe('Onboarding', () => {
});
describe('signInByLinkingDevice', () => {
const polledDisplayName = 'Hello World';
Sinon.stub(getSwarmPollingInstance(), 'pollOnceForOurDisplayName').resolves(polledDisplayName);
const abortController = new AbortController();
it('should return a valid pubkey/account id and display name given a valid recovery password', async () => {
@ -122,12 +126,12 @@ describe('Onboarding', () => {
'english',
abortController.signal
);
expect(pubKeyString, 'should not be null').to.not.be.null;
expect(pubKeyString, 'should be a string').to.be.a('string');
expect(PubKey.validate(pubKeyString!), 'should be a valid pubkey').to.equal(true);
expect(displayName, 'should not be null').to.not.be.null;
expect(displayName, 'should be a string').to.be.a('string');
expect(displayName, `should equal ${polledDisplayName}`).to.equal(polledDisplayName);
});
});
});

@ -101,6 +101,23 @@ export const stubWindowFeatureFlags = () => {
stubWindow('sessionFeatureFlags', { debug: {} } as any);
};
export const stubWindowWhisper = () => {
stubWindow('Whisper', {
events: {
on: (name: string, callback: (param1?: any, param2?: any) => void) => {
if (enableLogRedirect) {
console.info(`Whisper Event registered ${name} ${callback}`);
}
callback();
},
trigger: (name: string, param1?: any, param2?: any) =>
enableLogRedirect
? console.info(`Whisper Event triggered ${name} ${param1} ${param2}`)
: {},
},
});
};
export async function expectAsyncToThrow(toAwait: () => Promise<any>, errorMessageToCatch: string) {
try {
await toAwait();
@ -116,3 +133,25 @@ export const stubI18n = () => {
const locale = load({ appLocale: 'en', logger: window.log });
stubWindow('i18n', setupi18n('en', locale.messages));
};
export const stubStorage = () => {
return {
get: (key: string, defaultValue?: any) => {
if (enableLogRedirect) {
console.info(`Storage.get ${key} returns ${defaultValue || key}`);
}
return defaultValue || key;
},
put: (key: string, value: any) => {
if (enableLogRedirect) {
console.info(`Storage.put ${key} with ${value}`);
}
},
remove: (key: string) => {
if (enableLogRedirect) {
console.info(`Storage.remove ${key}`);
}
},
};
};

@ -47,7 +47,7 @@ function get(key: string, defaultValue?: ValueType) {
async function remove(key: string) {
if (!ready) {
window.log.warn('Called storage.get before storage is ready. key:', key);
window.log.warn('Called storage.remove before storage is ready. key:', key);
}
delete items[key];

Loading…
Cancel
Save