From f71d9891a82d541dca77d3ffff0f06135718b378 Mon Sep 17 00:00:00 2001 From: Xiao Yijun Date: Wed, 3 Aug 2022 17:20:22 +0800 Subject: [PATCH] test: integration test for deleting user identities (#1728) --- .../integration-tests/src/api/admin-user.ts | 3 ++ packages/integration-tests/src/helpers.ts | 52 +++++++++++++++++++ .../tests/admin-user.test.ts | 31 ++++++++++- 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/packages/integration-tests/src/api/admin-user.ts b/packages/integration-tests/src/api/admin-user.ts index 688de014d..903dc17ab 100644 --- a/packages/integration-tests/src/api/admin-user.ts +++ b/packages/integration-tests/src/api/admin-user.ts @@ -36,3 +36,6 @@ export const updateUserPassword = (userId: string, password: string) => }, }) .json(); + +export const deleteUserIdentity = (userId: string, connectorTarget: string) => + authedAdminApi.delete(`users/${userId}/identities/${connectorTarget}`); diff --git a/packages/integration-tests/src/helpers.ts b/packages/integration-tests/src/helpers.ts index e30e85fe9..8c520698e 100644 --- a/packages/integration-tests/src/helpers.ts +++ b/packages/integration-tests/src/helpers.ts @@ -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 => { // 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; +}; diff --git a/packages/integration-tests/tests/admin-user.test.ts b/packages/integration-tests/tests/admin-user.test.ts index 5c95ae854..65be770e5 100644 --- a/packages/integration-tests/tests/admin-user.test.ts +++ b/packages/integration-tests/tests/admin-user.test.ts @@ -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); + }); });