sintonia_webapp/tests/Unit/Auth/AuthenticationTest.php

94 lines
2.8 KiB
PHP

<?php
use App\Actions\Fortify\CreateNewUser;
use App\Actions\Fortify\LoginUser;
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);
$result = sendLoginInfos($userInfo);
expect($result)->toBeInstanceOf(User::class);
User::where('login', $userInfo['username'])->delete();
}
);
it('returns null for non-existent user', function () {
$userInfo = [
'username' => 'nonexistantuser',
'password' => 'testestest',
];
$result = sendLoginInfos($userInfo);
// Assert
expect($result)->toBeNull();
});
it(
/**
* @throws Exception
*/
'returns password error for existent user',
function () {
$userInfo = [
'username' => 'testestest2',
'password' => 'testestest2',
'password_confirmation' => 'testestest2',
'email' => 'testestest2@testestest.testestest',
'type' => 'dj'
];
try {
$action = new CreateNewUser();
$action->create($userInfo);
$userInfo['password'] = 'wrongpass';
$result = sendLoginInfos($userInfo);
expect($result)->toBeNull();
} catch (Exception $e) {
throw new Exception($e);
} finally {
User::where('login', $userInfo['username'])->delete();
}
}
);
});