0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -05:00

test(test): add get access token flow test (#1701)

add get access token flow test
This commit is contained in:
simeng-li 2022-08-01 15:50:24 +08:00 committed by GitHub
parent 054899fb90
commit ef9bc1e3fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 8 deletions

View file

@ -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;
}

View file

@ -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,

View file

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