Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/SharedFoldersTest.php
Show First 20 Lines • Show All 202 Lines • ▼ Show 20 Lines | class SharedFoldersTest extends TestCase | ||||
{ | { | ||||
$jack = $this->getTestUser('jack@kolab.org'); | $jack = $this->getTestUser('jack@kolab.org'); | ||||
$john = $this->getTestUser('john@kolab.org'); | $john = $this->getTestUser('john@kolab.org'); | ||||
$ned = $this->getTestUser('ned@kolab.org'); | $ned = $this->getTestUser('ned@kolab.org'); | ||||
$folder = $this->getTestSharedFolder('folder-test@kolab.org'); | $folder = $this->getTestSharedFolder('folder-test@kolab.org'); | ||||
$folder->assignToWallet($john->wallets->first()); | $folder->assignToWallet($john->wallets->first()); | ||||
$folder->setSetting('acl', '["anyone, full"]'); | $folder->setSetting('acl', '["anyone, full"]'); | ||||
$folder->setAliases(['folder-alias@kolab.org']); | |||||
// Test unauthenticated access | // Test unauthenticated access | ||||
$response = $this->get("/api/v4/shared-folders/{$folder->id}"); | $response = $this->get("/api/v4/shared-folders/{$folder->id}"); | ||||
$response->assertStatus(401); | $response->assertStatus(401); | ||||
// Test unauthorized access to a shared folder of another user | // Test unauthorized access to a shared folder of another user | ||||
$response = $this->actingAs($jack)->get("/api/v4/shared-folders/{$folder->id}"); | $response = $this->actingAs($jack)->get("/api/v4/shared-folders/{$folder->id}"); | ||||
$response->assertStatus(403); | $response->assertStatus(403); | ||||
// John: Account owner - non-existing folder | // John: Account owner - non-existing folder | ||||
$response = $this->actingAs($john)->get("/api/v4/shared-folders/abc"); | $response = $this->actingAs($john)->get("/api/v4/shared-folders/abc"); | ||||
$response->assertStatus(404); | $response->assertStatus(404); | ||||
// John: Account owner | // John: Account owner | ||||
$response = $this->actingAs($john)->get("/api/v4/shared-folders/{$folder->id}"); | $response = $this->actingAs($john)->get("/api/v4/shared-folders/{$folder->id}"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame($folder->id, $json['id']); | $this->assertSame($folder->id, $json['id']); | ||||
$this->assertSame($folder->email, $json['email']); | $this->assertSame($folder->email, $json['email']); | ||||
$this->assertSame($folder->name, $json['name']); | $this->assertSame($folder->name, $json['name']); | ||||
$this->assertSame($folder->type, $json['type']); | $this->assertSame($folder->type, $json['type']); | ||||
$this->assertSame(['folder-alias@kolab.org'], $json['aliases']); | |||||
$this->assertTrue(!empty($json['statusInfo'])); | $this->assertTrue(!empty($json['statusInfo'])); | ||||
$this->assertArrayHasKey('isDeleted', $json); | $this->assertArrayHasKey('isDeleted', $json); | ||||
$this->assertArrayHasKey('isActive', $json); | $this->assertArrayHasKey('isActive', $json); | ||||
$this->assertArrayHasKey('isLdapReady', $json); | $this->assertArrayHasKey('isLdapReady', $json); | ||||
$this->assertArrayHasKey('isImapReady', $json); | $this->assertArrayHasKey('isImapReady', $json); | ||||
$this->assertSame(['acl' => ['anyone, full']], $json['config']); | $this->assertSame(['acl' => ['anyone, full']], $json['config']); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 157 Lines • ▼ Show 20 Lines | public function testStore(): void | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('error', $json['status']); | $this->assertSame('error', $json['status']); | ||||
$this->assertSame("The name field is required.", $json['errors']['name'][0]); | $this->assertSame("The name field is required.", $json['errors']['name'][0]); | ||||
$this->assertSame("The type field is required.", $json['errors']['type'][0]); | $this->assertSame("The type field is required.", $json['errors']['type'][0]); | ||||
$this->assertCount(2, $json); | $this->assertCount(2, $json); | ||||
$this->assertCount(2, $json['errors']); | $this->assertCount(2, $json['errors']); | ||||
// Test too long name | // Test too long name, invalid alias domain | ||||
$post = ['domain' => 'kolab.org', 'name' => str_repeat('A', 192), 'type' => 'unknown']; | $post = [ | ||||
'domain' => 'kolab.org', | |||||
'name' => str_repeat('A', 192), | |||||
'type' => 'unknown', | |||||
'aliases' => ['folder-alias@unknown.org'], | |||||
]; | |||||
$response = $this->actingAs($john)->post("/api/v4/shared-folders", $post); | $response = $this->actingAs($john)->post("/api/v4/shared-folders", $post); | ||||
$response->assertStatus(422); | $response->assertStatus(422); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('error', $json['status']); | $this->assertSame('error', $json['status']); | ||||
$this->assertCount(2, $json); | $this->assertCount(2, $json); | ||||
$this->assertSame(["The name may not be greater than 191 characters."], $json['errors']['name']); | $this->assertSame(["The name may not be greater than 191 characters."], $json['errors']['name']); | ||||
$this->assertSame(["The specified type is invalid."], $json['errors']['type']); | $this->assertSame(["The specified type is invalid."], $json['errors']['type']); | ||||
$this->assertCount(2, $json['errors']); | $this->assertSame(["The specified domain is invalid."], $json['errors']['aliases']); | ||||
$this->assertCount(3, $json['errors']); | |||||
// Test successful folder creation | // Test successful folder creation | ||||
$post['name'] = 'Test Folder'; | $post['name'] = 'Test Folder'; | ||||
$post['type'] = 'event'; | $post['type'] = 'event'; | ||||
$post['aliases'] = ['folder-alias@kolab.org']; // expected to be ignored | |||||
$response = $this->actingAs($john)->post("/api/v4/shared-folders", $post); | $response = $this->actingAs($john)->post("/api/v4/shared-folders", $post); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertSame("Shared folder created successfully.", $json['message']); | $this->assertSame("Shared folder created successfully.", $json['message']); | ||||
$this->assertCount(2, $json); | $this->assertCount(2, $json); | ||||
$folder = SharedFolder::where('name', $post['name'])->first(); | $folder = SharedFolder::where('name', $post['name'])->first(); | ||||
$this->assertInstanceOf(SharedFolder::class, $folder); | $this->assertInstanceOf(SharedFolder::class, $folder); | ||||
$this->assertSame($post['type'], $folder->type); | $this->assertSame($post['type'], $folder->type); | ||||
$this->assertTrue($john->sharedFolders()->get()->contains($folder)); | $this->assertTrue($john->sharedFolders()->get()->contains($folder)); | ||||
$this->assertSame([], $folder->aliases()->pluck('alias')->all()); | |||||
// Shared folder name must be unique within a domain | // Shared folder name must be unique within a domain | ||||
$post['type'] = 'mail'; | $post['type'] = 'mail'; | ||||
$response = $this->actingAs($john)->post("/api/v4/shared-folders", $post); | $response = $this->actingAs($john)->post("/api/v4/shared-folders", $post); | ||||
$response->assertStatus(422); | $response->assertStatus(422); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('error', $json['status']); | $this->assertSame('error', $json['status']); | ||||
$this->assertCount(2, $json); | $this->assertCount(2, $json); | ||||
$this->assertCount(1, $json['errors']); | $this->assertCount(1, $json['errors']); | ||||
$this->assertSame("The specified name is not available.", $json['errors']['name'][0]); | $this->assertSame("The specified name is not available.", $json['errors']['name'][0]); | ||||
$folder->forceDelete(); | |||||
// Test successful folder creation with aliases | |||||
$post['name'] = 'Test Folder'; | |||||
$post['type'] = 'mail'; | |||||
$post['aliases'] = ['folder-alias@kolab.org']; | |||||
$response = $this->actingAs($john)->post("/api/v4/shared-folders", $post); | |||||
$json = $response->json(); | |||||
$response->assertStatus(200); | |||||
$folder = SharedFolder::where('name', $post['name'])->first(); | |||||
$this->assertSame(['folder-alias@kolab.org'], $folder->aliases()->pluck('alias')->all()); | |||||
} | } | ||||
/** | /** | ||||
* Test shared folder update (PUT /api/v4/shared-folders/<folder) | * Test shared folder update (PUT /api/v4/shared-folders/<folder) | ||||
*/ | */ | ||||
public function testUpdate(): void | public function testUpdate(): void | ||||
{ | { | ||||
Queue::fake(); | Queue::fake(); | ||||
Show All 24 Lines | public function testUpdate(): void | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertSame("Shared folder updated successfully.", $json['message']); | $this->assertSame("Shared folder updated successfully.", $json['message']); | ||||
$this->assertCount(2, $json); | $this->assertCount(2, $json); | ||||
$folder->refresh(); | $folder->refresh(); | ||||
$this->assertSame($post['name'], $folder->name); | $this->assertSame($post['name'], $folder->name); | ||||
// Aliases with error | |||||
$post['aliases'] = ['folder-alias1@kolab.org', 'folder-alias2@unknown.com']; | |||||
$response = $this->actingAs($john)->put("/api/v4/shared-folders/{$folder->id}", $post); | |||||
$json = $response->json(); | |||||
$response->assertStatus(422); | |||||
$this->assertSame('error', $json['status']); | |||||
$this->assertCount(2, $json); | |||||
$this->assertCount(1, $json['errors']); | |||||
$this->assertCount(1, $json['errors']['aliases']); | |||||
$this->assertSame("The specified domain is invalid.", $json['errors']['aliases'][1]); | |||||
$this->assertSame([], $folder->aliases()->pluck('alias')->all()); | |||||
// Aliases with success expected | |||||
$post['aliases'] = ['folder-alias1@kolab.org', 'folder-alias2@kolab.org']; | |||||
$response = $this->actingAs($john)->put("/api/v4/shared-folders/{$folder->id}", $post); | |||||
$json = $response->json(); | |||||
$response->assertStatus(200); | |||||
$this->assertSame('success', $json['status']); | |||||
$this->assertSame("Shared folder updated successfully.", $json['message']); | |||||
$this->assertCount(2, $json); | |||||
$this->assertSame($post['aliases'], $folder->aliases()->pluck('alias')->all()); | |||||
// All aliases removal | |||||
$post['aliases'] = []; | |||||
$response = $this->actingAs($john)->put("/api/v4/shared-folders/{$folder->id}", $post); | |||||
$response->assertStatus(200); | |||||
$this->assertSame($post['aliases'], $folder->aliases()->pluck('alias')->all()); | |||||
} | } | ||||
} | } |