Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/UserTest.php
Show First 20 Lines • Show All 86 Lines • ▼ Show 20 Lines | class UserTest extends TestCase | ||||
/** | /** | ||||
* Test User::canRead() method | * Test User::canRead() method | ||||
*/ | */ | ||||
public function testCanRead(): void | public function testCanRead(): void | ||||
{ | { | ||||
$john = $this->getTestUser('john@kolab.org'); | $john = $this->getTestUser('john@kolab.org'); | ||||
$ned = $this->getTestUser('ned@kolab.org'); | $ned = $this->getTestUser('ned@kolab.org'); | ||||
$jack = $this->getTestUser('jack@kolab.org'); | $jack = $this->getTestUser('jack@kolab.org'); | ||||
$reseller1 = $this->getTestUser('reseller@kolabnow.com'); | $reseller1 = $this->getTestUser('reseller@' . \config('app.domain')); | ||||
$reseller2 = $this->getTestUser('reseller@reseller.com'); | $reseller2 = $this->getTestUser('reseller@sample-tenant.dev-local'); | ||||
$admin = $this->getTestUser('jeroen@jeroen.jeroen'); | $admin = $this->getTestUser('jeroen@jeroen.jeroen'); | ||||
$domain = $this->getTestDomain('kolab.org'); | $domain = $this->getTestDomain('kolab.org'); | ||||
// Admin | // Admin | ||||
$this->assertTrue($admin->canRead($admin)); | $this->assertTrue($admin->canRead($admin)); | ||||
$this->assertTrue($admin->canRead($john)); | $this->assertTrue($admin->canRead($john)); | ||||
$this->assertTrue($admin->canRead($jack)); | $this->assertTrue($admin->canRead($jack)); | ||||
$this->assertTrue($admin->canRead($reseller1)); | $this->assertTrue($admin->canRead($reseller1)); | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | class UserTest extends TestCase | ||||
/** | /** | ||||
* Test User::canUpdate() method | * Test User::canUpdate() method | ||||
*/ | */ | ||||
public function testCanUpdate(): void | public function testCanUpdate(): void | ||||
{ | { | ||||
$john = $this->getTestUser('john@kolab.org'); | $john = $this->getTestUser('john@kolab.org'); | ||||
$ned = $this->getTestUser('ned@kolab.org'); | $ned = $this->getTestUser('ned@kolab.org'); | ||||
$jack = $this->getTestUser('jack@kolab.org'); | $jack = $this->getTestUser('jack@kolab.org'); | ||||
$reseller1 = $this->getTestUser('reseller@kolabnow.com'); | $reseller1 = $this->getTestUser('reseller@' . \config('app.domain')); | ||||
$reseller2 = $this->getTestUser('reseller@reseller.com'); | $reseller2 = $this->getTestUser('reseller@sample-tenant.dev-local'); | ||||
$admin = $this->getTestUser('jeroen@jeroen.jeroen'); | $admin = $this->getTestUser('jeroen@jeroen.jeroen'); | ||||
$domain = $this->getTestDomain('kolab.org'); | $domain = $this->getTestDomain('kolab.org'); | ||||
// Admin | // Admin | ||||
$this->assertTrue($admin->canUpdate($admin)); | $this->assertTrue($admin->canUpdate($admin)); | ||||
$this->assertTrue($admin->canUpdate($john)); | $this->assertTrue($admin->canUpdate($john)); | ||||
$this->assertTrue($admin->canUpdate($jack)); | $this->assertTrue($admin->canUpdate($jack)); | ||||
$this->assertTrue($admin->canUpdate($reseller1)); | $this->assertTrue($admin->canUpdate($reseller1)); | ||||
▲ Show 20 Lines • Show All 117 Lines • ▼ Show 20 Lines | */ | ||||
} | } | ||||
/** | /** | ||||
* Tests for User::domains() | * Tests for User::domains() | ||||
*/ | */ | ||||
public function testDomains(): void | public function testDomains(): void | ||||
{ | { | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$domain = $this->getTestDomain('useraccount.com', [ | $domain = $this->getTestDomain('useraccount.com', [ | ||||
'status' => Domain::STATUS_NEW | Domain::STATUS_ACTIVE, | 'status' => Domain::STATUS_NEW | Domain::STATUS_ACTIVE, | ||||
'type' => Domain::TYPE_PUBLIC, | 'type' => Domain::TYPE_PUBLIC, | ||||
]); | ]); | ||||
$domains = collect($user->domains())->pluck('namespace')->all(); | $domains = collect($user->domains())->pluck('namespace')->all(); | ||||
$this->assertContains($domain->namespace, $domains); | $this->assertContains($domain->namespace, $domains); | ||||
$this->assertContains('kolab.org', $domains); | $this->assertContains('kolab.org', $domains); | ||||
// Jack is not the wallet controller, so for him the list should not | // Jack is not the wallet controller, so for him the list should not | ||||
// include John's domains, kolab.org specifically | // include John's domains, kolab.org specifically | ||||
$user = $this->getTestUser('jack@kolab.org'); | $user = $this->getTestUser('jack@kolab.org'); | ||||
$domains = collect($user->domains())->pluck('namespace')->all(); | $domains = collect($user->domains())->pluck('namespace')->all(); | ||||
$this->assertContains($domain->namespace, $domains); | $this->assertContains($domain->namespace, $domains); | ||||
$this->assertNotContains('kolab.org', $domains); | $this->assertNotContains('kolab.org', $domains); | ||||
// Public domains of other tenants should not be returned | // Public domains of other tenants should not be returned | ||||
$domain->tenant_id = 2; | $tenant = \App\Tenant::where('id', '!=', \config('app.tenant_id'))->first(); | ||||
$domain->tenant_id = $tenant->id; | |||||
$domain->save(); | $domain->save(); | ||||
$domains = collect($user->domains())->pluck('namespace')->all(); | $domains = collect($user->domains())->pluck('namespace')->all(); | ||||
$this->assertNotContains($domain->namespace, $domains); | $this->assertNotContains($domain->namespace, $domains); | ||||
} | } | ||||
/** | |||||
* Test User::hasSku() method | |||||
*/ | |||||
public function testHasSku(): void | |||||
{ | |||||
$john = $this->getTestUser('john@kolab.org'); | |||||
$this->assertTrue($john->hasSku('mailbox')); | |||||
$this->assertTrue($john->hasSku('storage')); | |||||
$this->assertFalse($john->hasSku('beta')); | |||||
$this->assertFalse($john->hasSku('unknown')); | |||||
} | |||||
public function testUserQuota(): void | public function testUserQuota(): void | ||||
{ | { | ||||
// TODO: This test does not test much, probably could be removed | // TODO: This test does not test much, probably could be removed | ||||
// or moved to somewhere else, or extended with | // or moved to somewhere else, or extended with | ||||
// other entitlements() related cases. | // other entitlements() related cases. | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$storage_sku = \App\Sku::where('title', 'storage')->first(); | $storage_sku = \App\Sku::withEnvTenantContext()->where('title', 'storage')->first(); | ||||
$count = 0; | $count = 0; | ||||
foreach ($user->entitlements()->get() as $entitlement) { | foreach ($user->entitlements()->get() as $entitlement) { | ||||
if ($entitlement->sku_id == $storage_sku->id) { | if ($entitlement->sku_id == $storage_sku->id) { | ||||
$count += 1; | $count += 1; | ||||
} | } | ||||
} | } | ||||
$this->assertTrue($count == 2); | $this->assertTrue($count == 5); | ||||
} | } | ||||
/** | /** | ||||
* Test user deletion | * Test user deletion | ||||
*/ | */ | ||||
public function testDelete(): void | public function testDelete(): void | ||||
{ | { | ||||
Queue::fake(); | Queue::fake(); | ||||
$user = $this->getTestUser('user-test@' . \config('app.domain')); | $user = $this->getTestUser('user-test@' . \config('app.domain')); | ||||
$package = \App\Package::where('title', 'kolab')->first(); | $package = \App\Package::withEnvTenantContext()->where('title', 'kolab')->first(); | ||||
$user->assignPackage($package); | $user->assignPackage($package); | ||||
$id = $user->id; | $id = $user->id; | ||||
$this->assertCount(4, $user->entitlements()->get()); | $this->assertCount(7, $user->entitlements()->get()); | ||||
$user->delete(); | $user->delete(); | ||||
$this->assertCount(0, $user->entitlements()->get()); | $this->assertCount(0, $user->entitlements()->get()); | ||||
$this->assertTrue($user->fresh()->trashed()); | $this->assertTrue($user->fresh()->trashed()); | ||||
$this->assertFalse($user->fresh()->isDeleted()); | $this->assertFalse($user->fresh()->isDeleted()); | ||||
// Delete the user for real | // Delete the user for real | ||||
$job = new \App\Jobs\User\DeleteJob($id); | $job = new \App\Jobs\User\DeleteJob($id); | ||||
$job->handle(); | $job->handle(); | ||||
$this->assertTrue(User::withTrashed()->where('id', $id)->first()->isDeleted()); | $this->assertTrue(User::withTrashed()->where('id', $id)->first()->isDeleted()); | ||||
$user->forceDelete(); | $user->forceDelete(); | ||||
$this->assertCount(0, User::withTrashed()->where('id', $id)->get()); | $this->assertCount(0, User::withTrashed()->where('id', $id)->get()); | ||||
// Test an account with users, domain, and group | // Test an account with users, domain, and group | ||||
$userA = $this->getTestUser('UserAccountA@UserAccount.com'); | $userA = $this->getTestUser('UserAccountA@UserAccount.com'); | ||||
$userB = $this->getTestUser('UserAccountB@UserAccount.com'); | $userB = $this->getTestUser('UserAccountB@UserAccount.com'); | ||||
$userC = $this->getTestUser('UserAccountC@UserAccount.com'); | $userC = $this->getTestUser('UserAccountC@UserAccount.com'); | ||||
$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(); | ||||
$domain = $this->getTestDomain('UserAccount.com', [ | $domain = $this->getTestDomain('UserAccount.com', [ | ||||
'status' => Domain::STATUS_NEW, | 'status' => Domain::STATUS_NEW, | ||||
'type' => Domain::TYPE_HOSTED, | 'type' => Domain::TYPE_HOSTED, | ||||
]); | ]); | ||||
$userA->assignPackage($package_kolab); | $userA->assignPackage($package_kolab); | ||||
$domain->assignPackage($package_domain, $userA); | $domain->assignPackage($package_domain, $userA); | ||||
$userA->assignPackage($package_kolab, $userB); | $userA->assignPackage($package_kolab, $userB); | ||||
$userA->assignPackage($package_kolab, $userC); | $userA->assignPackage($package_kolab, $userC); | ||||
$group = $this->getTestGroup('test-group@UserAccount.com'); | $group = $this->getTestGroup('test-group@UserAccount.com'); | ||||
$group->assignToWallet($userA->wallets->first()); | $group->assignToWallet($userA->wallets->first()); | ||||
$entitlementsA = \App\Entitlement::where('entitleable_id', $userA->id); | $entitlementsA = \App\Entitlement::where('entitleable_id', $userA->id); | ||||
$entitlementsB = \App\Entitlement::where('entitleable_id', $userB->id); | $entitlementsB = \App\Entitlement::where('entitleable_id', $userB->id); | ||||
$entitlementsC = \App\Entitlement::where('entitleable_id', $userC->id); | $entitlementsC = \App\Entitlement::where('entitleable_id', $userC->id); | ||||
$entitlementsDomain = \App\Entitlement::where('entitleable_id', $domain->id); | $entitlementsDomain = \App\Entitlement::where('entitleable_id', $domain->id); | ||||
$entitlementsGroup = \App\Entitlement::where('entitleable_id', $group->id); | $entitlementsGroup = \App\Entitlement::where('entitleable_id', $group->id); | ||||
$this->assertSame(4, $entitlementsA->count()); | $this->assertSame(7, $entitlementsA->count()); | ||||
$this->assertSame(4, $entitlementsB->count()); | $this->assertSame(7, $entitlementsB->count()); | ||||
$this->assertSame(4, $entitlementsC->count()); | $this->assertSame(7, $entitlementsC->count()); | ||||
$this->assertSame(1, $entitlementsDomain->count()); | $this->assertSame(1, $entitlementsDomain->count()); | ||||
$this->assertSame(1, $entitlementsGroup->count()); | $this->assertSame(1, $entitlementsGroup->count()); | ||||
// Delete non-controller user | // Delete non-controller user | ||||
$userC->delete(); | $userC->delete(); | ||||
$this->assertTrue($userC->fresh()->trashed()); | $this->assertTrue($userC->fresh()->trashed()); | ||||
$this->assertFalse($userC->fresh()->isDeleted()); | $this->assertFalse($userC->fresh()->isDeleted()); | ||||
Show All 29 Lines | */ | ||||
/** | /** | ||||
* Test user deletion vs. group membership | * Test user deletion vs. group membership | ||||
*/ | */ | ||||
public function testDeleteAndGroups(): void | public function testDeleteAndGroups(): void | ||||
{ | { | ||||
Queue::fake(); | Queue::fake(); | ||||
$package_kolab = \App\Package::where('title', 'kolab')->first(); | $package_kolab = \App\Package::withEnvTenantContext()->where('title', 'kolab')->first(); | ||||
$userA = $this->getTestUser('UserAccountA@UserAccount.com'); | $userA = $this->getTestUser('UserAccountA@UserAccount.com'); | ||||
$userB = $this->getTestUser('UserAccountB@UserAccount.com'); | $userB = $this->getTestUser('UserAccountB@UserAccount.com'); | ||||
$userA->assignPackage($package_kolab, $userB); | $userA->assignPackage($package_kolab, $userB); | ||||
$group = $this->getTestGroup('test-group@UserAccount.com'); | $group = $this->getTestGroup('test-group@UserAccount.com'); | ||||
$group->members = ['test@gmail.com', $userB->email]; | $group->members = ['test@gmail.com', $userB->email]; | ||||
$group->assignToWallet($userA->wallets->first()); | $group->assignToWallet($userA->wallets->first()); | ||||
$group->save(); | $group->save(); | ||||
▲ Show 20 Lines • Show All 143 Lines • ▼ Show 20 Lines | */ | ||||
{ | { | ||||
Queue::fake(); | Queue::fake(); | ||||
// Test an account with users and domain | // Test an account with users and domain | ||||
$userA = $this->getTestUser('UserAccountA@UserAccount.com', [ | $userA = $this->getTestUser('UserAccountA@UserAccount.com', [ | ||||
'status' => User::STATUS_LDAP_READY | User::STATUS_IMAP_READY | User::STATUS_SUSPENDED, | 'status' => User::STATUS_LDAP_READY | User::STATUS_IMAP_READY | User::STATUS_SUSPENDED, | ||||
]); | ]); | ||||
$userB = $this->getTestUser('UserAccountB@UserAccount.com'); | $userB = $this->getTestUser('UserAccountB@UserAccount.com'); | ||||
$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(); | ||||
$domainA = $this->getTestDomain('UserAccount.com', [ | $domainA = $this->getTestDomain('UserAccount.com', [ | ||||
'status' => Domain::STATUS_NEW, | 'status' => Domain::STATUS_NEW, | ||||
'type' => Domain::TYPE_HOSTED, | 'type' => Domain::TYPE_HOSTED, | ||||
]); | ]); | ||||
$domainB = $this->getTestDomain('UserAccountAdd.com', [ | $domainB = $this->getTestDomain('UserAccountAdd.com', [ | ||||
'status' => Domain::STATUS_NEW, | 'status' => Domain::STATUS_NEW, | ||||
'type' => Domain::TYPE_HOSTED, | 'type' => Domain::TYPE_HOSTED, | ||||
]); | ]); | ||||
$userA->assignPackage($package_kolab); | $userA->assignPackage($package_kolab); | ||||
$domainA->assignPackage($package_domain, $userA); | $domainA->assignPackage($package_domain, $userA); | ||||
$domainB->assignPackage($package_domain, $userA); | $domainB->assignPackage($package_domain, $userA); | ||||
$userA->assignPackage($package_kolab, $userB); | $userA->assignPackage($package_kolab, $userB); | ||||
$storage_sku = \App\Sku::where('title', 'storage')->first(); | $storage_sku = \App\Sku::withEnvTenantContext()->where('title', 'storage')->first(); | ||||
$now = \Carbon\Carbon::now(); | $now = \Carbon\Carbon::now(); | ||||
$wallet_id = $userA->wallets->first()->id; | $wallet_id = $userA->wallets->first()->id; | ||||
// add an extra storage entitlement | // add an extra storage entitlement | ||||
$ent1 = \App\Entitlement::create([ | $ent1 = \App\Entitlement::create([ | ||||
'wallet_id' => $wallet_id, | 'wallet_id' => $wallet_id, | ||||
'sku_id' => $storage_sku->id, | 'sku_id' => $storage_sku->id, | ||||
'cost' => 0, | 'cost' => 0, | ||||
Show All 40 Lines | public function testRestore(): void | ||||
$this->assertFalse($userA->isImapReady()); | $this->assertFalse($userA->isImapReady()); | ||||
$this->assertTrue($userA->isActive()); | $this->assertTrue($userA->isActive()); | ||||
$this->assertTrue($userB->fresh()->trashed()); | $this->assertTrue($userB->fresh()->trashed()); | ||||
$this->assertTrue($domainB->fresh()->trashed()); | $this->assertTrue($domainB->fresh()->trashed()); | ||||
$this->assertFalse($domainA->fresh()->trashed()); | $this->assertFalse($domainA->fresh()->trashed()); | ||||
// Assert entitlements | // Assert entitlements | ||||
$this->assertSame(4, $entitlementsA->count()); // mailbox + groupware + 2 x storage | $this->assertSame(7, $entitlementsA->count()); // mailbox + groupware + 5 x storage | ||||
machniak: 2 -> 5 in the comment | |||||
$this->assertTrue($ent1->fresh()->trashed()); | $this->assertTrue($ent1->fresh()->trashed()); | ||||
$entitlementsA->get()->each(function ($ent) { | $entitlementsA->get()->each(function ($ent) { | ||||
$this->assertTrue($ent->updated_at->greaterThan(\Carbon\Carbon::now()->subSeconds(5))); | $this->assertTrue($ent->updated_at->greaterThan(\Carbon\Carbon::now()->subSeconds(5))); | ||||
}); | }); | ||||
// We expect only CreateJob + UpdateJob pair for both user and domain. | // We expect only CreateJob + UpdateJob pair for both user and domain. | ||||
// Because how Illuminate/Database/Eloquent/SoftDeletes::restore() method | // Because how Illuminate/Database/Eloquent/SoftDeletes::restore() method | ||||
// is implemented we cannot skip the UpdateJob in any way. | // is implemented we cannot skip the UpdateJob in any way. | ||||
▲ Show 20 Lines • Show All 194 Lines • Show Last 20 Lines |
2 -> 5 in the comment