add all controllers logic

This commit is contained in:
root
2026-03-11 17:53:15 -04:00
parent 3e6c577085
commit 2ef71cc92b
421 changed files with 12009 additions and 5211 deletions
@@ -0,0 +1,182 @@
<?php
namespace Tests\Feature\Api\V1\Auth;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class IpBanControllerTest extends TestCase
{
use RefreshDatabase;
public function test_index_requires_admin(): void
{
$user = $this->createUser('teacher');
Sanctum::actingAs($user);
$response = $this->getJson('/api/v1/ip-bans');
$response->assertForbidden();
}
public function test_index_returns_bans(): void
{
$admin = $this->createUser('admin');
Sanctum::actingAs($admin);
DB::table('ip_attempts')->insert([
'ip_address' => '10.0.0.1',
'attempts' => 5,
'blocked_until' => now('UTC')->addHours(2),
]);
$response = $this->getJson('/api/v1/ip-bans?status=active');
$response->assertOk();
$response->assertJsonPath('data.bans.0.ip_address', '10.0.0.1');
$response->assertJsonStructure(['data' => ['meta']]);
}
public function test_show_returns_ban(): void
{
$admin = $this->createUser('admin');
Sanctum::actingAs($admin);
DB::table('ip_attempts')->insert([
'ip_address' => '10.0.0.2',
'attempts' => 3,
'blocked_until' => now('UTC')->addHours(1),
]);
$response = $this->getJson('/api/v1/ip-bans/1');
$response->assertOk();
$response->assertJsonPath('data.ban.ip_address', '10.0.0.2');
}
public function test_ban_creates_block(): void
{
$admin = $this->createUser('admin');
Sanctum::actingAs($admin);
DB::table('ip_attempts')->insert([
'ip_address' => '10.0.0.3',
'attempts' => 1,
]);
$response = $this->postJson('/api/v1/ip-bans/ban', [
'ip' => '10.0.0.3',
'hours' => 12,
]);
$response->assertOk();
$response->assertJsonPath('data.ban.ip_address', '10.0.0.3');
$this->assertDatabaseHas('ip_attempts', [
'ip_address' => '10.0.0.3',
]);
}
public function test_unban_single(): void
{
$admin = $this->createUser('admin');
Sanctum::actingAs($admin);
DB::table('ip_attempts')->insert([
'ip_address' => '10.0.0.4',
'attempts' => 5,
'blocked_until' => now('UTC')->addHours(2),
]);
$response = $this->postJson('/api/v1/ip-bans/unban', [
'ip' => '10.0.0.4',
]);
$response->assertOk();
$this->assertDatabaseHas('ip_attempts', [
'ip_address' => '10.0.0.4',
'blocked_until' => null,
'attempts' => 0,
]);
}
public function test_unban_all(): void
{
$admin = $this->createUser('admin');
Sanctum::actingAs($admin);
DB::table('ip_attempts')->insert([
[
'ip_address' => '10.0.0.5',
'attempts' => 5,
'blocked_until' => now('UTC')->addHours(2),
],
[
'ip_address' => '10.0.0.6',
'attempts' => 2,
'blocked_until' => now('UTC')->addHours(2),
],
]);
$response = $this->postJson('/api/v1/ip-bans/unban', [
'all' => true,
]);
$response->assertOk();
$this->assertDatabaseHas('ip_attempts', [
'ip_address' => '10.0.0.5',
'blocked_until' => null,
]);
$this->assertDatabaseHas('ip_attempts', [
'ip_address' => '10.0.0.6',
'blocked_until' => null,
]);
}
public function test_ban_validation_requires_ip_or_id(): void
{
$admin = $this->createUser('admin');
Sanctum::actingAs($admin);
$response = $this->postJson('/api/v1/ip-bans/ban', [
'hours' => 12,
]);
$response->assertStatus(422);
$response->assertJsonStructure(['message', 'errors']);
}
private function createUser(string $roleName): User
{
$roleId = DB::table('roles')->insertGetId([
'name' => $roleName,
'priority' => 1,
'is_active' => 1,
]);
$user = User::query()->create([
'firstname' => 'Test',
'lastname' => 'User',
'email' => $roleName . '@example.com',
'cellphone' => '5555555555',
'address_street' => '123 Main',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'status' => 'Active',
'password' => bcrypt('secret'),
'semester' => 'Fall',
'school_year' => '2025-2026',
]);
DB::table('user_roles')->insert([
'user_id' => $user->id,
'role_id' => $roleId,
]);
return $user;
}
}
@@ -39,6 +39,8 @@ class RegisterControllerTest extends TestCase
'is_active' => 1,
]);
$captcha = $this->getJson('/api/v1/auth/register/captcha')->json('captcha');
$payload = [
'firstname' => 'Parent',
'lastname' => 'User',
@@ -52,13 +54,12 @@ class RegisterControllerTest extends TestCase
'state' => 'CT',
'zip' => '12345',
'accept_school_policy' => 1,
'captcha' => 'ABCD',
'captcha' => $captcha,
'is_parent' => 1,
'no_second_parent_info' => 1,
];
$response = $this->withSession(['captcha_answer' => 'ABCD'])
->postJson('/api/v1/auth/register', $payload);
$response = $this->postJson('/api/v1/auth/register', $payload);
$response->assertCreated();
$response->assertJsonPath('ok', true);
@@ -1,123 +0,0 @@
<?php
namespace Tests\Feature\Api\V1\Auth;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class SessionTimeoutControllerTest extends TestCase
{
use RefreshDatabase;
public function test_config_returns_timeout_values(): void
{
$user = $this->seedUser();
Sanctum::actingAs($user);
$response = $this->getJson('/api/v1/session/timeout-config');
$response->assertOk();
$response->assertJsonPath('status', true);
$this->assertArrayHasKey('timeout', $response->json('data.config'));
$this->assertArrayHasKey('keep_alive_url', $response->json('data.config'));
}
public function test_check_timeout_returns_active_status(): void
{
$user = $this->seedUser();
Sanctum::actingAs($user);
$response = $this
->withSession(['last_activity' => time() - 10])
->getJson('/api/v1/session/check-timeout');
$response->assertOk();
$response->assertJsonPath('status', true);
$response->assertJsonPath('data.session.status', 'active');
}
public function test_check_timeout_returns_warning_status(): void
{
$user = $this->seedUser();
Sanctum::actingAs($user);
$response = $this
->withSession(['last_activity' => time() - 1000])
->getJson('/api/v1/session/check-timeout');
$response->assertOk();
$response->assertJsonPath('status', true);
$response->assertJsonPath('data.session.status', 'warning');
}
public function test_check_timeout_returns_expired_status(): void
{
$user = $this->seedUser();
Sanctum::actingAs($user);
$response = $this
->withSession(['last_activity' => time() - 5000])
->getJson('/api/v1/session/check-timeout');
$response->assertStatus(401);
$response->assertJsonPath('status', false);
$response->assertJsonPath('errors.status', 'expired');
}
public function test_ping_updates_activity(): void
{
$user = $this->seedUser();
Sanctum::actingAs($user);
$response = $this
->withSession(['last_activity' => time() - 10])
->postJson('/api/v1/session/ping');
$response->assertOk();
$response->assertJsonPath('status', true);
$response->assertJsonPath('data.session.status', 'active');
}
public function test_ping_returns_expired_when_missing_activity(): void
{
$user = $this->seedUser();
Sanctum::actingAs($user);
$response = $this->postJson('/api/v1/session/ping');
$response->assertStatus(401);
$response->assertJsonPath('status', false);
}
public function test_requires_authentication(): void
{
$response = $this->getJson('/api/v1/session/timeout-config');
$response->assertStatus(401);
}
private function seedUser(): User
{
$userId = DB::table('users')->insertGetId([
'firstname' => 'Admin',
'lastname' => 'User',
'cellphone' => '9999999999',
'email' => 'session@example.com',
'address_street' => '123 Street',
'city' => 'City',
'state' => 'ST',
'zip' => '12345',
'accept_school_policy' => 1,
'password' => bcrypt('password'),
'user_type' => 'primary',
'semester' => 'Fall',
'school_year' => '2025-2026',
'status' => 'Active',
]);
return User::query()->findOrFail($userId);
}
}