diff --git a/phpunit.xml b/phpunit.xml index bc86714..0f4dfc4 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -21,8 +21,12 @@ - - + + + + + + diff --git a/tests/Feature/Auth/RegistrationTest.php b/tests/Feature/Auth/RegistrationTest.php index d7510ab..1190677 100644 --- a/tests/Feature/Auth/RegistrationTest.php +++ b/tests/Feature/Auth/RegistrationTest.php @@ -1,15 +1,22 @@ '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 () { $response = postJson(route('register'), [ diff --git a/tests/Pest.php b/tests/Pest.php index c63c07f..42f1dc6 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -14,6 +14,7 @@ use Tests\TestCase; */ uses(TestCase::class)->in('Feature'); +uses(TestCase::class)->in('Unit'); /* |-------------------------------------------------------------------------- diff --git a/tests/Unit/Auth/AuthenticationTest.php b/tests/Unit/Auth/AuthenticationTest.php index eeed0dd..e38f16d 100644 --- a/tests/Unit/Auth/AuthenticationTest.php +++ b/tests/Unit/Auth/AuthenticationTest.php @@ -1,59 +1,94 @@ $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 () { - $userInfo = [ - 'username' => 'testuser', - 'password' => 'password', - ]; + $result = sendLoginInfos($userInfo); - $response = post( - '/login', - ['username' => $userInfo['username'], 'password' => $userInfo['password']], - ['Accept' => 'application/json'] + expect($result)->toBeInstanceOf(User::class); + User::where('login', $userInfo['username'])->delete(); + } ); - $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('logs in a normal user with hashed password', function () { - // Arrange - $request = (object)['username' => 'testestest', 'password' => 'testestest']; - - // Act - $result = LoginUser::login($request); - - // Assert - expect($result)->toBe($this->user); -}); - -it('returns null for non-existent user', function () { - // Arrange - $request = (object)['username' => 'nonexistent', 'password' => 'password']; - $this->userMock->shouldReceive('where->first')->andReturn(null); - - // Act - $result = LoginUser::login($request); - - // Assert - expect($result)->toBeNull(); -}); - -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(); + 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(); + } + } + ); }); diff --git a/tests/Unit/Auth/RegistrationTest.php b/tests/Unit/Auth/RegistrationTest.php index 002a50f..ea0965c 100644 --- a/tests/Unit/Auth/RegistrationTest.php +++ b/tests/Unit/Auth/RegistrationTest.php @@ -3,12 +3,11 @@ use App\Actions\Fortify\CreateNewUser; use App\Models\User; 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 { $action->create($userInfo); expect(true)->toBeFalse( @@ -21,13 +20,14 @@ function createUserTest(CreateNewUser $action, array $userInfo, $error_message) } describe('CreateNewUser Action', function () { - it('should create a new user successfully', 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); @@ -42,34 +42,26 @@ describe('CreateNewUser Action', function () { }); it('should fail validation if username is missing', function () { - $action = new CreateNewUser(); $userInfo = [ 'password' => 'password', 'password_confirmation' => 'password', ]; $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 () { - $action = new CreateNewUser(); - $userInfo = [ + $userInfo = [ 'username' => 'blabla', 'password' => 'password', 'password_confirmation' => 'password', ]; $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 () { - User::create([ - 'login' => 'existinguser', - 'pass' => Hash::make('password'), - ]); - - $action = new CreateNewUser(); $userInfo = [ 'username' => 'existinguser', '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.'; - createUserTest($action, $userInfo, $errorMessage); + createUserTest($userInfo, $errorMessage); }); it('should fail validation if password does not meet complexity rules', function () { - $action = new CreateNewUser(); $userInfo = [ 'username' => 'testuser', '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.'; - createUserTest($action, $userInfo, $errorMessage); - + createUserTest($userInfo, $errorMessage); }); it('should return an exception on validation failure', function () { - $action = new CreateNewUser(); $userInfo = [ 'username' => null, 'password' => 'password', @@ -102,6 +91,6 @@ describe('CreateNewUser Action', function () { ]; $errorMessage = 'Test failed because no exception was thrown when validating passwd.'; - createUserTest($action, $userInfo, $errorMessage); + createUserTest($userInfo, $errorMessage); }); }); diff --git a/tests/Unit/ExampleTest.php b/tests/Unit/ExampleTest.php index 44a4f33..fd998e9 100644 --- a/tests/Unit/ExampleTest.php +++ b/tests/Unit/ExampleTest.php @@ -1,5 +1,7 @@ assertNotNull(DB::connection()->getPdo()); expect(true)->toBeTrue(); });