Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reCAPTCHA support and update captcha provider handling #647

Merged
merged 10 commits into from
Dec 18, 2024
Merged
3 changes: 3 additions & 0 deletions api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ STRIPE_TEST_DEFAULT_PRICING_YEARLY=
H_CAPTCHA_SITE_KEY=
H_CAPTCHA_SECRET_KEY=

RE_CAPTCHA_SITE_KEY=
RE_CAPTCHA_SECRET_KEY=
Comment on lines +75 to +76
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Documentation needed for reCAPTCHA environment variables

The new reCAPTCHA environment variables are not documented in the environment variables configuration file (docs/configuration/environment-variables.mdx). Please add the following to the Configuration Environment Variables table:

  • RE_CAPTCHA_SITE_KEY: Site key for reCAPTCHA integration
  • RE_CAPTCHA_SECRET_KEY: Secret key for reCAPTCHA integration
🔗 Analysis chain

Verify documentation for new reCAPTCHA configuration

The environment variables are correctly added. Please ensure these new variables are documented in:

  1. Installation guide
  2. Configuration documentation
  3. Deployment checklist
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if documentation exists for new variables
fd -e md -e mdx | xargs rg -l "RE_CAPTCHA_(SITE|SECRET)_KEY"

Length of output: 62


Script:

#!/bin/bash
# Let's first check if there are any markdown files in docs or similar directories
fd -e md -e mdx

# Also check for README files that might contain documentation
fd README

Length of output: 907


Script:

#!/bin/bash
# Let's check the environment variables documentation and installation guides
cat docs/configuration/environment-variables.mdx
cat docs/deployment/local-deployment.mdx

Length of output: 5373


MUX_WORKSPACE_ID=
MUX_API_TOKEN=

Expand Down
6 changes: 3 additions & 3 deletions api/app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use App\Rules\ValidHCaptcha;
use App\Rules\ValidReCaptcha;

class RegisterController extends Controller
{
Expand Down Expand Up @@ -71,8 +71,8 @@ protected function validator(array $data)
'utm_data' => ['nullable', 'array'],
];

if (config('services.h_captcha.secret_key')) {
$rules['h-captcha-response'] = [new ValidHCaptcha()];
if (config('services.re_captcha.secret_key')) {
$rules['g-recaptcha-response'] = [new ValidReCaptcha()];
}

return Validator::make($data, $rules, [
Expand Down
9 changes: 7 additions & 2 deletions api/app/Http/Requests/AnswerFormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Rules\StorageFile;
use App\Rules\ValidHCaptcha;
use App\Rules\ValidPhoneInputRule;
use App\Rules\ValidReCaptcha;
use App\Rules\ValidUrl;
use App\Service\Forms\FormLogicPropertyResolver;
use Illuminate\Foundation\Http\FormRequest;
Expand Down Expand Up @@ -116,9 +117,13 @@ public function rules()
$this->requestRules[$propertyId] = $rules;
}

// Validate hCaptcha
// Validate Captcha
if ($this->form->use_captcha) {
$this->requestRules['h-captcha-response'] = [new ValidHCaptcha()];
if ($this->form->captcha_provider === 'recaptcha') {
$this->requestRules['g-recaptcha-response'] = [new ValidReCaptcha()];
} elseif ($this->form->captcha_provider === 'hcaptcha') {
$this->requestRules['h-captcha-response'] = [new ValidHCaptcha()];
}
Comment on lines +122 to +126
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add default case handling for captcha validation.

While the current implementation handles both reCAPTCHA and hCaptcha, it lacks handling for unexpected captcha_provider values. This could lead to a security issue where no captcha validation is applied even when use_captcha is true.

Consider adding a default case:

 if ($this->form->use_captcha) {
     if ($this->form->captcha_provider === 'recaptcha') {
         $this->requestRules['g-recaptcha-response'] = [new ValidReCaptcha()];
     } elseif ($this->form->captcha_provider === 'hcaptcha') {
         $this->requestRules['h-captcha-response'] = [new ValidHCaptcha()];
+    } else {
+        // Default to hCaptcha if provider is invalid
+        $this->requestRules['h-captcha-response'] = [new ValidHCaptcha()];
     }
 }

Committable suggestion skipped: line range outside the PR's diff.

}

// Validate submission_id for edit mode
Expand Down
1 change: 1 addition & 0 deletions api/app/Http/Requests/UserFormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public function rules()
'can_be_indexed' => 'boolean',
'password' => 'sometimes|nullable',
'use_captcha' => 'boolean',
'captcha_provider' => ['sometimes', Rule::in(['recaptcha', 'hcaptcha'])],

// Custom SEO
'seo_meta' => 'nullable|array',
Expand Down
1 change: 1 addition & 0 deletions api/app/Models/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class Form extends Model implements CachableAttributes
'submitted_text',
'redirect_url',
'use_captcha',
'captcha_provider',
'closes_at',
'closed_text',
'max_submissions_count',
Expand Down
6 changes: 3 additions & 3 deletions api/app/Rules/ValidHCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ValidHCaptcha implements ImplicitRule
{
public const H_CAPTCHA_VERIFY_URL = 'https://hcaptcha.com/siteverify';

private $error = 'Invalid CAPTCHA. Please prove you\'re not a bot.';
private $error = 'validation.invalid_captcha';

/**
* Determine if the validation rule passes.
Expand All @@ -22,7 +22,7 @@ class ValidHCaptcha implements ImplicitRule
public function passes($attribute, $value)
{
if (empty($value)) {
$this->error = 'Please complete the captcha.';
$this->error = 'validation.complete_captcha';

return false;
}
Expand All @@ -46,6 +46,6 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
*/
public function message()
{
return $this->error;
return trans($this->error);
}
}
51 changes: 51 additions & 0 deletions api/app/Rules/ValidReCaptcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ImplicitRule;
use Illuminate\Support\Facades\Http;

class ValidReCaptcha implements ImplicitRule
{
public const RECAPTCHA_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';

private $error = 'validation.invalid_captcha';

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
if (empty($value)) {
$this->error = 'validation.complete_captcha';

return false;
}

return Http::asForm()->post(self::RECAPTCHA_VERIFY_URL, [
'secret' => config('services.re_captcha.secret_key'),
'response' => $value,
])->json('success');
}
Comment on lines +30 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance reCAPTCHA verification security and reliability

The verification request should include additional security measures and error handling:

  1. Include the user's IP address in the verification request (recommended by Google)
  2. Add timeout and error handling for the HTTP request
     return Http::asForm()->post(self::RECAPTCHA_VERIFY_URL, [
         'secret' => config('services.re_captcha.secret_key'),
         'response' => $value,
+        'remoteip' => request()->ip(),
-    ])->json('success');
+    ])
+    ->timeout(5)
+    ->throw(function($response, $e) {
+        $this->error = 'CAPTCHA verification failed. Please try again.';
+        return false;
+    })
+    ->json('success');
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return Http::asForm()->post(self::RECAPTCHA_VERIFY_URL, [
'secret' => config('services.re_captcha.secret_key'),
'response' => $value,
])->json('success');
}
return Http::asForm()->post(self::RECAPTCHA_VERIFY_URL, [
'secret' => config('services.re_captcha.secret_key'),
'response' => $value,
'remoteip' => request()->ip(),
])
->timeout(5)
->throw(function($response, $e) {
$this->error = 'CAPTCHA verification failed. Please try again.';
return false;
})
->json('success');
}

public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!$this->passes($attribute, $value)) {
$fail($this->message());
}
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return trans($this->error);
}
}
5 changes: 5 additions & 0 deletions api/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
'secret_key' => env('H_CAPTCHA_SECRET_KEY'),
],

're_captcha' => [
'site_key' => env('RE_CAPTCHA_SITE_KEY'),
'secret_key' => env('RE_CAPTCHA_SECRET_KEY'),
],

'canny' => [
'api_key' => env('CANNY_API_KEY'),
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
public function up()
{
Schema::table('forms', function (Blueprint $table) {
$table->string('captcha_provider')->default('hcaptcha')->after('use_captcha');
});
}

public function down()
{
Schema::table('forms', function (Blueprint $table) {
$table->dropColumn('captcha_provider');
});
}
};
2 changes: 2 additions & 0 deletions api/resources/lang/ar/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@
'attributes' => [],

'invalid_json' => 'إدخال غير صالح. يرجى التصحيح والمحاولة مرة أخرى.',
'invalid_captcha' => 'التحقق من صحة الحقل غير صحيح. يرجى التحقق من صحة الحقل والمحاولة مرة أخرى.',
'complete_captcha' => 'يرجى إكمال التحقق من صحة الحقل.',
];
2 changes: 2 additions & 0 deletions api/resources/lang/bn/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@
'attributes' => [],

'invalid_json' => 'অবৈধ ইনপুট। অনুগ্রহ করে সংশোধন করে আবার চেষ্টা করুন।',
'invalid_captcha' => 'অবৈধ টিপস। অনুগ্রহ করে টিপস টিপুন।',
'complete_captcha' => 'অনুগ্রহ করে টিপস টিপুন।',
Comment on lines +119 to +120
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Translation needs improvement for clarity

The current Bengali translations for CAPTCHA-related messages use "টিপস" (tips) which is incorrect. Consider using "ক্যাপচা" (captcha) instead for better clarity. Here are the suggested translations:

-    'invalid_captcha' => 'অবৈধ টিপস। অনুগ্রহ করে টিপস টিপুন।',
-    'complete_captcha' => 'অনুগ্রহ করে টিপস টিপুন।',
+    'invalid_captcha' => 'অবৈধ ক্যাপচা। অনুগ্রহ করে ক্যাপচা সম্পূর্ণ করুন।',
+    'complete_captcha' => 'অনুগ্রহ করে ক্যাপচা সম্পূর্ণ করুন।',

This will make it clearer to Bengali-speaking users that they need to complete a CAPTCHA verification.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
'invalid_captcha' => 'অবৈধ টিপস। অনুগ্রহ করে টিপস টিপুন',
'complete_captcha' => 'অনুগ্রহ করে টিপস টিপুন',
'invalid_captcha' => 'অবৈধ ক্যাপচা। অনুগ্রহ করে ক্যাপচা সম্পূর্ণ করুন',
'complete_captcha' => 'অনুগ্রহ করে ক্যাপচা সম্পূর্ণ করুন',

];
2 changes: 2 additions & 0 deletions api/resources/lang/de/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@
'attributes' => [],

'invalid_json' => 'Ungültige Eingabe. Bitte korrigieren Sie und versuchen Sie es erneut.',
'invalid_captcha' => 'Ungültige CAPTCHA. Bitte beweisen Sie, dass Sie kein Bot sind.',
'complete_captcha' => 'Bitte füllen Sie die CAPTCHA aus.',
];
2 changes: 2 additions & 0 deletions api/resources/lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,7 @@
'attributes' => [],

'invalid_json' => 'Invalid input. Please correct and try again.',
'invalid_captcha' => 'Invalid CAPTCHA. Please prove you\'re not a bot.',
'complete_captcha' => 'Please complete the captcha.',

];
2 changes: 2 additions & 0 deletions api/resources/lang/es/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,7 @@
],

'invalid_json' => 'Entrada no válida. Por favor, corrija e intente nuevamente.',
'invalid_captcha' => 'Captcha no válido. Por favor, demuestre que no es un bot.',
'complete_captcha' => 'Por favor, complete el captcha.',

];
2 changes: 2 additions & 0 deletions api/resources/lang/fr/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@
'attributes' => [],

'invalid_json' => 'Entrée invalide. Veuillez corriger et réessayer.',
'invalid_captcha' => 'Captcha invalide. Veuillez prouver que vous n\'êtes pas un bot.',
'complete_captcha' => 'Veuillez compléter le captcha.',
];
2 changes: 2 additions & 0 deletions api/resources/lang/hi/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@
'attributes' => [],

'invalid_json' => 'अमान्य इनपुट। कृपया सुधारें और पुनः प्रयास करें।',
'invalid_captcha' => 'अमान्य कैप्चा। कृपया दिखाएं कि आप एक बॉट नहीं हैं।',
'complete_captcha' => 'कृपया कैप्चा भरें।',
];
2 changes: 2 additions & 0 deletions api/resources/lang/ja/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@
'attributes' => [],

'invalid_json' => '無効な入力です。修正して再度お試しください。',
'invalid_captcha' => '無効なCAPTCHAです。ボットではないことを証明してください。',
'complete_captcha' => 'CAPTCHAを完了してください。',
];
2 changes: 2 additions & 0 deletions api/resources/lang/jv/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@
'attributes' => [],

'invalid_json' => 'Input ora valid. Mangga dibenakake lan dicoba maneh.',
'invalid_captcha' => 'CAPTCHA ora valid. Mangga dibenakake lan dicoba maneh.',
'complete_captcha' => 'CAPTCHA kudu diisi.',
];
2 changes: 2 additions & 0 deletions api/resources/lang/ko/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@
'attributes' => [],

'invalid_json' => '잘못된 입력입니다. 수정 후 다시 시도해 주세요.',
'invalid_captcha' => '잘못된 캡차입니다. 봇이 아님을 증명해 주세요.',
'complete_captcha' => '캡차를 완료해 주세요.',
];
2 changes: 2 additions & 0 deletions api/resources/lang/mr/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,6 @@
'attributes' => [],

'invalid_json' => 'अवैध इनपुट. कृपया सुधारून पुन्हा प्रयत्न करा.',
'invalid_captcha' => 'अवैध कैप्चा। कृपया कैप्चा टिपला आणि पुन्हा प्रयत्न करा।',
'complete_captcha' => 'कृपया कैप्चा भरा.',
];
4 changes: 3 additions & 1 deletion api/resources/lang/pa/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,7 @@

'attributes' => [],

'invalid_json' => 'ਗਲਤ ਇਨਪੁਟ। ਕਿਰਪਾ ਕਰਕੇ ਸਹੀ ਕਰੋ ਅਤੇ ਦੁਬਾਰਾ ਕੋਸ਼ਿਸ਼ ਕਰੋ।',
'invalid_json' => 'ਗਲਤ ਇਨਪੁਟ। ਕਿਰਪਾ ਕਰਕਕੇ ਸਹੀ ਕਰੋ ਅਤੇ ਦੁਬਾਰਾ ਕੋਸ਼ਿਸ਼ ਕਰੋ।',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Consider reverting the spelling change in invalid_json message

The change from "ਕਿਰਪਾ ਕਰਕੇ" to "ਕਿਰਪਾ ਕਰਕਕੇ" appears to be a typo. The original spelling "ਕਿਰਪਾ ਕਰਕੇ" is the correct form in Punjabi.

-    'invalid_json' => 'ਗਲਤ ਇਨਪੁਟ। ਕਿਰਪਾ ਕਰਕਕੇ ਸਹੀ ਕਰੋ ਅਤੇ ਦੁਬਾਰਾ ਕੋਸ਼ਿਸ਼ ਕਰੋ।',
+    'invalid_json' => 'ਗਲਤ ਇਨਪੁਟ। ਕਿਰਪਾ ਕਰਕੇ ਸਹੀ ਕਰੋ ਅਤੇ ਦੁਬਾਰਾ ਕੋਸ਼ਿਸ਼ ਕਰੋ।',
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
'invalid_json' => 'ਗਲਤ ਇਨਪੁਟ। ਕਿਰਪਾ ਕਰਕਕੇ ਸਹੀ ਕਰੋ ਅਤੇ ਦੁਬਾਰਾ ਕੋਸ਼ਿਸ਼ ਕਰੋ।',
'invalid_json' => 'ਗਲਤ ਇਨਪੁਟ। ਕਿਰਪਾ ਕਰਕੇ ਸਹੀ ਕਰੋ ਅਤੇ ਦੁਬਾਰਾ ਕੋਸ਼ਿਸ਼ ਕਰੋ।',

'invalid_captcha' => 'ਗਲਤ ਕੈਪਚਾ। ਕਿਰਪਾ ਕਰੋ ਕੈਪਚਾ ਟੀਪ ਅਤੇ ਦੁਬਾਰਾ ਕੋਸ਼ਿਸ਼ ਕਰੋ।',
'complete_captcha' => 'ਕਿਰਪਾ ਕਰੋ ਕੈਪਚਾ ਭਰੋ।',
];
2 changes: 2 additions & 0 deletions api/resources/lang/pt-BR/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,6 @@
],

'invalid_json' => 'Entrada inválida. Por favor, corrija e tente novamente.',
'invalid_captcha' => 'Captcha inválido. Por favor, demonstre que não é um bot.',
'complete_captcha' => 'Por favor, complete o captcha.',
];
2 changes: 2 additions & 0 deletions api/resources/lang/pt/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@
'attributes' => [],

'invalid_json' => 'Entrada inválida. Por favor, corrija e tente novamente.',
'invalid_captcha' => 'Captcha inválido. Por favor, demonstre que não é um bot.',
'complete_captcha' => 'Por favor, complete o captcha.',
];
2 changes: 2 additions & 0 deletions api/resources/lang/ru/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@
'attributes' => [],

'invalid_json' => 'Неверный ввод. Пожалуйста, исправьте и попробуйте снова.',
'invalid_captcha' => 'Неверный CAPTCHA. Пожалуйста, докажите, что вы не бот.',
'complete_captcha' => 'Пожалуйста, заполните CAPTCHA.',
];
2 changes: 2 additions & 0 deletions api/resources/lang/ta/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,6 @@
'attributes' => [],

'invalid_json' => 'தவறான உள்ளீடு. சரிசெய்து மீண்டும் முயற்சிக்கவும்.',
'invalid_captcha' => 'தவறான கையால் வைத்த முறை. மீண்டும் முயற்சிக்கவும்.',
'complete_captcha' => 'கையால் வைத்த முறையை நிரப்பவும்.',
];
2 changes: 2 additions & 0 deletions api/resources/lang/te/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@
'attributes' => [],

'invalid_json' => 'చెల్లని ఇన్‌పుట్. దయచేసి సరిచేసి మళ్లీ ప్రయత్నించండి.',
'invalid_captcha' => 'అవైధ క్యాప్చా. దయచేసి క్యాప్చా టైప్ మరియు మళ్లీ ప్రయత్నించండి.',
'complete_captcha' => 'దయచేసి క్యాప్చా భరించండి.',
];
2 changes: 2 additions & 0 deletions api/resources/lang/tr/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,6 @@
'attributes' => [],

'invalid_json' => 'Geçersiz girdi. Lütfen düzeltin ve tekrar deneyin.',
'invalid_captcha' => 'Geçersiz CAPTCHA. Lütfen bot olmadığınızı gösterin.',
'complete_captcha' => 'Lütfen CAPTCHA\'yı tamamlayın.',
];
2 changes: 2 additions & 0 deletions api/resources/lang/ur/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@
'attributes' => [],

'invalid_json' => 'غلط ان پٹ۔ براہ کرم درست کریں اور دوبارہ کوشش کریں۔',
'invalid_captcha' => 'غلط کپچا۔ براہ کرم کپچا ٹائپ کریں اور دوبارہ کوشش کریں۔',
'complete_captcha' => 'براہ کرم کپچا پر کام کریں۔',
];
2 changes: 2 additions & 0 deletions api/resources/lang/vi/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@
'attributes' => [],

'invalid_json' => 'Dữ liệu không hợp lệ. Vui lòng sửa và thử lại.',
'invalid_captcha' => 'Mã bảo vệ không đúng. Vui lòng nhập lại và thử lại.',
'complete_captcha' => 'Vui lòng nhập mã bảo vệ.',
];
2 changes: 2 additions & 0 deletions api/resources/lang/zh-CN/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,6 @@
'attributes' => [],

'invalid_json' => '输入无效。请修正后重试。',
'invalid_captcha' => '验证码错误。请重新输入并重试。',
'complete_captcha' => '请完成验证码。',
];
2 changes: 2 additions & 0 deletions api/resources/lang/zh/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@
'attributes' => [],

'invalid_json' => '输入无效。请修正后重试。',
'invalid_captcha' => '验证码错误。请重新输入并重试。',
'complete_captcha' => '请完成验证码。',
];
23 changes: 22 additions & 1 deletion api/tests/Feature/Forms/FormCaptchaTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

it('create form with captcha and raise validation issue', function () {
it('create form with hcaptcha and raise validation issue', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace, [
'use_captcha' => true,
'captcha_provider' => 'hcaptcha',
]);

$this->postJson(route('forms.answer', $form->slug), [])
Expand All @@ -18,3 +19,23 @@
],
]);
});

it('create form with recaptcha and raise validation issue', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace, [
'use_captcha' => true,
'captcha_provider' => 'recaptcha',
]);

$this->postJson(route('forms.answer', $form->slug), [])
->assertStatus(422)
->assertJson([
'message' => 'Please complete the captcha.',
'errors' => [
'g-recaptcha-response' => [
'Please complete the captcha.',
],
],
]);
});
Loading
Loading