Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ tasks:

db:seed:
desc: Seed the Auth module database
cmd: php artisan modules:seed --module=auth
cmd: '{{.APP}} php artisan modules:seed --module=auth'

make-admin:
desc: Promote an user to Admin
cmd: '{{.APP}} php artisan auth:make-admin'

# ── Code Generation ────────────────────────────────────────────

types:generate:
desc: Generate TypeScript types from PHP DTOs and enums
cmd: php artisan module:generate-types auth
cmd: '{{.APP}} php artisan module:generate-types auth'
15 changes: 1 addition & 14 deletions database/seeders/AuthDatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,14 @@ class AuthDatabaseSeeder extends Seeder
*/
public function run(): void
{
$adminUser = User::firstOrCreate(
$user = User::firstOrCreate(
['email' => 'chef@saucebase.dev'],
[
'name' => 'Admin Chef',
'password' => bcrypt('secretsauce'),
]
);

// Assign the admin role to the admin user
$adminUser->assignRole('admin');

// Create test users for E2E tests
$user = User::firstOrCreate(
['email' => 'test@example.com'],
[
'name' => 'Test User',
'password' => bcrypt('secretsauce'),
'email_verified_at' => now(),
]
);

$user->assignRole('user');
}
}
37 changes: 37 additions & 0 deletions src/Console/Commands/MakeAdminCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Modules\Auth\Console\Commands;

use App\Enums\Role;
use App\Models\User;
use Illuminate\Console\Command;

use function Laravel\Prompts\text;

class MakeAdminCommand extends Command
{
protected $signature = 'auth:make-admin
{email? : Email address of the user to promote}';

protected $description = 'Promote an existing user to admin';

public function handle(): int
{
$email = $this->argument('email') ?? text('Email address', required: true);

$user = User::where('email', $email)->first();

if (! $user) {
$this->error("No user found with email: {$email}");
$this->line('Register an account first, then re-run this command.');

return self::FAILURE;
}

$user->syncRoles([Role::ADMIN->value]);

$this->info("Promoted admin: {$user->name} <{$user->email}>");

return self::SUCCESS;
}
}
53 changes: 53 additions & 0 deletions tests/Feature/MakeAdminCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Modules\Auth\Tests\Feature;

use App\Enums\Role;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class MakeAdminCommandTest extends TestCase
{
use RefreshDatabase;

public function test_promotes_existing_user_to_admin(): void
{
$user = $this->createUser();

$this->artisan('auth:make-admin', ['email' => $user->email])
->assertSuccessful()
->expectsOutputToContain('Promoted admin');

$this->assertTrue($user->fresh()->hasRole(Role::ADMIN));
}

public function test_fails_when_user_not_found(): void
{
$this->artisan('auth:make-admin', ['email' => 'nobody@example.com'])
->assertFailed()
->expectsOutputToContain('No user found');
}

public function test_replaces_existing_role_with_admin(): void
{
$user = $this->createUser();
$user->assignRole(Role::USER);

$this->artisan('auth:make-admin', ['email' => $user->email])
->assertSuccessful();

$this->assertTrue($user->fresh()->hasRole(Role::ADMIN));
$this->assertFalse($user->fresh()->hasRole(Role::USER));
}

public function test_is_idempotent(): void
{
$user = $this->createUser();

$this->artisan('auth:make-admin', ['email' => $user->email])->assertSuccessful();
$this->artisan('auth:make-admin', ['email' => $user->email])->assertSuccessful();

$this->assertCount(1, $user->fresh()->roles);
$this->assertTrue($user->fresh()->hasRole(Role::ADMIN));
}
}
5 changes: 3 additions & 2 deletions tests/e2e/tests/login/login.security.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { test, expect } from '@e2e/fixtures';
import { LoginPage } from '../../pages/LoginPage';

test.describe('Login Security', () => {
test.describe.configure({ mode: 'serial' });

let loginPage: LoginPage;

test.beforeEach(async ({ page }) => {
Expand All @@ -10,8 +12,7 @@ test.describe('Login Security', () => {
});

test.describe('Rate Limiting', () => {
test.describe.configure({ mode: 'serial' });


test('blocks login after too many failed attempts', async () => {
const invalidUser = { email: 'invalid@example.com', password: 'wrongpassword' };

Expand Down
Loading