From 6eeb04a332d671b29788388a1d5fa1e286212799 Mon Sep 17 00:00:00 2001 From: Boudewijn Date: Sun, 2 Mar 2025 22:56:41 +0100 Subject: [PATCH] CreateUser crashes on missing factory and faker. Perhaps caused by me not including the Artisan test script. For now, do not prompt for credentials or attempt to crate the user --- conf/CreateUserTest.php | 103 ++++++++++++++++++++++++++++++++++++++++ manifest.toml | 20 ++++---- scripts/install | 1 + 3 files changed, 114 insertions(+), 10 deletions(-) create mode 100644 conf/CreateUserTest.php diff --git a/conf/CreateUserTest.php b/conf/CreateUserTest.php new file mode 100644 index 0000000..a343de1 --- /dev/null +++ b/conf/CreateUserTest.php @@ -0,0 +1,103 @@ +artisan('app:create-user', [ + 'name' => 'ajaxray', + 'email' => 'user@ajaxray.com', + 'password' => '12345678', + ])->expectsOutputToContain('User created'); + + $this->assertDatabaseCount('users', 1); + } + + public function test_cannot_create_user_with_invalid_email(): void + { + $this->artisan('app:create-user', [ + 'name' => 'ajaxray', + 'email' => 'user@.com', + 'password' => '222222', + ])->assertFailed() + ->expectsOutputToContain('must be a valid email address.'); + + $this->assertDatabaseEmpty('users'); + } + + public function test_cannot_create_user_with_duplicate_name(): void + { + $this->artisan('app:create-user', [ + 'name' => 'ajaxray', + 'email' => 'user1@ajaxray.com', + 'password' => '111111', + ])->assertSuccessful(); + + $this->artisan('app:create-user', [ + 'name' => 'ajaxray', + 'email' => 'user2@ajaxray.com', + 'password' => '222222', + ])->assertFailed() + ->expectsOutputToContain('name has already been taken.'); + + $this->assertDatabaseCount('users', 1); + } + + public function test_cannot_create_user_with_duplicate_email(): void + { + $this->artisan('app:create-user', [ + 'name' => 'user1', + 'email' => 'user@ajaxray.com', + 'password' => '111111', + ])->assertSuccessful(); + + $this->artisan('app:create-user', [ + 'name' => 'user2', + 'email' => 'user@ajaxray.com', + 'password' => '222222', + ])->assertFailed() + ->expectsOutputToContain('email has already been taken.'); + + $this->assertDatabaseCount('users', 1); + } + + public function test_created_users_are_verified_by_default(): void + { + $this->assertDatabaseEmpty('users'); + + $this->freezeTime(function (Carbon $time) { + $dateTimeStr = date('Y-m-d H:i:s'); + + $this->artisan('app:create-user', [ + 'name' => 'user1', + 'email' => 'user@ajaxray.com', + 'password' => '111111', + ])->assertSuccessful(); + + $this->assertDatabaseHas('users', ['email_verified_at' => $dateTimeStr]); + }); + } + + public function test_created_users_can_be_kept_unverified(): void + { + $this->assertDatabaseEmpty('users'); + + $this->artisan('app:create-user', [ + 'name' => 'user1', + 'email' => 'user@ajaxray.com', + 'password' => '111111', + '--unverified' => true + ])->assertSuccessful(); + + $this->assertDatabaseHas('users', ['email_verified_at' => null]); + } +} diff --git a/manifest.toml b/manifest.toml index f1d89dd..484b298 100644 --- a/manifest.toml +++ b/manifest.toml @@ -46,16 +46,16 @@ ram.runtime = "100M" default = "all_users" # for now no credentials configured, make it install first - [install.user] - help.en = "The name of the user" - type = "text" - [install.email] - help.en = "The email address used for logging in to the application" - type = "email" - [install.password] - help.en = "Use the help field to add an information for the admin about this question." - help.fr = "Utilisez le champ aide pour ajouter une information à l'intention de l'administrateur à propos de cette question." - type = "password" +# [install.user] +# help.en = "The name of the user" +# type = "string" +# [install.email] +# help.en = "The email address used for logging in to the application" +# type = "email" +# [install.password] +# help.en = "Use the help field to add an information for the admin about this question." +# help.fr = "Utilisez le champ aide pour ajouter une information à l'intention de l'administrateur à propos de cette question." +# type = "password" [resources] # See the packaging documentation for the full set diff --git a/scripts/install b/scripts/install index 1b9a9c1..7ab2bca 100755 --- a/scripts/install +++ b/scripts/install @@ -47,6 +47,7 @@ chown "$app:www-data" "$install_dir/.env" # Inject CreateUser.php into app/Console/Commands/ to extend the upstream version with this functionality # Using this, we can create the user with the credentials provided ynh_config_add --template=CreateUser.php --destination="$install_dir/app/Console/Commands/CreateUser.php" +ynh_config_add --template=CreateUserTest.php --destination="$install_dir/app/Console/Commands/CreateUserTest.php" #================================================= # SYSTEM CONFIGURATION