96 lines
3.3 KiB
PHP
96 lines
3.3 KiB
PHP
<?php
|
|
|
|
use App\Actions\Fortify\CreateNewUser;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
|
|
function createUserTest(array $userInfo, $error_message)
|
|
{
|
|
$action = new CreateNewUser();
|
|
try {
|
|
$action->create($userInfo);
|
|
expect(true)->toBeFalse(
|
|
$error_message
|
|
);
|
|
User::where('login', $userInfo['username'])->delete();
|
|
} catch (Exception $e) {
|
|
expect($e)->toBeInstanceOf(Exception::class);
|
|
}
|
|
}
|
|
|
|
describe('CreateNewUser Action', function () {
|
|
it('should create a new dj user successfully', function () {
|
|
$action = new CreateNewUser();
|
|
$userInfo = [
|
|
'username' => 'testuser',
|
|
'password' => 'password',
|
|
'password_confirmation' => 'password',
|
|
'email' => 'email',
|
|
'type' => 'dj',
|
|
];
|
|
|
|
$user = $action->create($userInfo);
|
|
|
|
expect($user)->toBeInstanceOf(User::class);
|
|
expect($user->login)->toBe('testuser');
|
|
expect(Hash::check('password', $user->pass))->toBeTrue();
|
|
$this->assertDatabaseHas('cc_subjs', ['login' => 'testuser']);
|
|
|
|
// Clean up the created user
|
|
$user->delete();
|
|
});
|
|
|
|
it('should fail validation if username is missing', function () {
|
|
$userInfo = [
|
|
'password' => 'password',
|
|
'password_confirmation' => 'password',
|
|
];
|
|
|
|
$errorMessage = 'Test failed because no exception was thrown when creating a user without an username.';
|
|
createUserTest($userInfo, $errorMessage);
|
|
});
|
|
|
|
it('should fail validation if email is missing', function () {
|
|
$userInfo = [
|
|
'username' => 'blabla',
|
|
'password' => 'password',
|
|
'password_confirmation' => 'password',
|
|
];
|
|
$errorMessage = 'Test failed because no exception was thrown when creating a user without an email.';
|
|
createUserTest($userInfo, $errorMessage);
|
|
});
|
|
|
|
it('should fail validation if username is not unique', function () {
|
|
$userInfo = [
|
|
'username' => 'existinguser',
|
|
'password' => 'password',
|
|
'password_confirmation' => 'password',
|
|
];
|
|
|
|
$errorMessage = 'Test failed because no exception was thrown when creating a user with a duplicate username.';
|
|
createUserTest($userInfo, $errorMessage);
|
|
});
|
|
|
|
it('should fail validation if password does not meet complexity rules', function () {
|
|
$userInfo = [
|
|
'username' => 'testuser',
|
|
'password' => 'weak', // Does not meet default password rules
|
|
'password_confirmation' => 'weak',
|
|
];
|
|
|
|
$errorMessage = 'Test failed because no exception was thrown when validating passwd.';
|
|
createUserTest($userInfo, $errorMessage);
|
|
});
|
|
|
|
it('should return an exception on validation failure', function () {
|
|
$userInfo = [
|
|
'username' => null,
|
|
'password' => 'password',
|
|
'password_confirmation' => 'password'
|
|
];
|
|
|
|
$errorMessage = 'Test failed because no exception was thrown when validating passwd.';
|
|
createUserTest($userInfo, $errorMessage);
|
|
});
|
|
});
|