Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/Reseller/DiscountsTest.php
<?php | <?php | ||||
namespace Tests\Feature\Controller\Reseller; | namespace Tests\Feature\Controller\Reseller; | ||||
use App\Discount; | use App\Discount; | ||||
use App\Tenant; | use App\Tenant; | ||||
use Tests\TestCase; | use Tests\TestCase; | ||||
class DiscountsTest extends TestCase | class DiscountsTest extends TestCase | ||||
{ | { | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function setUp(): void | public function setUp(): void | ||||
{ | { | ||||
parent::setUp(); | parent::setUp(); | ||||
$tenant = Tenant::where('title', 'Sample Tenant')->first(); | |||||
$tenant->discounts()->delete(); | |||||
self::useResellerUrl(); | self::useResellerUrl(); | ||||
} | } | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
\config(['app.tenant_id' => 1]); | |||||
$tenant = Tenant::where('title', 'Sample Tenant')->first(); | |||||
$tenant->discounts()->delete(); | |||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test listing discounts (/api/v4/discounts) | * Test listing discounts (GET /api/v4/users/{id}/discounts) | ||||
*/ | */ | ||||
public function testIndex(): void | public function testUserDiscounts(): void | ||||
{ | { | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$admin = $this->getTestUser('jeroen@jeroen.jeroen'); | $admin = $this->getTestUser('jeroen@jeroen.jeroen'); | ||||
$reseller = $this->getTestUser('reseller@reseller.com'); | $reseller1 = $this->getTestUser('reseller@' . \config('app.domain')); | ||||
$reseller2 = $this->getTestUser('reseller@kolabnow.com'); | $reseller2 = $this->getTestUser('reseller@sample-tenant.dev-local'); | ||||
$tenant = Tenant::where('title', 'Sample Tenant')->first(); | |||||
\config(['app.tenant_id' => $tenant->id]); | |||||
// Non-admin user | // Non-admin user | ||||
$response = $this->actingAs($user)->get("api/v4/discounts"); | $response = $this->actingAs($user)->get("api/v4/users/{$user->id}/discounts"); | ||||
$response->assertStatus(403); | $response->assertStatus(403); | ||||
// Admin user | // Admin user | ||||
$response = $this->actingAs($admin)->get("api/v4/discounts"); | $response = $this->actingAs($admin)->get("api/v4/users/{$user->id}/discounts"); | ||||
$response->assertStatus(403); | $response->assertStatus(403); | ||||
// Reseller user, but different tenant | // Reseller user, but different tenant | ||||
$response = $this->actingAs($reseller2)->get("api/v4/discounts"); | $response = $this->actingAs($reseller2)->get("api/v4/users/{$user->id}/discounts"); | ||||
$response->assertStatus(403); | $response->assertStatus(404); | ||||
// Reseller (empty list) | // Reseller | ||||
$response = $this->actingAs($reseller)->get("api/v4/discounts"); | $response = $this->actingAs($reseller1)->get("api/v4/users/{$user->id}/discounts"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame(0, $json['count']); | $discount_test = Discount::where('code', 'TEST')->first(); | ||||
$discount_free = Discount::where('discount', 100)->first(); | |||||
// Add some discounts | $this->assertSame(3, $json['count']); | ||||
$discount_test = Discount::create([ | $this->assertSame($discount_test->id, $json['list'][0]['id']); | ||||
'description' => 'Test reseller voucher', | $this->assertSame($discount_test->discount, $json['list'][0]['discount']); | ||||
'code' => 'RESELLER-TEST', | $this->assertSame($discount_test->code, $json['list'][0]['code']); | ||||
'discount' => 10, | $this->assertSame($discount_test->description, $json['list'][0]['description']); | ||||
'active' => true, | $this->assertSame('10% - Test voucher [TEST]', $json['list'][0]['label']); | ||||
]); | |||||
$this->assertSame($discount_free->id, $json['list'][2]['id']); | |||||
$discount_free = Discount::create([ | $this->assertSame($discount_free->discount, $json['list'][2]['discount']); | ||||
'description' => 'Free account', | $this->assertSame($discount_free->code, $json['list'][2]['code']); | ||||
'discount' => 100, | $this->assertSame($discount_free->description, $json['list'][2]['description']); | ||||
'active' => true, | $this->assertSame('100% - Free Account', $json['list'][2]['label']); | ||||
]); | |||||
// A user in another tenant's user | |||||
$discount_test->tenant_id = $tenant->id; | $user = $this->getTestUser('user@sample-tenant.dev-local'); | ||||
$discount_test->save(); | |||||
$discount_free->tenant_id = $tenant->id; | $response = $this->actingAs($reseller1)->get("api/v4/users/{$user->id}/discounts"); | ||||
$discount_free->save(); | $response->assertStatus(404); | ||||
$response = $this->actingAs($reseller)->get("api/v4/discounts"); | $response = $this->actingAs($reseller2)->get("api/v4/users/{$user->id}/discounts"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame(2, $json['count']); | $discount = Discount::withObjectTenantContext($user)->where('discount', 10)->first(); | ||||
$this->assertSame($discount_test->id, $json['list'][0]['id']); | |||||
$this->assertSame($discount_test->discount, $json['list'][0]['discount']); | |||||
$this->assertSame($discount_test->code, $json['list'][0]['code']); | |||||
$this->assertSame($discount_test->description, $json['list'][0]['description']); | |||||
$this->assertSame('10% - Test reseller voucher [RESELLER-TEST]', $json['list'][0]['label']); | |||||
$this->assertSame($discount_free->id, $json['list'][1]['id']); | $this->assertSame(1, $json['count']); | ||||
$this->assertSame($discount_free->discount, $json['list'][1]['discount']); | $this->assertSame($discount->id, $json['list'][0]['id']); | ||||
$this->assertSame($discount_free->code, $json['list'][1]['code']); | $this->assertSame($discount->discount, $json['list'][0]['discount']); | ||||
$this->assertSame($discount_free->description, $json['list'][1]['description']); | $this->assertSame($discount->code, $json['list'][0]['code']); | ||||
$this->assertSame('100% - Free account', $json['list'][1]['label']); | $this->assertSame($discount->description, $json['list'][0]['description']); | ||||
$this->assertSame('10% - ' . $discount->description, $json['list'][0]['label']); | |||||
} | } | ||||
} | } |