feat: implement user registration functionality with validation and response handling
This commit is contained in:
58
tests/Feature/Auth/RegisterControllerTest.php
Normal file
58
tests/Feature/Auth/RegisterControllerTest.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Domains\Auth\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class RegisterControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_it_registers_a_user(): void
|
||||
{
|
||||
$response = $this->postJson('/api/register', [
|
||||
'nombre_apellido' => 'Ada Lovelace',
|
||||
'email' => 'ada@example.com',
|
||||
'password' => 'secret123',
|
||||
'password_confirmation' => 'secret123',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertCreated()
|
||||
->assertJsonPath('message', 'Usuario registrado correctamente.')
|
||||
->assertJsonPath('data.nombre_apellido', 'Ada Lovelace')
|
||||
->assertJsonPath('data.email', 'ada@example.com');
|
||||
|
||||
$this->assertDatabaseHas('users', [
|
||||
'nombre_apellido' => 'Ada Lovelace',
|
||||
'email' => 'ada@example.com',
|
||||
]);
|
||||
|
||||
$user = User::query()->where('email', 'ada@example.com')->firstOrFail();
|
||||
|
||||
$this->assertNotSame('secret123', $user->password);
|
||||
}
|
||||
|
||||
public function test_it_validates_required_fields_and_unique_email(): void
|
||||
{
|
||||
User::factory()->create([
|
||||
'nombre_apellido' => 'Existing User',
|
||||
'email' => 'existing@example.com',
|
||||
]);
|
||||
|
||||
$response = $this->postJson('/api/register', [
|
||||
'nombre_apellido' => '',
|
||||
'email' => 'existing@example.com',
|
||||
'password' => 'secret123',
|
||||
'password_confirmation' => 'different-secret',
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()->assertJsonValidationErrors([
|
||||
'nombre_apellido',
|
||||
'email',
|
||||
'password',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user