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\Domain; | use App\Domain; | ||||
use App\Http\Controllers\API\UsersController; | use App\Http\Controllers\API\UsersController; | ||||
use App\Package; | |||||
use App\Sku; | |||||
use App\User; | use App\User; | ||||
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 | ||||
{ | { | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 385 Lines • ▼ Show 20 Lines | class UsersTest extends TestCase | ||||
/** | /** | ||||
* 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'); | ||||
// Test getting profile of self | // Test getting profile of self | ||||
$response = $this->actingAs($userA, 'api')->get("/api/v4/users/{$userA->id}"); | $response = $this->actingAs($userA)->get("/api/v4/users/{$userA->id}"); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertEquals($userA->id, $json['id']); | $this->assertEquals($userA->id, $json['id']); | ||||
$this->assertEquals($userA->email, $json['email']); | $this->assertEquals($userA->email, $json['email']); | ||||
$this->assertTrue(is_array($json['statusInfo'])); | $this->assertTrue(is_array($json['statusInfo'])); | ||||
$this->assertTrue(is_array($json['settings'])); | $this->assertTrue(is_array($json['settings'])); | ||||
$this->assertTrue(is_array($json['aliases'])); | $this->assertTrue(is_array($json['aliases'])); | ||||
$this->assertSame([], $json['skus']); | |||||
$john = $this->getTestUser('john@kolab.org'); | $john = $this->getTestUser('john@kolab.org'); | ||||
$jack = $this->getTestUser('jack@kolab.org'); | $jack = $this->getTestUser('jack@kolab.org'); | ||||
$ned = $this->getTestUser('ned@kolab.org'); | $ned = $this->getTestUser('ned@kolab.org'); | ||||
// Test unauthorized access to a profile of other user | // Test unauthorized access to a profile of other user | ||||
$response = $this->actingAs($jack)->get("/api/v4/users/{$userA->id}"); | $response = $this->actingAs($jack)->get("/api/v4/users/{$userA->id}"); | ||||
$response->assertStatus(403); | $response->assertStatus(403); | ||||
// Test authorized access to a profile of other user | // Test authorized access to a profile of other user | ||||
// Ned: Additional account controller | // Ned: Additional account controller | ||||
$response = $this->actingAs($ned)->get("/api/v4/users/{$john->id}"); | $response = $this->actingAs($ned)->get("/api/v4/users/{$john->id}"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$response = $this->actingAs($ned)->get("/api/v4/users/{$jack->id}"); | $response = $this->actingAs($ned)->get("/api/v4/users/{$jack->id}"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
// John: Account owner | // John: Account owner | ||||
$response = $this->actingAs($john)->get("/api/v4/users/{$jack->id}"); | $response = $this->actingAs($john)->get("/api/v4/users/{$jack->id}"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$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(); | |||||
$storage_sku = Sku::where('title', 'storage')->first(); | |||||
$groupware_sku = Sku::where('title', 'groupware')->first(); | |||||
$mailbox_sku = Sku::where('title', 'mailbox')->first(); | |||||
$this->assertCount(3, $json['skus']); | |||||
$this->assertSame(2, $json['skus'][$storage_sku->id]['count']); | |||||
$this->assertSame(1, $json['skus'][$groupware_sku->id]['count']); | |||||
$this->assertSame(1, $json['skus'][$mailbox_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 46 Lines • ▼ Show 20 Lines | public function testStore(): void | ||||
$response->assertStatus(422); | $response->assertStatus(422); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('error', $json['status']); | $this->assertSame('error', $json['status']); | ||||
$this->assertCount(2, $json); | $this->assertCount(2, $json); | ||||
$this->assertSame('The specified email is not available.', $json['errors']['email']); | $this->assertSame('The specified email is not available.', $json['errors']['email']); | ||||
// Test full user data | $package_kolab = \App\Package::where('title', 'kolab')->first(); | ||||
$package_domain = \App\Package::where('title', 'domain-hosting')->first(); | |||||
$post = [ | $post = [ | ||||
'password' => 'simple', | 'password' => 'simple', | ||||
'password_confirmation' => 'simple', | 'password_confirmation' => 'simple', | ||||
'first_name' => 'John2', | 'first_name' => 'John2', | ||||
'last_name' => 'Doe2', | 'last_name' => 'Doe2', | ||||
'email' => 'john2.doe2@kolab.org', | 'email' => 'john2.doe2@kolab.org', | ||||
'aliases' => ['useralias1@kolab.org', 'useralias2@kolab.org'] | 'aliases' => ['useralias1@kolab.org', 'useralias2@kolab.org'], | ||||
]; | ]; | ||||
// Missing package | |||||
$response = $this->actingAs($john)->post("/api/v4/users", $post); | |||||
$json = $response->json(); | |||||
$response->assertStatus(422); | |||||
$this->assertSame('error', $json['status']); | |||||
$this->assertSame("Package is required.", $json['errors']['package']); | |||||
$this->assertCount(2, $json); | |||||
// Invalid package | |||||
$post['package'] = $package_domain->id; | |||||
$response = $this->actingAs($john)->post("/api/v4/users", $post); | |||||
$json = $response->json(); | |||||
$response->assertStatus(422); | |||||
$this->assertSame('error', $json['status']); | |||||
$this->assertSame("Invalid package selected.", $json['errors']['package']); | |||||
$this->assertCount(2, $json); | |||||
// Test full and valid data | |||||
$post['package'] = $package_kolab->id; | |||||
$response = $this->actingAs($john)->post("/api/v4/users", $post); | $response = $this->actingAs($john)->post("/api/v4/users", $post); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertSame("User created successfully.", $json['message']); | $this->assertSame("User created successfully.", $json['message']); | ||||
$this->assertCount(2, $json); | $this->assertCount(2, $json); | ||||
$user = User::where('email', 'john2.doe2@kolab.org')->first(); | $user = User::where('email', 'john2.doe2@kolab.org')->first(); | ||||
$this->assertInstanceOf(User::class, $user); | $this->assertInstanceOf(User::class, $user); | ||||
$this->assertSame('John2', $user->getSetting('first_name')); | $this->assertSame('John2', $user->getSetting('first_name')); | ||||
$this->assertSame('Doe2', $user->getSetting('last_name')); | $this->assertSame('Doe2', $user->getSetting('last_name')); | ||||
$aliases = $user->aliases()->orderBy('alias')->get(); | $aliases = $user->aliases()->orderBy('alias')->get(); | ||||
$this->assertCount(2, $aliases); | $this->assertCount(2, $aliases); | ||||
$this->assertSame('useralias1@kolab.org', $aliases[0]->alias); | $this->assertSame('useralias1@kolab.org', $aliases[0]->alias); | ||||
$this->assertSame('useralias2@kolab.org', $aliases[1]->alias); | $this->assertSame('useralias2@kolab.org', $aliases[1]->alias); | ||||
// Assert the new user entitlements | |||||
// TODO: Test assigning a package to new user | $this->assertUserEntitlements($user, ['groupware', 'mailbox', 'storage', 'storage']); | ||||
// TODO: Test the wallet to which the new user should be assigned to | // Assert the wallet to which the new user should be assigned to | ||||
$wallet = $user->wallet(); | |||||
$this->assertSame($john->wallets()->first()->id, $wallet->id); | |||||
// Test acting as account controller (not owner) | // Test acting as account controller (not owner) | ||||
/* | /* | ||||
// FIXME: How do we know to which wallet the new user should be assigned to? | // FIXME: How do we know to which wallet the new user should be assigned to? | ||||
$this->deleteTestUser('john2.doe2@kolab.org'); | $this->deleteTestUser('john2.doe2@kolab.org'); | ||||
$response = $this->actingAs($ned)->post("/api/v4/users", $post); | $response = $this->actingAs($ned)->post("/api/v4/users", $post); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
▲ Show 20 Lines • Show All 128 Lines • ▼ Show 20 Lines | public function testUpdate(): void | ||||
$this->assertSame("The specified domain is not available.", $json['errors']['aliases'][1]); | $this->assertSame("The specified domain is not available.", $json['errors']['aliases'][1]); | ||||
$this->assertSame("The password confirmation does not match.", $json['errors']['password'][0]); | $this->assertSame("The password confirmation does not match.", $json['errors']['password'][0]); | ||||
// Test authorized update of other user | // Test authorized update of other user | ||||
$response = $this->actingAs($ned)->get("/api/v4/users/{$jack->id}", []); | $response = $this->actingAs($ned)->get("/api/v4/users/{$jack->id}", []); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
// TODO: Test error on aliases with invalid/non-existing/other-user's domain | // TODO: Test error on aliases with invalid/non-existing/other-user's domain | ||||
// Create entitlements and additional user for following tests | |||||
$owner = $this->getTestUser('UsersControllerTest1@userscontroller.com'); | |||||
$user = $this->getTestUser('UsersControllerTest2@userscontroller.com'); | |||||
$package_domain = Package::where('title', 'domain-hosting')->first(); | |||||
$package_kolab = Package::where('title', 'kolab')->first(); | |||||
$package_lite = Package::where('title', 'lite')->first(); | |||||
$sku_mailbox = Sku::where('title', 'mailbox')->first(); | |||||
$sku_storage = Sku::where('title', 'storage')->first(); | |||||
$sku_groupware = Sku::where('title', 'groupware')->first(); | |||||
$domain = $this->getTestDomain( | |||||
'userscontroller.com', | |||||
[ | |||||
'status' => Domain::STATUS_NEW, | |||||
'type' => Domain::TYPE_EXTERNAL, | |||||
] | |||||
); | |||||
$domain->assignPackage($package_domain, $owner); | |||||
$owner->assignPackage($package_kolab); | |||||
$owner->assignPackage($package_lite, $user); | |||||
// Non-controller cannot update his own entitlements | |||||
$post = ['skus' => []]; | |||||
$response = $this->actingAs($user)->put("/api/v4/users/{$user->id}", $post); | |||||
$response->assertStatus(422); | |||||
// Test updating entitlements | |||||
$post = [ | |||||
'skus' => [ | |||||
$sku_mailbox->id => 1, | |||||
$sku_storage->id => 3, | |||||
$sku_groupware->id => 1, | |||||
], | |||||
]; | |||||
$response = $this->actingAs($owner)->put("/api/v4/users/{$user->id}", $post); | |||||
$response->assertStatus(200); | |||||
$storage_cost = $user->entitlements() | |||||
->where('sku_id', $sku_storage->id) | |||||
->orderBy('cost') | |||||
->pluck('cost')->all(); | |||||
$this->assertUserEntitlements($user, ['groupware', 'mailbox', 'storage', 'storage', 'storage']); | |||||
$this->assertSame([0, 0, 25], $storage_cost); | |||||
} | |||||
/** | |||||
* Test UsersController::updateEntitlements() | |||||
*/ | |||||
public function testUpdateEntitlements(): void | |||||
{ | |||||
// TODO: Test more cases of entitlements update | |||||
$this->markTestIncomplete(); | |||||
} | } | ||||
/** | /** | ||||
* List of alias validation cases for testValidateEmail() | * List of alias validation cases for testValidateEmail() | ||||
* | * | ||||
* @return array Arguments for testValidateEmail() | * @return array Arguments for testValidateEmail() | ||||
*/ | */ | ||||
public function dataValidateEmail(): array | public function dataValidateEmail(): array | ||||
▲ Show 20 Lines • Show All 55 Lines • Show Last 20 Lines |