Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/DomainsTest.php
Show All 14 Lines | |||||
{ | { | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function setUp(): void | public function setUp(): void | ||||
{ | { | ||||
parent::setUp(); | parent::setUp(); | ||||
$this->deleteTestUser('test1@' . \config('app.domain')); | |||||
$this->deleteTestUser('test2@' . \config('app.domain')); | |||||
$this->deleteTestUser('test1@domainscontroller.com'); | $this->deleteTestUser('test1@domainscontroller.com'); | ||||
$this->deleteTestDomain('domainscontroller.com'); | $this->deleteTestDomain('domainscontroller.com'); | ||||
} | } | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
$this->deleteTestUser('test1@' . \config('app.domain')); | |||||
$this->deleteTestUser('test2@' . \config('app.domain')); | |||||
$this->deleteTestUser('test1@domainscontroller.com'); | $this->deleteTestUser('test1@domainscontroller.com'); | ||||
$this->deleteTestDomain('domainscontroller.com'); | $this->deleteTestDomain('domainscontroller.com'); | ||||
$domain = $this->getTestDomain('kolab.org'); | $domain = $this->getTestDomain('kolab.org'); | ||||
$domain->settings()->whereIn('key', ['spf_whitelist'])->delete(); | $domain->settings()->whereIn('key', ['spf_whitelist'])->delete(); | ||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test domain confirm request | * Test domain confirm request | ||||
*/ | */ | ||||
public function testConfirm(): void | public function testConfirm(): void | ||||
{ | { | ||||
Queue::fake(); | |||||
$sku_domain = Sku::withEnvTenantContext()->where('title', 'domain-hosting')->first(); | $sku_domain = Sku::withEnvTenantContext()->where('title', 'domain-hosting')->first(); | ||||
$john = $this->getTestUser('john@kolab.org'); | $john = $this->getTestUser('john@kolab.org'); | ||||
$ned = $this->getTestUser('ned@kolab.org'); | $ned = $this->getTestUser('ned@kolab.org'); | ||||
$user = $this->getTestUser('test1@domainscontroller.com'); | $user = $this->getTestUser('test1@domainscontroller.com'); | ||||
$domain = $this->getTestDomain('domainscontroller.com', [ | $domain = $this->getTestDomain('domainscontroller.com', [ | ||||
'status' => Domain::STATUS_NEW, | 'status' => Domain::STATUS_NEW, | ||||
'type' => Domain::TYPE_EXTERNAL, | 'type' => Domain::TYPE_EXTERNAL, | ||||
]); | ]); | ||||
Show All 32 Lines | public function testConfirm(): void | ||||
// Authorized access by additional account controller | // Authorized access by additional account controller | ||||
$domain = $this->getTestDomain('kolab.org'); | $domain = $this->getTestDomain('kolab.org'); | ||||
$response = $this->actingAs($ned)->get("api/v4/domains/{$domain->id}/confirm"); | $response = $this->actingAs($ned)->get("api/v4/domains/{$domain->id}/confirm"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
} | } | ||||
/** | /** | ||||
* Test domain delete request (DELETE /api/v4/domains/<id>) | |||||
*/ | |||||
public function testDestroy(): void | |||||
{ | |||||
Queue::fake(); | |||||
$sku_domain = Sku::withEnvTenantContext()->where('title', 'domain-hosting')->first(); | |||||
$john = $this->getTestUser('john@kolab.org'); | |||||
$johns_domain = $this->getTestDomain('kolab.org'); | |||||
$user1 = $this->getTestUser('test1@' . \config('app.domain')); | |||||
$user2 = $this->getTestUser('test2@' . \config('app.domain')); | |||||
$domain = $this->getTestDomain('domainscontroller.com', [ | |||||
'status' => Domain::STATUS_NEW, | |||||
'type' => Domain::TYPE_EXTERNAL, | |||||
]); | |||||
Entitlement::create([ | |||||
'wallet_id' => $user1->wallets()->first()->id, | |||||
'sku_id' => $sku_domain->id, | |||||
'entitleable_id' => $domain->id, | |||||
'entitleable_type' => Domain::class | |||||
]); | |||||
// Not authorized access | |||||
$response = $this->actingAs($john)->delete("api/v4/domains/{$domain->id}"); | |||||
$response->assertStatus(403); | |||||
// Can't delete non-empty domain | |||||
$response = $this->actingAs($john)->delete("api/v4/domains/{$johns_domain->id}"); | |||||
$response->assertStatus(422); | |||||
$json = $response->json(); | |||||
$this->assertCount(2, $json); | |||||
$this->assertEquals('error', $json['status']); | |||||
$this->assertEquals('Unable to delete a domain with assigned users or other objects.', $json['message']); | |||||
// Successful deletion | |||||
$response = $this->actingAs($user1)->delete("api/v4/domains/{$domain->id}"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertCount(2, $json); | |||||
$this->assertEquals('success', $json['status']); | |||||
$this->assertEquals('Domain deleted successfully.', $json['message']); | |||||
$this->assertTrue($domain->fresh()->trashed()); | |||||
// Authorized access by additional account controller | |||||
$this->deleteTestDomain('domainscontroller.com'); | |||||
$domain = $this->getTestDomain('domainscontroller.com', [ | |||||
'status' => Domain::STATUS_NEW, | |||||
'type' => Domain::TYPE_EXTERNAL, | |||||
]); | |||||
Entitlement::create([ | |||||
'wallet_id' => $user1->wallets()->first()->id, | |||||
'sku_id' => $sku_domain->id, | |||||
'entitleable_id' => $domain->id, | |||||
'entitleable_type' => Domain::class | |||||
]); | |||||
$user1->wallets()->first()->addController($user2); | |||||
$response = $this->actingAs($user2)->delete("api/v4/domains/{$domain->id}"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertCount(2, $json); | |||||
$this->assertEquals('success', $json['status']); | |||||
$this->assertEquals('Domain deleted successfully.', $json['message']); | |||||
$this->assertTrue($domain->fresh()->trashed()); | |||||
} | |||||
/** | |||||
* Test fetching domains list | * Test fetching domains list | ||||
*/ | */ | ||||
public function testIndex(): void | public function testIndex(): void | ||||
{ | { | ||||
// User with no domains | // User with no domains | ||||
$user = $this->getTestUser('test1@domainscontroller.com'); | $user = $this->getTestUser('test1@domainscontroller.com'); | ||||
$response = $this->actingAs($user)->get("api/v4/domains"); | $response = $this->actingAs($user)->get("api/v4/domains"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
▲ Show 20 Lines • Show All 332 Lines • ▼ Show 20 Lines | public function testStore(): void | ||||
// Assert the new domain entitlements | // Assert the new domain entitlements | ||||
$this->assertEntitlements($domain, ['domain-hosting']); | $this->assertEntitlements($domain, ['domain-hosting']); | ||||
// Assert the wallet to which the new domain should be assigned to | // Assert the wallet to which the new domain should be assigned to | ||||
$wallet = $domain->wallet(); | $wallet = $domain->wallet(); | ||||
$this->assertSame($john->wallets->first()->id, $wallet->id); | $this->assertSame($john->wallets->first()->id, $wallet->id); | ||||
// Test re-creating a domain | |||||
$domain->delete(); | |||||
$response = $this->actingAs($john)->post("/api/v4/domains", $post); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertSame('success', $json['status']); | |||||
$this->assertSame("Domain created successfully.", $json['message']); | |||||
$this->assertCount(2, $json); | |||||
$domain = Domain::where('namespace', $post['namespace'])->first(); | |||||
$this->assertInstanceOf(Domain::class, $domain); | |||||
$this->assertEntitlements($domain, ['domain-hosting']); | |||||
$wallet = $domain->wallet(); | |||||
$this->assertSame($john->wallets->first()->id, $wallet->id); | |||||
// Test creating a domain that is soft-deleted and belongs to another user | |||||
$domain->delete(); | |||||
$domain->entitlement()->withTrashed()->update(['wallet_id' => $jack->wallets->first()->id]); | |||||
$response = $this->actingAs($john)->post("/api/v4/domains", $post); | |||||
$response->assertStatus(422); | |||||
$json = $response->json(); | |||||
$this->assertSame('error', $json['status']); | |||||
$this->assertCount(2, $json); | |||||
$this->assertSame('The specified domain is not available.', $json['errors']['namespace']); | |||||
// Test acting as account controller (not owner) | // Test acting as account controller (not owner) | ||||
$this->markTestIncomplete(); | $this->markTestIncomplete(); | ||||
} | } | ||||
} | } |