diff --git a/src/Commands/InitCommand.php b/src/Commands/InitCommand.php index ec30e24..222c940 100644 --- a/src/Commands/InitCommand.php +++ b/src/Commands/InitCommand.php @@ -428,22 +428,26 @@ protected function runMigrations(): void protected function createAdminUser(string $kebabName, string $serviceKey = '', string $serviceName = ''): array { - $adminEmail = when(empty($serviceKey), "admin@{$kebabName}.com", "admin.{$serviceKey}@{$kebabName}.com"); + $isServiceAdmin = (!empty($serviceKey) && !empty($serviceName)); + + $adminEmail = when($isServiceAdmin, "admin.{$serviceKey}@{$kebabName}.com", "admin@{$kebabName}.com"); $defaultPassword = substr(md5(uniqid()), 0, 8); - $serviceLabel = when(!empty($serviceName), " for {$serviceName}"); + $serviceLabel = when($isServiceAdmin, " for {$serviceName}"); $adminCredentials = [ 'email' => $this->ask("Please enter admin email{$serviceLabel}", $adminEmail), 'password' => $this->ask("Please enter admin password{$serviceLabel}", $defaultPassword), ]; + $adminName = when($isServiceAdmin, "{$serviceName} Admin", 'Admin'); + if ($this->authType === AuthTypeEnum::None) { - $adminCredentials['name'] = $this->ask("Please enter admin name{$serviceLabel}", "{$serviceName} Admin"); + $adminCredentials['name'] = $this->ask("Please enter admin name{$serviceLabel}", $adminName); $adminCredentials['role_id'] = $this->ask("Please enter admin role id{$serviceLabel}", RoleEnum::Admin->value); } - if (empty($serviceName)) { + if (!$isServiceAdmin) { $this->adminCredentials = $adminCredentials; } diff --git a/tests/InitCommandTest.php b/tests/InitCommandTest.php index 4316a0e..3bc9866 100644 --- a/tests/InitCommandTest.php +++ b/tests/InitCommandTest.php @@ -2,6 +2,10 @@ namespace RonasIT\ProjectInitializator\Tests; +use ReflectionMethod; +use ReflectionProperty; +use RonasIT\ProjectInitializator\Commands\InitCommand; +use RonasIT\ProjectInitializator\Enums\AuthTypeEnum; use RonasIT\ProjectInitializator\Tests\Support\Traits\InitCommandMockTrait; class InitCommandTest extends TestCase @@ -926,4 +930,50 @@ public function testRunWithClerkAdditionalAdminsWithoutDefaultAdmin(): void ->expectsConfirmation('Do you want to uninstall project-initializator package?') ->assertExitCode(0); } + + /** + * @runInSeparateProcess + */ + public function testDefaultAdminsCredentials() + { + $this->mockNativeFunction( + 'RonasIT\ProjectInitializator\Commands', + $this->mockAdminDefaultPassword('123456'), + $this->callFilePutContent('database/migrations/2018_11_11_111111_add_default_admin.php', $this->getFixture('users_default_admin_default_credentials.php')), + $this->mockAdminDefaultPassword('654321'), + $this->callFilePutContent('database/migrations/2018_11_11_111111_add_telescope_admin.php', $this->getFixture('telescope_users_default_credentials.php')), + $this->mockAdminDefaultPassword('123456'), + $this->callFilePutContent('database/migrations/2018_11_11_111111_add_nova_admin.php', $this->getFixture('nova_users_default_credentials.php')), + $this->mockAdminDefaultPassword('123456'), + $this->callFilePutContent('database/migrations/2018_11_11_111111_add_default_admin.php', $this->getFixture('admins_add_default_admin_default_credentials.php')), + $this->mockAdminDefaultPassword('654321'), + $this->callFilePutContent('database/migrations/2018_11_11_111111_add_telescope_admin.php', $this->getFixture('admins_add_telescope_admin_default_credentials.php')), + $this->mockAdminDefaultPassword('123456'), + $this->callFilePutContent('database/migrations/2018_11_11_111111_add_nova_admin.php', $this->getFixture('admins_add_nova_admin_default_credentials.php')), + ); + + $commandMock = $this + ->getMockBuilder(InitCommand::class) + ->disableOriginalConstructor() + ->getMock(); + + $commandMock->method('ask')->willReturnCallback(fn ($question, $default) => $default); + + $authTypeProperty = new ReflectionProperty(InitCommand::class, 'authType'); + $authTypeProperty->setAccessible(true); + + $createAdminMethod = new ReflectionMethod(InitCommand::class, 'createAdminUser'); + + $authTypeProperty->setValue($commandMock, AuthTypeEnum::None); + + $createAdminMethod->invokeArgs($commandMock, ['my-app']); + $createAdminMethod->invokeArgs($commandMock, ['my-app', 'telescope', 'Laravel Telescope']); + $createAdminMethod->invokeArgs($commandMock, ['my-app', 'nova', 'Laravel Nova']); + + $authTypeProperty->setValue($commandMock, AuthTypeEnum::Clerk); + + $createAdminMethod->invokeArgs($commandMock, ['my-app']); + $createAdminMethod->invokeArgs($commandMock, ['my-app', 'telescope', 'Laravel Telescope']); + $createAdminMethod->invokeArgs($commandMock, ['my-app', 'nova', 'Laravel Nova']); + } } diff --git a/tests/Support/Traits/InitCommandMockTrait.php b/tests/Support/Traits/InitCommandMockTrait.php index 78abaf1..1cf90b4 100644 --- a/tests/Support/Traits/InitCommandMockTrait.php +++ b/tests/Support/Traits/InitCommandMockTrait.php @@ -60,4 +60,13 @@ protected function changeConfigFileCall(string $fileName, string $sourceFixture, $this->callFilePutContent(base_path($fileName), $this->getFixture($resultFixture)), ]; } + + protected function mockAdminDefaultPassword(string $resultPassword): array + { + return [ + $this->functionCall('substr', ['0058a062', 0, 8], $resultPassword), + $this->functionCall('md5', ['0058a062'], '0058a062'), + $this->functionCall('uniqid', [], '0058a062'), + ]; + } } diff --git a/tests/fixtures/InitCommandTest/admins_add_default_admin_default_credentials.php b/tests/fixtures/InitCommandTest/admins_add_default_admin_default_credentials.php new file mode 100644 index 0000000..00e43f0 --- /dev/null +++ b/tests/fixtures/InitCommandTest/admins_add_default_admin_default_credentials.php @@ -0,0 +1,28 @@ +insert([ + 'email' => 'admin@my-app.com', + 'password' => Hash::make('123456'), + ]); + } + } + + public function down(): void + { + if (!App::environment('testing')) { + DB::table('admins') + ->where('email', 'admin@my-app.com') + ->delete(); + } + } +}; diff --git a/tests/fixtures/InitCommandTest/admins_add_nova_admin_default_credentials.php b/tests/fixtures/InitCommandTest/admins_add_nova_admin_default_credentials.php new file mode 100644 index 0000000..d0dcf0c --- /dev/null +++ b/tests/fixtures/InitCommandTest/admins_add_nova_admin_default_credentials.php @@ -0,0 +1,28 @@ +insert([ + 'email' => 'admin.nova@my-app.com', + 'password' => Hash::make('123456'), + ]); + } + } + + public function down(): void + { + if (!App::environment('testing')) { + DB::table('admins') + ->where('email', 'admin.nova@my-app.com') + ->delete(); + } + } +}; diff --git a/tests/fixtures/InitCommandTest/admins_add_telescope_admin_default_credentials.php b/tests/fixtures/InitCommandTest/admins_add_telescope_admin_default_credentials.php new file mode 100644 index 0000000..0838c4b --- /dev/null +++ b/tests/fixtures/InitCommandTest/admins_add_telescope_admin_default_credentials.php @@ -0,0 +1,28 @@ +insert([ + 'email' => 'admin.telescope@my-app.com', + 'password' => Hash::make('654321'), + ]); + } + } + + public function down(): void + { + if (!App::environment('testing')) { + DB::table('admins') + ->where('email', 'admin.telescope@my-app.com') + ->delete(); + } + } +}; diff --git a/tests/fixtures/InitCommandTest/nova_users_default_credentials.php b/tests/fixtures/InitCommandTest/nova_users_default_credentials.php new file mode 100644 index 0000000..7e7cb88 --- /dev/null +++ b/tests/fixtures/InitCommandTest/nova_users_default_credentials.php @@ -0,0 +1,30 @@ +insert([ + 'name' => 'Laravel Nova Admin', + 'email' => 'admin.nova@my-app.com', + 'password' => Hash::make('123456'), + 'role_id' => 1, + ]); + } + } + + public function down(): void + { + if (!App::environment('testing')) { + DB::table('users') + ->where('email', 'admin.nova@my-app.com') + ->delete(); + } + } +}; diff --git a/tests/fixtures/InitCommandTest/telescope_users_default_credentials.php b/tests/fixtures/InitCommandTest/telescope_users_default_credentials.php new file mode 100644 index 0000000..de69839 --- /dev/null +++ b/tests/fixtures/InitCommandTest/telescope_users_default_credentials.php @@ -0,0 +1,30 @@ +insert([ + 'name' => 'Laravel Telescope Admin', + 'email' => 'admin.telescope@my-app.com', + 'password' => Hash::make('654321'), + 'role_id' => 1, + ]); + } + } + + public function down(): void + { + if (!App::environment('testing')) { + DB::table('users') + ->where('email', 'admin.telescope@my-app.com') + ->delete(); + } + } +}; diff --git a/tests/fixtures/InitCommandTest/users_default_admin_default_credentials.php b/tests/fixtures/InitCommandTest/users_default_admin_default_credentials.php new file mode 100644 index 0000000..b4608fa --- /dev/null +++ b/tests/fixtures/InitCommandTest/users_default_admin_default_credentials.php @@ -0,0 +1,30 @@ +insert([ + 'name' => 'Admin', + 'email' => 'admin@my-app.com', + 'password' => Hash::make('123456'), + 'role_id' => 1, + ]); + } + } + + public function down(): void + { + if (!App::environment('testing')) { + DB::table('users') + ->where('email', 'admin@my-app.com') + ->delete(); + } + } +};