mirror of https://github.com/kcal-app/kcal.git
Update tests for User refactoring
This commit is contained in:
parent
ea4b6b2eae
commit
beb69b4a0e
|
@ -40,6 +40,11 @@ class UserAdd extends Command
|
||||||
$arguments['username'] = $this->ask('Enter a username for the user');
|
$arguments['username'] = $this->ask('Enter a username for the user');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (empty($arguments['username'])) {
|
||||||
|
$this->error('Username is required.');
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
// Check for an existing user.
|
// Check for an existing user.
|
||||||
if (User::whereUsername($arguments['username'])->exists()) {
|
if (User::whereUsername($arguments['username'])->exists()) {
|
||||||
$this->error("User `{$arguments['username']}` already exists.");
|
$this->error("User `{$arguments['username']}` already exists.");
|
||||||
|
@ -67,11 +72,6 @@ class UserAdd extends Command
|
||||||
$options['name'] = $this->ask('Enter a name for the user (optional)');
|
$options['name'] = $this->ask('Enter a name for the user (optional)');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($arguments['username']) || empty($arguments['password'])) {
|
|
||||||
$this->error('Username and password must be provided.');
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
User::create([
|
User::create([
|
||||||
'username' => $arguments['username'],
|
'username' => $arguments['username'],
|
||||||
'password' => Hash::make($arguments['password']),
|
'password' => Hash::make($arguments['password']),
|
||||||
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Console;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class UserAddTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
public function testCanAddUserInteractively(): void
|
||||||
|
{
|
||||||
|
/** @var \App\Models\User $user */
|
||||||
|
$user = User::factory()->makeOne();
|
||||||
|
$this->artisan('user:add')
|
||||||
|
->expectsQuestion('Enter a username for the user', $user->username)
|
||||||
|
->expectsQuestion('Enter a password for the user (leave blank for a random password)', 'password')
|
||||||
|
->expectsQuestion('Re-type the password to confirm', 'password')
|
||||||
|
->expectsQuestion('Enter a name for the user (optional)', $user->name)
|
||||||
|
->assertExitCode(0);
|
||||||
|
/** @var \App\Models\User $new_user */
|
||||||
|
$new_user = User::whereUsername($user->username)->get()->first();
|
||||||
|
$this->assertEquals($user->username, $new_user->username);
|
||||||
|
$this->assertEquals($user->name, $new_user->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCanAddUserWithArguments(): void
|
||||||
|
{
|
||||||
|
/** @var \App\Models\User $user */
|
||||||
|
$user = User::factory()->makeOne();
|
||||||
|
$parameters = [
|
||||||
|
'username' => $user->username,
|
||||||
|
'password' => 'password',
|
||||||
|
'--name' => $user->name,
|
||||||
|
];
|
||||||
|
$this->artisan('user:add', $parameters)
|
||||||
|
->assertExitCode(0);
|
||||||
|
/** @var \App\Models\User $new_user */
|
||||||
|
$new_user = User::whereUsername($user->username)->get()->first();
|
||||||
|
$this->assertEquals($user->username, $new_user->username);
|
||||||
|
$this->assertEquals($user->name, $new_user->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCanNotAddExistingUsername(): void
|
||||||
|
{
|
||||||
|
/** @var \App\Models\User $user */
|
||||||
|
$user = User::factory()->createOne(['username' => 'test_user']);
|
||||||
|
$this->artisan('user:add', ['username' => $user->username])
|
||||||
|
->expectsOutput("User `{$user->username}` already exists.")
|
||||||
|
->assertExitCode(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCanNotLeaveUsernameBlank(): void
|
||||||
|
{
|
||||||
|
/** @var \App\Models\User $user */
|
||||||
|
$this->artisan('user:add')
|
||||||
|
->expectsQuestion('Enter a username for the user', NULL)
|
||||||
|
->expectsOutput('Username is required.')
|
||||||
|
->assertExitCode(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPasswordsMustMatch(): void
|
||||||
|
{
|
||||||
|
/** @var \App\Models\User $user */
|
||||||
|
$this->artisan('user:add', ['username' => 'test_user'])
|
||||||
|
->expectsQuestion('Enter a password for the user (leave blank for a random password)', 'password')
|
||||||
|
->expectsQuestion('Re-type the password to confirm', 'password1')
|
||||||
|
->expectsOutput('Passwords did not match.')
|
||||||
|
->assertExitCode(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRandomPasswordCanBeGenerated(): void
|
||||||
|
{
|
||||||
|
/** @var \App\Models\User $user */
|
||||||
|
$this->artisan('user:add', ['username' => 'test_user', '--name' => 'Test User'])
|
||||||
|
->expectsQuestion('Enter a password for the user (leave blank for a random password)', NULL)
|
||||||
|
->expectsOutput('User `test_user` added!')
|
||||||
|
->assertExitCode(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -4,21 +4,25 @@ namespace Tests\Feature\Http\Controllers\Auth;
|
||||||
|
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Providers\RouteServiceProvider;
|
use App\Providers\RouteServiceProvider;
|
||||||
|
use Illuminate\Auth\Events\Lockout;
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Support\Facades\Event;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
|
||||||
class AuthenticationTest extends TestCase
|
class AuthenticationTest extends TestCase
|
||||||
{
|
{
|
||||||
use RefreshDatabase;
|
use RefreshDatabase;
|
||||||
|
|
||||||
public function testLoginScreenCanRendered()
|
public function testLoginScreenCanRendered(): void
|
||||||
{
|
{
|
||||||
$response = $this->get('/login');
|
$response = $this->get('/login');
|
||||||
$response->assertStatus(200);
|
$response->assertStatus(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUserCanAuthenticateUsingLoginScreen()
|
public function testUserCanAuthenticateUsingLoginScreen(): void
|
||||||
{
|
{
|
||||||
|
/** @var \App\Models\User $user */
|
||||||
$user = User::factory()->create();
|
$user = User::factory()->create();
|
||||||
$response = $this->post('/login', [
|
$response = $this->post('/login', [
|
||||||
'username' => $user->username,
|
'username' => $user->username,
|
||||||
|
@ -28,8 +32,9 @@ class AuthenticationTest extends TestCase
|
||||||
$response->assertRedirect(RouteServiceProvider::HOME);
|
$response->assertRedirect(RouteServiceProvider::HOME);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUserCannotAuthenticateWithInvalidPassword()
|
public function testUserCannotAuthenticateWithInvalidPassword(): void
|
||||||
{
|
{
|
||||||
|
/** @var \App\Models\User $user */
|
||||||
$user = User::factory()->create();
|
$user = User::factory()->create();
|
||||||
$this->post('/login', [
|
$this->post('/login', [
|
||||||
'username' => $user->username,
|
'username' => $user->username,
|
||||||
|
@ -38,9 +43,33 @@ class AuthenticationTest extends TestCase
|
||||||
$this->assertGuest();
|
$this->assertGuest();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUserCanLogout()
|
public function testUserCanLogout(): void
|
||||||
{
|
{
|
||||||
|
/** @var \App\Models\User $user */
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$this->post('/login', ['username' => $user->username, 'password' => 'password']);
|
||||||
|
$this->assertAuthenticated();
|
||||||
$this->followingRedirects()->post('/logout');
|
$this->followingRedirects()->post('/logout');
|
||||||
$this->assertGuest();
|
$this->assertGuest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testExcessiveLoginRequestsAreRateLimited(): void
|
||||||
|
{
|
||||||
|
/** @var \App\Models\User $user */
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
Event::fake();
|
||||||
|
|
||||||
|
for ($i = 0; $i < 5; $i++) {
|
||||||
|
$this->post('/login', [
|
||||||
|
'username' => $user->username,
|
||||||
|
'password' => 'wrong-password',
|
||||||
|
]);
|
||||||
|
if ($i < 5) {
|
||||||
|
Event::assertNotDispatched(Lockout::class);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Event::assertDispatched(Lockout::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue