Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/Admin/UsersTest.php
<?php | <?php | ||||
namespace Tests\Feature\Controller\Admin; | namespace Tests\Feature\Controller\Admin; | ||||
use App\Domain; | |||||
use App\User; | |||||
use Tests\TestCase; | use Tests\TestCase; | ||||
class UsersTest extends TestCase | class UsersTest extends TestCase | ||||
{ | { | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function setUp(): void | public function setUp(): void | ||||
{ | { | ||||
parent::setUp(); | parent::setUp(); | ||||
self::useAdminUrl(); | |||||
// This will set base URL for all tests in this file | $jack = $this->getTestUser('jack@kolab.org'); | ||||
// If we wanted to access both user and admin in one test | $jack->setSetting('external_email', null); | ||||
// we can also just call post/get/whatever with full url | |||||
\config(['app.url' => str_replace('//', '//admin.', \config('app.url'))]); | |||||
} | } | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
$jack = $this->getTestUser('jack@kolab.org'); | |||||
$jack->setSetting('external_email', null); | |||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test (/api/v4/index) | * Test users searching (/api/v4/users) | ||||
*/ | */ | ||||
public function testIndex(): void | public function testIndex(): void | ||||
{ | { | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$admin = $this->getTestUser('jeroen@jeroen.jeroen'); | $admin = $this->getTestUser('jeroen@jeroen.jeroen'); | ||||
// Non-admin user | |||||
$response = $this->actingAs($user)->get("api/v4/users"); | $response = $this->actingAs($user)->get("api/v4/users"); | ||||
$response->assertStatus(403); | $response->assertStatus(403); | ||||
// Search with no search criteria | |||||
$response = $this->actingAs($admin)->get("api/v4/users"); | $response = $this->actingAs($admin)->get("api/v4/users"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
// TODO: Test the response | $json = $response->json(); | ||||
$this->markTestIncomplete(); | |||||
$this->assertSame(0, $json['count']); | |||||
$this->assertSame([], $json['list']); | |||||
// Search with no matches expected | |||||
$response = $this->actingAs($admin)->get("api/v4/users?search=abcd1234efgh5678"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertSame(0, $json['count']); | |||||
$this->assertSame([], $json['list']); | |||||
// Search by domain | |||||
$response = $this->actingAs($admin)->get("api/v4/users?search=kolab.org"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertSame(1, $json['count']); | |||||
$this->assertCount(1, $json['list']); | |||||
$this->assertSame($user->id, $json['list'][0]['id']); | |||||
$this->assertSame($user->email, $json['list'][0]['email']); | |||||
// Search by user ID | |||||
$response = $this->actingAs($admin)->get("api/v4/users?search={$user->id}"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertSame(1, $json['count']); | |||||
$this->assertCount(1, $json['list']); | |||||
$this->assertSame($user->id, $json['list'][0]['id']); | |||||
$this->assertSame($user->email, $json['list'][0]['email']); | |||||
// Search by email (primary) | |||||
$response = $this->actingAs($admin)->get("api/v4/users?search=john@kolab.org"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertSame(1, $json['count']); | |||||
$this->assertCount(1, $json['list']); | |||||
$this->assertSame($user->id, $json['list'][0]['id']); | |||||
$this->assertSame($user->email, $json['list'][0]['email']); | |||||
// Search by email (alias) | |||||
$response = $this->actingAs($admin)->get("api/v4/users?search=john.doe@kolab.org"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertSame(1, $json['count']); | |||||
$this->assertCount(1, $json['list']); | |||||
$this->assertSame($user->id, $json['list'][0]['id']); | |||||
$this->assertSame($user->email, $json['list'][0]['email']); | |||||
// Search by email (external), expect two users in a result | |||||
$jack = $this->getTestUser('jack@kolab.org'); | |||||
$jack->setSetting('external_email', 'john.doe.external@gmail.com'); | |||||
$response = $this->actingAs($admin)->get("api/v4/users?search=john.doe.external@gmail.com"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertSame(2, $json['count']); | |||||
$this->assertCount(2, $json['list']); | |||||
$emails = array_column($json['list'], 'email'); | |||||
$this->assertContains($user->email, $emails); | |||||
$this->assertContains($jack->email, $emails); | |||||
} | } | ||||
} | } |