2022-08-01 09:51:27 +08:00
|
|
|
import fs from 'fs/promises';
|
|
|
|
import path from 'path';
|
|
|
|
|
2022-07-22 10:50:21 +08:00
|
|
|
import { User } from '@logto/schemas';
|
2022-07-26 11:50:40 +08:00
|
|
|
import { assert } from '@silverhand/essentials';
|
2022-08-03 17:20:22 +08:00
|
|
|
import { HTTPError } from 'got';
|
2022-07-22 10:50:21 +08:00
|
|
|
|
2022-07-26 11:50:40 +08:00
|
|
|
import {
|
2022-07-28 10:13:21 +08:00
|
|
|
createUser,
|
2022-07-26 11:50:40 +08:00
|
|
|
registerUserWithUsernameAndPassword,
|
|
|
|
signInWithUsernameAndPassword,
|
2022-08-01 09:51:27 +08:00
|
|
|
updateConnectorConfig,
|
|
|
|
enableConnector,
|
2022-08-03 17:20:22 +08:00
|
|
|
bindWithSocial,
|
|
|
|
getAuthWithSocial,
|
|
|
|
signInWithSocial,
|
2022-07-28 10:13:21 +08:00
|
|
|
} from '@/api';
|
|
|
|
import MockClient from '@/client';
|
|
|
|
import { generateUsername, generatePassword } from '@/utils';
|
2022-07-22 10:50:21 +08:00
|
|
|
|
2022-08-03 17:20:22 +08:00
|
|
|
import { mockSocialConnectorId } from './__mocks__/connectors-mock';
|
|
|
|
|
2022-07-28 10:13:21 +08:00
|
|
|
export const createUserByAdmin = (_username?: string, _password?: string) => {
|
|
|
|
const username = _username ?? generateUsername();
|
|
|
|
const password = _password ?? generatePassword();
|
2022-07-22 10:50:21 +08:00
|
|
|
|
2022-07-28 10:13:21 +08:00
|
|
|
return createUser({
|
2022-07-27 11:23:10 +08:00
|
|
|
username,
|
|
|
|
password,
|
|
|
|
name: username,
|
|
|
|
}).json<User>();
|
2022-07-22 10:50:21 +08:00
|
|
|
};
|
2022-07-26 11:50:40 +08:00
|
|
|
|
2022-07-28 10:13:21 +08:00
|
|
|
export const registerNewUser = async (username: string, password: string) => {
|
|
|
|
const client = new MockClient();
|
|
|
|
await client.initSession();
|
|
|
|
|
|
|
|
assert(client.interactionCookie, new Error('Session not found'));
|
|
|
|
|
|
|
|
const { redirectTo } = await registerUserWithUsernameAndPassword(
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
client.interactionCookie
|
|
|
|
);
|
|
|
|
|
|
|
|
await client.processSession(redirectTo);
|
|
|
|
|
|
|
|
assert(client.isAuthenticated, new Error('Sign in failed'));
|
|
|
|
};
|
|
|
|
|
|
|
|
export const signIn = async (username: string, password: string) => {
|
|
|
|
const client = new MockClient();
|
|
|
|
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 failed'));
|
|
|
|
};
|
2022-08-01 09:51:27 +08:00
|
|
|
|
|
|
|
export const setUpConnector = async (connectorId: string, config: Record<string, unknown>) => {
|
|
|
|
await updateConnectorConfig(connectorId, config);
|
|
|
|
const connector = await enableConnector(connectorId);
|
|
|
|
assert(connector.enabled, new Error('Connector Setup Failed'));
|
|
|
|
};
|
|
|
|
|
|
|
|
type PasscodeRecord = {
|
|
|
|
phone?: string;
|
|
|
|
address?: string;
|
|
|
|
code: string;
|
|
|
|
type: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const readPasscode = async (): Promise<PasscodeRecord> => {
|
|
|
|
const buffer = await fs.readFile(path.join('/tmp', 'logto_mock_passcode_record.txt'));
|
|
|
|
const content = buffer.toString();
|
|
|
|
|
|
|
|
// For test use only
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
return JSON.parse(content) as PasscodeRecord;
|
|
|
|
};
|
2022-08-03 17:20:22 +08:00
|
|
|
|
|
|
|
export const bindSocialToNewCreatedUser = async () => {
|
|
|
|
const username = generateUsername();
|
|
|
|
const password = generatePassword();
|
|
|
|
|
|
|
|
await createUserByAdmin(username, password);
|
|
|
|
|
|
|
|
const state = 'mock_state';
|
|
|
|
const redirectUri = 'http://mock.com/callback';
|
|
|
|
const code = 'mock_code';
|
|
|
|
|
|
|
|
const client = new MockClient();
|
|
|
|
|
|
|
|
await client.initSession();
|
|
|
|
assert(client.interactionCookie, new Error('Session not found'));
|
|
|
|
|
|
|
|
await signInWithSocial(
|
|
|
|
{ state, connectorId: mockSocialConnectorId, redirectUri },
|
|
|
|
client.interactionCookie
|
|
|
|
);
|
|
|
|
|
|
|
|
const response = await getAuthWithSocial(
|
|
|
|
{ connectorId: mockSocialConnectorId, data: { state, redirectUri, code } },
|
|
|
|
client.interactionCookie
|
|
|
|
).catch((error: unknown) => error);
|
|
|
|
|
|
|
|
// User with social does not exist
|
|
|
|
assert(
|
|
|
|
response instanceof HTTPError && response.response.statusCode === 422,
|
|
|
|
new Error('Auth with social failed')
|
|
|
|
);
|
|
|
|
|
|
|
|
const { redirectTo } = await signInWithUsernameAndPassword(
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
client.interactionCookie
|
|
|
|
);
|
|
|
|
|
|
|
|
await bindWithSocial(mockSocialConnectorId, client.interactionCookie);
|
|
|
|
|
|
|
|
await client.processSession(redirectTo);
|
|
|
|
|
2022-09-09 18:37:04 +08:00
|
|
|
const { sub } = await client.getIdTokenClaims();
|
2022-08-03 17:20:22 +08:00
|
|
|
|
|
|
|
return sub;
|
|
|
|
};
|