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:
parent
054899fb90
commit
ef9bc1e3fc
3 changed files with 43 additions and 8 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
39
packages/integration-tests/tests/get-access-token.test.ts
Normal file
39
packages/integration-tests/tests/get-access-token.test.ts
Normal 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();
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue