Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/SkusTest.php
<?php | <?php | ||||
namespace Tests\Feature\Controller; | namespace Tests\Feature\Controller; | ||||
use App\Entitlement; | |||||
use App\Http\Controllers\API\V4\SkusController; | use App\Http\Controllers\API\V4\SkusController; | ||||
use App\Sku; | use App\Sku; | ||||
use App\Tenant; | use App\Tenant; | ||||
use Tests\TestCase; | use Tests\TestCase; | ||||
class SkusTest extends TestCase | class SkusTest extends TestCase | ||||
{ | { | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function setUp(): void | public function setUp(): void | ||||
{ | { | ||||
parent::setUp(); | parent::setUp(); | ||||
$this->deleteTestUser('jane@kolabnow.com'); | |||||
$this->clearBetaEntitlements(); | $this->clearBetaEntitlements(); | ||||
$this->clearMeetEntitlements(); | $this->clearMeetEntitlements(); | ||||
Sku::where('title', 'test')->delete(); | Sku::where('title', 'test')->delete(); | ||||
} | } | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
$this->deleteTestUser('jane@kolabnow.com'); | |||||
$this->clearBetaEntitlements(); | $this->clearBetaEntitlements(); | ||||
$this->clearMeetEntitlements(); | $this->clearMeetEntitlements(); | ||||
Sku::where('title', 'test')->delete(); | Sku::where('title', 'test')->delete(); | ||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test fetching SKUs list for a domain (GET /domains/<id>/skus) | |||||
*/ | |||||
public function testDomainSkus(): void | |||||
{ | |||||
$user = $this->getTestUser('john@kolab.org'); | |||||
$domain = $this->getTestDomain('kolab.org'); | |||||
// Unauth access not allowed | |||||
$response = $this->get("api/v4/domains/{$domain->id}/skus"); | |||||
$response->assertStatus(401); | |||||
// Create an sku for another tenant, to make sure it is not included in the result | |||||
$nsku = Sku::create([ | |||||
'title' => 'test', | |||||
'name' => 'Test', | |||||
'description' => '', | |||||
'active' => true, | |||||
'cost' => 100, | |||||
'handler_class' => 'App\Handlers\Domain', | |||||
]); | |||||
$tenant = Tenant::whereNotIn('id', [\config('app.tenant_id')])->first(); | |||||
$nsku->tenant_id = $tenant->id; | |||||
$nsku->save(); | |||||
$response = $this->actingAs($user)->get("api/v4/domains/{$domain->id}/skus"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertCount(1, $json); | |||||
$this->assertSkuElement('domain-hosting', $json[0], [ | |||||
'prio' => 0, | |||||
'type' => 'domain', | |||||
'handler' => 'DomainHosting', | |||||
'enabled' => false, | |||||
'readonly' => false, | |||||
]); | |||||
} | |||||
/** | |||||
* Test fetching SKUs list | * Test fetching SKUs list | ||||
*/ | */ | ||||
public function testIndex(): void | public function testIndex(): void | ||||
{ | { | ||||
// Unauth access not allowed | // Unauth access not allowed | ||||
$response = $this->get("api/v4/skus"); | $response = $this->get("api/v4/skus"); | ||||
$response->assertStatus(401); | $response->assertStatus(401); | ||||
$user = $this->getTestUser('john@kolab.org'); | $john = $this->getTestUser('john@kolab.org'); | ||||
$jack = $this->getTestUser('jack@kolab.org'); | |||||
$sku = Sku::withEnvTenantContext()->where('title', 'mailbox')->first(); | $sku = Sku::withEnvTenantContext()->where('title', 'mailbox')->first(); | ||||
// Create an sku for another tenant, to make sure it is not included in the result | // Create an sku for another tenant, to make sure it is not included in the result | ||||
$nsku = Sku::create([ | $nsku = Sku::create([ | ||||
'title' => 'test', | 'title' => 'test', | ||||
'name' => 'Test', | 'name' => 'Test', | ||||
'description' => '', | 'description' => '', | ||||
'active' => true, | 'active' => true, | ||||
'cost' => 100, | 'cost' => 100, | ||||
'handler_class' => 'App\Handlers\Mailbox', | 'handler_class' => 'App\Handlers\Mailbox', | ||||
]); | ]); | ||||
$tenant = Tenant::whereNotIn('id', [\config('app.tenant_id')])->first(); | $tenant = Tenant::whereNotIn('id', [\config('app.tenant_id')])->first(); | ||||
$nsku->tenant_id = $tenant->id; | $nsku->tenant_id = $tenant->id; | ||||
$nsku->save(); | $nsku->save(); | ||||
$response = $this->actingAs($user)->get("api/v4/skus"); | $response = $this->actingAs($john)->get("api/v4/skus"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertCount(10, $json); | $this->assertCount(10, $json); | ||||
$this->assertSame(100, $json[0]['prio']); | $this->assertSame(100, $json[0]['prio']); | ||||
$this->assertSame($sku->id, $json[0]['id']); | $this->assertSame($sku->id, $json[0]['id']); | ||||
$this->assertSame($sku->title, $json[0]['title']); | $this->assertSame($sku->title, $json[0]['title']); | ||||
$this->assertSame($sku->name, $json[0]['name']); | $this->assertSame($sku->name, $json[0]['name']); | ||||
$this->assertSame($sku->description, $json[0]['description']); | $this->assertSame($sku->description, $json[0]['description']); | ||||
$this->assertSame($sku->cost, $json[0]['cost']); | $this->assertSame($sku->cost, $json[0]['cost']); | ||||
$this->assertSame($sku->units_free, $json[0]['units_free']); | $this->assertSame($sku->units_free, $json[0]['units_free']); | ||||
$this->assertSame($sku->period, $json[0]['period']); | $this->assertSame($sku->period, $json[0]['period']); | ||||
$this->assertSame($sku->active, $json[0]['active']); | $this->assertSame($sku->active, $json[0]['active']); | ||||
$this->assertSame('user', $json[0]['type']); | $this->assertSame('user', $json[0]['type']); | ||||
$this->assertSame('Mailbox', $json[0]['handler']); | $this->assertSame('Mailbox', $json[0]['handler']); | ||||
} | |||||
/** | |||||
* Test fetching SKUs list for a user (GET /users/<id>/skus) | |||||
*/ | |||||
public function testUserSkus(): void | |||||
{ | |||||
$user = $this->getTestUser('john@kolab.org'); | |||||
// Unauth access not allowed | |||||
$response = $this->get("api/v4/users/{$user->id}/skus"); | |||||
$response->assertStatus(401); | |||||
// Create an sku for another tenant, to make sure it is not included in the result | // Test the type filter, and nextCost property (user with one domain) | ||||
$nsku = Sku::create([ | $response = $this->actingAs($john)->get("api/v4/skus?type=domain"); | ||||
'title' => 'test', | |||||
'name' => 'Test', | |||||
'description' => '', | |||||
'active' => true, | |||||
'cost' => 100, | |||||
'handler_class' => 'Mailbox', | |||||
]); | |||||
$tenant = Tenant::whereNotIn('id', [\config('app.tenant_id')])->first(); | |||||
$nsku->tenant_id = $tenant->id; | |||||
$nsku->save(); | |||||
$response = $this->actingAs($user)->get("api/v4/users/{$user->id}/skus"); | |||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertCount(6, $json); | $this->assertCount(1, $json); | ||||
$this->assertSame('domain-hosting', $json[0]['title']); | |||||
$this->assertSkuElement('mailbox', $json[0], [ | $this->assertSame(100, $json[0]['nextCost']); // second domain costs 100 | ||||
'prio' => 100, | |||||
'type' => 'user', | |||||
'handler' => 'Mailbox', | |||||
'enabled' => true, | |||||
'readonly' => true, | |||||
]); | |||||
$this->assertSkuElement('storage', $json[1], [ | |||||
'prio' => 90, | |||||
'type' => 'user', | |||||
'handler' => 'Storage', | |||||
'enabled' => true, | |||||
'readonly' => true, | |||||
'range' => [ | |||||
'min' => 5, | |||||
'max' => 100, | |||||
'unit' => 'GB', | |||||
] | |||||
]); | |||||
$this->assertSkuElement('groupware', $json[2], [ | |||||
'prio' => 80, | |||||
'type' => 'user', | |||||
'handler' => 'Groupware', | |||||
'enabled' => false, | |||||
'readonly' => false, | |||||
]); | |||||
$this->assertSkuElement('activesync', $json[3], [ | |||||
'prio' => 70, | |||||
'type' => 'user', | |||||
'handler' => 'Activesync', | |||||
'enabled' => false, | |||||
'readonly' => false, | |||||
'required' => ['Groupware'], | |||||
]); | |||||
$this->assertSkuElement('2fa', $json[4], [ | // Test the type filter, and nextCost property (user with no domain) | ||||
'prio' => 60, | $jane = $this->getTestUser('jane@kolabnow.com'); | ||||
'type' => 'user', | $kolab = \App\Package::withEnvTenantContext()->where('title', 'kolab')->first(); | ||||
'handler' => 'Auth2F', | $jane->assignPackage($kolab); | ||||
'enabled' => false, | |||||
'readonly' => false, | |||||
'forbidden' => ['Activesync'], | |||||
]); | |||||
$this->assertSkuElement('meet', $json[5], [ | $response = $this->actingAs($jane)->get("api/v4/skus?type=domain"); | ||||
'prio' => 50, | |||||
'type' => 'user', | |||||
'handler' => 'Meet', | |||||
'enabled' => false, | |||||
'readonly' => false, | |||||
'required' => ['Groupware'], | |||||
]); | |||||
// Test inclusion of beta SKUs | |||||
$sku = Sku::withEnvTenantContext()->where('title', 'beta')->first(); | |||||
$user->assignSku($sku); | |||||
$response = $this->actingAs($user)->get("api/v4/users/{$user->id}/skus"); | |||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertCount(6, $json); | $this->assertCount(1, $json); | ||||
$this->assertSame('domain-hosting', $json[0]['title']); | |||||
$this->assertSkuElement('beta', $json[6], [ | $this->assertSame(0, $json[0]['nextCost']); // first domain costs 0 | ||||
'prio' => 10, | |||||
'type' => 'user', | |||||
'handler' => 'Beta', | |||||
'enabled' => false, | |||||
'readonly' => false, | |||||
]); | |||||
} | |||||
/** | |||||
* Assert content of the SKU element in an API response | |||||
* | |||||
* @param string $sku_title The SKU title | |||||
* @param array $result The result to assert | |||||
* @param array $other Other items the SKU itself does not include | |||||
*/ | |||||
protected function assertSkuElement($sku_title, $result, $other = []): void | |||||
{ | |||||
$sku = Sku::withEnvTenantContext()->where('title', $sku_title)->first(); | |||||
$this->assertSame($sku->id, $result['id']); | |||||
$this->assertSame($sku->title, $result['title']); | |||||
$this->assertSame($sku->name, $result['name']); | |||||
$this->assertSame($sku->description, $result['description']); | |||||
$this->assertSame($sku->cost, $result['cost']); | |||||
$this->assertSame($sku->units_free, $result['units_free']); | |||||
$this->assertSame($sku->period, $result['period']); | |||||
$this->assertSame($sku->active, $result['active']); | |||||
foreach ($other as $key => $value) { | |||||
$this->assertSame($value, $result[$key]); | |||||
} | |||||
$this->assertCount(8 + count($other), $result); | |||||
} | } | ||||
} | } |