This commit is contained in:
2024-03-17 12:30:19 +01:00
commit c805b5ebaa
133 changed files with 24036 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Str;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;
class AuthenticationTest extends TestCase
{
use RefreshDatabase;
public function test_users_can_authenticate_using_the_login_screen(): void
{
$user = User::factory()->create();
$response = $this->postJson('/api/v1/login', [
'email' => $user->email,
'password' => 'password',
]);
$response->assertStatus(200);
$response->assertJson(fn (AssertableJson $json) => $json
->hasAll(['ok', 'token'])
);
}
public function test_users_can_not_authenticate_with_invalid_password(): void
{
$user = User::factory()->create();
$response = $this->post('/api/v1/login', [
'email' => $user->email,
'password' => 'wrong-password',
]);
$response->assertJson(fn (AssertableJson $json) => $json
->hasAll(['ok', 'message', 'errors'])
->missing('token')
);
}
public function test_users_can_logout(): void
{
$user = User::factory()->create([
'ulid' => Str::ulid()->toBase32(),
]);
$token = $user->createToken('test-token')->plainTextToken;
$response = $this->post('/api/v1/logout', [], [
'Authorization' => 'Bearer '.$token,
]);
$response->assertJson(['ok' => true], true);
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\URL;
use Str;
use Tests\TestCase;
class EmailVerificationTest extends TestCase
{
use RefreshDatabase;
public function test_email_can_be_verified(): void
{
$this->withoutMiddleware();
$user = User::factory()->create([
'email_verified_at' => null,
'ulid' => Str::ulid()->toBase32(),
]);
Event::fake();
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['ulid' => $user->ulid, 'hash' => sha1($user->email)]
);
$response = $this->get($verificationUrl);
Event::assertDispatched(Verified::class);
$this->assertTrue($user->fresh()->hasVerifiedEmail());
$response->assertJson(['ok' => true], true);
}
public function test_email_is_not_verified_with_invalid_hash(): void
{
$this->withoutMiddleware();
$user = User::factory()->create([
'email_verified_at' => null,
'ulid' => Str::ulid()->toBase32(),
]);
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['ulid' => $user->ulid, 'hash' => sha1('wrong-email')]
);
$this->get($verificationUrl);
$this->assertFalse($user->fresh()->hasVerifiedEmail());
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;
class PasswordResetTest extends TestCase
{
use RefreshDatabase;
public function test_reset_password_link_can_be_requested(): void
{
$this->withoutMiddleware();
$user = User::factory()->create();
$response = $this->post('/api/v1/forgot-password', ['email' => $user->email]);
$response->assertOk();
$response->assertJson(fn (AssertableJson $json) => $json
->hasAll(['ok', 'message'])
);
}
public function test_password_can_be_reset_with_valid_token(): void
{
$this->withoutMiddleware();
Notification::fake();
$user = User::factory()->create();
$this->post('/api/v1/forgot-password', ['email' => $user->email]);
Notification::assertSentTo($user, ResetPassword::class, function (object $notification) use ($user) {
$response = $this->post('/api/v1/reset-password', [
'token' => $notification->token,
'email' => $user->email,
'password' => 'password',
'password_confirmation' => 'password',
]);
$response->assertJson(fn (AssertableJson $json) => $json
->hasAll(['ok', 'message'])
->missing('errors')
);
return true;
});
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Tests\Feature\Auth;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Spatie\Permission\Models\Role;
use Tests\TestCase;
class RegistrationTest extends TestCase
{
use RefreshDatabase;
public function test_new_users_can_register(): void
{
$role = Role::create(['name' => 'user']);
$response = $this->post('/api/v1/register', [
'name' => 'Test User',
'email' => 'test@example.com',
'password' => 'password',
'password_confirmation' => 'password',
]);
$response->assertCreated();
$response->assertJson(['ok' => true], true);
$role->delete();
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Tests\Feature;
// use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*/
public function test_the_application_returns_a_successful_response(): void
{
$response = $this->get('/');
$response->assertStatus(200);
}
}

10
tests/TestCase.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
//
}

View File

@@ -0,0 +1,16 @@
<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*/
public function test_that_true_is_true(): void
{
$this->assertTrue(true);
}
}