From 08c86102d8310abc16d2b51e51c7087143a23d39 Mon Sep 17 00:00:00 2001 From: giancarlo Date: Fri, 3 May 2024 23:56:23 +0700 Subject: [PATCH] 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. --- apps/e2e/tests/authentication/auth.po.ts | 33 ++++++++-------- .../e2e/tests/invitations/invitations.spec.ts | 6 ++- apps/e2e/tests/utils/mailbox.ts | 38 +++++++++++-------- apps/web/app/api/db/webhook/route.ts | 29 ++++++++------ .../members/remove-member-dialog.tsx | 1 - 5 files changed, 62 insertions(+), 45 deletions(-) diff --git a/apps/e2e/tests/authentication/auth.po.ts b/apps/e2e/tests/authentication/auth.po.ts index cc0cb2736..dc3973a4c 100644 --- a/apps/e2e/tests/authentication/auth.po.ts +++ b/apps/e2e/tests/authentication/auth.po.ts @@ -1,4 +1,5 @@ -import { expect, Page } from '@playwright/test'; +import { Page, expect } from '@playwright/test'; + import { Mailbox } from '../utils/mailbox'; export class AuthPageObject { @@ -23,10 +24,7 @@ export class AuthPageObject { await this.page.click('[data-test="account-dropdown-sign-out"]'); } - async signIn(params: { - email: string, - password: string - }) { + async signIn(params: { email: string; password: string }) { await this.page.waitForTimeout(1000); await this.page.fill('input[name="email"]', params.email); @@ -35,9 +33,9 @@ export class AuthPageObject { } async signUp(params: { - email: string, - password: string, - repeatPassword: string + email: string; + password: string; + repeatPassword: string; }) { await this.page.waitForTimeout(1000); @@ -48,14 +46,19 @@ export class AuthPageObject { await this.page.click('button[type="submit"]'); } - async visitConfirmEmailLink(email: string, params: { - deleteAfter: boolean - } = { - deleteAfter: true - }) { - return expect(async() => { + async visitConfirmEmailLink( + email: string, + params: { + deleteAfter: boolean; + } = { + deleteAfter: true, + }, + ) { + return expect(async () => { const res = await this.mailbox.visitMailbox(email, params); + console.log(res); + expect(res).not.toBeNull(); }).toPass(); } @@ -79,4 +82,4 @@ export class AuthPageObject { await this.visitConfirmEmailLink(email); } -} \ No newline at end of file +} diff --git a/apps/e2e/tests/invitations/invitations.spec.ts b/apps/e2e/tests/invitations/invitations.spec.ts index 262d9663b..6f317842d 100644 --- a/apps/e2e/tests/invitations/invitations.spec.ts +++ b/apps/e2e/tests/invitations/invitations.spec.ts @@ -97,9 +97,11 @@ test.describe('Full Invitation Flow', () => { // sign out and sign in with the first email 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({ email: firstEmail, diff --git a/apps/e2e/tests/utils/mailbox.ts b/apps/e2e/tests/utils/mailbox.ts index cd91aecb7..933e7205e 100644 --- a/apps/e2e/tests/utils/mailbox.ts +++ b/apps/e2e/tests/utils/mailbox.ts @@ -2,17 +2,17 @@ import { Page } from '@playwright/test'; import { parse } from 'node-html-parser'; export class Mailbox { - constructor( - private readonly page: Page - ) { - } + constructor(private readonly page: Page) {} - async visitMailbox(email: string, params?: { - deleteAfter: boolean - }) { + async visitMailbox( + email: string, + params: { + deleteAfter: boolean; + }, + ) { const mailbox = email.split('@')[0]; - console.log(`Visiting mailbox ${email} ...`) + console.log(`Visiting mailbox ${email} ...`); if (!mailbox) { throw new Error('Invalid email'); @@ -20,10 +20,12 @@ export class Mailbox { const json = await this.getInviteEmail(mailbox, params); - if (!json.body) { + if (!json?.body) { throw new Error('Email body was not found'); } + console.log('Email found'); + const html = (json.body as { html: string }).html; const el = parse(html); @@ -40,9 +42,9 @@ export class Mailbox { async getInviteEmail( mailbox: string, - params = { - deleteAfter: false, - } + params: { + deleteAfter: boolean; + }, ) { const url = `http://localhost:54324/api/v1/mailbox/${mailbox}`; @@ -69,11 +71,17 @@ export class Mailbox { // delete message if (params.deleteAfter) { - await fetch(messageUrl, { - method: 'DELETE' + console.log(`Deleting email ${messageId} ...`); + + const res = await fetch(messageUrl, { + method: 'DELETE', }); + + if (!res.ok) { + console.error(`Failed to delete email: ${res.statusText}`); + } } return await messageResponse.json(); } -} \ No newline at end of file +} diff --git a/apps/web/app/api/db/webhook/route.ts b/apps/web/app/api/db/webhook/route.ts index 7b10316d3..d3581d231 100644 --- a/apps/web/app/api/db/webhook/route.ts +++ b/apps/web/app/api/db/webhook/route.ts @@ -5,17 +5,22 @@ import { enhanceRouteHandler } from '@kit/next/routes'; * @name POST * @description POST handler for the webhook route that handles the webhook event */ -export const POST = enhanceRouteHandler(async ({ request }) => { - const service = getDatabaseWebhookHandlerService(); +export const POST = enhanceRouteHandler( + async ({ request }) => { + const service = getDatabaseWebhookHandlerService(); - try { - // handle the webhook event - await service.handleWebhook(request); + try { + // handle the webhook event + await service.handleWebhook(request); - // return a successful response - return new Response(null, { status: 200 }); - } catch (error) { - // return an error response - return new Response(null, { status: 500 }); - } -}); + // return a successful response + return new Response(null, { status: 200 }); + } catch (error) { + // return an error response + return new Response(null, { status: 500 }); + } + }, + { + auth: false, + }, +); diff --git a/packages/features/team-accounts/src/components/members/remove-member-dialog.tsx b/packages/features/team-accounts/src/components/members/remove-member-dialog.tsx index 9dd8ec884..2239282bb 100644 --- a/packages/features/team-accounts/src/components/members/remove-member-dialog.tsx +++ b/packages/features/team-accounts/src/components/members/remove-member-dialog.tsx @@ -89,7 +89,6 @@ function RemoveMemberForm({ data-test={'confirm-remove-member'} variant={'destructive'} disabled={isSubmitting} - onClick={onMemberRemoved} >