2022-07-25 17:39:09 +08:00
|
|
|
import { LogtoConfig } from '@logto/node';
|
2022-07-21 15:32:05 +08:00
|
|
|
import { demoAppApplicationId } from '@logto/schemas/lib/seeds';
|
2022-06-16 13:00:01 +08:00
|
|
|
|
2022-07-25 17:39:09 +08:00
|
|
|
import LogtoClient from '@/client/logto-client';
|
|
|
|
import { demoAppRedirectUri, logtoUrl } from '@/constants';
|
|
|
|
import {
|
|
|
|
consentUserAndGetSignInCallbackUri,
|
|
|
|
registerUserWithUsernameAndPassword,
|
|
|
|
signInWithUsernameAndPassword,
|
|
|
|
visitSignInUri,
|
|
|
|
} from '@/ui-actions';
|
|
|
|
import { generatePassword, generateUsername } from '@/utils';
|
2022-06-16 13:00:01 +08:00
|
|
|
|
|
|
|
describe('username and password flow', () => {
|
2022-07-25 17:39:09 +08:00
|
|
|
it('should register and sign in with username and password successfully', async () => {
|
|
|
|
const logtoConfig: LogtoConfig = {
|
|
|
|
endpoint: logtoUrl,
|
|
|
|
appId: demoAppApplicationId,
|
|
|
|
persistAccessToken: false,
|
2022-06-16 13:00:01 +08:00
|
|
|
};
|
|
|
|
|
2022-07-25 17:39:09 +08:00
|
|
|
const logtoClient = new LogtoClient(logtoConfig);
|
2022-06-16 13:00:01 +08:00
|
|
|
|
2022-07-25 17:39:09 +08:00
|
|
|
await logtoClient.signIn(demoAppRedirectUri);
|
2022-06-16 13:00:01 +08:00
|
|
|
|
2022-07-25 17:39:09 +08:00
|
|
|
expect(logtoClient.navigateUrl).toBeTruthy();
|
2022-06-16 13:00:01 +08:00
|
|
|
|
2022-07-25 17:39:09 +08:00
|
|
|
const interactionCookie = await visitSignInUri(logtoClient.navigateUrl);
|
2022-06-16 13:00:01 +08:00
|
|
|
|
2022-07-25 17:39:09 +08:00
|
|
|
const username = generateUsername();
|
|
|
|
const password = generatePassword();
|
2022-06-16 13:00:01 +08:00
|
|
|
|
2022-07-25 17:39:09 +08:00
|
|
|
await registerUserWithUsernameAndPassword(username, password, interactionCookie);
|
2022-06-16 13:00:01 +08:00
|
|
|
|
2022-07-25 17:39:09 +08:00
|
|
|
const interactionCookieWithSession = await signInWithUsernameAndPassword(
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
interactionCookie
|
2022-06-16 13:00:01 +08:00
|
|
|
);
|
|
|
|
|
2022-07-25 17:39:09 +08:00
|
|
|
const signInCallbackUri = await consentUserAndGetSignInCallbackUri(
|
|
|
|
interactionCookieWithSession
|
2022-06-16 13:00:01 +08:00
|
|
|
);
|
|
|
|
|
2022-07-25 17:39:09 +08:00
|
|
|
await logtoClient.handleSignInCallback(signInCallbackUri);
|
|
|
|
|
|
|
|
expect(logtoClient.isAuthenticated).toBeTruthy();
|
2022-06-16 13:00:01 +08:00
|
|
|
});
|
|
|
|
});
|