diff --git a/packages/integration-tests/src/client/index.ts b/packages/integration-tests/src/client/index.ts index b7f62f99e..7fa11e728 100644 --- a/packages/integration-tests/src/client/index.ts +++ b/packages/integration-tests/src/client/index.ts @@ -83,6 +83,10 @@ export default class MockClient { await this.consent(); } + public async getAccessToken(resource?: string) { + return this.logto.getAccessToken(resource); + } + public get isAuthenticated() { return this.logto.isAuthenticated; } diff --git a/packages/integration-tests/src/helpers.ts b/packages/integration-tests/src/helpers.ts index f77a45600..e30e85fe9 100644 --- a/packages/integration-tests/src/helpers.ts +++ b/packages/integration-tests/src/helpers.ts @@ -31,10 +31,6 @@ export const registerNewUser = async (username: string, password: string) => { assert(client.interactionCookie, new Error('Session not found')); - if (!client.interactionCookie) { - return; - } - const { redirectTo } = await registerUserWithUsernameAndPassword( username, password, @@ -52,10 +48,6 @@ export const signIn = async (username: string, password: string) => { assert(client.interactionCookie, new Error('Session not found')); - if (!client.interactionCookie) { - return; - } - const { redirectTo } = await signInWithUsernameAndPassword( username, password, diff --git a/packages/integration-tests/tests/get-access-token.test.ts b/packages/integration-tests/tests/get-access-token.test.ts new file mode 100644 index 000000000..94e10e720 --- /dev/null +++ b/packages/integration-tests/tests/get-access-token.test.ts @@ -0,0 +1,39 @@ +import { managementResource } from '@logto/schemas/lib/seeds'; +import { assert } from '@silverhand/essentials'; + +import { signInWithUsernameAndPassword } from '@/api'; +import MockClient from '@/client'; +import { createUserByAdmin } from '@/helpers'; +import { generateUsername, generatePassword } from '@/utils'; + +describe('get access token', () => { + const username = generateUsername(); + const password = generatePassword(); + + beforeAll(async () => { + await createUserByAdmin(username, password); + }); + + it('sign-in and getAccessToken', async () => { + const client = new MockClient({ resources: [managementResource.indicator] }); + await client.initSession(); + assert(client.interactionCookie, new Error('Session not found')); + + const { redirectTo } = await signInWithUsernameAndPassword( + username, + password, + client.interactionCookie + ); + + await client.processSession(redirectTo); + + assert(client.isAuthenticated, new Error('Sign in get get access token failed')); + + const accessToken = await client.getAccessToken(managementResource.indicator); + + expect(accessToken).not.toBeNull(); + + // Request for invalid resource should throw + void expect(client.getAccessToken('api.foo.com')).rejects.toThrow(); + }); +});