Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/UsersTest.php
<?php | <?php | ||||
namespace Tests\Feature\Controller; | namespace Tests\Feature\Controller; | ||||
use App\Discount; | |||||
use App\Domain; | use App\Domain; | ||||
use App\Http\Controllers\API\UsersController; | use App\Http\Controllers\API\UsersController; | ||||
use App\Package; | use App\Package; | ||||
use App\Sku; | use App\Sku; | ||||
use App\User; | use App\User; | ||||
use App\Wallet; | |||||
use Illuminate\Support\Facades\Queue; | use Illuminate\Support\Facades\Queue; | ||||
use Illuminate\Support\Str; | use Illuminate\Support\Str; | ||||
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(); | ||||
$this->deleteTestUser('UsersControllerTest1@userscontroller.com'); | $this->deleteTestUser('UsersControllerTest1@userscontroller.com'); | ||||
$this->deleteTestUser('UsersControllerTest2@userscontroller.com'); | $this->deleteTestUser('UsersControllerTest2@userscontroller.com'); | ||||
$this->deleteTestUser('UsersControllerTest3@userscontroller.com'); | $this->deleteTestUser('UsersControllerTest3@userscontroller.com'); | ||||
$this->deleteTestUser('UserEntitlement2A@UserEntitlement.com'); | $this->deleteTestUser('UserEntitlement2A@UserEntitlement.com'); | ||||
$this->deleteTestUser('john2.doe2@kolab.org'); | $this->deleteTestUser('john2.doe2@kolab.org'); | ||||
$this->deleteTestDomain('userscontroller.com'); | $this->deleteTestDomain('userscontroller.com'); | ||||
$user = $this->getTestUser('john@kolab.org'); | |||||
$wallet = $user->wallets()->first(); | |||||
$wallet->discount()->dissociate(); | |||||
$wallet->save(); | |||||
} | } | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
$this->deleteTestUser('UsersControllerTest1@userscontroller.com'); | $this->deleteTestUser('UsersControllerTest1@userscontroller.com'); | ||||
$this->deleteTestUser('UsersControllerTest2@userscontroller.com'); | $this->deleteTestUser('UsersControllerTest2@userscontroller.com'); | ||||
$this->deleteTestUser('UsersControllerTest3@userscontroller.com'); | $this->deleteTestUser('UsersControllerTest3@userscontroller.com'); | ||||
$this->deleteTestUser('UserEntitlement2A@UserEntitlement.com'); | $this->deleteTestUser('UserEntitlement2A@UserEntitlement.com'); | ||||
$this->deleteTestUser('john2.doe2@kolab.org'); | $this->deleteTestUser('john2.doe2@kolab.org'); | ||||
$this->deleteTestDomain('userscontroller.com'); | $this->deleteTestDomain('userscontroller.com'); | ||||
$user = $this->getTestUser('john@kolab.org'); | |||||
$wallet = $user->wallets()->first(); | |||||
$wallet->discount()->dissociate(); | |||||
$wallet->save(); | |||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test fetching current user info (/api/auth/info) | * Test fetching current user info (/api/auth/info) | ||||
*/ | */ | ||||
public function testInfo(): void | public function testInfo(): void | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 127 Lines • ▼ Show 20 Lines | class UsersTest extends TestCase | ||||
*/ | */ | ||||
public function testIndex(): void | public function testIndex(): void | ||||
{ | { | ||||
// Test unauth access | // Test unauth access | ||||
$response = $this->get("api/v4/users"); | $response = $this->get("api/v4/users"); | ||||
$response->assertStatus(401); | $response->assertStatus(401); | ||||
$jack = $this->getTestUser('jack@kolab.org'); | $jack = $this->getTestUser('jack@kolab.org'); | ||||
$joe = $this->getTestUser('joe@kolab.org'); | |||||
$john = $this->getTestUser('john@kolab.org'); | $john = $this->getTestUser('john@kolab.org'); | ||||
$ned = $this->getTestUser('ned@kolab.org'); | $ned = $this->getTestUser('ned@kolab.org'); | ||||
$response = $this->actingAs($jack)->get("/api/v4/users"); | $response = $this->actingAs($jack)->get("/api/v4/users"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertCount(0, $json); | $this->assertCount(0, $json); | ||||
$response = $this->actingAs($john)->get("/api/v4/users"); | $response = $this->actingAs($john)->get("/api/v4/users"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertCount(3, $json); | $this->assertCount(4, $json); | ||||
$this->assertSame($jack->email, $json[0]['email']); | $this->assertSame($jack->email, $json[0]['email']); | ||||
$this->assertSame($john->email, $json[1]['email']); | $this->assertSame($joe->email, $json[1]['email']); | ||||
$this->assertSame($ned->email, $json[2]['email']); | $this->assertSame($john->email, $json[2]['email']); | ||||
$this->assertSame($ned->email, $json[3]['email']); | |||||
// Values below are tested by Unit tests | // Values below are tested by Unit tests | ||||
$this->assertArrayHasKey('isDeleted', $json[0]); | $this->assertArrayHasKey('isDeleted', $json[0]); | ||||
$this->assertArrayHasKey('isSuspended', $json[0]); | $this->assertArrayHasKey('isSuspended', $json[0]); | ||||
$this->assertArrayHasKey('isActive', $json[0]); | $this->assertArrayHasKey('isActive', $json[0]); | ||||
$this->assertArrayHasKey('isLdapReady', $json[0]); | $this->assertArrayHasKey('isLdapReady', $json[0]); | ||||
$this->assertArrayHasKey('isImapReady', $json[0]); | $this->assertArrayHasKey('isImapReady', $json[0]); | ||||
$response = $this->actingAs($ned)->get("/api/v4/users"); | $response = $this->actingAs($ned)->get("/api/v4/users"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertCount(3, $json); | $this->assertCount(4, $json); | ||||
$this->assertSame($jack->email, $json[0]['email']); | $this->assertSame($jack->email, $json[0]['email']); | ||||
$this->assertSame($john->email, $json[1]['email']); | $this->assertSame($joe->email, $json[1]['email']); | ||||
$this->assertSame($ned->email, $json[2]['email']); | $this->assertSame($john->email, $json[2]['email']); | ||||
$this->assertSame($ned->email, $json[3]['email']); | |||||
} | } | ||||
/** | /** | ||||
* Test /api/auth/login | * Test /api/auth/login | ||||
*/ | */ | ||||
public function testLogin(): string | public function testLogin(): string | ||||
{ | { | ||||
// Request with no data | // Request with no data | ||||
▲ Show 20 Lines • Show All 148 Lines • ▼ Show 20 Lines | public function testUserResponse(): void | ||||
$this->assertSame('US', $result['settings']['country']); | $this->assertSame('US', $result['settings']['country']); | ||||
$this->assertSame('USD', $result['settings']['currency']); | $this->assertSame('USD', $result['settings']['currency']); | ||||
$this->assertTrue(is_array($result['accounts'])); | $this->assertTrue(is_array($result['accounts'])); | ||||
$this->assertTrue(is_array($result['wallets'])); | $this->assertTrue(is_array($result['wallets'])); | ||||
$this->assertCount(0, $result['accounts']); | $this->assertCount(0, $result['accounts']); | ||||
$this->assertCount(1, $result['wallets']); | $this->assertCount(1, $result['wallets']); | ||||
$this->assertSame($wallet->id, $result['wallet']['id']); | $this->assertSame($wallet->id, $result['wallet']['id']); | ||||
$this->assertArrayNotHasKey('discount', $result['wallet']); | |||||
$ned = $this->getTestUser('ned@kolab.org'); | $ned = $this->getTestUser('ned@kolab.org'); | ||||
$ned_wallet = $ned->wallets()->first(); | $ned_wallet = $ned->wallets()->first(); | ||||
$result = $this->invokeMethod(new UsersController(), 'userResponse', [$ned]); | $result = $this->invokeMethod(new UsersController(), 'userResponse', [$ned]); | ||||
$this->assertEquals($ned->id, $result['id']); | $this->assertEquals($ned->id, $result['id']); | ||||
$this->assertEquals($ned->email, $result['email']); | $this->assertEquals($ned->email, $result['email']); | ||||
$this->assertTrue(is_array($result['accounts'])); | $this->assertTrue(is_array($result['accounts'])); | ||||
$this->assertTrue(is_array($result['wallets'])); | $this->assertTrue(is_array($result['wallets'])); | ||||
$this->assertCount(1, $result['accounts']); | $this->assertCount(1, $result['accounts']); | ||||
$this->assertCount(1, $result['wallets']); | $this->assertCount(1, $result['wallets']); | ||||
$this->assertSame($wallet->id, $result['wallet']['id']); | $this->assertSame($wallet->id, $result['wallet']['id']); | ||||
$this->assertSame($wallet->id, $result['accounts'][0]['id']); | $this->assertSame($wallet->id, $result['accounts'][0]['id']); | ||||
$this->assertSame($ned_wallet->id, $result['wallets'][0]['id']); | $this->assertSame($ned_wallet->id, $result['wallets'][0]['id']); | ||||
// Test discount in a response | |||||
$discount = Discount::where('code', 'TEST')->first(); | |||||
$wallet->discount()->associate($discount); | |||||
$wallet->save(); | |||||
$user->refresh(); | |||||
$result = $this->invokeMethod(new UsersController(), 'userResponse', [$user]); | |||||
$this->assertEquals($user->id, $result['id']); | |||||
$this->assertSame($discount->id, $result['wallet']['discount_id']); | |||||
$this->assertSame($discount->discount, $result['wallet']['discount']); | |||||
$this->assertSame($discount->description, $result['wallet']['discount_description']); | |||||
$this->assertSame($discount->id, $result['wallets'][0]['discount_id']); | |||||
$this->assertSame($discount->discount, $result['wallets'][0]['discount']); | |||||
$this->assertSame($discount->description, $result['wallets'][0]['discount_description']); | |||||
} | } | ||||
/** | /** | ||||
* Test fetching user data/profile (GET /api/v4/users/<user-id>) | * Test fetching user data/profile (GET /api/v4/users/<user-id>) | ||||
*/ | */ | ||||
public function testShow(): void | public function testShow(): void | ||||
{ | { | ||||
$userA = $this->getTestUser('UserEntitlement2A@UserEntitlement.com'); | $userA = $this->getTestUser('UserEntitlement2A@UserEntitlement.com'); | ||||
Show All 40 Lines | public function testShow(): void | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$storage_sku = Sku::where('title', 'storage')->first(); | $storage_sku = Sku::where('title', 'storage')->first(); | ||||
$groupware_sku = Sku::where('title', 'groupware')->first(); | $groupware_sku = Sku::where('title', 'groupware')->first(); | ||||
$mailbox_sku = Sku::where('title', 'mailbox')->first(); | $mailbox_sku = Sku::where('title', 'mailbox')->first(); | ||||
$secondfactor_sku = Sku::where('title', '2fa')->first(); | $secondfactor_sku = Sku::where('title', '2fa')->first(); | ||||
$this->assertCount(4, $json['skus']); | $this->assertCount(5, $json['skus']); | ||||
$this->assertSame(2, $json['skus'][$storage_sku->id]['count']); | $this->assertSame(2, $json['skus'][$storage_sku->id]['count']); | ||||
$this->assertSame(1, $json['skus'][$groupware_sku->id]['count']); | $this->assertSame(1, $json['skus'][$groupware_sku->id]['count']); | ||||
$this->assertSame(1, $json['skus'][$mailbox_sku->id]['count']); | $this->assertSame(1, $json['skus'][$mailbox_sku->id]['count']); | ||||
$this->assertSame(1, $json['skus'][$secondfactor_sku->id]['count']); | $this->assertSame(1, $json['skus'][$secondfactor_sku->id]['count']); | ||||
} | } | ||||
/** | /** | ||||
* Test user creation (POST /api/v4/users) | * Test user creation (POST /api/v4/users) | ||||
▲ Show 20 Lines • Show All 378 Lines • Show Last 20 Lines |