-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OIDC error handling and implement token method in SDK
- Loading branch information
Showing
3 changed files
with
117 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import createSdk from '../src/index'; | ||
import webauthn from '../src/sdk/webauthn'; | ||
|
||
const coreJs = { | ||
oauth: { | ||
|
This file contains 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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { OidcClient } from 'oidc-client-ts'; | ||
import createOidc from '../src/sdk/oidc'; | ||
import { CoreSdk } from '../src/types'; | ||
|
||
jest.mock('oidc-client-ts', () => { | ||
return { | ||
OidcClient: jest.fn().mockImplementation(() => ({ | ||
createSigninRequest: jest.fn(), | ||
processSigninResponse: jest.fn(), | ||
createSignoutRequest: jest.fn(), | ||
})), | ||
WebStorageStateStore: jest.fn(), | ||
}; | ||
}); | ||
|
||
describe('OIDC', () => { | ||
let sdk: CoreSdk; | ||
|
||
beforeAll(() => { | ||
sdk = { | ||
httpClient: { | ||
buildUrl: jest.fn().mockReturnValue('http://example.com'), | ||
}, | ||
refresh: jest.fn(), | ||
} as unknown as CoreSdk; | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
const unload = () => | ||
setTimeout(() => window.dispatchEvent(new Event('unload')), 200); | ||
|
||
const location = Object.defineProperties( | ||
{}, | ||
{ | ||
...Object.getOwnPropertyDescriptors(window.location), | ||
assign: { | ||
enumerable: true, | ||
value: jest.fn(unload), | ||
}, | ||
replace: { | ||
enumerable: true, | ||
value: jest.fn(unload), | ||
}, | ||
}, | ||
); | ||
Object.defineProperty(window, 'location', { | ||
enumerable: true, | ||
get: () => location, | ||
}); | ||
}); | ||
|
||
describe('authorize', () => { | ||
it('should call createSigninRequest with correct params', async () => { | ||
const mockCreateSigninRequest = jest | ||
.fn() | ||
.mockResolvedValue({ url: 'mockUrl' }); | ||
(OidcClient as jest.Mock).mockImplementation(() => ({ | ||
createSigninRequest: mockCreateSigninRequest, | ||
})); | ||
|
||
const oidc = createOidc(sdk, 'projectID'); | ||
const response = await oidc.authorize(); | ||
|
||
expect(mockCreateSigninRequest).toHaveBeenCalledWith({}); | ||
expect(response).toEqual({ ok: true, data: { url: 'mockUrl' } }); | ||
}); | ||
}); | ||
|
||
describe('token', () => { | ||
it('should call processSigninResponse and sdk.refresh with correct params', async () => { | ||
const mockProcessSigninResponse = jest | ||
.fn() | ||
.mockResolvedValue({ refresh_token: 'mockRefreshToken' }); | ||
(OidcClient as jest.Mock).mockImplementation(() => ({ | ||
processSigninResponse: mockProcessSigninResponse, | ||
})); | ||
|
||
const oidc = createOidc(sdk, 'projectID'); | ||
const response = await oidc.token(); | ||
|
||
expect(mockProcessSigninResponse).toHaveBeenCalledWith( | ||
window.location.href, | ||
); | ||
expect(sdk.refresh).toHaveBeenCalledWith('mockRefreshToken'); | ||
}); | ||
}); | ||
|
||
describe('logout', () => { | ||
it('should call createSignoutRequest with correct params', async () => { | ||
const mockCreateSignoutRequest = jest | ||
.fn() | ||
.mockResolvedValue({ url: 'mockLogoutUrl' }); | ||
(OidcClient as jest.Mock).mockImplementation(() => ({ | ||
createSignoutRequest: mockCreateSignoutRequest, | ||
})); | ||
|
||
const oidc = createOidc(sdk, 'projectID'); | ||
|
||
await oidc.logout(); | ||
|
||
expect(mockCreateSignoutRequest).toHaveBeenCalled(); | ||
expect(window.location.replace).toHaveBeenCalledWith('mockLogoutUrl'); | ||
}); | ||
}); | ||
}); |