diff --git a/app/Domains/Auth/Controllers/RegisterController.php b/app/Domains/Auth/Controllers/RegisterController.php new file mode 100644 index 0000000..98ec675 --- /dev/null +++ b/app/Domains/Auth/Controllers/RegisterController.php @@ -0,0 +1,27 @@ +registerUserService->register($request->validated()); + + return UserResource::make($user) + ->additional(['message' => 'Usuario registrado correctamente.']) + ->response() + ->setStatusCode(201); + } +} diff --git a/app/Models/User.php b/app/Domains/Auth/Models/User.php similarity index 77% rename from app/Models/User.php rename to app/Domains/Auth/Models/User.php index f6ba1d2..ff5db7a 100644 --- a/app/Models/User.php +++ b/app/Domains/Auth/Models/User.php @@ -1,8 +1,7 @@ */ use HasFactory, Notifiable; + protected static function newFactory(): UserFactory + { + return UserFactory::new(); + } + /** - * Get the attributes that should be cast. - * * @return array */ protected function casts(): array diff --git a/app/Domains/Auth/Requests/RegisterUserRequest.php b/app/Domains/Auth/Requests/RegisterUserRequest.php new file mode 100644 index 0000000..0438c27 --- /dev/null +++ b/app/Domains/Auth/Requests/RegisterUserRequest.php @@ -0,0 +1,26 @@ + + */ + public function rules(): array + { + return [ + 'nombre_apellido' => ['required', 'string', 'max:255'], + 'email' => ['required', 'string', 'email', 'max:255', Rule::unique('users', 'email')], + 'password' => ['required', 'string', 'confirmed'], + ]; + } +} diff --git a/app/Domains/Auth/Resources/UserResource.php b/app/Domains/Auth/Resources/UserResource.php new file mode 100644 index 0000000..0748df6 --- /dev/null +++ b/app/Domains/Auth/Resources/UserResource.php @@ -0,0 +1,24 @@ + + */ + public function toArray(Request $request): array + { + return [ + 'id' => $this->id, + 'nombre_apellido' => $this->nombre_apellido, + 'email' => $this->email, + ]; + } +} diff --git a/app/Domains/Auth/Services/RegisterUserService.php b/app/Domains/Auth/Services/RegisterUserService.php new file mode 100644 index 0000000..61f9238 --- /dev/null +++ b/app/Domains/Auth/Services/RegisterUserService.php @@ -0,0 +1,20 @@ +create([ + 'nombre_apellido' => $data['nombre_apellido'], + 'email' => $data['email'], + 'password' => $data['password'], + ]); + } +} diff --git a/app/Domains/Auth/routes/api.php b/app/Domains/Auth/routes/api.php new file mode 100644 index 0000000..002c11f --- /dev/null +++ b/app/Domains/Auth/routes/api.php @@ -0,0 +1,6 @@ + fake()->name(), + 'nombre_apellido' => fake()->name(), 'email' => fake()->unique()->safeEmail(), 'email_verified_at' => now(), 'password' => static::$password ??= Hash::make('password'), diff --git a/database/migrations/2026_07_02_000000_rename_name_to_nombre_apellido_on_users_table.php b/database/migrations/2026_07_02_000000_rename_name_to_nombre_apellido_on_users_table.php new file mode 100644 index 0000000..b5dbc5a --- /dev/null +++ b/database/migrations/2026_07_02_000000_rename_name_to_nombre_apellido_on_users_table.php @@ -0,0 +1,22 @@ +renameColumn('name', 'nombre_apellido'); + }); + } + + public function down(): void + { + Schema::table('users', function (Blueprint $table): void { + $table->renameColumn('nombre_apellido', 'name'); + }); + } +}; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 0ada740..4d95f3b 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -2,7 +2,7 @@ namespace Database\Seeders; -use App\Models\User; +use App\Domains\Auth\Models\User; use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; @@ -18,7 +18,7 @@ class DatabaseSeeder extends Seeder // User::factory(10)->create(); User::factory()->create([ - 'name' => 'Test User', + 'nombre_apellido' => 'Test User', 'email' => 'test@example.com', ]); diff --git a/routes/api.php b/routes/api.php index 7d8479d..22fc9dd 100644 --- a/routes/api.php +++ b/routes/api.php @@ -7,6 +7,7 @@ Route::get('/user', function (Request $request) { return $request->user(); })->middleware('auth:sanctum'); +require __DIR__.'/../app/Domains/Auth/routes/api.php'; require __DIR__.'/../app/Domains/Catalog/routes/api.php'; require __DIR__.'/../app/Domains/Cart/routes/api.php'; require __DIR__.'/../app/Domains/StorageTest/routes/api.php'; diff --git a/tests/Feature/Auth/RegisterControllerTest.php b/tests/Feature/Auth/RegisterControllerTest.php new file mode 100644 index 0000000..926f491 --- /dev/null +++ b/tests/Feature/Auth/RegisterControllerTest.php @@ -0,0 +1,58 @@ +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', + ]); + } +} diff --git a/tests/Feature/Cart/CartControllerTest.php b/tests/Feature/Cart/CartControllerTest.php index ecdaa39..61a008e 100644 --- a/tests/Feature/Cart/CartControllerTest.php +++ b/tests/Feature/Cart/CartControllerTest.php @@ -2,13 +2,13 @@ namespace Tests\Feature\Cart; +use App\Domains\Auth\Models\User; use App\Domains\Catalog\Models\Product; use App\Domains\Catalog\Models\Attribute; use App\Domains\Catalog\Models\ProductVariant; use App\Domains\Catalog\Models\ProductAttribute; use App\Domains\Catalog\Models\ProductVariantDefinition; use App\Domains\Tenant\Models\Tenant; -use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase;