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\V4\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'); | ||||
parent::tearDown(); | $user = $this->getTestUser('john@kolab.org'); | ||||
} | $wallet = $user->wallets()->first(); | ||||
$wallet->discount()->dissociate(); | |||||
/** | $wallet->save(); | ||||
* Test fetching current user info (/api/auth/info) | |||||
*/ | |||||
public function testInfo(): void | |||||
{ | |||||
$user = $this->getTestUser('UsersControllerTest1@userscontroller.com'); | |||||
$domain = $this->getTestDomain('userscontroller.com', [ | |||||
'status' => Domain::STATUS_NEW, | |||||
'type' => Domain::TYPE_PUBLIC, | |||||
]); | |||||
$response = $this->actingAs($user)->get("api/auth/info"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertEquals($user->id, $json['id']); | |||||
$this->assertEquals($user->email, $json['email']); | |||||
$this->assertEquals(User::STATUS_NEW | User::STATUS_ACTIVE, $json['status']); | |||||
$this->assertTrue(is_array($json['statusInfo'])); | |||||
$this->assertTrue(is_array($json['settings'])); | |||||
$this->assertTrue(is_array($json['aliases'])); | |||||
// Note: Details of the content are tested in testUserResponse() | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test user deleting (DELETE /api/v4/users/<id>) | * Test user deleting (DELETE /api/v4/users/<id>) | ||||
*/ | */ | ||||
public function testDestroy(): void | public function testDestroy(): void | ||||
{ | { | ||||
// First create some users/accounts to delete | // First create some users/accounts to delete | ||||
▲ Show 20 Lines • Show All 100 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 | |||||
*/ | |||||
public function testLogin(): string | |||||
{ | |||||
// Request with no data | |||||
$response = $this->post("api/auth/login", []); | |||||
$response->assertStatus(422); | |||||
$json = $response->json(); | |||||
$this->assertSame('error', $json['status']); | |||||
$this->assertCount(2, $json['errors']); | |||||
$this->assertArrayHasKey('email', $json['errors']); | |||||
$this->assertArrayHasKey('password', $json['errors']); | |||||
// Request with invalid password | |||||
$post = ['email' => 'john@kolab.org', 'password' => 'wrong']; | |||||
$response = $this->post("api/auth/login", $post); | |||||
$response->assertStatus(401); | |||||
$json = $response->json(); | |||||
$this->assertSame('error', $json['status']); | |||||
$this->assertSame('Invalid username or password.', $json['message']); | |||||
// Valid user+password | |||||
$post = ['email' => 'john@kolab.org', 'password' => 'simple123']; | |||||
$response = $this->post("api/auth/login", $post); | |||||
$json = $response->json(); | |||||
$response->assertStatus(200); | |||||
$this->assertTrue(!empty($json['access_token'])); | |||||
$this->assertEquals(\config('jwt.ttl') * 60, $json['expires_in']); | |||||
$this->assertEquals('bearer', $json['token_type']); | |||||
return $json['access_token']; | |||||
} | |||||
/** | |||||
* Test /api/auth/logout | |||||
* | |||||
* @depends testLogin | |||||
*/ | |||||
public function testLogout($token): void | |||||
{ | |||||
// Request with no token, testing that it requires auth | |||||
$response = $this->post("api/auth/logout"); | |||||
$response->assertStatus(401); | |||||
// Test the same using JSON mode | |||||
$response = $this->json('POST', "api/auth/logout", []); | |||||
$response->assertStatus(401); | |||||
// Request with valid token | |||||
$response = $this->withHeaders(['Authorization' => 'Bearer ' . $token])->post("api/auth/logout"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertEquals('success', $json['status']); | |||||
$this->assertEquals('Successfully logged out.', $json['message']); | |||||
// Check if it really destroyed the token? | |||||
$response = $this->withHeaders(['Authorization' => 'Bearer ' . $token])->get("api/auth/info"); | |||||
$response->assertStatus(401); | |||||
} | |||||
public function testRefresh(): void | |||||
{ | |||||
// TODO | |||||
$this->markTestIncomplete(); | |||||
} | } | ||||
public function testStatusInfo(): void | public function testStatusInfo(): void | ||||
{ | { | ||||
$user = $this->getTestUser('UsersControllerTest1@userscontroller.com'); | $user = $this->getTestUser('UsersControllerTest1@userscontroller.com'); | ||||
$domain = $this->getTestDomain('userscontroller.com', [ | $domain = $this->getTestDomain('userscontroller.com', [ | ||||
'status' => Domain::STATUS_NEW, | 'status' => Domain::STATUS_NEW, | ||||
'type' => Domain::TYPE_PUBLIC, | 'type' => Domain::TYPE_PUBLIC, | ||||
▲ Show 20 Lines • Show All 73 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 38 Lines | public function testShow(): void | ||||
$response = $this->actingAs($john)->get("/api/v4/users/{$ned->id}"); | $response = $this->actingAs($john)->get("/api/v4/users/{$ned->id}"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$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(); | |||||
$this->assertCount(3, $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']); | |||||
} | } | ||||
/** | /** | ||||
* Test user creation (POST /api/v4/users) | * Test user creation (POST /api/v4/users) | ||||
*/ | */ | ||||
public function testStore(): void | public function testStore(): void | ||||
{ | { | ||||
$jack = $this->getTestUser('jack@kolab.org'); | $jack = $this->getTestUser('jack@kolab.org'); | ||||
▲ Show 20 Lines • Show All 361 Lines • ▼ Show 20 Lines | class UsersTest extends TestCase | ||||
* | * | ||||
* Note: Technically these include unit tests, but let's keep it here for now. | * Note: Technically these include unit tests, but let's keep it here for now. | ||||
* FIXME: Shall we do a http request for each case? | * FIXME: Shall we do a http request for each case? | ||||
* | * | ||||
* @dataProvider dataValidateEmail | * @dataProvider dataValidateEmail | ||||
*/ | */ | ||||
public function testValidateEmail($alias, $user, $is_alias, $expected_result): void | public function testValidateEmail($alias, $user, $is_alias, $expected_result): void | ||||
{ | { | ||||
$result = $this->invokeMethod(new UsersController(), 'validateEmail', [$alias, $user, $is_alias]); | $result = $this->invokeMethod(new \App\Utils(), 'validateEmail', [$alias, $user, $is_alias]); | ||||
$this->assertSame($expected_result, $result); | $this->assertSame($expected_result, $result); | ||||
} | } | ||||
} | } |