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 af42cb4 commit c53b1cd
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 56 deletions.
4 changes: 4 additions & 0 deletions packages/integration-tests/jest.setup.js
Expand Up @@ -9,6 +9,10 @@ dotenv.config();
/* eslint-disable @silverhand/fp/no-mutation */
global.TextDecoder = TextDecoder;
global.TextEncoder = TextEncoder;
global.fail = (message) => {
throw new Error(message);
};

/* eslint-enable @silverhand/fp/no-mutation */

// GitHub Actions default runners need more time for UI tests
Expand Down
1 change: 1 addition & 0 deletions packages/integration-tests/src/api/interaction-sso.ts
Expand Up @@ -16,6 +16,7 @@ export const getSsoAuthorizationUrl = async (
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
})
.json<{ redirectTo: string }>();
};
Expand Down
26 changes: 0 additions & 26 deletions packages/integration-tests/src/api/interaction.ts
Expand Up @@ -26,17 +26,13 @@ export const putInteraction = async (cookie: string, payload: InteractionPayload
.put('interaction', {
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
})
.json();

export const deleteInteraction = async (cookie: string) =>
api
.delete('interaction', {
headers: { cookie },
redirect: 'manual',
throwHttpErrors: false,
})
.json();

Expand All @@ -48,8 +44,6 @@ export const patchInteractionIdentifiers = async (cookie: string, payload: Ident
.patch('interaction/identifiers', {
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
})
.json();

Expand All @@ -58,8 +52,6 @@ export const patchInteractionProfile = async (cookie: string, payload: Profile)
.patch('interaction/profile', {
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
})
.json();

Expand All @@ -68,8 +60,6 @@ export const putInteractionProfile = async (cookie: string, payload: Profile) =>
.put('interaction/profile', {
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
})
.json();

Expand All @@ -78,8 +68,6 @@ export const postInteractionBindMfa = async (cookie: string, payload: BindMfaPay
.post('interaction/bind-mfa', {
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
})
.json();

Expand All @@ -88,17 +76,13 @@ export const putInteractionMfa = async (cookie: string, payload: VerifyMfaPayloa
.put('interaction/mfa', {
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
})
.json();

export const deleteInteractionProfile = async (cookie: string) =>
api
.delete('interaction/profile', {
headers: { cookie },
redirect: 'manual',
throwHttpErrors: false,
})
.json();

Expand All @@ -114,8 +98,6 @@ export const sendVerificationCode = async (
api.post('interaction/verification/verification-code', {
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
});

export type SocialAuthorizationUriPayload = {
Expand All @@ -131,8 +113,6 @@ export const createSocialAuthorizationUri = async (
api.post('interaction/verification/social-authorization-uri', {
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
});

export const initTotp = async (cookie: string) =>
Expand All @@ -151,8 +131,6 @@ export const skipMfaBinding = async (cookie: string) =>
json: {
mfaSkipped: true,
},
redirect: 'manual',
throwHttpErrors: false,
});

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

Expand All @@ -182,6 +158,4 @@ export const createSingleSignOnAuthorizationUri = async (
api.post('interaction/verification/sso-authorization-uri', {
headers: { cookie },
json: payload,
redirect: 'manual',
throwHttpErrors: false,
});
Expand Up @@ -9,11 +9,11 @@ import { UserApiTest } from '#src/helpers/user.js';
const getUsers = async <T>(
init: string[][] | Record<string, string> | URLSearchParams
): Promise<{ headers: Headers; json: T }> => {
const { headers, json } = await authedAdminApi.get('users', {
const response = await authedAdminApi.get('users', {
searchParams: new URLSearchParams(init),
});

return { headers, json: (await json()) as T };
return { headers: response.headers, json: (await response.json()) as T };
};

describe('admin console user search params', () => {
Expand Down
@@ -1,10 +1,15 @@
import { InteractionEvent } from '@logto/schemas';
import { ConnectorType, InteractionEvent } from '@logto/schemas';

import { putInteraction, sendVerificationCode } from '#src/api/interaction.js';
import { initClient } from '#src/helpers/client.js';
import { clearConnectorsByTypes } from '#src/helpers/connector.js';
import { expectRejects } from '#src/helpers/index.js';
import { generateEmail, generatePhone } from '#src/utils.js';

beforeAll(async () => {
await clearConnectorsByTypes([ConnectorType.Email, ConnectorType.Sms]);
});

/**
* Note: These test cases are designed to cover exceptional scenarios of API calls that
* cannot be covered within the auth flow.
Expand Down
Expand Up @@ -20,14 +20,11 @@ describe('content-type: application/json compatibility', () => {
headers,
json: payload,
}),
(error) => {
async (error) => {
if (!(error instanceof HTTPError)) {
throw new TypeError('Error is not a HTTPError instance.');
}
expect(JSON.parse(String(error.response.body))).toHaveProperty(
'error_description',
errorMessage
);
expect(await error.response.json()).toHaveProperty('error_description', errorMessage);
}
);
};
Expand Down
Expand Up @@ -68,7 +68,7 @@ class MockOrganizationClient extends MockClient {
return json;
} catch (error) {
if (error instanceof HTTPError) {
throw new GrantError(error.response.status, JSON.parse(String(error.response.body)));
throw new GrantError(error.response.status, await error.response.json());
}
throw error;
}
Expand Down
Expand Up @@ -11,12 +11,10 @@ import { OrganizationApiTest, OrganizationInvitationApiTest } from '#src/helpers

const randomId = () => generateStandardId(4);

const expectErrorResponse = (error: unknown, statusCode: number, code: string) => {
const expectErrorResponse = async (error: unknown, statusCode: number, code: string) => {
assert(error instanceof HTTPError);
const { status, body: raw } = error.response;
const body: unknown = JSON.parse(String(raw));
expect(status).toBe(statusCode);
expect(body).toMatchObject({ code });
expect(error.response.status).toBe(statusCode);
expect(await error.response.json()).toMatchObject({ code });
};

describe('organization invitation creation', () => {
Expand Down Expand Up @@ -105,7 +103,7 @@ describe('organization invitation creation', () => {
})
.catch((error: unknown) => error);

expectErrorResponse(error, 501, 'connector.not_found');
await expectErrorResponse(error, 501, 'connector.not_found');
});

it('should not be able to create invitations with the same email', async () => {
Expand All @@ -124,7 +122,7 @@ describe('organization invitation creation', () => {
})
.catch((error: unknown) => error);

expectErrorResponse(error, 422, 'entity.unique_integrity_violation');
await expectErrorResponse(error, 422, 'entity.unique_integrity_violation');
});

it('should be able to create invitations with the same email for different organizations', async () => {
Expand Down Expand Up @@ -157,7 +155,7 @@ describe('organization invitation creation', () => {
})
.catch((error: unknown) => error);

expectErrorResponse(error, 400, 'request.invalid_input');
await expectErrorResponse(error, 400, 'request.invalid_input');
});

it('should not be able to create invitations if the invitee is already a member of the organization', async () => {
Expand All @@ -174,7 +172,7 @@ describe('organization invitation creation', () => {
})
.catch((error: unknown) => error);

expectErrorResponse(error, 422, 'request.invalid_input');
await expectErrorResponse(error, 422, 'request.invalid_input');
});

it('should not be able to create invitations with an invalid email', async () => {
Expand All @@ -187,7 +185,7 @@ describe('organization invitation creation', () => {
})
.catch((error: unknown) => error);

expectErrorResponse(error, 400, 'guard.invalid_input');
await expectErrorResponse(error, 400, 'guard.invalid_input');
});

it('should not be able to create invitations with an invalid organization id', async () => {
Expand All @@ -199,7 +197,7 @@ describe('organization invitation creation', () => {
})
.catch((error: unknown) => error);

expectErrorResponse(error, 422, 'entity.relation_foreign_key_not_found');
await expectErrorResponse(error, 422, 'entity.relation_foreign_key_not_found');
});

it('should be able to create invitations with organization role ids', async () => {
Expand Down
Expand Up @@ -9,12 +9,10 @@ import { UserApiTest } from '#src/helpers/user.js';

const randomId = () => generateStandardId(4);

const expectErrorResponse = (error: unknown, statusCode: number, code: string) => {
const expectErrorResponse = async (error: unknown, statusCode: number, code: string) => {
assert(error instanceof HTTPError);
const { status, body: raw } = error.response;
const body: unknown = JSON.parse(String(raw));
expect(status).toBe(statusCode);
expect(body).toMatchObject({ code });
expect(error.response.status).toBe(statusCode);
expect(await error.response.json()).toMatchObject({ code });
};

describe('organization invitation status update', () => {
Expand Down Expand Up @@ -50,7 +48,7 @@ describe('organization invitation status update', () => {
const error = await invitationApi
.updateStatus(invitation.id, OrganizationInvitationStatus.Accepted)
.catch((error: unknown) => error);
expectErrorResponse(error, 422, 'request.invalid_input');
await expectErrorResponse(error, 422, 'request.invalid_input');
});

it('should be able to accept an invitation', async () => {
Expand Down Expand Up @@ -130,7 +128,7 @@ describe('organization invitation status update', () => {
.updateStatus(invitation.id, OrganizationInvitationStatus.Accepted, user.id)
.catch((error: unknown) => error);

expectErrorResponse(error, 422, 'request.invalid_input');
await expectErrorResponse(error, 422, 'request.invalid_input');
});

it('should not be able to accept an invitation with an invalid user id', async () => {
Expand All @@ -146,7 +144,7 @@ describe('organization invitation status update', () => {
.updateStatus(invitation.id, OrganizationInvitationStatus.Accepted, 'invalid')
.catch((error: unknown) => error);

expectErrorResponse(error, 404, 'entity.not_found');
await expectErrorResponse(error, 404, 'entity.not_found');
});

it('should not be able to update the status of an ended invitation', async () => {
Expand All @@ -164,6 +162,6 @@ describe('organization invitation status update', () => {
.updateStatus(invitation.id, OrganizationInvitationStatus.Accepted)
.catch((error: unknown) => error);

expectErrorResponse(error, 422, 'request.invalid_input');
await expectErrorResponse(error, 422, 'request.invalid_input');
});
});
Expand Up @@ -22,7 +22,7 @@ describe('organization scope APIs', () => {

assert(response instanceof HTTPError);

const { status: status, body: raw } = response.response;
const { status, body: raw } = response.response;
const body: unknown = JSON.parse(String(raw));
expect(status).toBe(422);
expect(isKeyInObject(body, 'code') && body.code).toBe('entity.unique_integrity_violation');
Expand Down

0 comments on commit c53b1cd

Please sign in to comment.