-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Mermaid-Chart/fix/handle-relative-urls-in-…
…handleAuthorizationResponse fix(sdk): handle relative URLs in `#handleAuthorizationResponse()`
- Loading branch information
Showing
5 changed files
with
130 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
name: Build | ||
on: | ||
pull_request: | ||
push: | ||
|
||
jobs: | ||
test: | ||
timeout-minutes: 15 | ||
strategy: | ||
matrix: | ||
node: ['18.18.x'] | ||
pkg: ['sdk'] | ||
runs-on: | ||
labels: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- uses: pnpm/action-setup@v2 | ||
with: | ||
version: 8 | ||
|
||
- name: Setup Node.js ${{ matrix.node }} | ||
uses: actions/setup-node@v4 | ||
with: | ||
cache: pnpm | ||
node-version: ${{ matrix.node }} | ||
|
||
- name: Install dependencies for ${{ matrix.pkg }} | ||
run: | | ||
pnpm install --frozen-lockfile --filter='...${{ matrix.pkg }}' | ||
- name: Test ${{ matrix.pkg }} | ||
run: | | ||
pnpm --filter='${{ matrix.pkg }}' test |
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
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,80 @@ | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import { MermaidChart } from './index.js'; | ||
import { AuthorizationData } from './types.js'; | ||
|
||
import { OAuth2Client } from '@badgateway/oauth2-client'; | ||
|
||
describe('MermaidChart', () => { | ||
let client: MermaidChart; | ||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
|
||
vi.spyOn(OAuth2Client.prototype, 'request').mockImplementation( | ||
async (endpoint: 'tokenEndpoint' | 'introspectionEndpoint', _body: Record<string, any>) => { | ||
switch (endpoint) { | ||
case 'tokenEndpoint': | ||
return { | ||
access_token: 'test-example-access_token', | ||
refresh_token: 'test-example-refresh_token', | ||
token_type: 'Bearer', | ||
expires_in: 3600, | ||
}; | ||
default: | ||
throw new Error('mock unimplemented'); | ||
} | ||
}, | ||
); | ||
|
||
client = new MermaidChart({ | ||
clientID: '00000000-0000-0000-0000-000000000dead', | ||
baseURL: 'https://test.mermaidchart.invalid', | ||
redirectURI: 'https://localhost.invalid', | ||
}); | ||
|
||
vi.spyOn(client, 'getUser').mockImplementation(async () => { | ||
return { | ||
fullName: 'Test User', | ||
emailAddress: '[email protected]', | ||
}; | ||
}); | ||
}); | ||
|
||
describe('#handleAuthorizationResponse', () => { | ||
let state: AuthorizationData['state']; | ||
beforeEach(async () => { | ||
({ state } = await client.getAuthorizationData({ state })); | ||
}); | ||
|
||
it('should set token', async () => { | ||
const code = 'hello-world'; | ||
|
||
await client.handleAuthorizationResponse( | ||
`https://response.invalid?code=${code}&state=${state}`, | ||
); | ||
await expect(client.getAccessToken()).resolves.toBe('test-example-access_token'); | ||
}); | ||
|
||
it('should throw with invalid state', async () => { | ||
await expect(() => | ||
client.handleAuthorizationResponse( | ||
'https://response.invalid?code=hello-world&state=my-invalid-state', | ||
), | ||
).rejects.toThrowError('invalid_state'); | ||
}); | ||
|
||
it('should throw with nicer error if URL has no query params', async () => { | ||
await expect(() => | ||
client.handleAuthorizationResponse( | ||
// missing the ? so it's not read as a query | ||
'code=hello-world&state=my-invalid-state', | ||
), | ||
).rejects.toThrowError(/no query parameters/); | ||
}); | ||
|
||
it('should work in Node.JS with url fragment', async () => { | ||
const code = 'hello-nodejs-world'; | ||
await client.handleAuthorizationResponse(`?code=${code}&state=${state}`); | ||
await expect(client.getAccessToken()).resolves.toBe('test-example-access_token'); | ||
}); | ||
}); | ||
}); |
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