-
Notifications
You must be signed in to change notification settings - Fork 12
@W-19036340 feat: add programmatic API for LWC dev server with JWT authentication #455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Shinoni
wants to merge
2
commits into
main
Choose a base branch
from
feature/lwc-dev-server
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,9 @@ | |
import { expect } from 'chai'; | ||
import { LWCServer, Workspace } from '@lwc/lwc-dev-server'; | ||
import esmock from 'esmock'; | ||
import sinon from 'sinon'; | ||
import { TestContext } from '@salesforce/core/testSetup'; | ||
import { AuthInfo, Logger, SfProject } from '@salesforce/core'; | ||
import * as devServer from '../../src/lwc-dev-server/index.js'; | ||
import { ConfigUtils } from '../../src/shared/configUtils.js'; | ||
|
||
|
@@ -18,6 +20,10 @@ describe('lwc-dev-server', () => { | |
stopServer: () => {}, | ||
} as LWCServer; | ||
let lwcDevServer: typeof devServer; | ||
let mockLogger: Logger; | ||
let mockProject: Partial<SfProject>; | ||
let getLocalDevServerPortsStub: sinon.SinonStub; | ||
let getLocalDevServerWorkspaceStub: sinon.SinonStub; | ||
|
||
before(async () => { | ||
lwcDevServer = await esmock<typeof devServer>('../../src/lwc-dev-server/index.js', { | ||
|
@@ -28,8 +34,22 @@ describe('lwc-dev-server', () => { | |
}); | ||
|
||
beforeEach(async () => { | ||
$$.SANDBOX.stub(ConfigUtils, 'getLocalDevServerPorts').resolves({ httpPort: 1234, httpsPort: 5678 }); | ||
$$.SANDBOX.stub(ConfigUtils, 'getLocalDevServerWorkspace').resolves(Workspace.SfCli); | ||
getLocalDevServerPortsStub = $$.SANDBOX.stub(ConfigUtils, 'getLocalDevServerPorts').resolves({ | ||
httpPort: 1234, | ||
httpsPort: 5678, | ||
}); | ||
getLocalDevServerWorkspaceStub = $$.SANDBOX.stub(ConfigUtils, 'getLocalDevServerWorkspace').resolves( | ||
Workspace.SfCli | ||
); | ||
|
||
mockLogger = await Logger.child('test'); | ||
mockProject = { | ||
getDefaultPackage: $$.SANDBOX.stub().returns({ fullPath: '/fake/path' }), | ||
getPackageDirectories: $$.SANDBOX.stub().returns([{ fullPath: '/fake/path' }]), | ||
resolveProjectConfig: $$.SANDBOX.stub().resolves({ namespace: '' }), | ||
}; | ||
|
||
$$.SANDBOX.stub(SfProject, 'resolve').resolves(mockProject as unknown as SfProject); | ||
}); | ||
|
||
afterEach(() => { | ||
|
@@ -40,9 +60,88 @@ describe('lwc-dev-server', () => { | |
expect(lwcDevServer.startLWCServer).to.be.a('function'); | ||
}); | ||
|
||
// it('calling startLWCServer returns an LWCServer', async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I uncommented and fixed this test. |
||
// const fakeIdentityToken = 'PFT1vw8v65aXd2b9HFvZ3Zu4OcKZwjI60bq7BEjj5k4='; | ||
// const s = await lwcDevServer.startLWCServer(logger, path.resolve(__dirname, './__mocks__'), fakeIdentityToken, ''); | ||
// expect(s).to.equal(server); | ||
// }); | ||
describe('JWT Authentication Error Handling', () => { | ||
it('should throw helpful error when no authorization information is found', async () => { | ||
const authError = new Error('No authorization information found for user test-user@example.com'); | ||
$$.SANDBOX.stub(AuthInfo, 'create').rejects(authError); | ||
|
||
try { | ||
await lwcDevServer.startLWCServer(mockLogger, '/fake/path', 'fake-token', 'test-user@example.com'); | ||
expect.fail('Expected function to throw an error'); | ||
} catch (error) { | ||
expect(error).to.be.an('error'); | ||
expect((error as Error).message).to.include('JWT authentication not found for user test-user@example.com'); | ||
expect((error as Error).message).to.include("Please run 'sf org login jwt' or 'sf org login web' first"); | ||
} | ||
}); | ||
|
||
it('should throw helpful error when JWT token is expired', async () => { | ||
const authError = new Error('JWT token expired for user test-user@example.com'); | ||
$$.SANDBOX.stub(AuthInfo, 'create').rejects(authError); | ||
|
||
try { | ||
await lwcDevServer.startLWCServer(mockLogger, '/fake/path', 'fake-token', 'test-user@example.com'); | ||
expect.fail('Expected function to throw an error'); | ||
} catch (error) { | ||
expect(error).to.be.an('error'); | ||
expect((error as Error).message).to.include( | ||
'JWT authentication expired or invalid for user test-user@example.com' | ||
); | ||
expect((error as Error).message).to.include( | ||
"Please re-authenticate using 'sf org login jwt' or 'sf org login web'" | ||
); | ||
} | ||
}); | ||
|
||
it('should throw helpful error when JWT token is invalid', async () => { | ||
const authError = new Error('Invalid JWT token for user test-user@example.com'); | ||
$$.SANDBOX.stub(AuthInfo, 'create').rejects(authError); | ||
|
||
try { | ||
await lwcDevServer.startLWCServer(mockLogger, '/fake/path', 'fake-token', 'test-user@example.com'); | ||
expect.fail('Expected function to throw an error'); | ||
} catch (error) { | ||
expect(error).to.be.an('error'); | ||
expect((error as Error).message).to.include( | ||
'JWT authentication expired or invalid for user test-user@example.com' | ||
); | ||
expect((error as Error).message).to.include( | ||
"Please re-authenticate using 'sf org login jwt' or 'sf org login web'" | ||
); | ||
} | ||
}); | ||
|
||
it('should throw helpful error for generic authentication failures', async () => { | ||
const authError = new Error('Some other authentication error'); | ||
$$.SANDBOX.stub(AuthInfo, 'create').rejects(authError); | ||
|
||
try { | ||
await lwcDevServer.startLWCServer(mockLogger, '/fake/path', 'fake-token', 'test-user@example.com'); | ||
expect.fail('Expected function to throw an error'); | ||
} catch (error) { | ||
expect(error).to.be.an('error'); | ||
expect((error as Error).message).to.include( | ||
'JWT authentication not found or invalid for user test-user@example.com' | ||
); | ||
expect((error as Error).message).to.include('Some other authentication error'); | ||
} | ||
}); | ||
}); | ||
|
||
it('calling startLWCServer returns an LWCServer', async () => { | ||
const mockAuthInfo = { | ||
getUsername: () => 'test-user@example.com', | ||
}; | ||
const authInfoStub = $$.SANDBOX.stub(AuthInfo, 'create').resolves(mockAuthInfo as unknown as AuthInfo); | ||
|
||
const fakeIdentityToken = 'PFT1vw8v65aXd2b9HFvZ3Zu4OcKZwjI60bq7BEjj5k4='; | ||
|
||
const s = await lwcDevServer.startLWCServer(mockLogger, '/fake/path', fakeIdentityToken, 'test-user@example.com'); | ||
|
||
expect(s).to.equal(server); | ||
expect(getLocalDevServerPortsStub.calledOnce).to.be.true; | ||
expect(getLocalDevServerWorkspaceStub.calledOnce).to.be.true; | ||
|
||
expect(authInfoStub.calledOnceWith({ username: 'test-user@example.com' })).to.be.true; | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if this is the right place for this API. Other consumers would be taking a dependency on the CLI plugin to access this API which seems a little odd.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Shinoni Lets hold off on merging this for now. I'm exploring a couple options for how we can do this with the CLI folks and then we can make a decision on where this code should live.