feat: add optional 'dni' and 'telefono' fields to User model and registration process
This commit is contained in:
@@ -10,7 +10,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
|
|||||||
use Illuminate\Notifications\Notifiable;
|
use Illuminate\Notifications\Notifiable;
|
||||||
use Laravel\Sanctum\HasApiTokens;
|
use Laravel\Sanctum\HasApiTokens;
|
||||||
|
|
||||||
#[Fillable(['nombre_apellido', 'email', 'password'])]
|
#[Fillable(['nombre_apellido', 'email', 'password', 'dni', 'telefono'])]
|
||||||
#[Hidden(['password', 'remember_token'])]
|
#[Hidden(['password', 'remember_token'])]
|
||||||
class User extends Authenticatable
|
class User extends Authenticatable
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ class RegisterUserRequest extends FormRequest
|
|||||||
'nombre_apellido' => ['required', 'string', 'max:255'],
|
'nombre_apellido' => ['required', 'string', 'max:255'],
|
||||||
'email' => ['required', 'string', 'email', 'max:255', Rule::unique('users', 'email')],
|
'email' => ['required', 'string', 'email', 'max:255', Rule::unique('users', 'email')],
|
||||||
'password' => ['required', 'string', 'confirmed'],
|
'password' => ['required', 'string', 'confirmed'],
|
||||||
|
'dni' => ['nullable', 'string', 'max:255'],
|
||||||
|
'telefono' => ['nullable', 'string', 'max:255'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ class UserResource extends JsonResource
|
|||||||
'id' => $this->id,
|
'id' => $this->id,
|
||||||
'nombre_apellido' => $this->nombre_apellido,
|
'nombre_apellido' => $this->nombre_apellido,
|
||||||
'email' => $this->email,
|
'email' => $this->email,
|
||||||
|
'dni' => $this->dni,
|
||||||
|
'telefono' => $this->telefono,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use App\Domains\Auth\Models\User;
|
|||||||
class RegisterUserService
|
class RegisterUserService
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param array{nombre_apellido: string, email: string, password: string} $data
|
* @param array{nombre_apellido: string, email: string, password: string, dni?: string|null, telefono?: string|null} $data
|
||||||
*/
|
*/
|
||||||
public function register(array $data): User
|
public function register(array $data): User
|
||||||
{
|
{
|
||||||
@@ -15,6 +15,8 @@ class RegisterUserService
|
|||||||
'nombre_apellido' => $data['nombre_apellido'],
|
'nombre_apellido' => $data['nombre_apellido'],
|
||||||
'email' => $data['email'],
|
'email' => $data['email'],
|
||||||
'password' => $data['password'],
|
'password' => $data['password'],
|
||||||
|
'dni' => $data['dni'] ?? null,
|
||||||
|
'telefono' => $data['telefono'] ?? null,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('users', function (Blueprint $table) {
|
||||||
|
$table->string('dni')->nullable()->after('email');
|
||||||
|
$table->string('telefono')->nullable()->after('dni');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('users', function (Blueprint $table) {
|
||||||
|
$table->dropColumn(['dni', 'telefono']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -36,7 +36,7 @@ class LoginControllerTest extends TestCase
|
|||||||
$this->assertNotEmpty($response->json('token'));
|
$this->assertNotEmpty($response->json('token'));
|
||||||
|
|
||||||
$this->withHeader('Authorization', 'Bearer '.$response->json('token'))
|
$this->withHeader('Authorization', 'Bearer '.$response->json('token'))
|
||||||
->getJson('/api/user')
|
->getJson('/api/me')
|
||||||
->assertOk()
|
->assertOk()
|
||||||
->assertJsonPath('id', $user->id)
|
->assertJsonPath('id', $user->id)
|
||||||
->assertJsonPath('email', 'grace@example.com');
|
->assertJsonPath('email', 'grace@example.com');
|
||||||
@@ -44,7 +44,7 @@ class LoginControllerTest extends TestCase
|
|||||||
|
|
||||||
public function test_it_rejects_access_to_the_current_user_endpoint_without_a_token(): void
|
public function test_it_rejects_access_to_the_current_user_endpoint_without_a_token(): void
|
||||||
{
|
{
|
||||||
$this->getJson('/api/user')->assertUnauthorized();
|
$this->getJson('/api/me')->assertUnauthorized();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_it_logs_out_the_current_token(): void
|
public function test_it_logs_out_the_current_token(): void
|
||||||
|
|||||||
@@ -17,17 +17,23 @@ class RegisterControllerTest extends TestCase
|
|||||||
'email' => 'ada@example.com',
|
'email' => 'ada@example.com',
|
||||||
'password' => 'secret123',
|
'password' => 'secret123',
|
||||||
'password_confirmation' => 'secret123',
|
'password_confirmation' => 'secret123',
|
||||||
|
'dni' => '12345678A',
|
||||||
|
'telefono' => '+541122334455',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response
|
$response
|
||||||
->assertCreated()
|
->assertCreated()
|
||||||
->assertJsonPath('message', 'Usuario registrado correctamente.')
|
->assertJsonPath('message', 'Usuario registrado correctamente.')
|
||||||
->assertJsonPath('data.nombre_apellido', 'Ada Lovelace')
|
->assertJsonPath('data.nombre_apellido', 'Ada Lovelace')
|
||||||
->assertJsonPath('data.email', 'ada@example.com');
|
->assertJsonPath('data.email', 'ada@example.com')
|
||||||
|
->assertJsonPath('data.dni', '12345678A')
|
||||||
|
->assertJsonPath('data.telefono', '+541122334455');
|
||||||
|
|
||||||
$this->assertDatabaseHas('users', [
|
$this->assertDatabaseHas('users', [
|
||||||
'nombre_apellido' => 'Ada Lovelace',
|
'nombre_apellido' => 'Ada Lovelace',
|
||||||
'email' => 'ada@example.com',
|
'email' => 'ada@example.com',
|
||||||
|
'dni' => '12345678A',
|
||||||
|
'telefono' => '+541122334455',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$user = User::query()->where('email', 'ada@example.com')->firstOrFail();
|
$user = User::query()->where('email', 'ada@example.com')->firstOrFail();
|
||||||
@@ -35,6 +41,31 @@ class RegisterControllerTest extends TestCase
|
|||||||
$this->assertNotSame('secret123', $user->password);
|
$this->assertNotSame('secret123', $user->password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_it_registers_a_user_without_optional_fields(): void
|
||||||
|
{
|
||||||
|
$response = $this->postJson('/api/register', [
|
||||||
|
'nombre_apellido' => 'Alan Turing',
|
||||||
|
'email' => 'alan@example.com',
|
||||||
|
'password' => 'secret123',
|
||||||
|
'password_confirmation' => 'secret123',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response
|
||||||
|
->assertCreated()
|
||||||
|
->assertJsonPath('message', 'Usuario registrado correctamente.')
|
||||||
|
->assertJsonPath('data.nombre_apellido', 'Alan Turing')
|
||||||
|
->assertJsonPath('data.email', 'alan@example.com')
|
||||||
|
->assertJsonPath('data.dni', null)
|
||||||
|
->assertJsonPath('data.telefono', null);
|
||||||
|
|
||||||
|
$this->assertDatabaseHas('users', [
|
||||||
|
'nombre_apellido' => 'Alan Turing',
|
||||||
|
'email' => 'alan@example.com',
|
||||||
|
'dni' => null,
|
||||||
|
'telefono' => null,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
public function test_it_validates_required_fields_and_unique_email(): void
|
public function test_it_validates_required_fields_and_unique_email(): void
|
||||||
{
|
{
|
||||||
User::factory()->create([
|
User::factory()->create([
|
||||||
|
|||||||
Reference in New Issue
Block a user