0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-23 20:33:16 -05:00
logto/packages/integration-tests/tests/get-access-token.test.ts
simeng-li ef9bc1e3fc
test(test): add get access token flow test (#1701)
add get access token flow test
2022-08-01 15:50:24 +08:00

39 lines
1.2 KiB
TypeScript

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();
});
});