add controllers, servoices
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Auth;
|
||||
|
||||
use App\Services\Auth\RegistrationCaptchaService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class RegistrationCaptchaServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_generate_sets_session_value(): void
|
||||
{
|
||||
$service = new RegistrationCaptchaService();
|
||||
$value = $service->generate(6);
|
||||
|
||||
$this->assertNotEmpty($value);
|
||||
$this->assertSame($value, session()->get('captcha_answer'));
|
||||
}
|
||||
|
||||
public function test_verify_and_clear(): void
|
||||
{
|
||||
$service = new RegistrationCaptchaService();
|
||||
session()->put('captcha_answer', 'ABC123');
|
||||
|
||||
$this->assertTrue($service->verify('ABC123'));
|
||||
$this->assertFalse($service->verify('WRONG'));
|
||||
|
||||
$service->clear();
|
||||
$this->assertNull(session()->get('captcha_answer'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Auth;
|
||||
|
||||
use App\Services\Auth\RegistrationFormatterService;
|
||||
use App\Services\PhoneFormatterService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class RegistrationFormatterServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_format_normalizes_parent_fields(): void
|
||||
{
|
||||
$formatter = new RegistrationFormatterService(new PhoneFormatterService());
|
||||
|
||||
$result = $formatter->format([
|
||||
'firstname' => 'john',
|
||||
'lastname' => 'doe',
|
||||
'email' => 'USER@EXAMPLE.COM',
|
||||
'gender' => 'Male',
|
||||
'city' => 'new haven',
|
||||
'cellphone' => '(203) 555-1234',
|
||||
'address_street' => '123 main st',
|
||||
'apt' => 'a1',
|
||||
'state' => 'ct',
|
||||
'zip' => '06510',
|
||||
'accept_school_policy' => 1,
|
||||
'no_second_parent_info' => 1,
|
||||
]);
|
||||
|
||||
$this->assertSame('John', $result['firstname']);
|
||||
$this->assertSame('Doe', $result['lastname']);
|
||||
$this->assertSame('user@example.com', $result['email']);
|
||||
$this->assertSame('New Haven', $result['city']);
|
||||
$this->assertSame('203-555-1234', $result['cellphone']);
|
||||
$this->assertSame('CT', $result['state']);
|
||||
$this->assertArrayNotHasKey('second_firstname', $result);
|
||||
}
|
||||
|
||||
public function test_format_includes_second_parent_when_present(): void
|
||||
{
|
||||
$formatter = new RegistrationFormatterService(new PhoneFormatterService());
|
||||
|
||||
$result = $formatter->format([
|
||||
'firstname' => 'john',
|
||||
'lastname' => 'doe',
|
||||
'email' => 'user@example.com',
|
||||
'gender' => 'Male',
|
||||
'city' => 'new haven',
|
||||
'cellphone' => '2035551234',
|
||||
'address_street' => '123 main st',
|
||||
'apt' => 'a1',
|
||||
'state' => 'ct',
|
||||
'zip' => '06510',
|
||||
'accept_school_policy' => 1,
|
||||
'second_firstname' => 'jane',
|
||||
'second_lastname' => 'doe',
|
||||
'second_gender' => 'Female',
|
||||
'second_email' => 'jane@example.com',
|
||||
'second_cellphone' => '2035559876',
|
||||
]);
|
||||
|
||||
$this->assertSame('Jane', $result['second_firstname']);
|
||||
$this->assertSame('Doe', $result['second_lastname']);
|
||||
$this->assertSame('jane@example.com', $result['second_email']);
|
||||
$this->assertSame('203-555-9876', $result['second_cellphone']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Auth;
|
||||
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
use App\Services\Auth\RegistrationCaptchaService;
|
||||
use App\Services\Auth\RegistrationFormatterService;
|
||||
use App\Services\Auth\RegistrationService;
|
||||
use App\Services\EmailService;
|
||||
use App\Services\PhoneFormatterService;
|
||||
use App\Services\SchoolIdService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class RegistrationServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_register_creates_parent_user(): void
|
||||
{
|
||||
DB::table('configuration')->insert([
|
||||
['id' => 2001, 'config_key' => 'school_year', 'config_value' => '2025-2026'],
|
||||
['id' => 2002, 'config_key' => 'semester', 'config_value' => 'Fall'],
|
||||
]);
|
||||
|
||||
Role::query()->create([
|
||||
'id' => 1,
|
||||
'name' => 'parent',
|
||||
'slug' => 'parent',
|
||||
'description' => 'Parent',
|
||||
'priority' => 1,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
session()->put('captcha_answer', 'ABCD');
|
||||
|
||||
$emailService = Mockery::mock(EmailService::class);
|
||||
$emailService->shouldReceive('send')->once()->andReturn(true);
|
||||
|
||||
$service = new RegistrationService(
|
||||
$emailService,
|
||||
new SchoolIdService(),
|
||||
new RegistrationFormatterService(new PhoneFormatterService()),
|
||||
new RegistrationCaptchaService()
|
||||
);
|
||||
|
||||
$result = $service->register([
|
||||
'firstname' => 'Parent',
|
||||
'lastname' => 'User',
|
||||
'gender' => 'Male',
|
||||
'email' => 'parent@example.com',
|
||||
'confirm_email' => 'parent@example.com',
|
||||
'cellphone' => '5555555555',
|
||||
'address_street' => '123 Main',
|
||||
'apt' => 'A',
|
||||
'city' => 'City',
|
||||
'state' => 'CT',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'captcha' => 'ABCD',
|
||||
'is_parent' => 1,
|
||||
'no_second_parent_info' => 1,
|
||||
]);
|
||||
|
||||
$this->assertTrue($result['ok']);
|
||||
$this->assertDatabaseHas('users', [
|
||||
'email' => 'parent@example.com',
|
||||
'status' => 'Inactive',
|
||||
]);
|
||||
$this->assertDatabaseHas('user_roles', [
|
||||
'user_id' => $result['user']->id,
|
||||
'role_id' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_register_rejects_pending_activation(): void
|
||||
{
|
||||
DB::table('configuration')->insert([
|
||||
['id' => 2001, 'config_key' => 'school_year', 'config_value' => '2025-2026'],
|
||||
['id' => 2002, 'config_key' => 'semester', 'config_value' => 'Fall'],
|
||||
]);
|
||||
|
||||
Role::query()->create([
|
||||
'id' => 1,
|
||||
'name' => 'parent',
|
||||
'slug' => 'parent',
|
||||
'description' => 'Parent',
|
||||
'priority' => 1,
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
User::query()->create([
|
||||
'school_id' => '2500001',
|
||||
'firstname' => 'Existing',
|
||||
'lastname' => 'User',
|
||||
'gender' => 'Male',
|
||||
'cellphone' => '555-555-5555',
|
||||
'email' => 'existing@example.com',
|
||||
'address_street' => '123 Main',
|
||||
'city' => 'City',
|
||||
'state' => 'CT',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'is_verified' => 0,
|
||||
'status' => 'Inactive',
|
||||
'is_suspended' => 0,
|
||||
'failed_attempts' => 0,
|
||||
'password' => pbkdf2_hash('secret'),
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
'token' => 'token',
|
||||
]);
|
||||
|
||||
session()->put('captcha_answer', 'ABCD');
|
||||
|
||||
$emailService = Mockery::mock(EmailService::class);
|
||||
$emailService->shouldNotReceive('send');
|
||||
|
||||
$service = new RegistrationService(
|
||||
$emailService,
|
||||
new SchoolIdService(),
|
||||
new RegistrationFormatterService(new PhoneFormatterService()),
|
||||
new RegistrationCaptchaService()
|
||||
);
|
||||
|
||||
$result = $service->register([
|
||||
'firstname' => 'Parent',
|
||||
'lastname' => 'User',
|
||||
'gender' => 'Male',
|
||||
'email' => 'existing@example.com',
|
||||
'confirm_email' => 'existing@example.com',
|
||||
'cellphone' => '5555555555',
|
||||
'address_street' => '123 Main',
|
||||
'apt' => 'A',
|
||||
'city' => 'City',
|
||||
'state' => 'CT',
|
||||
'zip' => '12345',
|
||||
'accept_school_policy' => 1,
|
||||
'captcha' => 'ABCD',
|
||||
'is_parent' => 1,
|
||||
'no_second_parent_info' => 1,
|
||||
]);
|
||||
|
||||
$this->assertFalse($result['ok']);
|
||||
$this->assertSame('pending_activation', $result['code']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user