user->attachPermission( Permission::where('name', 'read-admin-panel')->firstOrFail() ); } public function test_super_admin_can_add_multiple_encrypted_database_configurations(): void { $this->loginAs() ->post(route('system-settings.databases.store'), $this->payload([ 'name' => 'JianSun 测试库', 'source_key' => 'jiansun', 'password' => 'first-secret', ])) ->assertRedirect(route('system-settings.databases.index')); $this->post(route('system-settings.databases.store'), $this->payload([ 'name' => '云数据库二号', 'source_key' => '', 'host' => '127.0.0.2', 'password' => 'second-secret', ]))->assertRedirect(route('system-settings.databases.index')); $this->assertSame(2, DatabaseConnection::where('company_id', 0)->count()); $connection = DatabaseConnection::where('source_key', 'jiansun')->firstOrFail(); $this->assertSame('first-secret', $connection->password); $this->assertNotSame( 'first-secret', DB::table('system_database_connections')->where('id', $connection->id)->value('password') ); $sources = app(UpstreamSourceRegistry::class); $this->assertSame('127.0.0.1', $sources->value('jiansun', 'connection.host')); $this->assertSame('first-secret', $sources->value('jiansun', 'connection.password')); $this->get(route('system-settings.databases.index')) ->assertOk() ->assertSeeText('JianSun 测试库') ->assertSeeText('云数据库二号') ->assertDontSee('first-secret'); } public function test_editing_without_password_preserves_existing_password(): void { $connection = $this->connection('original-secret'); $this->loginAs() ->patch(route('system-settings.databases.update', $connection->id), $this->payload([ 'name' => $connection->name, 'password' => '', 'host' => '127.0.0.2', ])) ->assertRedirect(route('system-settings.databases.index')); $connection->refresh(); $this->assertSame('original-secret', $connection->password); $this->assertSame('127.0.0.2', $connection->host); $this->get(route('system-settings.databases.edit', $connection->id)) ->assertOk() ->assertDontSee('original-secret'); } public function test_global_wildcard_scope_is_rejected(): void { $this->withExceptionHandling(); $this->loginAs() ->post(route('system-settings.databases.store'), $this->payload([ 'allowed_scopes' => '*', ])) ->assertSessionHasErrors('allowed_scopes'); $this->assertDatabaseMissing('system_database_connections', ['name' => '测试云数据库']); } public function test_regular_admin_cannot_open_database_settings(): void { $this->user->roles()->sync([ Role::where('name', 'admin')->firstOrFail()->id, ]); $this->withExceptionHandling(); $this->loginAs() ->get(route('system-settings.databases.index')) ->assertForbidden(); } public function test_database_page_separates_system_and_cloud_database_configuration(): void { $connectionName = (string) config('database.default'); config(["database.connections.$connectionName.password" => 'system-database-test-secret']); $this->loginAs() ->get(route('system-settings.databases.index')) ->assertOk() ->assertSeeText('当前系统数据库') ->assertSeeText('修改系统数据库') ->assertSeeText('云数据库'); $this->get(route('system-settings.databases.system.edit')) ->assertOk() ->assertSeeText('测试并保存') ->assertDontSee('system-database-test-secret'); } public function test_production_stored_configuration_requires_explicit_private_flag(): void { $connection = $this->connection('production-secret'); $connection->update(['source_key' => 'jiansun']); $originalEnvironment = $this->app->environment(); try { $this->app['env'] = 'production'; config(['upstreams.stored_configuration.enabled' => false]); $disabled = app(UpstreamStoredConfiguration::class); $this->assertFalse($disabled->editable()); $this->assertNull($disabled->find('jiansun')); config(['upstreams.stored_configuration.enabled' => true]); $enabled = new UpstreamStoredConfiguration(); $this->assertTrue($enabled->editable()); $this->assertSame($connection->id, $enabled->find('jiansun')?->id); } finally { $this->app['env'] = $originalEnvironment; } } private function payload(array $overrides = []): array { return array_merge([ 'name' => '测试云数据库', 'source_key' => '', 'host' => '127.0.0.1', 'port' => 1433, 'database_name' => 'test_finance', 'username' => 'readonly_user', 'password' => 'secret', 'odbc_driver' => 'FreeTDS', 'tds_version' => '7.4', 'connect_timeout' => 3, 'query_timeout' => 8, 'allowed_scopes' => 'supplier.options', 'enabled' => '1', ], $overrides); } private function connection(string $password): DatabaseConnection { return DatabaseConnection::create([ 'company_id' => 0, 'source_key' => null, 'name' => '已有云数据库', 'driver' => 'sqlsrv', 'host' => '127.0.0.1', 'port' => 1433, 'database_name' => 'test_finance', 'username' => 'readonly_user', 'password' => $password, 'odbc_driver' => 'FreeTDS', 'tds_version' => '7.4', 'connect_timeout' => 3, 'query_timeout' => 8, 'allowed_scopes' => ['supplier.options'], 'charset' => 'UTF-8', 'enabled' => true, ]); } }