Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/GroupsTest.php
Show All 12 Lines | class GroupsTest extends TestCase | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function setUp(): void | public function setUp(): void | ||||
{ | { | ||||
parent::setUp(); | parent::setUp(); | ||||
$this->deleteTestGroup('group-test@kolab.org'); | $this->deleteTestGroup('group-test@kolab.org'); | ||||
$this->deleteTestGroup('group-test2@kolab.org'); | |||||
} | } | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
$this->deleteTestGroup('group-test@kolab.org'); | $this->deleteTestGroup('group-test@kolab.org'); | ||||
$this->deleteTestGroup('group-test2@kolab.org'); | |||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test group deleting (DELETE /api/v4/groups/<id>) | * Test group deleting (DELETE /api/v4/groups/<id>) | ||||
*/ | */ | ||||
public function testDestroy(): void | public function testDestroy(): void | ||||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | public function testIndex(): void | ||||
$response = $this->actingAs($john)->get("/api/v4/groups"); | $response = $this->actingAs($john)->get("/api/v4/groups"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertCount(1, $json); | $this->assertCount(1, $json); | ||||
$this->assertSame($group->id, $json[0]['id']); | $this->assertSame($group->id, $json[0]['id']); | ||||
$this->assertSame($group->email, $json[0]['email']); | $this->assertSame($group->email, $json[0]['email']); | ||||
$this->assertSame($group->name, $json[0]['name']); | |||||
$this->assertArrayHasKey('isDeleted', $json[0]); | $this->assertArrayHasKey('isDeleted', $json[0]); | ||||
$this->assertArrayHasKey('isSuspended', $json[0]); | $this->assertArrayHasKey('isSuspended', $json[0]); | ||||
$this->assertArrayHasKey('isActive', $json[0]); | $this->assertArrayHasKey('isActive', $json[0]); | ||||
$this->assertArrayHasKey('isLdapReady', $json[0]); | $this->assertArrayHasKey('isLdapReady', $json[0]); | ||||
// Test that another wallet controller has access to groups | // Test that another wallet controller has access to groups | ||||
$response = $this->actingAs($ned)->get("/api/v4/groups"); | $response = $this->actingAs($ned)->get("/api/v4/groups"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
▲ Show 20 Lines • Show All 109 Lines • ▼ Show 20 Lines | public function testShow(): void | ||||
// John: Group owner | // John: Group owner | ||||
$response = $this->actingAs($john)->get("/api/v4/groups/{$group->id}"); | $response = $this->actingAs($john)->get("/api/v4/groups/{$group->id}"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame($group->id, $json['id']); | $this->assertSame($group->id, $json['id']); | ||||
$this->assertSame($group->email, $json['email']); | $this->assertSame($group->email, $json['email']); | ||||
$this->assertSame($group->name, $json['name']); | |||||
$this->assertSame($group->members, $json['members']); | $this->assertSame($group->members, $json['members']); | ||||
$this->assertTrue(!empty($json['statusInfo'])); | $this->assertTrue(!empty($json['statusInfo'])); | ||||
$this->assertArrayHasKey('isDeleted', $json); | $this->assertArrayHasKey('isDeleted', $json); | ||||
$this->assertArrayHasKey('isSuspended', $json); | $this->assertArrayHasKey('isSuspended', $json); | ||||
$this->assertArrayHasKey('isActive', $json); | $this->assertArrayHasKey('isActive', $json); | ||||
$this->assertArrayHasKey('isLdapReady', $json); | $this->assertArrayHasKey('isLdapReady', $json); | ||||
$this->assertSame(['sender_policy' => ['test']], $json['config']); | $this->assertSame(['sender_policy' => ['test']], $json['config']); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 142 Lines • ▼ Show 20 Lines | public function testStore(): void | ||||
// Test empty request | // Test empty request | ||||
$response = $this->actingAs($john)->post("/api/v4/groups", []); | $response = $this->actingAs($john)->post("/api/v4/groups", []); | ||||
$response->assertStatus(422); | $response->assertStatus(422); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('error', $json['status']); | $this->assertSame('error', $json['status']); | ||||
$this->assertSame("The email field is required.", $json['errors']['email']); | $this->assertSame("The email field is required.", $json['errors']['email']); | ||||
$this->assertSame("At least one recipient is required.", $json['errors']['members']); | |||||
$this->assertSame("The name field is required.", $json['errors']['name'][0]); | |||||
$this->assertCount(2, $json); | $this->assertCount(2, $json); | ||||
$this->assertCount(3, $json['errors']); | |||||
// Test missing members | // Test missing members and name | ||||
$post = ['email' => 'group-test@kolab.org']; | $post = ['email' => 'group-test@kolab.org']; | ||||
$response = $this->actingAs($john)->post("/api/v4/groups", $post); | $response = $this->actingAs($john)->post("/api/v4/groups", $post); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$response->assertStatus(422); | $response->assertStatus(422); | ||||
$this->assertSame('error', $json['status']); | $this->assertSame('error', $json['status']); | ||||
$this->assertSame("At least one recipient is required.", $json['errors']['members']); | $this->assertSame("At least one recipient is required.", $json['errors']['members']); | ||||
$this->assertSame("The name field is required.", $json['errors']['name'][0]); | |||||
$this->assertCount(2, $json); | $this->assertCount(2, $json); | ||||
$this->assertCount(2, $json['errors']); | |||||
// Test invalid email | // Test invalid email and too long name | ||||
$post = ['email' => 'invalid']; | $post = ['email' => 'invalid', 'name' => str_repeat('A', 192)]; | ||||
$response = $this->actingAs($john)->post("/api/v4/groups", $post); | $response = $this->actingAs($john)->post("/api/v4/groups", $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 specified email is invalid.', $json['errors']['email']); | $this->assertSame("The specified email is invalid.", $json['errors']['email']); | ||||
$this->assertSame("The name may not be greater than 191 characters.", $json['errors']['name'][0]); | |||||
$this->assertCount(3, $json['errors']); | |||||
// Test successful group creation | // Test successful group creation | ||||
$post = [ | $post = [ | ||||
'name' => 'Test Group', | |||||
'email' => 'group-test@kolab.org', | 'email' => 'group-test@kolab.org', | ||||
'members' => ['test1@domain.tld', 'test2@domain.tld'] | 'members' => ['test1@domain.tld', 'test2@domain.tld'] | ||||
]; | ]; | ||||
$response = $this->actingAs($john)->post("/api/v4/groups", $post); | $response = $this->actingAs($john)->post("/api/v4/groups", $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("Distribution list created successfully.", $json['message']); | $this->assertSame("Distribution list created successfully.", $json['message']); | ||||
$this->assertCount(2, $json); | $this->assertCount(2, $json); | ||||
$group = Group::where('email', 'group-test@kolab.org')->first(); | $group = Group::where('email', 'group-test@kolab.org')->first(); | ||||
$this->assertInstanceOf(Group::class, $group); | $this->assertInstanceOf(Group::class, $group); | ||||
$this->assertSame($post['email'], $group->email); | $this->assertSame($post['email'], $group->email); | ||||
$this->assertSame($post['members'], $group->members); | $this->assertSame($post['members'], $group->members); | ||||
$this->assertTrue($john->groups()->get()->contains($group)); | $this->assertTrue($john->groups()->get()->contains($group)); | ||||
// Group name must be unique within a domain | |||||
$post['email'] = 'group-test2@kolab.org'; | |||||
$post['members'] = ['test1@domain.tld']; | |||||
$response = $this->actingAs($john)->post("/api/v4/groups", $post); | |||||
$response->assertStatus(422); | |||||
$json = $response->json(); | |||||
$this->assertSame('error', $json['status']); | |||||
$this->assertCount(2, $json); | |||||
$this->assertCount(1, $json['errors']); | |||||
$this->assertSame("The specified name is not available.", $json['errors']['name'][0]); | |||||
} | } | ||||
/** | /** | ||||
* Test group update (PUT /api/v4/groups/<group-id>) | * Test group update (PUT /api/v4/groups/<group-id>) | ||||
*/ | */ | ||||
public function testUpdate(): void | public function testUpdate(): void | ||||
{ | { | ||||
Queue::fake(); | Queue::fake(); | ||||
Show All 29 Lines | public function testUpdate(): void | ||||
$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 specified email address is invalid.', $json['errors']['members'][1]); | $this->assertSame('The specified email address is invalid.', $json['errors']['members'][1]); | ||||
// Valid data - members changed | // Valid data - members and name changed | ||||
$post = [ | $post = [ | ||||
'name' => 'Test Gr', | |||||
'members' => ['member1@test.domain', 'member2@test.domain'] | 'members' => ['member1@test.domain', 'member2@test.domain'] | ||||
]; | ]; | ||||
$response = $this->actingAs($john)->put("/api/v4/groups/{$group->id}", $post); | $response = $this->actingAs($john)->put("/api/v4/groups/{$group->id}", $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("Distribution list updated successfully.", $json['message']); | $this->assertSame("Distribution list updated successfully.", $json['message']); | ||||
$this->assertCount(2, $json); | $this->assertCount(2, $json); | ||||
$this->assertSame($group->fresh()->members, $post['members']); | |||||
$group->refresh(); | |||||
$this->assertSame($post['name'], $group->name); | |||||
$this->assertSame($post['members'], $group->members); | |||||
} | } | ||||
/** | /** | ||||
* Group email address validation. | * Group email address validation. | ||||
*/ | */ | ||||
public function testValidateGroupEmail(): void | public function testValidateGroupEmail(): void | ||||
{ | { | ||||
$john = $this->getTestUser('john@kolab.org'); | $john = $this->getTestUser('john@kolab.org'); | ||||
▲ Show 20 Lines • Show All 73 Lines • Show Last 20 Lines |