Skip to content

Commit c3dcbd6

Browse files
authored
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 310246b. * 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
1 parent b018b16 commit c3dcbd6

File tree

10 files changed

+52
-34
lines changed

10 files changed

+52
-34
lines changed

.env.example

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ PUBLIC_SITE_URL=http://localhost:5173
2121

2222
PUBLIC_RECAPTCHA_SITE_KEY=
2323
RECAPTCHA_SECRET_KEY=
24-
#RECAPTCHA_DISABLED=true
24+
PUBLIC_RECAPTCHA_ENABLED=true

.github/workflows/test.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ jobs:
3535
ORIGIN: ${{ vars.ORIGIN }}
3636
SECRET_KEY: ${{ secrets.SECRET_KEY }}
3737
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
38-
PUBLIC_RECAPTCHA_SITE_KEY: ${{ secrets.PUBLIC_RECAPTCHA_SITE_KEY }}
39-
RECAPTCHA_SECRET_KEY: ${{ secrets.RECAPTCHA_SECRET_KEY }}
40-
RECAPTCHA_DISABLED: true
38+
PUBLIC_RECAPTCHA_SITE_KEY: 'any-value'
39+
RECAPTCHA_SECRET_KEY: 'any-value'
40+
PUBLIC_RECAPTCHA_ENABLED: false
4141
steps:
4242
- name: Checkout repository
4343
uses: actions/checkout@v4

src/lib/utils/recaptcha.client.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PUBLIC_RECAPTCHA_SITE_KEY } from '$env/static/public'
1+
import { PUBLIC_RECAPTCHA_SITE_KEY, PUBLIC_RECAPTCHA_ENABLED } from '$env/static/public'
22

33
export interface ReCaptcha {
44
ready(callback: () => void): void
@@ -30,3 +30,5 @@ export const executeRecaptcha = async (grecaptcha: ReCaptcha): Promise<string> =
3030
})
3131
})
3232
}
33+
34+
export const isRecaptchaEnabled = PUBLIC_RECAPTCHA_ENABLED === 'true'

src/lib/utils/recaptcha.server.ts

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { RECAPTCHA_SECRET_KEY } from '$env/static/private'
2-
import { env } from '$env/dynamic/private'
32

43
export async function verifyRecaptcha(response: string) {
5-
if (env.RECAPTCHA_DISABLED === 'true') return
6-
74
const recaptchaResponse = await fetch('https://www.google.com/recaptcha/api/siteverify', {
85
method: 'POST',
96
headers: {

src/routes/forgot-password/+page.server.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { fail } from '@sveltejs/kit'
55
import { z, ZodError } from 'zod'
66
import type { Actions } from './$types'
77
import { verifyRecaptcha } from '$lib/utils/recaptcha.server'
8+
import { isRecaptchaEnabled } from '$lib/utils/recaptcha.client'
89

910
export const actions: Actions = {
1011
default: async ({ request, url }) => {
@@ -13,11 +14,11 @@ export const actions: Actions = {
1314
const schema = z
1415
.object({
1516
email: z.string().email().toLowerCase(),
16-
recaptchaToken: z.string().min(1),
17+
recaptchaToken: z.string().optional(),
1718
})
1819
.parse(fields)
1920

20-
await verifyRecaptcha(schema.recaptchaToken)
21+
if (isRecaptchaEnabled) await verifyRecaptcha(schema.recaptchaToken || '')
2122

2223
let user = await getUserByEmail(schema.email)
2324

src/routes/forgot-password/+page.svelte

+11-6
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55
import TacoIcon from '$lib/components/icons/TacoIcon.svelte'
66
import type { ActionData } from './$types'
77
import { PUBLIC_RECAPTCHA_SITE_KEY } from '$env/static/public'
8-
import { executeRecaptcha } from '$lib/utils/recaptcha.client'
8+
import { executeRecaptcha, isRecaptchaEnabled } from '$lib/utils/recaptcha.client'
99
1010
export let form: ActionData
1111
</script>
1212

1313
<svelte:head>
1414
<title>Forgot Password</title>
15-
<script
16-
src={`https://www.google.com/recaptcha/api.js?render=${PUBLIC_RECAPTCHA_SITE_KEY}`}
17-
></script>
15+
{#if isRecaptchaEnabled}
16+
<script
17+
src={`https://www.google.com/recaptcha/api.js?render=${PUBLIC_RECAPTCHA_SITE_KEY}`}
18+
></script>
19+
{/if}
1820
</svelte:head>
1921

2022
<div
@@ -43,8 +45,11 @@
4345
method="POST"
4446
novalidate
4547
use:enhance={async ({ formData }) => {
46-
const recaptchaToken = await executeRecaptcha(window.grecaptcha)
47-
formData.append('recaptchaToken', recaptchaToken)
48+
if (isRecaptchaEnabled) {
49+
const recaptchaToken = await executeRecaptcha(window.grecaptcha)
50+
formData.append('recaptchaToken', recaptchaToken)
51+
}
52+
4853
return async ({ update }) => {
4954
return update({ reset: false })
5055
}

src/routes/signin/+page.server.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { verifyRecaptcha } from '$lib/utils/recaptcha.server'
77
import { fail, redirect } from '@sveltejs/kit'
88
import { z, ZodError } from 'zod'
99
import type { Actions } from './$types'
10+
import { isRecaptchaEnabled } from '$lib/utils/recaptcha.client'
1011

1112
export const actions: Actions = {
1213
default: async ({ request, cookies, url }) => {
@@ -17,15 +18,16 @@ export const actions: Actions = {
1718
email: z.string().email(),
1819
password: z.string(),
1920
remember: z.preprocess((value) => value === 'on', z.boolean()),
20-
recaptchaToken: z.string().min(1),
21+
recaptchaToken: z.string().optional(),
2122
})
2223
.refine(async (data) => doesCredentialsMatch(data.email, data.password), {
2324
message: 'Wrong credentials',
2425
path: ['password'],
2526
})
2627
.parseAsync(fields)
2728

28-
await verifyRecaptcha(schema.recaptchaToken)
29+
if (isRecaptchaEnabled) await verifyRecaptcha(schema.recaptchaToken || '')
30+
2931
const user = await getUserByEmail(schema.email)
3032

3133
if (!user) {

src/routes/signin/+page.svelte

+12-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import Alert from '$lib/components/Alert.svelte'
44
import Input from '$lib/components/Input.svelte'
55
import TacoIcon from '$lib/components/icons/TacoIcon.svelte'
6-
import { executeRecaptcha } from '$lib/utils/recaptcha.client'
6+
import { executeRecaptcha, isRecaptchaEnabled } from "$lib/utils/recaptcha.client";
77
import type { ActionData } from './$types'
88
import { PUBLIC_RECAPTCHA_SITE_KEY } from '$env/static/public'
99
import Spinner from '$lib/components/Spinner.svelte'
@@ -14,9 +14,11 @@
1414

1515
<svelte:head>
1616
<title>Sign in</title>
17-
<script
18-
src={`https://www.google.com/recaptcha/api.js?render=${PUBLIC_RECAPTCHA_SITE_KEY}`}
19-
></script>
17+
{#if isRecaptchaEnabled}
18+
<script
19+
src={`https://www.google.com/recaptcha/api.js?render=${PUBLIC_RECAPTCHA_SITE_KEY}`}
20+
></script>
21+
{/if}
2022
</svelte:head>
2123

2224
<div class="min-h-screen bg-gray-50 dark:bg-gray-900 flex flex-col justify-center sm:px-6 lg:px-8">
@@ -43,9 +45,12 @@
4345
method="POST"
4446
novalidate
4547
use:enhance={async ({ formData }) => {
46-
const recaptchaToken = await executeRecaptcha(window.grecaptcha)
47-
formLoading = true
48-
formData.append('recaptchaToken', recaptchaToken)
48+
if (isRecaptchaEnabled) {
49+
const recaptchaToken = await executeRecaptcha(window.grecaptcha)
50+
formLoading = true
51+
formData.append('recaptchaToken', recaptchaToken)
52+
}
53+
4954
return async ({ update }) => {
5055
formLoading = false
5156
return update({ reset: false })

src/routes/signup/+page.server.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'
55
import { fail, redirect } from '@sveltejs/kit'
66
import { z, ZodError } from 'zod'
77
import type { Actions } from './$types'
8+
import { isRecaptchaEnabled } from '$lib/utils/recaptcha.client'
89

910
export const actions: Actions = {
1011
default: async ({ request, cookies, url }) => {
@@ -17,15 +18,15 @@ export const actions: Actions = {
1718
email: z.string().email().toLowerCase(),
1819
password: z.string().min(6),
1920
confirmPassword: z.string().min(6),
20-
recaptchaToken: z.string().min(1),
21+
recaptchaToken: z.string().optional(),
2122
})
2223
.refine((data) => data.password === data.confirmPassword, {
2324
message: "Passwords don't match",
2425
path: ['confirmPassword'],
2526
})
2627
.parse(fields)
2728

28-
await verifyRecaptcha(schema.recaptchaToken)
29+
if (isRecaptchaEnabled) await verifyRecaptcha(schema.recaptchaToken || '')
2930

3031
const user = await createUser(schema.name, schema.email, schema.password)
3132

src/routes/signup/+page.svelte

+12-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import Alert from '$lib/components/Alert.svelte'
44
import TacoIcon from '$lib/components/icons/TacoIcon.svelte'
55
import Input from '$lib/components/Input.svelte'
6-
import { executeRecaptcha } from '$lib/utils/recaptcha.client'
6+
import { executeRecaptcha, isRecaptchaEnabled } from '$lib/utils/recaptcha.client'
77
import type { ActionData } from './$types'
88
import { PUBLIC_RECAPTCHA_SITE_KEY } from '$env/static/public'
99
import Spinner from '$lib/components/Spinner.svelte'
@@ -14,9 +14,11 @@
1414

1515
<svelte:head>
1616
<title>Signup</title>
17-
<script
18-
src={`https://www.google.com/recaptcha/api.js?render=${PUBLIC_RECAPTCHA_SITE_KEY}`}
19-
></script>
17+
{#if isRecaptchaEnabled}
18+
<script
19+
src={`https://www.google.com/recaptcha/api.js?render=${PUBLIC_RECAPTCHA_SITE_KEY}`}
20+
></script>
21+
{/if}
2022
</svelte:head>
2123

2224
<div class="min-h-screen bg-gray-50 dark:bg-gray-900 flex flex-col justify-center sm:px-6 lg:px-8">
@@ -43,9 +45,12 @@
4345
method="POST"
4446
novalidate
4547
use:enhance={async ({ formData }) => {
46-
const recaptchaToken = await executeRecaptcha(window.grecaptcha)
47-
formLoading = true
48-
formData.append('recaptchaToken', recaptchaToken)
48+
if (isRecaptchaEnabled) {
49+
const recaptchaToken = await executeRecaptcha(window.grecaptcha)
50+
formLoading = true
51+
formData.append('recaptchaToken', recaptchaToken)
52+
}
53+
4954
return async ({ update }) => {
5055
formLoading = false
5156
return update({ reset: false })

0 commit comments

Comments
 (0)