add all controllers logic
This commit is contained in:
@@ -10,24 +10,24 @@ class RegistrationCaptchaServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_generate_sets_session_value(): void
|
||||
public function test_generate_sets_cache_value(): void
|
||||
{
|
||||
$service = new RegistrationCaptchaService();
|
||||
$value = $service->generate(6);
|
||||
|
||||
$this->assertNotEmpty($value);
|
||||
$this->assertSame($value, session()->get('captcha_answer'));
|
||||
$this->assertTrue($service->verify($value));
|
||||
}
|
||||
|
||||
public function test_verify_and_clear(): void
|
||||
{
|
||||
$service = new RegistrationCaptchaService();
|
||||
session()->put('captcha_answer', 'ABC123');
|
||||
$value = $service->generate(6);
|
||||
|
||||
$this->assertTrue($service->verify('ABC123'));
|
||||
$this->assertTrue($service->verify($value));
|
||||
$this->assertFalse($service->verify('WRONG'));
|
||||
|
||||
$service->clear();
|
||||
$this->assertNull(session()->get('captcha_answer'));
|
||||
$this->assertFalse($service->verify($value));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,8 @@ class RegistrationServiceTest extends TestCase
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
session()->put('captcha_answer', 'ABCD');
|
||||
$captchaService = new RegistrationCaptchaService();
|
||||
$captcha = $captchaService->generate(4);
|
||||
|
||||
$emailService = Mockery::mock(EmailService::class);
|
||||
$emailService->shouldReceive('send')->once()->andReturn(true);
|
||||
@@ -44,7 +45,7 @@ class RegistrationServiceTest extends TestCase
|
||||
$emailService,
|
||||
new SchoolIdService(),
|
||||
new RegistrationFormatterService(new PhoneFormatterService()),
|
||||
new RegistrationCaptchaService()
|
||||
$captchaService
|
||||
);
|
||||
|
||||
$result = $service->register([
|
||||
@@ -60,7 +61,7 @@ class RegistrationServiceTest extends TestCase
|
||||
'state' => 'CT',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'captcha' => 'ABCD',
|
||||
'captcha' => $captcha,
|
||||
'is_parent' => 1,
|
||||
'no_second_parent_info' => 1,
|
||||
]);
|
||||
@@ -114,7 +115,8 @@ class RegistrationServiceTest extends TestCase
|
||||
'token' => 'token',
|
||||
]);
|
||||
|
||||
session()->put('captcha_answer', 'ABCD');
|
||||
$captchaService = new RegistrationCaptchaService();
|
||||
$captcha = $captchaService->generate(4);
|
||||
|
||||
$emailService = Mockery::mock(EmailService::class);
|
||||
$emailService->shouldNotReceive('send');
|
||||
@@ -123,7 +125,7 @@ class RegistrationServiceTest extends TestCase
|
||||
$emailService,
|
||||
new SchoolIdService(),
|
||||
new RegistrationFormatterService(new PhoneFormatterService()),
|
||||
new RegistrationCaptchaService()
|
||||
$captchaService
|
||||
);
|
||||
|
||||
$result = $service->register([
|
||||
@@ -139,7 +141,7 @@ class RegistrationServiceTest extends TestCase
|
||||
'state' => 'CT',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'captcha' => 'ABCD',
|
||||
'captcha' => $captcha,
|
||||
'is_parent' => 1,
|
||||
'no_second_parent_info' => 1,
|
||||
]);
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Auth;
|
||||
|
||||
use App\Services\Auth\SessionActivityService;
|
||||
use Illuminate\Session\ArraySessionHandler;
|
||||
use Illuminate\Session\Store;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SessionActivityServiceTest extends TestCase
|
||||
{
|
||||
public function test_set_and_get_last_activity(): void
|
||||
{
|
||||
$store = $this->makeSessionStore();
|
||||
$service = new SessionActivityService($store);
|
||||
|
||||
$service->setLastActivity(12345);
|
||||
|
||||
$this->assertTrue($service->hasLastActivity());
|
||||
$this->assertSame(12345, $service->getLastActivity());
|
||||
}
|
||||
|
||||
public function test_clear_last_activity(): void
|
||||
{
|
||||
$store = $this->makeSessionStore();
|
||||
$service = new SessionActivityService($store);
|
||||
|
||||
$service->setLastActivity(12345);
|
||||
$service->clearLastActivity();
|
||||
|
||||
$this->assertFalse($service->hasLastActivity());
|
||||
}
|
||||
|
||||
private function makeSessionStore(): Store
|
||||
{
|
||||
$handler = new ArraySessionHandler(10);
|
||||
$store = new Store('test', $handler);
|
||||
$store->start();
|
||||
|
||||
return $store;
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Auth;
|
||||
|
||||
use App\Services\Auth\SessionTimeoutConfigService;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SessionTimeoutConfigServiceTest extends TestCase
|
||||
{
|
||||
public function test_config_returns_expected_keys(): void
|
||||
{
|
||||
$service = new SessionTimeoutConfigService();
|
||||
$config = $service->config();
|
||||
|
||||
$this->assertArrayHasKey('timeout', $config);
|
||||
$this->assertArrayHasKey('warning_time', $config);
|
||||
$this->assertArrayHasKey('check_interval', $config);
|
||||
$this->assertArrayHasKey('logout_url', $config);
|
||||
$this->assertArrayHasKey('keep_alive_url', $config);
|
||||
$this->assertArrayHasKey('check_url', $config);
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Auth;
|
||||
|
||||
use App\Config\SessionTimeout;
|
||||
use App\Services\Auth\SessionActivityService;
|
||||
use App\Services\Auth\SessionTimeoutService;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SessionTimeoutServiceTest extends TestCase
|
||||
{
|
||||
public function test_check_timeout_returns_expired_when_missing_activity(): void
|
||||
{
|
||||
$activity = Mockery::mock(SessionActivityService::class);
|
||||
$activity->shouldReceive('hasLastActivity')->andReturn(false);
|
||||
$activity->shouldReceive('clearLastActivity')->once();
|
||||
$activity->shouldReceive('invalidate')->once();
|
||||
|
||||
$service = new SessionTimeoutService($activity);
|
||||
$result = $service->checkTimeout();
|
||||
|
||||
$this->assertSame('expired', $result['status']);
|
||||
}
|
||||
|
||||
public function test_check_timeout_returns_warning(): void
|
||||
{
|
||||
$activity = Mockery::mock(SessionActivityService::class);
|
||||
$activity->shouldReceive('hasLastActivity')->andReturn(true);
|
||||
$activity->shouldReceive('getLastActivity')->andReturn(time() - (SessionTimeout::WARNING_THRESHOLD + 10));
|
||||
|
||||
$service = new SessionTimeoutService($activity);
|
||||
$result = $service->checkTimeout();
|
||||
|
||||
$this->assertSame('warning', $result['status']);
|
||||
}
|
||||
|
||||
public function test_ping_returns_expired_when_timed_out(): void
|
||||
{
|
||||
$activity = Mockery::mock(SessionActivityService::class);
|
||||
$activity->shouldReceive('hasLastActivity')->andReturn(true);
|
||||
$activity->shouldReceive('getLastActivity')->andReturn(time() - (SessionTimeout::TIMEOUT_DURATION + 10));
|
||||
$activity->shouldReceive('clearLastActivity')->once();
|
||||
$activity->shouldReceive('invalidate')->once();
|
||||
|
||||
$service = new SessionTimeoutService($activity);
|
||||
$result = $service->pingActivity();
|
||||
|
||||
$this->assertSame('expired', $result['status']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Auth;
|
||||
|
||||
use App\Services\Auth\UserRoleService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Tests\TestCase;
|
||||
|
||||
class UserRoleServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_has_permission_for_crud_checks_roles(): void
|
||||
{
|
||||
$permissionId = DB::table('permissions')->insertGetId([
|
||||
'name' => 'manage_students',
|
||||
'description' => 'Manage students',
|
||||
]);
|
||||
|
||||
$roleId = DB::table('roles')->insertGetId([
|
||||
'name' => 'admin',
|
||||
'priority' => 1,
|
||||
]);
|
||||
|
||||
DB::table('user_roles')->insert([
|
||||
'user_id' => 5,
|
||||
'role_id' => $roleId,
|
||||
]);
|
||||
|
||||
DB::table('role_permissions')->insert([
|
||||
'role_id' => $roleId,
|
||||
'permission_id' => $permissionId,
|
||||
'can_read' => 1,
|
||||
'can_manage' => 0,
|
||||
]);
|
||||
|
||||
$service = new UserRoleService();
|
||||
$roleIds = $service->getRoleIds(5);
|
||||
|
||||
$this->assertTrue($service->hasPermissionForCrud($roleIds, 'manage_students', 'read'));
|
||||
$this->assertFalse($service->hasPermissionForCrud($roleIds, 'manage_students', 'delete'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user