feat(purchase): add checkout expiration settings and reservation status to purchase items
- Introduced a new configuration file for purchase settings, including checkout and payment expiration times. - Added a migration to include a `reservation_status` column in the `compra_items` table, defaulting to 'active' and updating existing records based on purchase status. - Created a migration to add an `expires_at` timestamp to the `compras` table, updating it for existing records based on their status. - Scoped unique indexes in the `carritos` table to only active carts, adding virtual columns for active user and guest tokens. - Implemented a console command to expire overdue purchases and release stock reservations, scheduled to run every minute. - Added tests for Google token exchange and login functionality, ensuring proper cart handling and user authentication. - Updated purchase-related tests to reflect changes in item handling and customer data validation.
This commit is contained in:
@@ -2,7 +2,14 @@
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Auth\Models\User;
|
||||
use App\Domains\Cart\Models\Cart;
|
||||
use App\Domains\Catalog\Enums\InventoryPolicy;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\Inventory;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Tests\TestCase;
|
||||
@@ -13,6 +20,7 @@ class LoginControllerTest extends TestCase
|
||||
|
||||
public function test_it_logs_in_a_user_and_returns_a_bearer_token(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
$user = User::query()->create([
|
||||
'nombre_apellido' => 'Grace Hopper',
|
||||
'email' => 'grace@example.com',
|
||||
@@ -22,6 +30,7 @@ class LoginControllerTest extends TestCase
|
||||
$response = $this->postJson('/api/login', [
|
||||
'email' => 'grace@example.com',
|
||||
'password' => 'secret123',
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
]);
|
||||
|
||||
$response
|
||||
@@ -83,6 +92,7 @@ class LoginControllerTest extends TestCase
|
||||
|
||||
public function test_it_returns_a_validation_error_for_invalid_credentials(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
User::query()->create([
|
||||
'nombre_apellido' => 'Grace Hopper',
|
||||
'email' => 'grace@example.com',
|
||||
@@ -92,6 +102,7 @@ class LoginControllerTest extends TestCase
|
||||
$this->postJson('/api/login', [
|
||||
'email' => 'grace@example.com',
|
||||
'password' => 'wrong-password',
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
])->assertUnprocessable()->assertJsonValidationErrors(['email']);
|
||||
}
|
||||
|
||||
@@ -103,6 +114,96 @@ class LoginControllerTest extends TestCase
|
||||
])->assertUnprocessable()->assertJsonValidationErrors([
|
||||
'email',
|
||||
'password',
|
||||
'tenant_codigo',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_replaces_the_active_user_cart_with_the_guest_cart_on_login(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
$user = User::factory()->create([
|
||||
'email' => 'grace@example.com',
|
||||
'password' => Hash::make('secret123'),
|
||||
]);
|
||||
$catalogItem = $this->createCatalogItem($tenant);
|
||||
$userCart = Cart::query()->create([
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
'user_id' => $user->id,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$guestCart = Cart::query()->create([
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
'guest_token' => 'guest-cart-token',
|
||||
'status' => 'active',
|
||||
]);
|
||||
$guestCart->items()->create([
|
||||
'catalog_item_id' => $catalogItem->id,
|
||||
'variant_id' => null,
|
||||
'cantidad' => 2,
|
||||
]);
|
||||
|
||||
$this->withCookie('guest_token', 'guest-cart-token')
|
||||
->postJson('/api/login', [
|
||||
'email' => 'grace@example.com',
|
||||
'password' => 'secret123',
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
])
|
||||
->assertOk()
|
||||
->assertCookieExpired('guest_token');
|
||||
|
||||
$this->assertSoftDeleted('carritos', [
|
||||
'id' => $userCart->id,
|
||||
'status' => 'converted',
|
||||
]);
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $guestCart->id,
|
||||
'user_id' => $user->id,
|
||||
'guest_token' => null,
|
||||
'status' => 'active',
|
||||
]);
|
||||
}
|
||||
|
||||
private function createCatalogItem(Tenant $tenant): CatalogItem
|
||||
{
|
||||
$inventory = Inventory::query()->create(['real_stock' => 10]);
|
||||
|
||||
return CatalogItem::query()->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'inventory_id' => $inventory->id,
|
||||
'slug' => 'login-item-'.$tenant->codigo,
|
||||
'nombre' => 'Login item',
|
||||
'precio' => '10.00',
|
||||
'inventory_policy' => InventoryPolicy::Tracked,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createTenant(string $code): Tenant
|
||||
{
|
||||
$headerLogo = $this->createAttachment("{$code}-header");
|
||||
$footerLogo = $this->createAttachment("{$code}-footer");
|
||||
|
||||
return Tenant::query()->create([
|
||||
'codigo' => $code,
|
||||
'nombre' => ucfirst($code),
|
||||
'dominio' => "{$code}.local",
|
||||
'primary_color' => '#000000',
|
||||
'secondary_color' => '#000000',
|
||||
'danger_color' => '#000000',
|
||||
'success_color' => '#000000',
|
||||
'header_bg_color' => '#000000',
|
||||
'footer_bg_color' => '#000000',
|
||||
'header_logo_id' => $headerLogo->id,
|
||||
'footer_logo_id' => $footerLogo->id,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createAttachment(string $name): Attachment
|
||||
{
|
||||
return Attachment::query()->create([
|
||||
'path' => "test/{$name}.png",
|
||||
'filename' => "{$name}.png",
|
||||
'type' => AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user