Refactor code and improve error handling in authentication methods

This commit contains several changes including the removal of an unused onClick event handler in remove-member-dialog.tsx. It also includes an update to the POST handler in the Webhook route of the database API where the authentication property has been updated. Lastly, it also brings improvements in error handling and logging in various areas, such as e2e authentication tests and the mailbox utility.
This commit is contained in:
giancarlo
2024-05-03 23:56:23 +07:00
parent e159c52d41
commit 08c86102d8
5 changed files with 62 additions and 45 deletions

View File

@@ -1,4 +1,5 @@
import { expect, Page } from '@playwright/test'; import { Page, expect } from '@playwright/test';
import { Mailbox } from '../utils/mailbox'; import { Mailbox } from '../utils/mailbox';
export class AuthPageObject { export class AuthPageObject {
@@ -23,10 +24,7 @@ export class AuthPageObject {
await this.page.click('[data-test="account-dropdown-sign-out"]'); await this.page.click('[data-test="account-dropdown-sign-out"]');
} }
async signIn(params: { async signIn(params: { email: string; password: string }) {
email: string,
password: string
}) {
await this.page.waitForTimeout(1000); await this.page.waitForTimeout(1000);
await this.page.fill('input[name="email"]', params.email); await this.page.fill('input[name="email"]', params.email);
@@ -35,9 +33,9 @@ export class AuthPageObject {
} }
async signUp(params: { async signUp(params: {
email: string, email: string;
password: string, password: string;
repeatPassword: string repeatPassword: string;
}) { }) {
await this.page.waitForTimeout(1000); await this.page.waitForTimeout(1000);
@@ -48,14 +46,19 @@ export class AuthPageObject {
await this.page.click('button[type="submit"]'); await this.page.click('button[type="submit"]');
} }
async visitConfirmEmailLink(email: string, params: { async visitConfirmEmailLink(
deleteAfter: boolean email: string,
} = { params: {
deleteAfter: true deleteAfter: boolean;
}) { } = {
return expect(async() => { deleteAfter: true,
},
) {
return expect(async () => {
const res = await this.mailbox.visitMailbox(email, params); const res = await this.mailbox.visitMailbox(email, params);
console.log(res);
expect(res).not.toBeNull(); expect(res).not.toBeNull();
}).toPass(); }).toPass();
} }
@@ -79,4 +82,4 @@ export class AuthPageObject {
await this.visitConfirmEmailLink(email); await this.visitConfirmEmailLink(email);
} }
} }

View File

@@ -97,9 +97,11 @@ test.describe('Full Invitation Flow', () => {
// sign out and sign in with the first email // sign out and sign in with the first email
await invitations.auth.signOut(); await invitations.auth.signOut();
await invitations.auth.visitConfirmEmailLink(invites[0]!.email); console.log(`Finding email to ${firstEmail} ...`);
console.log(`Signing up with ${firstEmail}`); await invitations.auth.visitConfirmEmailLink(firstEmail);
console.log(`Signing up with ${firstEmail} ...`);
await invitations.auth.signUp({ await invitations.auth.signUp({
email: firstEmail, email: firstEmail,

View File

@@ -2,17 +2,17 @@ import { Page } from '@playwright/test';
import { parse } from 'node-html-parser'; import { parse } from 'node-html-parser';
export class Mailbox { export class Mailbox {
constructor( constructor(private readonly page: Page) {}
private readonly page: Page
) {
}
async visitMailbox(email: string, params?: { async visitMailbox(
deleteAfter: boolean email: string,
}) { params: {
deleteAfter: boolean;
},
) {
const mailbox = email.split('@')[0]; const mailbox = email.split('@')[0];
console.log(`Visiting mailbox ${email} ...`) console.log(`Visiting mailbox ${email} ...`);
if (!mailbox) { if (!mailbox) {
throw new Error('Invalid email'); throw new Error('Invalid email');
@@ -20,10 +20,12 @@ export class Mailbox {
const json = await this.getInviteEmail(mailbox, params); const json = await this.getInviteEmail(mailbox, params);
if (!json.body) { if (!json?.body) {
throw new Error('Email body was not found'); throw new Error('Email body was not found');
} }
console.log('Email found');
const html = (json.body as { html: string }).html; const html = (json.body as { html: string }).html;
const el = parse(html); const el = parse(html);
@@ -40,9 +42,9 @@ export class Mailbox {
async getInviteEmail( async getInviteEmail(
mailbox: string, mailbox: string,
params = { params: {
deleteAfter: false, deleteAfter: boolean;
} },
) { ) {
const url = `http://localhost:54324/api/v1/mailbox/${mailbox}`; const url = `http://localhost:54324/api/v1/mailbox/${mailbox}`;
@@ -69,11 +71,17 @@ export class Mailbox {
// delete message // delete message
if (params.deleteAfter) { if (params.deleteAfter) {
await fetch(messageUrl, { console.log(`Deleting email ${messageId} ...`);
method: 'DELETE'
const res = await fetch(messageUrl, {
method: 'DELETE',
}); });
if (!res.ok) {
console.error(`Failed to delete email: ${res.statusText}`);
}
} }
return await messageResponse.json(); return await messageResponse.json();
} }
} }

View File

@@ -5,17 +5,22 @@ import { enhanceRouteHandler } from '@kit/next/routes';
* @name POST * @name POST
* @description POST handler for the webhook route that handles the webhook event * @description POST handler for the webhook route that handles the webhook event
*/ */
export const POST = enhanceRouteHandler(async ({ request }) => { export const POST = enhanceRouteHandler(
const service = getDatabaseWebhookHandlerService(); async ({ request }) => {
const service = getDatabaseWebhookHandlerService();
try { try {
// handle the webhook event // handle the webhook event
await service.handleWebhook(request); await service.handleWebhook(request);
// return a successful response // return a successful response
return new Response(null, { status: 200 }); return new Response(null, { status: 200 });
} catch (error) { } catch (error) {
// return an error response // return an error response
return new Response(null, { status: 500 }); return new Response(null, { status: 500 });
} }
}); },
{
auth: false,
},
);

View File

@@ -89,7 +89,6 @@ function RemoveMemberForm({
data-test={'confirm-remove-member'} data-test={'confirm-remove-member'}
variant={'destructive'} variant={'destructive'}
disabled={isSubmitting} disabled={isSubmitting}
onClick={onMemberRemoved}
> >
<Trans i18nKey={'teams:removeMemberSubmitLabel'} /> <Trans i18nKey={'teams:removeMemberSubmitLabel'} />
</Button> </Button>