From c3dcbd63a6aaeb7cf7dc20b815cdb30e59fe0603 Mon Sep 17 00:00:00 2001 From: Cr0s4k Date: Fri, 10 Jan 2025 12:45:20 +0100 Subject: [PATCH] Malibu-73/ Bug: Fix failing tests (#244) * upgrade playwright * RECAPTCHA_DISABLED env var added * added missing static env vars * remove sentry config from vite file * added correct env vars back * Revert "remove sentry config from vite file" This reverts commit 310246b42d6ff5b5ca5501c225ce3c06968059a4. * remove recaptcha from client if PUBLIC_RECAPTCHA_ENABLED false * update env vars from flow and .env.example * fix validation * rm RECAPTCHA_DISABLED usage * use isRecaptchaEnabled * recaptcha env vars can be any value in ci:test --- .env.example | 2 +- .github/workflows/test.yml | 6 +++--- src/lib/utils/recaptcha.client.ts | 4 +++- src/lib/utils/recaptcha.server.ts | 3 --- src/routes/forgot-password/+page.server.ts | 5 +++-- src/routes/forgot-password/+page.svelte | 17 +++++++++++------ src/routes/signin/+page.server.ts | 6 ++++-- src/routes/signin/+page.svelte | 19 ++++++++++++------- src/routes/signup/+page.server.ts | 5 +++-- src/routes/signup/+page.svelte | 19 ++++++++++++------- 10 files changed, 52 insertions(+), 34 deletions(-) diff --git a/.env.example b/.env.example index 8006c6d3..92fc7e60 100644 --- a/.env.example +++ b/.env.example @@ -21,4 +21,4 @@ PUBLIC_SITE_URL=http://localhost:5173 PUBLIC_RECAPTCHA_SITE_KEY= RECAPTCHA_SECRET_KEY= -#RECAPTCHA_DISABLED=true +PUBLIC_RECAPTCHA_ENABLED=true diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 82c86602..4fe0089e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -35,9 +35,9 @@ jobs: ORIGIN: ${{ vars.ORIGIN }} SECRET_KEY: ${{ secrets.SECRET_KEY }} OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} - PUBLIC_RECAPTCHA_SITE_KEY: ${{ secrets.PUBLIC_RECAPTCHA_SITE_KEY }} - RECAPTCHA_SECRET_KEY: ${{ secrets.RECAPTCHA_SECRET_KEY }} - RECAPTCHA_DISABLED: true + PUBLIC_RECAPTCHA_SITE_KEY: 'any-value' + RECAPTCHA_SECRET_KEY: 'any-value' + PUBLIC_RECAPTCHA_ENABLED: false steps: - name: Checkout repository uses: actions/checkout@v4 diff --git a/src/lib/utils/recaptcha.client.ts b/src/lib/utils/recaptcha.client.ts index eba696cc..e27ab11f 100644 --- a/src/lib/utils/recaptcha.client.ts +++ b/src/lib/utils/recaptcha.client.ts @@ -1,4 +1,4 @@ -import { PUBLIC_RECAPTCHA_SITE_KEY } from '$env/static/public' +import { PUBLIC_RECAPTCHA_SITE_KEY, PUBLIC_RECAPTCHA_ENABLED } from '$env/static/public' export interface ReCaptcha { ready(callback: () => void): void @@ -30,3 +30,5 @@ export const executeRecaptcha = async (grecaptcha: ReCaptcha): Promise = }) }) } + +export const isRecaptchaEnabled = PUBLIC_RECAPTCHA_ENABLED === 'true' diff --git a/src/lib/utils/recaptcha.server.ts b/src/lib/utils/recaptcha.server.ts index 9082c948..363377aa 100644 --- a/src/lib/utils/recaptcha.server.ts +++ b/src/lib/utils/recaptcha.server.ts @@ -1,9 +1,6 @@ import { RECAPTCHA_SECRET_KEY } from '$env/static/private' -import { env } from '$env/dynamic/private' export async function verifyRecaptcha(response: string) { - if (env.RECAPTCHA_DISABLED === 'true') return - const recaptchaResponse = await fetch('https://www.google.com/recaptcha/api/siteverify', { method: 'POST', headers: { diff --git a/src/routes/forgot-password/+page.server.ts b/src/routes/forgot-password/+page.server.ts index 3158391b..2b69d431 100644 --- a/src/routes/forgot-password/+page.server.ts +++ b/src/routes/forgot-password/+page.server.ts @@ -5,6 +5,7 @@ import { fail } from '@sveltejs/kit' import { z, ZodError } from 'zod' import type { Actions } from './$types' import { verifyRecaptcha } from '$lib/utils/recaptcha.server' +import { isRecaptchaEnabled } from '$lib/utils/recaptcha.client' export const actions: Actions = { default: async ({ request, url }) => { @@ -13,11 +14,11 @@ export const actions: Actions = { const schema = z .object({ email: z.string().email().toLowerCase(), - recaptchaToken: z.string().min(1), + recaptchaToken: z.string().optional(), }) .parse(fields) - await verifyRecaptcha(schema.recaptchaToken) + if (isRecaptchaEnabled) await verifyRecaptcha(schema.recaptchaToken || '') let user = await getUserByEmail(schema.email) diff --git a/src/routes/forgot-password/+page.svelte b/src/routes/forgot-password/+page.svelte index 667b4b7c..c277eed1 100644 --- a/src/routes/forgot-password/+page.svelte +++ b/src/routes/forgot-password/+page.svelte @@ -5,16 +5,18 @@ import TacoIcon from '$lib/components/icons/TacoIcon.svelte' import type { ActionData } from './$types' import { PUBLIC_RECAPTCHA_SITE_KEY } from '$env/static/public' - import { executeRecaptcha } from '$lib/utils/recaptcha.client' + import { executeRecaptcha, isRecaptchaEnabled } from '$lib/utils/recaptcha.client' export let form: ActionData Forgot Password - + {#if isRecaptchaEnabled} + + {/if}
{ - const recaptchaToken = await executeRecaptcha(window.grecaptcha) - formData.append('recaptchaToken', recaptchaToken) + if (isRecaptchaEnabled) { + const recaptchaToken = await executeRecaptcha(window.grecaptcha) + formData.append('recaptchaToken', recaptchaToken) + } + return async ({ update }) => { return update({ reset: false }) } diff --git a/src/routes/signin/+page.server.ts b/src/routes/signin/+page.server.ts index 5e831d68..f0c0a88c 100644 --- a/src/routes/signin/+page.server.ts +++ b/src/routes/signin/+page.server.ts @@ -7,6 +7,7 @@ import { verifyRecaptcha } from '$lib/utils/recaptcha.server' import { fail, redirect } from '@sveltejs/kit' import { z, ZodError } from 'zod' import type { Actions } from './$types' +import { isRecaptchaEnabled } from '$lib/utils/recaptcha.client' export const actions: Actions = { default: async ({ request, cookies, url }) => { @@ -17,7 +18,7 @@ export const actions: Actions = { email: z.string().email(), password: z.string(), remember: z.preprocess((value) => value === 'on', z.boolean()), - recaptchaToken: z.string().min(1), + recaptchaToken: z.string().optional(), }) .refine(async (data) => doesCredentialsMatch(data.email, data.password), { message: 'Wrong credentials', @@ -25,7 +26,8 @@ export const actions: Actions = { }) .parseAsync(fields) - await verifyRecaptcha(schema.recaptchaToken) + if (isRecaptchaEnabled) await verifyRecaptcha(schema.recaptchaToken || '') + const user = await getUserByEmail(schema.email) if (!user) { diff --git a/src/routes/signin/+page.svelte b/src/routes/signin/+page.svelte index 22b34193..0961b548 100644 --- a/src/routes/signin/+page.svelte +++ b/src/routes/signin/+page.svelte @@ -3,7 +3,7 @@ import Alert from '$lib/components/Alert.svelte' import Input from '$lib/components/Input.svelte' import TacoIcon from '$lib/components/icons/TacoIcon.svelte' - import { executeRecaptcha } from '$lib/utils/recaptcha.client' + import { executeRecaptcha, isRecaptchaEnabled } from "$lib/utils/recaptcha.client"; import type { ActionData } from './$types' import { PUBLIC_RECAPTCHA_SITE_KEY } from '$env/static/public' import Spinner from '$lib/components/Spinner.svelte' @@ -14,9 +14,11 @@ Sign in - + {#if isRecaptchaEnabled} + + {/if}
@@ -43,9 +45,12 @@ method="POST" novalidate use:enhance={async ({ formData }) => { - const recaptchaToken = await executeRecaptcha(window.grecaptcha) - formLoading = true - formData.append('recaptchaToken', recaptchaToken) + if (isRecaptchaEnabled) { + const recaptchaToken = await executeRecaptcha(window.grecaptcha) + formLoading = true + formData.append('recaptchaToken', recaptchaToken) + } + return async ({ update }) => { formLoading = false return update({ reset: false }) diff --git a/src/routes/signup/+page.server.ts b/src/routes/signup/+page.server.ts index b59fbf41..17b52c2f 100644 --- a/src/routes/signup/+page.server.ts +++ b/src/routes/signup/+page.server.ts @@ -5,6 +5,7 @@ import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library' import { fail, redirect } from '@sveltejs/kit' import { z, ZodError } from 'zod' import type { Actions } from './$types' +import { isRecaptchaEnabled } from '$lib/utils/recaptcha.client' export const actions: Actions = { default: async ({ request, cookies, url }) => { @@ -17,7 +18,7 @@ export const actions: Actions = { email: z.string().email().toLowerCase(), password: z.string().min(6), confirmPassword: z.string().min(6), - recaptchaToken: z.string().min(1), + recaptchaToken: z.string().optional(), }) .refine((data) => data.password === data.confirmPassword, { message: "Passwords don't match", @@ -25,7 +26,7 @@ export const actions: Actions = { }) .parse(fields) - await verifyRecaptcha(schema.recaptchaToken) + if (isRecaptchaEnabled) await verifyRecaptcha(schema.recaptchaToken || '') const user = await createUser(schema.name, schema.email, schema.password) diff --git a/src/routes/signup/+page.svelte b/src/routes/signup/+page.svelte index c37a14da..2695464a 100644 --- a/src/routes/signup/+page.svelte +++ b/src/routes/signup/+page.svelte @@ -3,7 +3,7 @@ import Alert from '$lib/components/Alert.svelte' import TacoIcon from '$lib/components/icons/TacoIcon.svelte' import Input from '$lib/components/Input.svelte' - import { executeRecaptcha } from '$lib/utils/recaptcha.client' + import { executeRecaptcha, isRecaptchaEnabled } from '$lib/utils/recaptcha.client' import type { ActionData } from './$types' import { PUBLIC_RECAPTCHA_SITE_KEY } from '$env/static/public' import Spinner from '$lib/components/Spinner.svelte' @@ -14,9 +14,11 @@ Signup - + {#if isRecaptchaEnabled} + + {/if}
@@ -43,9 +45,12 @@ method="POST" novalidate use:enhance={async ({ formData }) => { - const recaptchaToken = await executeRecaptcha(window.grecaptcha) - formLoading = true - formData.append('recaptchaToken', recaptchaToken) + if (isRecaptchaEnabled) { + const recaptchaToken = await executeRecaptcha(window.grecaptcha) + formLoading = true + formData.append('recaptchaToken', recaptchaToken) + } + return async ({ update }) => { formLoading = false return update({ reset: false })