Skip to content

Commit

Permalink
register page captcha test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
chiragchhatrala committed Dec 10, 2024
1 parent 7e4b2b7 commit c63571e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
35 changes: 30 additions & 5 deletions api/tests/Feature/RegisterTest.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
<?php

use App\Models\User;
use App\Rules\ValidHCaptcha;
use Illuminate\Support\Facades\Http;

it('can register', function () {

Http::fake([
ValidHCaptcha::H_CAPTCHA_VERIFY_URL => Http::response(['success' => true])
]);

$this->postJson('/register', [
'name' => 'Test User',
'email' => '[email protected]',
'hear_about_us' => 'google',
'password' => 'secret',
'password_confirmation' => 'secret',
'agree_terms' => true,
'h-captcha-response' => 'test-token', // Mock token for testing
])
->assertSuccessful()
->assertJsonStructure(['id', 'name', 'email']);
$this->assertDatabaseHas('users', [
'name' => 'Test User',
'email' => '[email protected]',
]);

$user = User::where('email', '[email protected]')->first();
expect($user)->not->toBeNull();
expect($user->meta)->toHaveKey('registration_ip');
expect($user->meta['registration_ip'])->toBe(request()->ip());
});

it('cannot register with existing email', function () {
Expand All @@ -27,6 +36,7 @@
'email' => '[email protected]',
'password' => 'secret',
'password_confirmation' => 'secret',
'h-captcha-response' => 'test-token',
])
->assertStatus(422)
->assertJsonValidationErrors(['email']);
Expand All @@ -48,15 +58,30 @@
'password' => 'secret',
'password_confirmation' => 'secret',
'agree_terms' => true,
'h-captcha-response' => 'test-token',
])
->assertStatus(422)
->assertJsonValidationErrors(['email'])
->assertJson([
'message' => 'Disposable email addresses are not allowed.',
'message' => 'Disposable email addresses are not allowed. (and 1 more error)',
'errors' => [
'email' => [
'Disposable email addresses are not allowed.',
],
],
]);
});

it('requires hcaptcha token in production', function () {
app()->detectEnvironment(fn() => 'production');

$this->postJson('/register', [
'name' => 'Test User',
'email' => '[email protected]',
'hear_about_us' => 'google',
'password' => 'secret',
'password_confirmation' => 'secret',
])
->assertStatus(422)
->assertJsonValidationErrors(['h-captcha-response']);
});
10 changes: 10 additions & 0 deletions api/tests/Feature/UserManagementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

use App\Models\UserInvite;
use Carbon\Carbon;
use App\Rules\ValidHCaptcha;
use Illuminate\Support\Facades\Http;

beforeEach(function () {
$this->user = $this->actingAsProUser();
$this->workspace = $this->createUserWorkspace($this->user);
Http::fake([
ValidHCaptcha::H_CAPTCHA_VERIFY_URL => Http::response(['success' => true])
]);
});


Expand All @@ -31,6 +36,7 @@
'password_confirmation' => 'secret',
'agree_terms' => true,
'invite_token' => $token,
'h-captcha-response' => 'test-token',
]);
$response->assertSuccessful();
expect($this->workspace->users()->count())->toBe(2);
Expand Down Expand Up @@ -59,6 +65,7 @@
'password_confirmation' => 'secret',
'agree_terms' => true,
'invite_token' => $token,
'h-captcha-response' => 'test-token',
]);
$response->assertStatus(400)->assertJson([
'message' => 'Invite token has expired.',
Expand Down Expand Up @@ -88,6 +95,7 @@
'password_confirmation' => 'secret',
'agree_terms' => true,
'invite_token' => $token,
'h-captcha-response' => 'test-token',
]);
$response->assertSuccessful();
expect($this->workspace->users()->count())->toBe(2);
Expand All @@ -104,6 +112,7 @@
'password_confirmation' => 'secret',
'agree_terms' => true,
'invite_token' => $token,
'h-captcha-response' => 'test-token',
]);

$response->assertStatus(422)->assertJson([
Expand Down Expand Up @@ -138,6 +147,7 @@
'password_confirmation' => 'secret',
'agree_terms' => true,
'invite_token' => $token,
'h-captcha-response' => 'test-token',
]);
$response->assertStatus(400)->assertJson([
'message' => 'Invite token is invalid.',
Expand Down

0 comments on commit c63571e

Please sign in to comment.