diff --git a/src/tests/Feature/Controller/Reseller/DiscountsTest.php b/src/tests/Feature/Controller/Reseller/DiscountsTest.php index 95093f69..0cb62719 100644 --- a/src/tests/Feature/Controller/Reseller/DiscountsTest.php +++ b/src/tests/Feature/Controller/Reseller/DiscountsTest.php @@ -1,97 +1,119 @@ deleteTestUser('test@reseller.com'); + $tenant = Tenant::where('title', 'Sample Tenant')->first(); $tenant->discounts()->delete(); self::useResellerUrl(); } /** * {@inheritDoc} */ public function tearDown(): void { + \config(['app.tenant_id' => 1]); + + $this->deleteTestUser('test@reseller.com'); + $tenant = Tenant::where('title', 'Sample Tenant')->first(); $tenant->discounts()->delete(); parent::tearDown(); } /** * Test listing discounts (/api/v4/discounts) */ public function testIndex(): void { $user = $this->getTestUser('john@kolab.org'); $admin = $this->getTestUser('jeroen@jeroen.jeroen'); $reseller = $this->getTestUser('reseller@reseller.com'); + $tenant = Tenant::where('title', 'Sample Tenant')->first(); + $tenant2 = Tenant::where('title', 'Kolab Now')->first(); + $reseller2 = $this->getTestUser('test@reseller.com'); + $reseller2->tenant_id = $tenant2->id; + $reseller2->role = 'reseller'; + $reseller2->save(); + + $reseller2->tenant_id = $tenant2->id; + $reseller2->role = 'reseller'; + $reseller2->save(); + + \config(['app.tenant_id' => $tenant->id]); // Non-admin user $response = $this->actingAs($user)->get("api/v4/discounts"); $response->assertStatus(403); // Admin user $response = $this->actingAs($admin)->get("api/v4/discounts"); $response->assertStatus(403); + // Reseller user, but different tenant + $response = $this->actingAs($reseller2)->get("api/v4/discounts"); + $response->assertStatus(403); + // Reseller (empty list) $response = $this->actingAs($reseller)->get("api/v4/discounts"); $response->assertStatus(200); $json = $response->json(); $this->assertSame(0, $json['count']); // Add some discounts $discount_test = Discount::create([ 'description' => 'Test reseller voucher', 'code' => 'RESELLER-TEST', 'discount' => 10, 'active' => true, ]); $discount_free = Discount::create([ 'description' => 'Free account', 'discount' => 100, 'active' => true, ]); $discount_test->tenant_id = $tenant->id; $discount_test->save(); $discount_free->tenant_id = $tenant->id; $discount_free->save(); $response = $this->actingAs($reseller)->get("api/v4/discounts"); $response->assertStatus(200); $json = $response->json(); $this->assertSame(2, $json['count']); $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($discount_free->discount, $json['list'][1]['discount']); $this->assertSame($discount_free->code, $json['list'][1]['code']); $this->assertSame($discount_free->description, $json['list'][1]['description']); $this->assertSame('100% - Free account', $json['list'][1]['label']); } } diff --git a/src/tests/Feature/Controller/Reseller/UsersTest.php b/src/tests/Feature/Controller/Reseller/UsersTest.php index e2981f0d..6a584d10 100644 --- a/src/tests/Feature/Controller/Reseller/UsersTest.php +++ b/src/tests/Feature/Controller/Reseller/UsersTest.php @@ -1,290 +1,292 @@ 1]); // $this->deleteTestUser('UsersControllerTest1@userscontroller.com'); + $this->deleteTestUser('test@reseller.com'); $this->deleteTestUser('test@testsearch.com'); $this->deleteTestDomain('testsearch.com'); } /** * {@inheritDoc} */ public function tearDown(): void { // $this->deleteTestUser('UsersControllerTest1@userscontroller.com'); + $this->deleteTestUser('test@reseller.com'); $this->deleteTestUser('test@testsearch.com'); $this->deleteTestDomain('testsearch.com'); \config(['app.tenant_id' => 1]); parent::tearDown(); } /** * Test users searching (/api/v4/users) */ public function testIndex(): void { Queue::fake(); $user = $this->getTestUser('john@kolab.org'); $admin = $this->getTestUser('jeroen@jeroen.jeroen'); $reseller = $this->getTestUser('reseller@reseller.com'); $reseller2 = $this->getTestUser('test@reseller.com'); $tenant = Tenant::where('title', 'Sample Tenant')->first(); $tenant2 = Tenant::where('title', 'Kolab Now')->first(); $reseller2->tenant_id = $tenant2->id; $reseller2->role = 'reseller'; $reseller2->save(); \config(['app.tenant_id' => $tenant->id]); // Normal user $response = $this->actingAs($user)->get("api/v4/users"); $response->assertStatus(403); // Admin user $response = $this->actingAs($admin)->get("api/v4/users"); $response->assertStatus(403); // Reseller from another tenant $response = $this->actingAs($reseller2)->get("api/v4/users"); $response->assertStatus(403); // Search with no search criteria $response = $this->actingAs($reseller)->get("api/v4/users"); $response->assertStatus(200); $json = $response->json(); $this->assertSame(0, $json['count']); $this->assertSame([], $json['list']); // Search with no matches expected $response = $this->actingAs($reseller)->get("api/v4/users?search=abcd1234efgh5678"); $response->assertStatus(200); $json = $response->json(); $this->assertSame(0, $json['count']); $this->assertSame([], $json['list']); // Search by domain in another tenant $response = $this->actingAs($reseller)->get("api/v4/users?search=kolab.org"); $response->assertStatus(200); $json = $response->json(); $this->assertSame(0, $json['count']); $this->assertSame([], $json['list']); // Search by user ID in another tenant $response = $this->actingAs($reseller)->get("api/v4/users?search={$user->id}"); $response->assertStatus(200); $json = $response->json(); $this->assertSame(0, $json['count']); $this->assertSame([], $json['list']); // Search by email (primary) - existing user in another tenant $response = $this->actingAs($reseller)->get("api/v4/users?search=john@kolab.org"); $response->assertStatus(200); $json = $response->json(); $this->assertSame(0, $json['count']); $this->assertSame([], $json['list']); // Search by owner - existing user in another tenant $response = $this->actingAs($reseller)->get("api/v4/users?owner={$user->id}"); $response->assertStatus(200); $json = $response->json(); $this->assertSame(0, $json['count']); $this->assertSame([], $json['list']); // Create a domain with some users in the Sample Tenant so we have anything to search for $domain = $this->getTestDomain('testsearch.com', ['type' => \App\Domain::TYPE_EXTERNAL]); $domain->tenant_id = $tenant->id; $domain->save(); $user = $this->getTestUser('test@testsearch.com'); $user->tenant_id = $tenant->id; $user->save(); $plan = \App\Plan::where('title', 'group')->first(); $user->assignPlan($plan, $domain); $user->setAliases(['alias@testsearch.com']); $user->setSetting('external_email', 'john.doe.external@gmail.com'); // Search by domain $response = $this->actingAs($reseller)->get("api/v4/users?search=testsearch.com"); $response->assertStatus(200); $json = $response->json(); $this->assertSame(1, $json['count']); $this->assertCount(1, $json['list']); $this->assertSame($user->id, $json['list'][0]['id']); $this->assertSame($user->email, $json['list'][0]['email']); // Search by user ID $response = $this->actingAs($reseller)->get("api/v4/users?search={$user->id}"); $response->assertStatus(200); $json = $response->json(); $this->assertSame(1, $json['count']); $this->assertCount(1, $json['list']); $this->assertSame($user->id, $json['list'][0]['id']); $this->assertSame($user->email, $json['list'][0]['email']); // Search by email (primary) - existing user in reseller's tenant $response = $this->actingAs($reseller)->get("api/v4/users?search=test@testsearch.com"); $response->assertStatus(200); $json = $response->json(); $this->assertSame(1, $json['count']); $this->assertCount(1, $json['list']); $this->assertSame($user->id, $json['list'][0]['id']); $this->assertSame($user->email, $json['list'][0]['email']); // Search by email (alias) $response = $this->actingAs($reseller)->get("api/v4/users?search=alias@testsearch.com"); $response->assertStatus(200); $json = $response->json(); $this->assertSame(1, $json['count']); $this->assertCount(1, $json['list']); $this->assertSame($user->id, $json['list'][0]['id']); $this->assertSame($user->email, $json['list'][0]['email']); // Search by email (external), there are two users with this email, but only one // in the reseller's tenant $response = $this->actingAs($reseller)->get("api/v4/users?search=john.doe.external@gmail.com"); $response->assertStatus(200); $json = $response->json(); $this->assertSame(1, $json['count']); $this->assertCount(1, $json['list']); $this->assertSame($user->id, $json['list'][0]['id']); $this->assertSame($user->email, $json['list'][0]['email']); // Search by owner $response = $this->actingAs($reseller)->get("api/v4/users?owner={$user->id}"); $response->assertStatus(200); $json = $response->json(); $this->assertSame(1, $json['count']); $this->assertCount(1, $json['list']); $this->assertSame($user->id, $json['list'][0]['id']); $this->assertSame($user->email, $json['list'][0]['email']); // Deleted users/domains $user->delete(); $response = $this->actingAs($reseller)->get("api/v4/users?search=test@testsearch.com"); $response->assertStatus(200); $json = $response->json(); $this->assertSame(1, $json['count']); $this->assertCount(1, $json['list']); $this->assertSame($user->id, $json['list'][0]['id']); $this->assertSame($user->email, $json['list'][0]['email']); $this->assertTrue($json['list'][0]['isDeleted']); $response = $this->actingAs($reseller)->get("api/v4/users?search=alias@testsearch.com"); $response->assertStatus(200); $json = $response->json(); $this->assertSame(1, $json['count']); $this->assertCount(1, $json['list']); $this->assertSame($user->id, $json['list'][0]['id']); $this->assertSame($user->email, $json['list'][0]['email']); $this->assertTrue($json['list'][0]['isDeleted']); $response = $this->actingAs($reseller)->get("api/v4/users?search=testsearch.com"); $response->assertStatus(200); $json = $response->json(); $this->assertSame(1, $json['count']); $this->assertCount(1, $json['list']); $this->assertSame($user->id, $json['list'][0]['id']); $this->assertSame($user->email, $json['list'][0]['email']); $this->assertTrue($json['list'][0]['isDeleted']); } /** * Test user update (PUT /api/v4/users/) */ public function testUpdate(): void { $this->markTestIncomplete(); /* $user = $this->getTestUser('UsersControllerTest1@userscontroller.com'); $admin = $this->getTestUser('jeroen@jeroen.jeroen'); // Test unauthorized access to admin API $response = $this->actingAs($user)->put("/api/v4/users/{$user->id}", []); $response->assertStatus(403); // Test updatig the user data (empty data) $response = $this->actingAs($admin)->put("/api/v4/users/{$user->id}", []); $response->assertStatus(200); $json = $response->json(); $this->assertSame('success', $json['status']); $this->assertSame("User data updated successfully.", $json['message']); $this->assertCount(2, $json); // Test error handling $post = ['external_email' => 'aaa']; $response = $this->actingAs($admin)->put("/api/v4/users/{$user->id}", $post); $response->assertStatus(422); $json = $response->json(); $this->assertSame('error', $json['status']); $this->assertSame("The external email must be a valid email address.", $json['errors']['external_email'][0]); $this->assertCount(2, $json); // Test real update $post = ['external_email' => 'modified@test.com']; $response = $this->actingAs($admin)->put("/api/v4/users/{$user->id}", $post); $response->assertStatus(200); $json = $response->json(); $this->assertSame('success', $json['status']); $this->assertSame("User data updated successfully.", $json['message']); $this->assertCount(2, $json); $this->assertSame('modified@test.com', $user->getSetting('external_email')); */ } }