|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ |
| 9 | +/* eslint-disable @typescript-eslint/no-unsafe-call */ |
| 10 | + |
| 11 | +import { expect } from 'chai'; |
| 12 | +import { Config, ConfigAggregator } from '@salesforce/core'; |
| 13 | +import { TestContext } from '@salesforce/core/testSetup'; |
| 14 | +import { CryptoUtils } from '@salesforce/lwc-dev-mobile-core'; |
| 15 | +import { IdentityUtils } from '../../src/shared/identityUtils.js'; |
| 16 | +import { ConfigVars } from '../../src/configMeta.js'; |
| 17 | + |
| 18 | +describe('identityUtils', () => { |
| 19 | + const $$ = new TestContext(); |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + $$.restore(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('getOrCreateIdentityToken resolves if token is found', async () => { |
| 26 | + const fakeIdentityToken = 'fake identity token'; |
| 27 | + $$.SANDBOX.stub(IdentityUtils, 'getIdentityToken').resolves(fakeIdentityToken); |
| 28 | + |
| 29 | + const resolved = await IdentityUtils.getOrCreateIdentityToken(); |
| 30 | + expect(resolved).to.equal(fakeIdentityToken); |
| 31 | + }); |
| 32 | + |
| 33 | + it('getOrCreateIdentityToken resolves and writeIdentityToken is called when there is no token', async () => { |
| 34 | + const fakeIdentityToken = 'fake identity token'; |
| 35 | + $$.SANDBOX.stub(IdentityUtils, 'getIdentityToken').resolves(undefined); |
| 36 | + $$.SANDBOX.stub(CryptoUtils, 'generateIdentityToken').resolves(fakeIdentityToken); |
| 37 | + const writeIdentityTokenStub = $$.SANDBOX.stub(IdentityUtils, 'writeIdentityToken').resolves(); |
| 38 | + |
| 39 | + const resolved = await IdentityUtils.getOrCreateIdentityToken(); |
| 40 | + expect(resolved).to.equal(fakeIdentityToken); |
| 41 | + expect(writeIdentityTokenStub.calledOnce).to.be.true; |
| 42 | + }); |
| 43 | + |
| 44 | + it('getIdentityToken resolves to undefined when identity token is not available', async () => { |
| 45 | + $$.SANDBOX.stub(ConfigAggregator, 'create').resolves(ConfigAggregator.prototype); |
| 46 | + $$.SANDBOX.stub(ConfigAggregator.prototype, 'reload').resolves(); |
| 47 | + $$.SANDBOX.stub(ConfigAggregator.prototype, 'getPropertyValue').returns(undefined); |
| 48 | + const resolved = await IdentityUtils.getIdentityToken(); |
| 49 | + |
| 50 | + expect(resolved).to.equal(undefined); |
| 51 | + }); |
| 52 | + |
| 53 | + it('getIdentityToken resolves to a string when identity token is available', async () => { |
| 54 | + const fakeIdentityToken = 'fake identity token'; |
| 55 | + $$.SANDBOX.stub(ConfigAggregator, 'create').resolves(ConfigAggregator.prototype); |
| 56 | + $$.SANDBOX.stub(ConfigAggregator.prototype, 'reload').resolves(); |
| 57 | + $$.SANDBOX.stub(ConfigAggregator.prototype, 'getPropertyValue').returns(fakeIdentityToken); |
| 58 | + |
| 59 | + const resolved = await IdentityUtils.getIdentityToken(); |
| 60 | + expect(resolved).to.equal(fakeIdentityToken); |
| 61 | + }); |
| 62 | + |
| 63 | + it('writeIdentityToken resolves', async () => { |
| 64 | + const fakeIdentityToken = 'fake identity token'; |
| 65 | + $$.SANDBOX.stub(Config, 'create').withArgs($$.SANDBOX.match.any).resolves(Config.prototype); |
| 66 | + $$.SANDBOX.stub(Config, 'addAllowedProperties').withArgs($$.SANDBOX.match.any); |
| 67 | + $$.SANDBOX.stub(Config.prototype, 'set').withArgs( |
| 68 | + ConfigVars.LOCAL_WEB_SERVER_IDENTITY_TOKEN, |
| 69 | + $$.SANDBOX.match.string |
| 70 | + ); |
| 71 | + $$.SANDBOX.stub(Config.prototype, 'write').resolves(); |
| 72 | + |
| 73 | + const resolved = await IdentityUtils.writeIdentityToken(fakeIdentityToken); |
| 74 | + expect(resolved).to.equal(undefined); |
| 75 | + }); |
| 76 | +}); |
0 commit comments