mirror of
https://github.com/NNTmux/newznab-tmux.git
synced 2026-08-29 17:28:55 +00:00
225 lines
7.9 KiB
PHP
225 lines
7.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\Admin;
|
|
|
|
use App\Http\Middleware\Google2FAMiddleware;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Support\Str;
|
|
use Spatie\Permission\Models\Role;
|
|
use Spatie\Permission\PermissionRegistrar;
|
|
use Tests\TestCase;
|
|
|
|
class AdminUserControllerTest extends TestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
config([
|
|
'database.default' => 'sqlite',
|
|
'database.connections.sqlite.database' => ':memory:',
|
|
'app.key' => 'base64:'.base64_encode(random_bytes(32)),
|
|
'session.driver' => 'array',
|
|
]);
|
|
|
|
DB::purge();
|
|
DB::reconnect();
|
|
|
|
$this->createSchema();
|
|
$this->seedSettings();
|
|
app(PermissionRegistrar::class)->forgetCachedPermissions();
|
|
$this->withoutMiddleware(Google2FAMiddleware::class);
|
|
}
|
|
|
|
public function test_admin_can_create_user_with_string_role_id(): void
|
|
{
|
|
$admin = $this->createUserWithRole('Admin', false);
|
|
$userRole = Role::query()->firstOrCreate(
|
|
['name' => 'User', 'guard_name' => 'web'],
|
|
['rate_limit' => 60, 'isdefault' => true, 'defaultinvites' => 3]
|
|
);
|
|
|
|
$response = $this->actingAs($admin)->post(route('admin.user-edit'), [
|
|
'action' => 'submit',
|
|
'username' => 'new_user',
|
|
'password' => 'password',
|
|
'email' => 'new-user@example.test',
|
|
'role' => (string) $userRole->id,
|
|
'notes' => 'created from test',
|
|
]);
|
|
|
|
$response->assertRedirect('admin/user-list');
|
|
$this->assertDatabaseHas('users', [
|
|
'username' => 'new_user',
|
|
'email' => 'new-user@example.test',
|
|
'roles_id' => $userRole->id,
|
|
'invites' => 3,
|
|
'notes' => 'created from test',
|
|
]);
|
|
}
|
|
|
|
public function test_admin_create_user_rejects_unknown_role_id(): void
|
|
{
|
|
$admin = $this->createUserWithRole('Admin', false);
|
|
|
|
$response = $this->from('admin/user-edit?action=add')
|
|
->actingAs($admin)
|
|
->post(route('admin.user-edit'), [
|
|
'action' => 'submit',
|
|
'username' => 'new_user',
|
|
'password' => 'password',
|
|
'email' => 'new-user@example.test',
|
|
'role' => '9999',
|
|
'notes' => '',
|
|
]);
|
|
|
|
$response->assertRedirect('admin/user-edit?action=add');
|
|
$response->assertSessionHasErrors('role');
|
|
$this->assertDatabaseMissing('users', ['email' => 'new-user@example.test']);
|
|
}
|
|
|
|
private function createSchema(): void
|
|
{
|
|
Schema::create('settings', function (Blueprint $table): void {
|
|
$table->string('name')->primary();
|
|
$table->text('value')->nullable();
|
|
});
|
|
|
|
Schema::create('roles', function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('name');
|
|
$table->string('guard_name');
|
|
$table->integer('rate_limit')->default(60);
|
|
$table->boolean('isdefault')->default(false);
|
|
$table->unsignedInteger('defaultinvites')->default(0);
|
|
$table->timestamps();
|
|
});
|
|
|
|
Schema::create('permissions', function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('name');
|
|
$table->string('guard_name');
|
|
$table->timestamps();
|
|
});
|
|
|
|
Schema::create('users', function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('username')->unique();
|
|
$table->string('email')->unique();
|
|
$table->string('password');
|
|
$table->string('host')->default('');
|
|
$table->unsignedInteger('roles_id')->default(1);
|
|
$table->unsignedInteger('invites')->default(0);
|
|
$table->unsignedInteger('invitedby')->nullable();
|
|
$table->text('notes')->nullable();
|
|
$table->integer('rate_limit')->default(60);
|
|
$table->string('api_token')->nullable();
|
|
$table->boolean('verified')->default(true);
|
|
$table->boolean('can_post')->default(true);
|
|
$table->timestamp('email_verified_at')->nullable();
|
|
$table->timestamp('lastlogin')->nullable();
|
|
$table->rememberToken();
|
|
$table->timestamps();
|
|
$table->softDeletes();
|
|
});
|
|
|
|
Schema::create('model_has_roles', function (Blueprint $table): void {
|
|
$table->unsignedInteger('role_id');
|
|
$table->string('model_type');
|
|
$table->unsignedInteger('model_id');
|
|
$table->primary(['role_id', 'model_id', 'model_type']);
|
|
});
|
|
|
|
Schema::create('model_has_permissions', function (Blueprint $table): void {
|
|
$table->unsignedInteger('permission_id');
|
|
$table->string('model_type');
|
|
$table->unsignedInteger('model_id');
|
|
$table->primary(['permission_id', 'model_id', 'model_type']);
|
|
});
|
|
|
|
Schema::create('role_has_permissions', function (Blueprint $table): void {
|
|
$table->unsignedInteger('permission_id');
|
|
$table->unsignedInteger('role_id');
|
|
$table->primary(['permission_id', 'role_id']);
|
|
});
|
|
|
|
Schema::create('user_activities', function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->unsignedInteger('user_id')->nullable();
|
|
$table->string('username');
|
|
$table->string('activity_type', 50);
|
|
$table->text('description');
|
|
$table->json('metadata')->nullable();
|
|
$table->timestamp('created_at')->nullable();
|
|
});
|
|
|
|
Schema::create('root_categories', function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('title')->default('');
|
|
$table->integer('status')->default(1);
|
|
$table->timestamps();
|
|
});
|
|
|
|
Schema::create('categories', function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('title')->default('');
|
|
$table->unsignedInteger('root_categories_id')->nullable();
|
|
$table->integer('status')->default(1);
|
|
$table->timestamps();
|
|
});
|
|
|
|
Schema::create('user_excluded_categories', function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->unsignedInteger('users_id');
|
|
$table->unsignedInteger('categories_id');
|
|
});
|
|
}
|
|
|
|
private function seedSettings(): void
|
|
{
|
|
DB::table('settings')->insert([
|
|
['name' => 'title', 'value' => 'NNTmux Test'],
|
|
['name' => 'home_link', 'value' => '/'],
|
|
['name' => 'categorizeforeign', 'value' => '0'],
|
|
['name' => 'catwebdl', 'value' => '0'],
|
|
]);
|
|
|
|
DB::table('root_categories')->insert([
|
|
'id' => 1,
|
|
'title' => 'General',
|
|
'status' => 1,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
}
|
|
|
|
private function createUserWithRole(string $roleName, bool $isDefault): User
|
|
{
|
|
$role = Role::query()->firstOrCreate(
|
|
['name' => $roleName, 'guard_name' => 'web'],
|
|
['rate_limit' => 60, 'isdefault' => $isDefault, 'defaultinvites' => 1]
|
|
);
|
|
|
|
$user = User::query()->create([
|
|
'username' => strtolower($roleName).'_'.Str::random(8),
|
|
'email' => Str::random(12).'@example.test',
|
|
'password' => bcrypt('password'),
|
|
'roles_id' => $role->id,
|
|
'rate_limit' => 60,
|
|
'api_token' => Str::random(32),
|
|
'verified' => true,
|
|
'email_verified_at' => now(),
|
|
'lastlogin' => now(),
|
|
]);
|
|
$user->assignRole($role);
|
|
|
|
return $user->fresh();
|
|
}
|
|
}
|
|
|