feat(tests): registration tests

This commit is contained in:
Michael 2025-02-11 16:07:09 +01:00
parent 7c30dd459a
commit a9baa6c526
6 changed files with 114 additions and 76 deletions

View file

@ -21,8 +21,12 @@
<env name="APP_ENV" value="testing"/> <env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/> <env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/> <env name="CACHE_DRIVER" value="array"/>
<!-- <env name="DB_CONNECTION" value="sqlite"/> --> <env name="DB_CONNECTION" value="pgsql"/>
<!-- <env name="DB_DATABASE" value=":memory:"/> --> <env name="DB_HOST" value="127.0.0.1"/>
<env name="DB_PORT" value="5432"/>
<env name="DB_DATABASE" value="libretime"/>
<env name="DB_USERNAME" value="libretime"/>
<env name="DB_PASSWORD" value="libretime"/>
<env name="MAIL_MAILER" value="array"/> <env name="MAIL_MAILER" value="array"/>
<env name="PULSE_ENABLED" value="false"/> <env name="PULSE_ENABLED" value="false"/>
<env name="QUEUE_CONNECTION" value="sync"/> <env name="QUEUE_CONNECTION" value="sync"/>

View file

@ -1,15 +1,22 @@
<?php <?php
use App\Http\Controllers\Auth\RegistrationController;
use App\Models\User; use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Laravel\Fortify\Contracts\CreatesNewUsers; use Laravel\Fortify\Contracts\CreatesNewUsers;
use Mockery;
use function Pest\Laravel\postJson; use function Pest\Laravel\postJson;
uses(RefreshDatabase::class); it('should return unauthenticated error', function () {
$response = postJson(route('/register'), [
'username' => 'unauthenticatedError',
'password' => 'password',
'password_confirmation' => 'password',
'email' => 'unauthenticatedError@unauthenticatedError.test',
'type' => 'dj',
]);
$response->assertStatus(401)
->assertJsonValidationErrors(['username' => 'The username field is required.']);
});
it('should return an error if username is missing', function () { it('should return an error if username is missing', function () {
$response = postJson(route('register'), [ $response = postJson(route('register'), [

View file

@ -14,6 +14,7 @@ use Tests\TestCase;
*/ */
uses(TestCase::class)->in('Feature'); uses(TestCase::class)->in('Feature');
uses(TestCase::class)->in('Unit');
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------

View file

@ -1,59 +1,94 @@
<?php <?php
use App\Actions\Fortify\CreateNewUser;
use App\Actions\Fortify\LoginUser; use App\Actions\Fortify\LoginUser;
use function Pest\Laravel\post; use App\Models\User;
use Illuminate\Http\Request;
describe('LoginUser Action', function () {
function sendLoginInfos(array $userInfo)
{
$loginAction = new LoginUser();
$request = Request::create(
'/register',
'POST',
['username' => $userInfo['username'], 'password' => $userInfo['password']]
);
return $loginAction->login($request);
}
it('logs in a user with MD5 password', function () {
$userInfo = [
'username' => 'admin',
'password' => 'admin',
];
$result = sendLoginInfos($userInfo);
expect($result)->toBeInstanceOf(User::class);
});
it(
/**
* @throws Exception
*/
'logs in a normal user with hashed password',
function () {
$userInfo = [
'username' => 'authenticationtestHashedPass',
'password' => 'authenticationtestHashedPass',
'password_confirmation' => 'authenticationtestHashedPass',
'email' => 'authenticationtestHashedPass@testestest.testestest',
'type' => 'dj'
];
$action = new CreateNewUser();
$action->create($userInfo);
it('logs in a user with MD5 password', function () { $result = sendLoginInfos($userInfo);
$userInfo = [
'username' => 'testuser',
'password' => 'password',
];
$response = post( expect($result)->toBeInstanceOf(User::class);
'/login', User::where('login', $userInfo['username'])->delete();
['username' => $userInfo['username'], 'password' => $userInfo['password']], }
['Accept' => 'application/json']
); );
$response->assertStatus(200); it('returns null for non-existent user', function () {
$userInfo = [
'username' => 'nonexistantuser',
'password' => 'testestest',
];
$result = sendLoginInfos($userInfo);
$data = $response->json()['data']; // Assert
expect($result)->toBeNull();
});
expect($data['username'])->toBe($userInfo['password']); it(
}); /**
* @throws Exception
it('logs in a normal user with hashed password', function () { */
// Arrange 'returns password error for existent user',
$request = (object)['username' => 'testestest', 'password' => 'testestest']; function () {
$userInfo = [
// Act 'username' => 'testestest2',
$result = LoginUser::login($request); 'password' => 'testestest2',
'password_confirmation' => 'testestest2',
// Assert 'email' => 'testestest2@testestest.testestest',
expect($result)->toBe($this->user); 'type' => 'dj'
}); ];
it('returns null for non-existent user', function () { try {
// Arrange $action = new CreateNewUser();
$request = (object)['username' => 'nonexistent', 'password' => 'password']; $action->create($userInfo);
$this->userMock->shouldReceive('where->first')->andReturn(null); $userInfo['password'] = 'wrongpass';
$result = sendLoginInfos($userInfo);
// Act expect($result)->toBeNull();
$result = LoginUser::login($request); } catch (Exception $e) {
throw new Exception($e);
// Assert } finally {
expect($result)->toBeNull(); User::where('login', $userInfo['username'])->delete();
}); }
}
it('returns password error for existent user', function () { );
// Arrange
$request = (object)['username' => 'testestest', 'password' => 'balaclava'];
$this->userMock->shouldReceive('where->first')->andReturn(null);
// Act
$result = LoginUser::login($request);
// Assert
expect($result)->toBeNull();
}); });

View file

@ -3,12 +3,11 @@
use App\Actions\Fortify\CreateNewUser; use App\Actions\Fortify\CreateNewUser;
use App\Models\User; use App\Models\User;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use Tests\TestCase;
uses(TestCase::class);
function createUserTest(CreateNewUser $action, array $userInfo, $error_message) function createUserTest(array $userInfo, $error_message)
{ {
$action = new CreateNewUser();
try { try {
$action->create($userInfo); $action->create($userInfo);
expect(true)->toBeFalse( expect(true)->toBeFalse(
@ -21,13 +20,14 @@ function createUserTest(CreateNewUser $action, array $userInfo, $error_message)
} }
describe('CreateNewUser Action', function () { describe('CreateNewUser Action', function () {
it('should create a new user successfully', function () { it('should create a new dj user successfully', function () {
$action = new CreateNewUser(); $action = new CreateNewUser();
$userInfo = [ $userInfo = [
'username' => 'testuser', 'username' => 'testuser',
'password' => 'password', 'password' => 'password',
'password_confirmation' => 'password', 'password_confirmation' => 'password',
'email' => 'email', 'email' => 'email',
'type' => 'dj',
]; ];
$user = $action->create($userInfo); $user = $action->create($userInfo);
@ -42,34 +42,26 @@ describe('CreateNewUser Action', function () {
}); });
it('should fail validation if username is missing', function () { it('should fail validation if username is missing', function () {
$action = new CreateNewUser();
$userInfo = [ $userInfo = [
'password' => 'password', 'password' => 'password',
'password_confirmation' => 'password', 'password_confirmation' => 'password',
]; ];
$errorMessage = 'Test failed because no exception was thrown when creating a user without an username.'; $errorMessage = 'Test failed because no exception was thrown when creating a user without an username.';
createUserTest($action, $userInfo, $errorMessage); createUserTest($userInfo, $errorMessage);
}); });
it('should fail validation if email is missing', function () { it('should fail validation if email is missing', function () {
$action = new CreateNewUser(); $userInfo = [
$userInfo = [
'username' => 'blabla', 'username' => 'blabla',
'password' => 'password', 'password' => 'password',
'password_confirmation' => 'password', 'password_confirmation' => 'password',
]; ];
$errorMessage = 'Test failed because no exception was thrown when creating a user without an email.'; $errorMessage = 'Test failed because no exception was thrown when creating a user without an email.';
createUserTest($action, $userInfo, $errorMessage); createUserTest($userInfo, $errorMessage);
}); });
it('should fail validation if username is not unique', function () { it('should fail validation if username is not unique', function () {
User::create([
'login' => 'existinguser',
'pass' => Hash::make('password'),
]);
$action = new CreateNewUser();
$userInfo = [ $userInfo = [
'username' => 'existinguser', 'username' => 'existinguser',
'password' => 'password', 'password' => 'password',
@ -77,11 +69,10 @@ describe('CreateNewUser Action', function () {
]; ];
$errorMessage = 'Test failed because no exception was thrown when creating a user with a duplicate username.'; $errorMessage = 'Test failed because no exception was thrown when creating a user with a duplicate username.';
createUserTest($action, $userInfo, $errorMessage); createUserTest($userInfo, $errorMessage);
}); });
it('should fail validation if password does not meet complexity rules', function () { it('should fail validation if password does not meet complexity rules', function () {
$action = new CreateNewUser();
$userInfo = [ $userInfo = [
'username' => 'testuser', 'username' => 'testuser',
'password' => 'weak', // Does not meet default password rules 'password' => 'weak', // Does not meet default password rules
@ -89,12 +80,10 @@ describe('CreateNewUser Action', function () {
]; ];
$errorMessage = 'Test failed because no exception was thrown when validating passwd.'; $errorMessage = 'Test failed because no exception was thrown when validating passwd.';
createUserTest($action, $userInfo, $errorMessage); createUserTest($userInfo, $errorMessage);
}); });
it('should return an exception on validation failure', function () { it('should return an exception on validation failure', function () {
$action = new CreateNewUser();
$userInfo = [ $userInfo = [
'username' => null, 'username' => null,
'password' => 'password', 'password' => 'password',
@ -102,6 +91,6 @@ describe('CreateNewUser Action', function () {
]; ];
$errorMessage = 'Test failed because no exception was thrown when validating passwd.'; $errorMessage = 'Test failed because no exception was thrown when validating passwd.';
createUserTest($action, $userInfo, $errorMessage); createUserTest($userInfo, $errorMessage);
}); });
}); });

View file

@ -1,5 +1,7 @@
<?php <?php
use Illuminate\Support\Facades\DB;
test('that true is true', function () { test('that true is true', function () {
$this->assertNotNull(DB::connection()->getPdo());
expect(true)->toBeTrue(); expect(true)->toBeTrue();
}); });