Skip to content

A free open source IT asset/license management system

Improved user create tests

snipe8ccd2e97a8fc
diff --git a/tests/Feature/Users/Api/CreateUserTest.php b/tests/Feature/Users/Api/CreateUserTest.php
index bc7c5e2252..080224d3e7 100644
--- a/tests/Feature/Users/Api/CreateUserTest.php
+++ b/tests/Feature/Users/Api/CreateUserTest.php
@@ -5,6 +5,8 @@ namespace Tests\Feature\Users\Api;
use App\Models\Company;
use App\Models\Department;
use App\Models\User;
+use App\Notifications\WelcomeNotification;
+use Illuminate\Support\Facades\Notification;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;
@@ -58,21 +60,61 @@ class CreateUserTest extends TestCase
});
}
- public function testCanStoreUser()
+ public function testCanCreateUser()
{
$this->actingAsForApi(User::factory()->createUsers()->create())
->postJson(route('api.users.store'), [
- 'first_name' => 'Darth',
- 'username' => 'darthvader',
- 'password' => 'darth_password',
- 'password_confirmation' => 'darth_password',
+ 'first_name' => 'Test First Name',
+ 'last_name' => 'Test Last Name',
+ 'username' => 'testuser',
+ 'password' => 'testpassword1235!!',
+ 'password_confirmation' => 'testpassword1235!!',
+ 'send_welcome' => '1',
+ 'activated' => '1',
+ 'notes' => 'Test Note',
])
->assertStatusMessageIs('success')
->assertOk();
$this->assertDatabaseHas('users', [
- 'first_name' => 'Darth',
- 'username' => 'darthvader',
+ 'first_name' => 'Test First Name',
+ 'last_name' => 'Test Last Name',
+ 'username' => 'testuser',
+ 'activated' => '1',
+ 'notes' => 'Test Note',
+
+ ]);
+ }
+
+ public function testCanCreateAndNotifyUser()
+ {
+ Notification::fake();
+
+ $this->actingAsForApi(User::factory()->createUsers()->create())
+ ->postJson(route('api.users.store'), [
+ 'first_name' => 'Test First Name',
+ 'last_name' => 'Test Last Name',
+ 'username' => 'testuser',
+ 'password' => 'testpassword1235!!',
+ 'password_confirmation' => 'testpassword1235!!',
+ 'send_welcome' => '1',
+ 'activated' => '1',
+ 'email' => '[email protected]',
+ 'notes' => 'Test Note',
+ ])
+ ->assertStatusMessageIs('success')
+ ->assertOk();
+
+ $this->assertDatabaseHas('users', [
+ 'first_name' => 'Test First Name',
+ 'last_name' => 'Test Last Name',
+ 'username' => 'testuser',
+ 'activated' => '1',
+ 'email' => '[email protected]',
+ 'notes' => 'Test Note',
]);
+
+ $user = User::where('username', 'testuser')->first();
+ Notification::assertSentTo($user, WelcomeNotification::class);
}
}
diff --git a/tests/Feature/Users/Ui/CreateUserTest.php b/tests/Feature/Users/Ui/CreateUserTest.php
index d63dc40f9e..e5ee870aae 100644
--- a/tests/Feature/Users/Ui/CreateUserTest.php
+++ b/tests/Feature/Users/Ui/CreateUserTest.php
@@ -3,6 +3,8 @@
namespace Tests\Feature\Users\Ui;
use App\Models\User;
+use App\Notifications\WelcomeNotification;
+use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
class CreateUserTest extends TestCase
@@ -25,6 +27,39 @@ class CreateUserTest extends TestCase
public function testCanCreateUser()
{
+ $response = $this->actingAs(User::factory()->createUsers()->viewUsers()->create())
+ ->from(route('users.index'))
+ ->post(route('users.store'), [
+ 'first_name' => 'Test First Name',
+ 'last_name' => 'Test Last Name',
+ 'username' => 'testuser',
+ 'password' => 'testpassword1235!!',
+ 'password_confirmation' => 'testpassword1235!!',
+ 'send_welcome' => '1',
+ 'activated' => '1',
+ 'notes' => 'Test Note',
+ ])
+ ->assertSessionHasNoErrors()
+ ->assertStatus(302)
+ ->assertRedirect(route('users.index'));
+
+ $this->assertDatabaseHas('users', [
+ 'first_name' => 'Test First Name',
+ 'last_name' => 'Test Last Name',
+ 'username' => 'testuser',
+ 'activated' => '1',
+ 'notes' => 'Test Note',
+
+ ]);
+
+ $this->followRedirects($response)->assertSee('Success');
+
+ }
+
+ public function testCanCreateAndNotifyUser()
+ {
+
+ Notification::fake();
$response = $this->actingAs(User::factory()->createUsers()->viewUsers()->create())
->from(route('users.index'))
@@ -33,11 +68,27 @@ class CreateUserTest extends TestCase
'last_name' => 'Test Last Name',
'username' => 'testuser',
'password' => 'testpassword1235!!',
- //'notes' => 'Test Note',
+ 'password_confirmation' => 'testpassword1235!!',
+ 'send_welcome' => '1',
+ 'activated' => '1',
+ 'email' => '[email protected]',
+ 'notes' => 'Test Note',
])
+ ->assertSessionHasNoErrors()
->assertStatus(302)
->assertRedirect(route('users.index'));
+ $this->assertDatabaseHas('users', [
+ 'first_name' => 'Test First Name',
+ 'last_name' => 'Test Last Name',
+ 'username' => 'testuser',
+ 'activated' => '1',
+ 'email' => '[email protected]',
+ 'notes' => 'Test Note',
+ ]);
+
+ $user = User::where('username', 'testuser')->first();
+ Notification::assertSentTo($user, WelcomeNotification::class);
$this->followRedirects($response)->assertSee('Success');
}