dress/tests/Feature/Admin/DashboardTest.php

99 lines
3.0 KiB
PHP

<?php
use App\Models\Expense;
use App\Models\Order;
use App\Models\Purchase;
use Spatie\Permission\Models\Role;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\get;
/*
|--------------------------------------------------------------------------
| Dashboard Module Tests
|--------------------------------------------------------------------------
*/
describe('Dashboard Module - Authorization', function () {
it('redirects to login when accessing dashboard unauthenticated', function () {
get(route('dashboard'))
->assertRedirect(route('login'));
});
it('returns 403 when user has no permission to view dashboard', function () {
actingAs(createUnauthorizedUser())
->get(route('dashboard'))
->assertStatus(403);
});
});
describe('Dashboard Module - Authorized Actions', function () {
beforeEach(function () {
$user = createAuthorizedUser([
'View:Dashboard',
]);
actingAs($user);
});
it('can access dashboard page and view statistics', function () {
// Create some dummy data to ensure queries don't fail and aggregations work
Order::factory()->count(3)->create([
'created_at' => now(),
'total' => 100000,
'cogs' => 50000,
]);
Expense::factory()->count(2)->create([
'created_at' => now(),
'amount' => 20000,
]);
Purchase::factory()->create([
'created_at' => now(),
'total' => 150000,
]);
get(route('dashboard'))
->assertOk()
->assertInertia(fn ($page) => $page
->component('dashboard')
->has('stats')
->has('stats.total_sales')
->has('stats.total_revenue')
->has('stats.total_expenses')
->has('stats.gross_profit')
->has('stats.net_profit')
->has('salesByHour')
->has('paymentMethods')
->has('orderStatuses')
->has('orderChannels')
->has('topProducts')
->has('topCustomers')
);
});
it('filters sensitive data for users with Admin role', function () {
Role::findOrCreate('Admin');
$user = createAuthorizedUser(['View:Dashboard']);
$user->assignRole('Admin');
actingAs($user);
get(route('dashboard'))
->assertOk()
->assertInertia(fn ($page) => $page
->component('dashboard')
->has('stats')
->has('stats.total_sales')
->has('stats.total_revenue')
->missing('stats.cogs')
->missing('stats.aov')
->missing('stats.gross_profit')
->missing('stats.net_profit')
->missing('stats.profit_margin')
->missing('stats.total_purchases')
->has('topCustomers')
);
});
});