diff --git a/ts/session/utils/libsession/libsession_utils_user_profile.ts b/ts/session/utils/libsession/libsession_utils_user_profile.ts index c7b3b01f0..b97a90b21 100644 --- a/ts/session/utils/libsession/libsession_utils_user_profile.ts +++ b/ts/session/utils/libsession/libsession_utils_user_profile.ts @@ -1,15 +1,15 @@ import { isEmpty } from 'lodash'; import { UserUtils } from '..'; +import { SettingsKey } from '../../../data/settings-key'; +import { CONVERSATION_PRIORITIES } from '../../../models/conversationAttributes'; +import { Storage } from '../../../util/storage'; import { UserConfigWrapperActions } from '../../../webworker/workers/browser/libsession_worker_interface'; import { getConversationController } from '../../conversations'; import { fromHexToArray } from '../String'; -import { CONVERSATION_PRIORITIES } from '../../../models/conversationAttributes'; -import { Storage } from '../../../util/storage'; -import { SettingsKey } from '../../../data/settings-key'; async function insertUserProfileIntoWrapper(convoId: string) { - if (!isUserProfileToStoreInWrapper(convoId)) { - return; + if (!SessionUtilUserProfile.isUserProfileToStoreInWrapper(convoId)) { + return null; } const us = UserUtils.getOurPubKeyStrFromCache(); const ourConvo = getConversationController().get(us); @@ -29,8 +29,14 @@ async function insertUserProfileIntoWrapper(convoId: string) { window.log.debug( `inserting into userprofile wrapper: username:"${dbName}", priority:${priority} image:${JSON.stringify( - { url: dbProfileUrl, key: dbProfileKey } - )}, settings: ${JSON.stringify({ areBlindedMsgRequestEnabled, expirySeconds })}` + { + url: dbProfileUrl, + key: dbProfileKey, + } + )}, settings: ${JSON.stringify({ + areBlindedMsgRequestEnabled, + expirySeconds, + })}` ); if (dbProfileUrl && !isEmpty(dbProfileKey)) { @@ -43,6 +49,15 @@ async function insertUserProfileIntoWrapper(convoId: string) { } await UserConfigWrapperActions.setEnableBlindedMsgRequest(areBlindedMsgRequestEnabled); await UserConfigWrapperActions.setNoteToSelfExpiry(expirySeconds); + + // returned testing purposes only + return { + id: convoId, + name: dbName, + priority, + avatarPointer: dbProfileUrl, + expirySeconds, + }; } function isUserProfileToStoreInWrapper(convoId: string) { diff --git a/ts/test/session/unit/libsession_wrapper/libsession_wrapper_contacts_test.ts b/ts/test/session/unit/libsession_wrapper/libsession_wrapper_contacts_test.ts index 6b3a05793..d81b5e607 100644 --- a/ts/test/session/unit/libsession_wrapper/libsession_wrapper_contacts_test.ts +++ b/ts/test/session/unit/libsession_wrapper/libsession_wrapper_contacts_test.ts @@ -19,6 +19,7 @@ describe('libsession_contacts', () => { const getLatestTimestampOffset = 200000; const ourNumber = '051234567890acbdef'; const validArgs = { + // NOTE we hardcode this key to make testing easier for bad whitespaces id: '050123456789abcdef050123456789abcdef0123456789abcdef050123456789ab', type: ConversationTypeEnum.PRIVATE, isApproved: true, @@ -222,7 +223,7 @@ describe('libsession_contacts', () => { expireTimer: 0, }; - it('the returned wrapper values matche with the inputted contact', async () => { + it('returns wrapper values that match with the inputted contact', async () => { const contact = new ConversationModel({ ...validArgs, ...contactArgs, diff --git a/ts/test/session/unit/libsession_wrapper/libsession_wrapper_user_profile_test.ts b/ts/test/session/unit/libsession_wrapper/libsession_wrapper_user_profile_test.ts index 1af72e7fc..040565e99 100644 --- a/ts/test/session/unit/libsession_wrapper/libsession_wrapper_user_profile_test.ts +++ b/ts/test/session/unit/libsession_wrapper/libsession_wrapper_user_profile_test.ts @@ -2,25 +2,140 @@ import { expect } from 'chai'; import Sinon from 'sinon'; -import { SessionUtilUserProfile } from '../../../../session/utils/libsession/libsession_utils_user_profile'; +import { ConversationModel } from '../../../../models/conversation'; +import { ConversationTypeEnum } from '../../../../models/conversationAttributes'; +import { GetNetworkTime } from '../../../../session/apis/snode_api/getNetworkTime'; +import { getConversationController } from '../../../../session/conversations'; import { UserUtils } from '../../../../session/utils'; +import { SessionUtilUserProfile } from '../../../../session/utils/libsession/libsession_utils_user_profile'; import { TestUtils } from '../../../test-utils'; +import { stubWindowLog } from '../../../test-utils/utils'; + +describe('libsession_user_profile', () => { + stubWindowLog(); + + const getLatestTimestampOffset = 200000; + const ourNumber = TestUtils.generateFakePubKeyStr(); + const validArgs = { + id: ourNumber, + type: ConversationTypeEnum.PRIVATE, + isApproved: true, + active_at: 123, + didApproveMe: true, + }; + + beforeEach(() => { + Sinon.stub(GetNetworkTime, 'getLatestTimestampOffset').returns(getLatestTimestampOffset); + Sinon.stub(UserUtils, 'getOurPubKeyStrFromCache').returns(ourNumber); + TestUtils.stubLibSessionWorker(undefined); + }); -describe('libsession_wrapper', () => { afterEach(() => { Sinon.restore(); }); - it('isUserProfileToStoreInWrapper returns true if thats our convo', () => { - const us = TestUtils.generateFakePubKeyStr(); - Sinon.stub(UserUtils, 'getOurPubKeyStrFromCache').returns(us); - expect(SessionUtilUserProfile.isUserProfileToStoreInWrapper(us)).to.be.true; + describe('isUserProfileToStoreInWrapper', () => { + it('returns true if thats our convo', () => { + expect(SessionUtilUserProfile.isUserProfileToStoreInWrapper(ourNumber)).to.be.true; + }); + + it('returns false if thats NOT our convo', () => { + const notUs = TestUtils.generateFakePubKeyStr(); + expect(SessionUtilUserProfile.isUserProfileToStoreInWrapper(notUs)).to.be.false; + }); }); - it('isUserProfileToStoreInWrapper returns false if thats NOT our convo', () => { - const us = TestUtils.generateFakePubKeyStr(); - const notUs = TestUtils.generateFakePubKeyStr(); - Sinon.stub(UserUtils, 'getOurPubKeyStrFromCache').returns(us); - expect(SessionUtilUserProfile.isUserProfileToStoreInWrapper(notUs)).to.be.false; + describe('insertUserProfileIntoWrapper', () => { + const contactArgs = { + displayNameInProfile: 'Tester', + nickname: 'Testie', + avatarPointer: 'http://filev2.abcdef.com/file/abcdefghijklmnop', + profileKey: 'profileKey', + isBlocked: () => false, + expirationMode: 'off', + expireTimer: 0, + }; + + it('returns wrapper values that match with the inputted user profile', async () => { + const contact = new ConversationModel({ + ...validArgs, + ...contactArgs, + } as any); + Sinon.stub(getConversationController(), 'get').returns(contact); + Sinon.stub(SessionUtilUserProfile, 'isUserProfileToStoreInWrapper').returns(true); + // Sinon.stub(ContactsWrapperActions, 'set').resolves(); + + const wrapperUserProfile = await SessionUtilUserProfile.insertUserProfileIntoWrapper( + ourNumber + ); + + expect(wrapperUserProfile, 'something should be returned from the wrapper').to.not.be.null; + if (!wrapperUserProfile) { + throw Error('something should be returned from the wrapper'); + } + + expect( + wrapperUserProfile.id, + 'id in the wrapper should match the inputted user profile' + ).to.equal(contact.id); + expect( + wrapperUserProfile.name, + 'name in the wrapper should match the inputted user profile' + ).to.equal(contact.get('displayNameInProfile')); + expect( + wrapperUserProfile.priority, + 'priority in the wrapper should match the inputted user profile' + ).to.equal(contact.get('priority')); + expect( + wrapperUserProfile.avatarPointer, + 'avatarPointer in the wrapper should match the inputted user profile' + ).to.equal(contact.get('avatarPointer')); + expect( + wrapperUserProfile.expirySeconds, + 'expirySeconds in the wrapper should match the inputted user profile' + ).to.equal(contact.get('expireTimer')); + }); + it("returns an error if the inputted user profile isn't our conversation", async () => { + const contact = new ConversationModel({ + ...validArgs, + ...contactArgs, + id: TestUtils.generateFakePubKeyStr(), + } as any); + Sinon.stub(getConversationController(), 'get').returns(contact); + Sinon.stub(SessionUtilUserProfile, 'isUserProfileToStoreInWrapper').returns(true); + // Sinon.stub(ContactsWrapperActions, 'set').resolves(); + + try { + await SessionUtilUserProfile.insertUserProfileIntoWrapper(ourNumber); + } catch (err) { + expect(err.message).to.equal('insertUserProfileIntoWrapper needs a ourConvo to exist'); + } + }); + it('if disappearing messages is on then the wrapper returned values should match the inputted user profile', async () => { + const contact = new ConversationModel({ + ...validArgs, + ...contactArgs, + expirationMode: 'deleteAfterSend', + expireTimer: 300, + id: ourNumber, + } as any); + Sinon.stub(getConversationController(), 'get').returns(contact); + Sinon.stub(SessionUtilUserProfile, 'isUserProfileToStoreInWrapper').returns(true); + // Sinon.stub(ContactsWrapperActions, 'set').resolves(); + + const wrapperUserProfile = await SessionUtilUserProfile.insertUserProfileIntoWrapper( + ourNumber + ); + + expect(wrapperUserProfile, 'something should be returned from the wrapper').to.not.be.null; + if (!wrapperUserProfile) { + throw Error('something should be returned from the wrapper'); + } + + expect( + wrapperUserProfile.expirySeconds, + 'expirySeconds in the wrapper should match the inputted user profile' + ).to.equal(contact.get('expireTimer')); + }); }); });