Steuerkram/tests/52_SettingsPasswordEncryptionTest.php

64 lines
2.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
use Steuerkram\Config;
use Steuerkram\Database;
use Steuerkram\Models\Setting;
/**
* Test 52: nextcloud_pass und mail_smtp_pass werden verschlüsselt gespeichert
* (Fix #119) und über Setting::get() wieder korrekt entschlüsselt.
*/
class SettingsPasswordEncryptionTest extends TestCase
{
protected string $testName = 'Test 52: Settings – Passwort-Verschlüsselung (Nextcloud/SMTP)';
public function run(): void
{
$oldKey = $_ENV['APP_KEY'] ?? null;
if (empty(Config::get('app_key', ''))) {
$_ENV['APP_KEY'] = str_repeat('c3', 32);
Config::load();
}
try {
$before = Setting::get();
Setting::update(array_merge($before, [
'firma' => $before['firma'] ?: 'Testfirma',
'strasse' => $before['strasse'] ?: 'Teststr. 1',
'plz' => $before['plz'] ?: '12345',
'ort' => $before['ort'] ?: 'Testort',
'nextcloud_pass' => 'geheimes-nc-passwort',
'mail_smtp_pass' => 'geheimes-smtp-passwort',
]));
// Roh-DB-Spalte muss verschlüsselt sein, nicht im Klartext
$raw = Database::fetchOne('SELECT nextcloud_pass, mail_smtp_pass FROM company_settings WHERE id = 1');
$this->assert(str_starts_with($raw['nextcloud_pass'], 'enc:v1:'), 'nextcloud_pass ist in der DB verschlüsselt');
$this->assert(str_starts_with($raw['mail_smtp_pass'], 'enc:v1:'), 'mail_smtp_pass ist in der DB verschlüsselt');
// Setting::get() liefert wieder den Klartext
$after = Setting::get();
$this->assertEquals('geheimes-nc-passwort', $after['nextcloud_pass'], 'nextcloud_pass entschlüsselt korrekt');
$this->assertEquals('geheimes-smtp-passwort', $after['mail_smtp_pass'], 'mail_smtp_pass entschlüsselt korrekt');
// Leerer Wert wird nicht "verschlüsselt" (Crypto::encrypt('') === '') und bleibt leer/null
Setting::update(array_merge($after, [
'nextcloud_pass' => '',
'mail_smtp_pass' => '',
]));
$rawEmpty = Database::fetchOne('SELECT nextcloud_pass, mail_smtp_pass FROM company_settings WHERE id = 1');
$this->assert(empty($rawEmpty['nextcloud_pass']), 'nextcloud_pass wird bei leerem Wert nicht verschlüsselt gespeichert');
$this->assert(empty($rawEmpty['mail_smtp_pass']), 'mail_smtp_pass wird bei leerem Wert nicht verschlüsselt gespeichert');
} finally {
if ($oldKey === null) {
unset($_ENV['APP_KEY']);
} else {
$_ENV['APP_KEY'] = $oldKey;
}
Config::load();
}
}
}