Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Test that casting tenancy_db_password to encrypted works as expected #1284

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions tests/TenantDatabaseManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,42 @@
expect($manager->databaseExists($name))->toBeTrue();
});

test('decrypted password can be used to connect to a tenant db while the password is saved as encrypted', function (string|null $tenantDbPassword) {
config([
'tenancy.database.managers.mysql' => PermissionControlledMySQLDatabaseManager::class,
'tenancy.database.template_tenant_connection' => 'mysql',
]);

Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener());

// Create a tenant, either with a specific password, or with a password generated by the DB manager
$tenant = TenantWithEncryptedPassword::create([
'tenancy_db_name' => $name = 'foo' . Str::random(8),
'tenancy_db_username' => 'user' . Str::random(4),
'tenancy_db_password' => $tenantDbPassword,
]);

$decryptedPassword = $tenant->tenancy_db_password;
$encryptedPassword = $tenant->getAttributes()['tenancy_db_password']; // Password encrypted using the TenantWithEncryptedPassword model's encrypted cast
expect($decryptedPassword)->not()->toBe($encryptedPassword);

$passwordSavedInDatabase = json_decode(DB::select('SELECT data FROM tenants LIMIT 1')[0]->data)->tenancy_db_password;
expect($encryptedPassword)->toBe($passwordSavedInDatabase);

app(DatabaseManager::class)->connectToTenant($tenant);

// Check if we got connected to the tenant DB
expect(config('database.default'))->toBe('tenant');
expect(config('database.connections.tenant.database'))->toBe($name);
// Check if the decrypted password is used to connect to the tenant DB
expect(config('database.connections.tenant.password'))->toBe($decryptedPassword);
})->with([
'decrypted' . Str::random(8), // Use this password as the tenant DB password
null, // Let the DB manager generate the tenant DB password
]);

test('path used by sqlite manager can be customized', function () {
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
return $event->tenant;
Expand Down Expand Up @@ -529,3 +565,13 @@ function createUsersTable()
$table->timestamps();
});
}

class TenantWithEncryptedPassword extends Tenant
{
protected function casts(): array
{
return [
'tenancy_db_password' => 'encrypted',
];
}
}
Loading