Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/UsersTest.php
Show First 20 Lines • Show All 269 Lines • ▼ Show 20 Lines | public function testShow(): void | ||||
// 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(); | $json = $response->json(); | ||||
$storage_sku = Sku::where('title', 'storage')->first(); | $storage_sku = Sku::withEnvTenantContext()->where('title', 'storage')->first(); | ||||
$groupware_sku = Sku::where('title', 'groupware')->first(); | $groupware_sku = Sku::withEnvTenantContext()->where('title', 'groupware')->first(); | ||||
$mailbox_sku = Sku::where('title', 'mailbox')->first(); | $mailbox_sku = Sku::withEnvTenantContext()->where('title', 'mailbox')->first(); | ||||
$secondfactor_sku = Sku::where('title', '2fa')->first(); | $secondfactor_sku = Sku::withEnvTenantContext()->where('title', '2fa')->first(); | ||||
$this->assertCount(5, $json['skus']); | $this->assertCount(5, $json['skus']); | ||||
$this->assertSame(2, $json['skus'][$storage_sku->id]['count']); | $this->assertSame(5, $json['skus'][$storage_sku->id]['count']); | ||||
$this->assertSame([0,0], $json['skus'][$storage_sku->id]['costs']); | $this->assertSame([0,0,0,0,0], $json['skus'][$storage_sku->id]['costs']); | ||||
$this->assertSame(1, $json['skus'][$groupware_sku->id]['count']); | $this->assertSame(1, $json['skus'][$groupware_sku->id]['count']); | ||||
$this->assertSame([555], $json['skus'][$groupware_sku->id]['costs']); | $this->assertSame([490], $json['skus'][$groupware_sku->id]['costs']); | ||||
$this->assertSame(1, $json['skus'][$mailbox_sku->id]['count']); | $this->assertSame(1, $json['skus'][$mailbox_sku->id]['count']); | ||||
$this->assertSame([444], $json['skus'][$mailbox_sku->id]['costs']); | $this->assertSame([500], $json['skus'][$mailbox_sku->id]['costs']); | ||||
$this->assertSame(1, $json['skus'][$secondfactor_sku->id]['count']); | $this->assertSame(1, $json['skus'][$secondfactor_sku->id]['count']); | ||||
$this->assertSame([0], $json['skus'][$secondfactor_sku->id]['costs']); | $this->assertSame([0], $json['skus'][$secondfactor_sku->id]['costs']); | ||||
} | } | ||||
/** | /** | ||||
* Test fetching user status (GET /api/v4/users/<user-id>/status) | * Test fetching user status (GET /api/v4/users/<user-id>/status) | ||||
* and forcing setup process update (?refresh=1) | * and forcing setup process update (?refresh=1) | ||||
* | * | ||||
▲ Show 20 Lines • Show All 142 Lines • ▼ Show 20 Lines | public function testStatusInfo(): void | ||||
$this->assertSame('domain-ldap-ready', $result['process'][4]['label']); | $this->assertSame('domain-ldap-ready', $result['process'][4]['label']); | ||||
$this->assertSame(false, $result['process'][4]['state']); | $this->assertSame(false, $result['process'][4]['state']); | ||||
$this->assertSame('domain-verified', $result['process'][5]['label']); | $this->assertSame('domain-verified', $result['process'][5]['label']); | ||||
$this->assertSame(true, $result['process'][5]['state']); | $this->assertSame(true, $result['process'][5]['state']); | ||||
$this->assertSame('domain-confirmed', $result['process'][6]['label']); | $this->assertSame('domain-confirmed', $result['process'][6]['label']); | ||||
$this->assertSame(false, $result['process'][6]['state']); | $this->assertSame(false, $result['process'][6]['state']); | ||||
// Test 'skus' property | // Test 'skus' property | ||||
$user->assignSku(Sku::where('title', 'beta')->first()); | $user->assignSku(Sku::withEnvTenantContext()->where('title', 'beta')->first()); | ||||
$result = UsersController::statusInfo($user); | $result = UsersController::statusInfo($user); | ||||
$this->assertSame(['beta'], $result['skus']); | $this->assertSame(['beta'], $result['skus']); | ||||
$user->assignSku(Sku::where('title', 'meet')->first()); | $user->assignSku(Sku::withEnvTenantContext()->where('title', 'meet')->first()); | ||||
$result = UsersController::statusInfo($user); | $result = UsersController::statusInfo($user); | ||||
$this->assertSame(['beta', 'meet'], $result['skus']); | $this->assertSame(['beta', 'meet'], $result['skus']); | ||||
$user->assignSku(Sku::where('title', 'meet')->first()); | $user->assignSku(Sku::withEnvTenantContext()->where('title', 'meet')->first()); | ||||
$result = UsersController::statusInfo($user); | $result = UsersController::statusInfo($user); | ||||
$this->assertSame(['beta', 'meet'], $result['skus']); | $this->assertSame(['beta', 'meet'], $result['skus']); | ||||
} | } | ||||
/** | /** | ||||
* Test user creation (POST /api/v4/users) | * Test user creation (POST /api/v4/users) | ||||
▲ Show 20 Lines • Show All 54 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']); | ||||
$package_kolab = \App\Package::where('title', 'kolab')->first(); | $package_kolab = \App\Package::withEnvTenantContext()->where('title', 'kolab')->first(); | ||||
$package_domain = \App\Package::where('title', 'domain-hosting')->first(); | $package_domain = \App\Package::withEnvTenantContext()->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', | ||||
'organization' => 'TestOrg', | 'organization' => 'TestOrg', | ||||
Show All 37 Lines | public function testStore(): void | ||||
$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')); | ||||
$this->assertSame('TestOrg', $user->getSetting('organization')); | $this->assertSame('TestOrg', $user->getSetting('organization')); | ||||
$aliases = $user->aliases()->orderBy('alias')->get(); | $aliases = $user->aliases()->orderBy('alias')->get(); | ||||
$this->assertCount(2, $aliases); | $this->assertCount(2, $aliases); | ||||
$this->assertSame('deleted@kolab.org', $aliases[0]->alias); | $this->assertSame('deleted@kolab.org', $aliases[0]->alias); | ||||
$this->assertSame('useralias1@kolab.org', $aliases[1]->alias); | $this->assertSame('useralias1@kolab.org', $aliases[1]->alias); | ||||
// Assert the new user entitlements | // Assert the new user entitlements | ||||
$this->assertUserEntitlements($user, ['groupware', 'mailbox', 'storage', 'storage']); | $this->assertUserEntitlements($user, ['groupware', 'mailbox', | ||||
'storage', 'storage', 'storage', 'storage', 'storage']); | |||||
// Assert 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(); | $wallet = $user->wallet(); | ||||
$this->assertSame($john->wallets()->first()->id, $wallet->id); | $this->assertSame($john->wallets()->first()->id, $wallet->id); | ||||
// Attempt to create a user previously deleted | // Attempt to create a user previously deleted | ||||
$user->delete(); | $user->delete(); | ||||
$post['package'] = $package_kolab->id; | $post['package'] = $package_kolab->id; | ||||
$post['aliases'] = []; | $post['aliases'] = []; | ||||
$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')); | ||||
$this->assertSame('TestOrg', $user->getSetting('organization')); | $this->assertSame('TestOrg', $user->getSetting('organization')); | ||||
$this->assertCount(0, $user->aliases()->get()); | $this->assertCount(0, $user->aliases()->get()); | ||||
$this->assertUserEntitlements($user, ['groupware', 'mailbox', 'storage', 'storage']); | $this->assertUserEntitlements($user, ['groupware', 'mailbox', | ||||
'storage', 'storage', 'storage', 'storage', 'storage']); | |||||
// Test acting as account controller (not owner) | // Test acting as account controller (not owner) | ||||
$this->markTestIncomplete(); | $this->markTestIncomplete(); | ||||
} | } | ||||
/** | /** | ||||
* Test user update (PUT /api/v4/users/<user-id>) | * Test user update (PUT /api/v4/users/<user-id>) | ||||
▲ Show 20 Lines • Show All 134 Lines • ▼ Show 20 Lines | public function testUpdate(): void | ||||
$this->assertTrue(empty($json['statusInfo'])); | $this->assertTrue(empty($json['statusInfo'])); | ||||
// 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 | // Create entitlements and additional user for following tests | ||||
$owner = $this->getTestUser('UsersControllerTest1@userscontroller.com'); | $owner = $this->getTestUser('UsersControllerTest1@userscontroller.com'); | ||||
$user = $this->getTestUser('UsersControllerTest2@userscontroller.com'); | $user = $this->getTestUser('UsersControllerTest2@userscontroller.com'); | ||||
$package_domain = Package::where('title', 'domain-hosting')->first(); | $package_domain = Package::withEnvTenantContext()->where('title', 'domain-hosting')->first(); | ||||
$package_kolab = Package::where('title', 'kolab')->first(); | $package_kolab = Package::withEnvTenantContext()->where('title', 'kolab')->first(); | ||||
$package_lite = Package::where('title', 'lite')->first(); | $package_lite = Package::withEnvTenantContext()->where('title', 'lite')->first(); | ||||
$sku_mailbox = Sku::where('title', 'mailbox')->first(); | $sku_mailbox = Sku::withEnvTenantContext()->where('title', 'mailbox')->first(); | ||||
$sku_storage = Sku::where('title', 'storage')->first(); | $sku_storage = Sku::withEnvTenantContext()->where('title', 'storage')->first(); | ||||
$sku_groupware = Sku::where('title', 'groupware')->first(); | $sku_groupware = Sku::withEnvTenantContext()->where('title', 'groupware')->first(); | ||||
$domain = $this->getTestDomain( | $domain = $this->getTestDomain( | ||||
'userscontroller.com', | 'userscontroller.com', | ||||
[ | [ | ||||
'status' => Domain::STATUS_NEW, | 'status' => Domain::STATUS_NEW, | ||||
'type' => Domain::TYPE_EXTERNAL, | 'type' => Domain::TYPE_EXTERNAL, | ||||
] | ] | ||||
); | ); | ||||
$domain->assignPackage($package_domain, $owner); | $domain->assignPackage($package_domain, $owner); | ||||
$owner->assignPackage($package_kolab); | $owner->assignPackage($package_kolab); | ||||
$owner->assignPackage($package_lite, $user); | $owner->assignPackage($package_lite, $user); | ||||
// Non-controller cannot update his own entitlements | // Non-controller cannot update his own entitlements | ||||
$post = ['skus' => []]; | $post = ['skus' => []]; | ||||
$response = $this->actingAs($user)->put("/api/v4/users/{$user->id}", $post); | $response = $this->actingAs($user)->put("/api/v4/users/{$user->id}", $post); | ||||
$response->assertStatus(422); | $response->assertStatus(422); | ||||
// Test updating entitlements | // Test updating entitlements | ||||
$post = [ | $post = [ | ||||
'skus' => [ | 'skus' => [ | ||||
$sku_mailbox->id => 1, | $sku_mailbox->id => 1, | ||||
$sku_storage->id => 3, | $sku_storage->id => 6, | ||||
$sku_groupware->id => 1, | $sku_groupware->id => 1, | ||||
], | ], | ||||
]; | ]; | ||||
$response = $this->actingAs($owner)->put("/api/v4/users/{$user->id}", $post); | $response = $this->actingAs($owner)->put("/api/v4/users/{$user->id}", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$storage_cost = $user->entitlements() | $storage_cost = $user->entitlements() | ||||
->where('sku_id', $sku_storage->id) | ->where('sku_id', $sku_storage->id) | ||||
->orderBy('cost') | ->orderBy('cost') | ||||
->pluck('cost')->all(); | ->pluck('cost')->all(); | ||||
$this->assertUserEntitlements( | $this->assertUserEntitlements( | ||||
$user, | $user, | ||||
['groupware', 'mailbox', 'storage', 'storage', 'storage'] | ['groupware', 'mailbox', 'storage', 'storage', 'storage', 'storage', 'storage', 'storage'] | ||||
); | ); | ||||
$this->assertSame([0, 0, 25], $storage_cost); | $this->assertSame([0, 0, 0, 0, 0, 25], $storage_cost); | ||||
$this->assertTrue(empty($json['statusInfo'])); | $this->assertTrue(empty($json['statusInfo'])); | ||||
} | } | ||||
/** | /** | ||||
* Test UsersController::updateEntitlements() | * Test UsersController::updateEntitlements() | ||||
*/ | */ | ||||
public function testUpdateEntitlements(): void | public function testUpdateEntitlements(): void | ||||
{ | { | ||||
$jane = $this->getTestUser('jane@kolabnow.com'); | $jane = $this->getTestUser('jane@kolabnow.com'); | ||||
$kolab = \App\Package::where('title', 'kolab')->first(); | $kolab = Package::withEnvTenantContext()->where('title', 'kolab')->first(); | ||||
$storage = \App\Sku::where('title', 'storage')->first(); | $storage = Sku::withEnvTenantContext()->where('title', 'storage')->first(); | ||||
$activesync = \App\Sku::where('title', 'activesync')->first(); | $activesync = Sku::withEnvTenantContext()->where('title', 'activesync')->first(); | ||||
$groupware = \App\Sku::where('title', 'groupware')->first(); | $groupware = Sku::withEnvTenantContext()->where('title', 'groupware')->first(); | ||||
$mailbox = \App\Sku::where('title', 'mailbox')->first(); | $mailbox = Sku::withEnvTenantContext()->where('title', 'mailbox')->first(); | ||||
// standard package, 1 mailbox, 1 groupware, 2 storage | // standard package, 1 mailbox, 1 groupware, 2 storage | ||||
$jane->assignPackage($kolab); | $jane->assignPackage($kolab); | ||||
// add 2 storage, 1 activesync | // add 2 storage, 1 activesync | ||||
$post = [ | $post = [ | ||||
'skus' => [ | 'skus' => [ | ||||
$mailbox->id => 1, | $mailbox->id => 1, | ||||
$groupware->id => 1, | $groupware->id => 1, | ||||
$storage->id => 4, | $storage->id => 7, | ||||
$activesync->id => 1 | $activesync->id => 1 | ||||
] | ] | ||||
]; | ]; | ||||
$response = $this->actingAs($jane)->put("/api/v4/users/{$jane->id}", $post); | $response = $this->actingAs($jane)->put("/api/v4/users/{$jane->id}", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertUserEntitlements( | $this->assertUserEntitlements( | ||||
$jane, | $jane, | ||||
[ | [ | ||||
'activesync', | 'activesync', | ||||
'groupware', | 'groupware', | ||||
'mailbox', | 'mailbox', | ||||
'storage', | 'storage', | ||||
'storage', | 'storage', | ||||
'storage', | 'storage', | ||||
'storage', | |||||
'storage', | |||||
'storage', | |||||
'storage' | 'storage' | ||||
] | ] | ||||
); | ); | ||||
// add 2 storage, remove 1 activesync | // add 2 storage, remove 1 activesync | ||||
$post = [ | $post = [ | ||||
'skus' => [ | 'skus' => [ | ||||
$mailbox->id => 1, | $mailbox->id => 1, | ||||
$groupware->id => 1, | $groupware->id => 1, | ||||
$storage->id => 6, | $storage->id => 9, | ||||
$activesync->id => 0 | $activesync->id => 0 | ||||
] | ] | ||||
]; | ]; | ||||
$response = $this->actingAs($jane)->put("/api/v4/users/{$jane->id}", $post); | $response = $this->actingAs($jane)->put("/api/v4/users/{$jane->id}", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertUserEntitlements( | $this->assertUserEntitlements( | ||||
$jane, | $jane, | ||||
[ | [ | ||||
'groupware', | 'groupware', | ||||
'mailbox', | 'mailbox', | ||||
'storage', | 'storage', | ||||
'storage', | 'storage', | ||||
'storage', | 'storage', | ||||
'storage', | 'storage', | ||||
'storage', | 'storage', | ||||
'storage', | |||||
'storage', | |||||
'storage', | |||||
'storage' | 'storage' | ||||
] | ] | ||||
); | ); | ||||
// add mailbox | // add mailbox | ||||
$post = [ | $post = [ | ||||
'skus' => [ | 'skus' => [ | ||||
$mailbox->id => 2, | $mailbox->id => 2, | ||||
$groupware->id => 1, | $groupware->id => 1, | ||||
$storage->id => 6, | $storage->id => 9, | ||||
$activesync->id => 0 | $activesync->id => 0 | ||||
] | ] | ||||
]; | ]; | ||||
$response = $this->actingAs($jane)->put("/api/v4/users/{$jane->id}", $post); | $response = $this->actingAs($jane)->put("/api/v4/users/{$jane->id}", $post); | ||||
$response->assertStatus(500); | $response->assertStatus(500); | ||||
$this->assertUserEntitlements( | $this->assertUserEntitlements( | ||||
$jane, | $jane, | ||||
[ | [ | ||||
'groupware', | 'groupware', | ||||
'mailbox', | 'mailbox', | ||||
'storage', | 'storage', | ||||
'storage', | 'storage', | ||||
'storage', | 'storage', | ||||
'storage', | 'storage', | ||||
'storage', | 'storage', | ||||
'storage', | |||||
'storage', | |||||
'storage', | |||||
'storage' | 'storage' | ||||
] | ] | ||||
); | ); | ||||
// remove mailbox | // remove mailbox | ||||
$post = [ | $post = [ | ||||
'skus' => [ | 'skus' => [ | ||||
$mailbox->id => 0, | $mailbox->id => 0, | ||||
$groupware->id => 1, | $groupware->id => 1, | ||||
$storage->id => 6, | $storage->id => 9, | ||||
$activesync->id => 0 | $activesync->id => 0 | ||||
] | ] | ||||
]; | ]; | ||||
$response = $this->actingAs($jane)->put("/api/v4/users/{$jane->id}", $post); | $response = $this->actingAs($jane)->put("/api/v4/users/{$jane->id}", $post); | ||||
$response->assertStatus(500); | $response->assertStatus(500); | ||||
$this->assertUserEntitlements( | $this->assertUserEntitlements( | ||||
$jane, | $jane, | ||||
[ | [ | ||||
'groupware', | 'groupware', | ||||
'mailbox', | 'mailbox', | ||||
'storage', | 'storage', | ||||
'storage', | 'storage', | ||||
'storage', | 'storage', | ||||
'storage', | 'storage', | ||||
'storage', | 'storage', | ||||
'storage', | |||||
'storage', | |||||
'storage', | |||||
'storage' | 'storage' | ||||
] | ] | ||||
); | ); | ||||
// less than free storage | // less than free storage | ||||
$post = [ | $post = [ | ||||
'skus' => [ | 'skus' => [ | ||||
$mailbox->id => 1, | $mailbox->id => 1, | ||||
$groupware->id => 1, | $groupware->id => 1, | ||||
$storage->id => 1, | $storage->id => 1, | ||||
$activesync->id => 0 | $activesync->id => 0 | ||||
] | ] | ||||
]; | ]; | ||||
$response = $this->actingAs($jane)->put("/api/v4/users/{$jane->id}", $post); | $response = $this->actingAs($jane)->put("/api/v4/users/{$jane->id}", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertUserEntitlements( | $this->assertUserEntitlements( | ||||
$jane, | $jane, | ||||
[ | [ | ||||
'groupware', | 'groupware', | ||||
'mailbox', | 'mailbox', | ||||
'storage', | 'storage', | ||||
'storage', | |||||
'storage', | |||||
'storage', | |||||
'storage' | 'storage' | ||||
] | ] | ||||
); | ); | ||||
} | } | ||||
/** | /** | ||||
* Test user data response used in show and info actions | * Test user data response used in show and info actions | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 305 Lines • Show Last 20 Lines |