Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu committed Dec 28, 2024
1 parent d08fea7 commit 46166a7
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/Feature/APIv1/UserSearchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Tests\Feature\APIv1;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Passport\Passport;
use Tests\ApiTestCase;

class UserSearchTest extends ApiTestCase
{

use RefreshDatabase;

public function testUserSearch(): void {
$alice = User::factory(['name' => 'Alice', 'username' => 'alice'])->create();
$bob = User::factory(['name' => 'Bob', 'username' => 'bob'])->create();
$charlie = User::factory(['name' => 'Charlie', 'username' => 'charlie'])->create();

Passport::actingAs($alice, ['*']);

// 1. Test Search in Path (username AND displayname) - legacy
$response = $this->getJson('/api/v1/user/search/charlie');
$response->assertOk();
$response->assertJsonCount(1, 'data');
$response->assertJsonFragment(['id' => $charlie->id]);

// 2. Test Search for username in query
$response = $this->getJson('/api/v1/user/search?username=Charlie');
$response->assertOk();
$response->assertJsonCount(1, 'data');
$response->assertJsonFragment(['id' => $charlie->id]);

// 3. Test Search for displayname in query
$response = $this->getJson('/api/v1/user/search?name=charlie');
$response->assertOk();
$response->assertJsonCount(1, 'data');
$response->assertJsonFragment(['id' => $charlie->id]);

// 4. Test without any parameters (should fail)
$response = $this->getJson('/api/v1/user/search');
$response->assertBadRequest();
}
}

0 comments on commit 46166a7

Please sign in to comment.