Skip to content

Commit

Permalink
refactor: fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
gao-sun committed Mar 29, 2024
1 parent 1246ad6 commit af42cb4
Show file tree
Hide file tree
Showing 55 changed files with 240 additions and 219 deletions.
15 changes: 15 additions & 0 deletions packages/integration-tests/src/api/interaction.ts
Expand Up @@ -27,6 +27,7 @@ export const putInteraction = async (cookie: string, payload: InteractionPayload
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
})
.json();

Expand All @@ -35,6 +36,7 @@ export const deleteInteraction = async (cookie: string) =>
.delete('interaction', {
headers: { cookie },
redirect: 'manual',
throwHttpErrors: false,
})
.json();

Expand All @@ -47,6 +49,7 @@ export const patchInteractionIdentifiers = async (cookie: string, payload: Ident
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
})
.json();

Expand All @@ -56,6 +59,7 @@ export const patchInteractionProfile = async (cookie: string, payload: Profile)
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
})
.json();

Expand All @@ -65,6 +69,7 @@ export const putInteractionProfile = async (cookie: string, payload: Profile) =>
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
})
.json();

Expand All @@ -74,6 +79,7 @@ export const postInteractionBindMfa = async (cookie: string, payload: BindMfaPay
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
})
.json();

Expand All @@ -83,6 +89,7 @@ export const putInteractionMfa = async (cookie: string, payload: VerifyMfaPayloa
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
})
.json();

Expand All @@ -91,6 +98,7 @@ export const deleteInteractionProfile = async (cookie: string) =>
.delete('interaction/profile', {
headers: { cookie },
redirect: 'manual',
throwHttpErrors: false,
})
.json();

Expand All @@ -107,6 +115,7 @@ export const sendVerificationCode = async (
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
});

export type SocialAuthorizationUriPayload = {
Expand All @@ -123,6 +132,7 @@ export const createSocialAuthorizationUri = async (
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
});

export const initTotp = async (cookie: string) =>
Expand All @@ -131,6 +141,7 @@ export const initTotp = async (cookie: string) =>
headers: { cookie },
json: {},
redirect: 'manual',
throwHttpErrors: false,
})
.json<{ secret: string }>();

Expand All @@ -141,6 +152,7 @@ export const skipMfaBinding = async (cookie: string) =>
mfaSkipped: true,
},
redirect: 'manual',
throwHttpErrors: false,
});

export const consent = async (api: KyInstance, cookie: string) =>
Expand All @@ -150,6 +162,7 @@ export const consent = async (api: KyInstance, cookie: string) =>
cookie,
},
redirect: 'manual',
throwHttpErrors: false,
})
.json<RedirectResponse>();

Expand All @@ -158,6 +171,7 @@ export const getConsentInfo = async (cookie: string) =>
.get('interaction/consent', {
headers: { cookie },
redirect: 'manual',
throwHttpErrors: false,
})
.json<ConsentInfoResponse>();

Expand All @@ -169,4 +183,5 @@ export const createSingleSignOnAuthorizationUri = async (
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
});
4 changes: 4 additions & 0 deletions packages/integration-tests/src/client/index.ts
Expand Up @@ -73,6 +73,7 @@ export default class MockClient {
// Mock SDK sign-in navigation
const response = await ky(this.navigateUrl, {
redirect: 'manual',
throwHttpErrors: false,
});

// Note: should redirect to sign-in page
Expand Down Expand Up @@ -106,6 +107,7 @@ export default class MockClient {
cookie: this.interactionCookie,
},
redirect: 'manual',
throwHttpErrors: false,
});

// Note: Should redirect to logto consent page
Expand Down Expand Up @@ -182,6 +184,7 @@ export default class MockClient {
cookie: this.interactionCookie,
},
redirect: 'manual',
throwHttpErrors: false,
});

// Consent page should auto consent and redirect to auth endpoint
Expand All @@ -196,6 +199,7 @@ export default class MockClient {
cookie: this.interactionCookie,
},
redirect: 'manual',
throwHttpErrors: false,
});

// Note: Should redirect to the signInCallbackUri
Expand Down
1 change: 1 addition & 0 deletions packages/integration-tests/src/helpers/admin-tenant.ts
Expand Up @@ -63,6 +63,7 @@ export const putInteraction = async (cookie: string, payload: InteractionPayload
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
})
.json();

Expand Down
10 changes: 5 additions & 5 deletions packages/integration-tests/src/helpers/index.ts
Expand Up @@ -77,7 +77,7 @@ export const removeConnectorMessage = async (

type ExpectedErrorInfo = {
code: string;
statusCode: number;
status: number;
messageIncludes?: string;
};

Expand All @@ -94,24 +94,24 @@ export const expectRejects = async <T = void>(
fail();
};

export const expectRequestError = <T = void>(error: unknown, expected: ExpectedErrorInfo) => {
const { code, statusCode, messageIncludes } = expected;
const expectRequestError = async <T = void>(error: unknown, expected: ExpectedErrorInfo) => {
const { code, status, messageIncludes } = expected;

if (!(error instanceof HTTPError)) {
fail('Error should be an instance of RequestError');
}

// JSON.parse returns `any`. Directly use `as` since we've already know the response body structure.
// eslint-disable-next-line no-restricted-syntax
const body = JSON.parse(String(error.response.body)) as {
const body = (await error.response.json()) as {
code: string;
message: string;
data: T;
};

expect(body.code).toEqual(code);

expect(error.response.status).toEqual(statusCode);
expect(error.response.status).toEqual(status);

if (messageIncludes) {
expect(body.message.includes(messageIncludes)).toBeTruthy();
Expand Down
2 changes: 1 addition & 1 deletion packages/integration-tests/src/helpers/interactions.ts
Expand Up @@ -80,7 +80,7 @@ export const createNewSocialUserWithUsernameAndPassword = async (connectorId: st

await expectRejects(client.submitInteraction(), {
code: 'user.identity_not_exist',
statusCode: 422,
status: 422,
});
await client.successSend(patchInteractionIdentifiers, { username, password });
await client.successSend(putInteractionProfile, { connectorId });
Expand Down
Expand Up @@ -21,7 +21,7 @@ describe('admin console user management (roles)', () => {
const m2mRole = await createRole({ type: RoleType.MachineToMachine });
await expectRejects(assignRolesToUser(user.id, [m2mRole.id]), {
code: 'user.invalid_role_type',
statusCode: 422,
status: 422,
});

await assignRolesToUser(user.id, [role1.id, role2.id]);
Expand Down
Expand Up @@ -172,7 +172,7 @@ describe('admin console user search params', () => {
['search.primaryEmail', 'jerry_swift_jr_2@geek.best'],
['search.primaryEmail', 'jerry_swift_jr_jr@gmail.com'],
]),
{ code: 'request.invalid_input', statusCode: 400, messageIncludes: '`exact`' }
{ code: 'request.invalid_input', status: 400, messageIncludes: '`exact`' }
);
});

Expand All @@ -184,7 +184,7 @@ describe('admin console user search params', () => {
]),
{
code: 'request.invalid_input',
statusCode: 400,
status: 400,
messageIncludes: 'cannot be empty',
}
);
Expand All @@ -198,7 +198,7 @@ describe('admin console user search params', () => {
]),
{
code: 'request.invalid_input',
statusCode: 400,
status: 400,
messageIncludes: 'case-insensitive',
}
);
Expand All @@ -213,13 +213,13 @@ describe('admin console user search params', () => {
]),
{
code: 'request.invalid_input',
statusCode: 400,
status: 400,
messageIncludes: 'is not valid',
}
),
expectRejects(getUsers<User[]>([['search.email', '%gmail%']]), {
code: 'request.invalid_input',
statusCode: 400,
status: 400,
messageIncludes: 'is not valid',
}),
expectRejects(
Expand All @@ -229,7 +229,7 @@ describe('admin console user search params', () => {
]),
{
code: 'request.invalid_input',
statusCode: 400,
status: 400,
messageIncludes: 'is not valid',
}
),
Expand Down
22 changes: 11 additions & 11 deletions packages/integration-tests/src/tests/api/admin-user.test.ts
Expand Up @@ -76,22 +76,22 @@ describe('admin console user management', () => {
await createUserByAdmin({ username, password, primaryEmail, primaryPhone });
await expectRejects(createUserByAdmin({ username, password }), {
code: 'user.username_already_in_use',
statusCode: 422,
status: 422,
});
await expectRejects(createUserByAdmin({ primaryEmail }), {
code: 'user.email_already_in_use',
statusCode: 422,
status: 422,
});
await expectRejects(createUserByAdmin({ primaryPhone }), {
code: 'user.phone_already_in_use',
statusCode: 422,
status: 422,
});
});

it('should fail when get user by invalid id', async () => {
await expectRejects(getUser('invalid-user-id'), {
code: 'entity.not_found',
statusCode: 404,
status: 404,
});
});

Expand Down Expand Up @@ -145,7 +145,7 @@ describe('admin console user management', () => {
const user = await createUserByAdmin();
await expectRejects(updateUser(user.id, {}), {
code: 'entity.invalid_input',
statusCode: 422,
status: 422,
});
});

Expand All @@ -160,17 +160,17 @@ describe('admin console user management', () => {

await expectRejects(updateUser(anotherUser.id, { username }), {
code: 'user.username_already_in_use',
statusCode: 422,
status: 422,
});

await expectRejects(updateUser(anotherUser.id, { primaryEmail }), {
code: 'user.email_already_in_use',
statusCode: 422,
status: 422,
});

await expectRejects(updateUser(anotherUser.id, { primaryPhone }), {
code: 'user.phone_already_in_use',
statusCode: 422,
status: 422,
});
});

Expand Down Expand Up @@ -297,15 +297,15 @@ describe('admin console user management', () => {

it('should return 204 if password is correct', async () => {
const user = await createUserByAdmin({ password: 'new_password' });
expect(await verifyUserPassword(user.id, 'new_password')).toHaveProperty('statusCode', 204);
expect(await verifyUserPassword(user.id, 'new_password')).toHaveProperty('status', 204);
await deleteUser(user.id);
});

it('should return 422 if password is incorrect', async () => {
const user = await createUserByAdmin({ password: 'new_password' });
await expectRejects(verifyUserPassword(user.id, 'wrong_password'), {
code: 'session.invalid_credentials',
statusCode: 422,
status: 422,
});
await deleteUser(user.id);
});
Expand All @@ -314,7 +314,7 @@ describe('admin console user management', () => {
const user = await createUserByAdmin();
await expectRejects(verifyUserPassword(user.id, ''), {
code: 'guard.invalid_input',
statusCode: 400,
status: 400,
});
});
});
Expand Up @@ -51,7 +51,7 @@ describe('application sign in experience', () => {
setApplicationSignInExperience('non-existent-application', applicationSignInExperiences),
{
code: 'entity.not_exists_with_id',
statusCode: 404,
status: 404,
}
);
});
Expand All @@ -64,7 +64,7 @@ describe('application sign in experience', () => {
),
{
code: 'application.third_party_application_only',
statusCode: 422,
status: 422,
}
);
});
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('application sign in experience', () => {

await expectRejects(getApplicationSignInExperience(application.id), {
code: 'entity.not_exists_with_id',
statusCode: 404,
status: 404,
});
});
});

0 comments on commit af42cb4

Please sign in to comment.