-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/Mermaid-Chart/plugins
- Loading branch information
Showing
13 changed files
with
333 additions
and
9 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,41 @@ | ||
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 | ||
- name: E2E tests (if present) for ${{ matrix.pkg }} | ||
env: | ||
TEST_MERMAIDCHART_API_TOKEN: ${{ secrets.TEST_MERMAIDCHART_API_TOKEN }} | ||
run: | | ||
pnpm --if-present --filter='${{ matrix.pkg }}' test:e2e |
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
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,75 @@ | ||
/** | ||
* E2E tests | ||
*/ | ||
import { MermaidChart } from './index.js'; | ||
import { beforeAll, describe, expect, it } from 'vitest'; | ||
|
||
import process from 'node:process'; | ||
import { AxiosError } from 'axios'; | ||
|
||
let client: MermaidChart; | ||
|
||
beforeAll(async() => { | ||
if (!process.env.TEST_MERMAIDCHART_API_TOKEN) { | ||
throw new Error( | ||
"Missing required environment variable TEST_MERMAIDCHART_API_TOKEN. " | ||
+ "Please go to https://test.mermaidchart.com/app/user/settings and create one." | ||
); | ||
} | ||
|
||
client = new MermaidChart({ | ||
clientID: '00000000-0000-0000-0000-000000git000test', | ||
baseURL: 'https://test.mermaidchart.com', | ||
redirectURI: 'https://localhost.invalid', | ||
}); | ||
|
||
await client.setAccessToken(process.env.TEST_MERMAIDCHART_API_TOKEN); | ||
}); | ||
|
||
describe('getUser', () => { | ||
it("should get user", async() => { | ||
const user = await client.getUser(); | ||
|
||
expect(user).toHaveProperty('emailAddress'); | ||
}); | ||
}); | ||
|
||
const documentMatcher = expect.objectContaining({ | ||
documentID: expect.any(String), | ||
major: expect.any(Number), | ||
minor: expect.any(Number), | ||
}); | ||
|
||
describe("getDocument", () => { | ||
it("should get publicly shared diagram", async() => { | ||
const latestDocument = await client.getDocument({ | ||
// owned by [email protected] | ||
documentID: '8bce727b-69b7-4f6e-a434-d578e2b363ff', | ||
}); | ||
|
||
expect(latestDocument).toStrictEqual(documentMatcher); | ||
|
||
const earliestDocument = await client.getDocument({ | ||
// owned by [email protected] | ||
documentID: '8bce727b-69b7-4f6e-a434-d578e2b363ff', | ||
major: 0, | ||
minor: 1, | ||
}); | ||
|
||
expect(earliestDocument).toStrictEqual(documentMatcher); | ||
}); | ||
|
||
it("should throw 404 on unknown document", async() => { | ||
let error: AxiosError | undefined = undefined; | ||
try { | ||
await client.getDocument({ | ||
documentID: '00000000-0000-0000-0000-0000deaddead', | ||
}); | ||
} catch (err) { | ||
error = err as AxiosError; | ||
} | ||
|
||
expect(error).toBeInstanceOf(AxiosError); | ||
expect(error?.response?.status).toBe(404); | ||
}); | ||
}); |
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,88 @@ | ||
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('#getAuthorizationData', () => { | ||
it('should set default state', async () => { | ||
const { state, url } = await client.getAuthorizationData(); | ||
|
||
expect(new URL(url).searchParams.has('state', state)).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
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
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
Oops, something went wrong.