0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

test: integration test for deleting user identities (#1728)

This commit is contained in:
Xiao Yijun 2022-08-03 17:20:22 +08:00 committed by GitHub
parent 3b7f86e2a2
commit f71d9891a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 2 deletions

View file

@ -36,3 +36,6 @@ export const updateUserPassword = (userId: string, password: string) =>
},
})
.json<User>();
export const deleteUserIdentity = (userId: string, connectorTarget: string) =>
authedAdminApi.delete(`users/${userId}/identities/${connectorTarget}`);

View file

@ -3,6 +3,7 @@ import path from 'path';
import { User } from '@logto/schemas';
import { assert } from '@silverhand/essentials';
import { HTTPError } from 'got';
import {
createUser,
@ -10,10 +11,15 @@ import {
signInWithUsernameAndPassword,
updateConnectorConfig,
enableConnector,
bindWithSocial,
getAuthWithSocial,
signInWithSocial,
} from '@/api';
import MockClient from '@/client';
import { generateUsername, generatePassword } from '@/utils';
import { mockSocialConnectorId } from './__mocks__/connectors-mock';
export const createUserByAdmin = (_username?: string, _password?: string) => {
const username = _username ?? generateUsername();
const password = _password ?? generatePassword();
@ -80,3 +86,49 @@ export const readPasscode = async (): Promise<PasscodeRecord> => {
// eslint-disable-next-line no-restricted-syntax
return JSON.parse(content) as PasscodeRecord;
};
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);
const { sub } = client.getIdTokenClaims();
return sub;
};

View file

@ -1,7 +1,19 @@
import { HTTPError } from 'got';
import { getUser, getUsers, updateUser, deleteUser, updateUserPassword } from '@/api';
import { createUserByAdmin } from '@/helpers';
import {
mockSocialConnectorConfig,
mockSocialConnectorId,
mockSocialConnectorTarget,
} from '@/__mocks__/connectors-mock';
import {
getUser,
getUsers,
updateUser,
deleteUser,
updateUserPassword,
deleteUserIdentity,
} from '@/api';
import { createUserByAdmin, bindSocialToNewCreatedUser, setUpConnector } from '@/helpers';
describe('admin console user management', () => {
it('should create user successfully', async () => {
@ -52,4 +64,19 @@ describe('admin console user management', () => {
const userEntity = await updateUserPassword(user.id, 'new_password');
expect(userEntity).toMatchObject(user);
});
it('should delete user identities successfully', async () => {
await setUpConnector(mockSocialConnectorId, mockSocialConnectorConfig);
const createdUserId = await bindSocialToNewCreatedUser();
const userInfo = await getUser(createdUserId);
expect(userInfo.identities).toHaveProperty(mockSocialConnectorTarget);
await deleteUserIdentity(createdUserId, mockSocialConnectorTarget);
const updatedUser = await getUser(createdUserId);
expect(updatedUser.identities).not.toHaveProperty(mockSocialConnectorTarget);
});
});