Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/UsersTest.php
Show First 20 Lines • Show All 515 Lines • ▼ Show 20 Lines | class UsersTest extends TestCase | ||||
* Test user config update (POST /api/v4/users/<user>/config) | * Test user config update (POST /api/v4/users/<user>/config) | ||||
*/ | */ | ||||
public function testSetConfig(): void | public function testSetConfig(): void | ||||
{ | { | ||||
$jack = $this->getTestUser('jack@kolab.org'); | $jack = $this->getTestUser('jack@kolab.org'); | ||||
$john = $this->getTestUser('john@kolab.org'); | $john = $this->getTestUser('john@kolab.org'); | ||||
$john->setSetting('greylist_enabled', null); | $john->setSetting('greylist_enabled', null); | ||||
$john->setSetting('password_policy', null); | |||||
// Test unknown user id | // Test unknown user id | ||||
$post = ['greylist_enabled' => 1]; | $post = ['greylist_enabled' => 1]; | ||||
$response = $this->actingAs($john)->post("/api/v4/users/123/config", $post); | $response = $this->actingAs($john)->post("/api/v4/users/123/config", $post); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$response->assertStatus(404); | $response->assertStatus(404); | ||||
Show All 18 Lines | public function testSetConfig(): void | ||||
$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 requested configuration parameter is not supported.', $json['errors']['grey']); | $this->assertSame('The requested configuration parameter is not supported.', $json['errors']['grey']); | ||||
$this->assertNull($john->fresh()->getSetting('greylist_enabled')); | $this->assertNull($john->fresh()->getSetting('greylist_enabled')); | ||||
// Test some valid data | // Test some valid data | ||||
$post = ['greylist_enabled' => 1]; | $post = ['greylist_enabled' => 1, 'password_policy' => 'min:10,max:255,upper,lower,digit,special']; | ||||
$response = $this->actingAs($john)->post("/api/v4/users/{$john->id}/config", $post); | $response = $this->actingAs($john)->post("/api/v4/users/{$john->id}/config", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertCount(2, $json); | $this->assertCount(2, $json); | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertSame('User settings updated successfully.', $json['message']); | $this->assertSame('User settings updated successfully.', $json['message']); | ||||
$this->assertSame('true', $john->fresh()->getSetting('greylist_enabled')); | $this->assertSame('true', $john->fresh()->getSetting('greylist_enabled')); | ||||
$this->assertSame('min:10,max:255,upper,lower,digit,special', $john->fresh()->getSetting('password_policy')); | |||||
// Test some valid data | // Test some valid data, acting as another account controller | ||||
$post = ['greylist_enabled' => 0]; | $ned = $this->getTestUser('ned@kolab.org'); | ||||
$response = $this->actingAs($john)->post("/api/v4/users/{$john->id}/config", $post); | $post = ['greylist_enabled' => 0, 'password_policy' => 'min:10,max:255,upper']; | ||||
$response = $this->actingAs($ned)->post("/api/v4/users/{$john->id}/config", $post); | |||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertCount(2, $json); | $this->assertCount(2, $json); | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertSame('User settings updated successfully.', $json['message']); | $this->assertSame('User settings updated successfully.', $json['message']); | ||||
$this->assertSame('false', $john->fresh()->getSetting('greylist_enabled')); | $this->assertSame('false', $john->fresh()->getSetting('greylist_enabled')); | ||||
$this->assertSame('min:10,max:255,upper', $john->fresh()->getSetting('password_policy')); | |||||
} | } | ||||
/** | /** | ||||
* Test user creation (POST /api/v4/users) | * Test user creation (POST /api/v4/users) | ||||
*/ | */ | ||||
public function testStore(): void | public function testStore(): void | ||||
{ | { | ||||
Queue::fake(); | Queue::fake(); | ||||
$jack = $this->getTestUser('jack@kolab.org'); | $jack = $this->getTestUser('jack@kolab.org'); | ||||
$john = $this->getTestUser('john@kolab.org'); | $john = $this->getTestUser('john@kolab.org'); | ||||
$john->setSetting('password_policy', 'min:8,max:100,digit'); | |||||
$deleted_priv = $this->getTestUser('deleted@kolab.org'); | $deleted_priv = $this->getTestUser('deleted@kolab.org'); | ||||
$deleted_priv->delete(); | $deleted_priv->delete(); | ||||
// Test empty request | // Test empty request | ||||
$response = $this->actingAs($john)->post("/api/v4/users", []); | $response = $this->actingAs($john)->post("/api/v4/users", []); | ||||
$response->assertStatus(422); | $response->assertStatus(422); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
Show All 23 Lines | public function testStore(): void | ||||
$this->assertSame('error', $json['status']); | $this->assertSame('error', $json['status']); | ||||
$this->assertCount(2, $json); | $this->assertCount(2, $json); | ||||
$this->assertSame('The password confirmation does not match.', $json['errors']['password'][0]); | $this->assertSame('The password confirmation does not match.', $json['errors']['password'][0]); | ||||
$this->assertSame('The specified email is invalid.', $json['errors']['email']); | $this->assertSame('The specified email is invalid.', $json['errors']['email']); | ||||
// Test existing user email | // Test existing user email | ||||
$post = [ | $post = [ | ||||
'password' => 'simple', | 'password' => 'simple123', | ||||
'password_confirmation' => 'simple', | 'password_confirmation' => 'simple123', | ||||
'first_name' => 'John2', | 'first_name' => 'John2', | ||||
'last_name' => 'Doe2', | 'last_name' => 'Doe2', | ||||
'email' => 'jack.daniels@kolab.org', | 'email' => 'jack.daniels@kolab.org', | ||||
]; | ]; | ||||
$response = $this->actingAs($john)->post("/api/v4/users", $post); | $response = $this->actingAs($john)->post("/api/v4/users", $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 not available.', $json['errors']['email']); | $this->assertSame('The specified email is not available.', $json['errors']['email']); | ||||
$package_kolab = \App\Package::withEnvTenantContext()->where('title', 'kolab')->first(); | $package_kolab = \App\Package::withEnvTenantContext()->where('title', 'kolab')->first(); | ||||
$package_domain = \App\Package::withEnvTenantContext()->where('title', 'domain-hosting')->first(); | $package_domain = \App\Package::withEnvTenantContext()->where('title', 'domain-hosting')->first(); | ||||
$post = [ | $post = [ | ||||
'password' => 'simple', | 'password' => 'simple123', | ||||
'password_confirmation' => 'simple', | 'password_confirmation' => 'simple123', | ||||
'first_name' => 'John2', | 'first_name' => 'John2', | ||||
'last_name' => 'Doe2', | 'last_name' => 'Doe2', | ||||
'email' => 'john2.doe2@kolab.org', | 'email' => 'john2.doe2@kolab.org', | ||||
'organization' => 'TestOrg', | 'organization' => 'TestOrg', | ||||
'aliases' => ['useralias1@kolab.org', 'deleted@kolab.org'], | 'aliases' => ['useralias1@kolab.org', 'deleted@kolab.org'], | ||||
]; | ]; | ||||
// Missing package | // Missing package | ||||
Show All 12 Lines | public function testStore(): void | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$response->assertStatus(422); | $response->assertStatus(422); | ||||
$this->assertSame('error', $json['status']); | $this->assertSame('error', $json['status']); | ||||
$this->assertSame("Invalid package selected.", $json['errors']['package']); | $this->assertSame("Invalid package selected.", $json['errors']['package']); | ||||
$this->assertCount(2, $json); | $this->assertCount(2, $json); | ||||
// Test full and valid data | // Test password policy checking | ||||
$post['package'] = $package_kolab->id; | $post['package'] = $package_kolab->id; | ||||
$post['password'] = 'password'; | |||||
$response = $this->actingAs($john)->post("/api/v4/users", $post); | |||||
$json = $response->json(); | |||||
$response->assertStatus(422); | |||||
$this->assertSame('error', $json['status']); | |||||
$this->assertSame("The password confirmation does not match.", $json['errors']['password'][0]); | |||||
$this->assertSame("Specified password does not comply with the policy.", $json['errors']['password'][1]); | |||||
$this->assertCount(2, $json); | |||||
// Test password confirmation | |||||
$post['password_confirmation'] = 'password'; | |||||
$response = $this->actingAs($john)->post("/api/v4/users", $post); | |||||
$json = $response->json(); | |||||
$response->assertStatus(422); | |||||
$this->assertSame('error', $json['status']); | |||||
$this->assertSame("Specified password does not comply with the policy.", $json['errors']['password'][0]); | |||||
$this->assertCount(2, $json); | |||||
// Test full and valid data | |||||
$post['password'] = 'password123'; | |||||
$post['password_confirmation'] = 'password123'; | |||||
$response = $this->actingAs($john)->post("/api/v4/users", $post); | $response = $this->actingAs($john)->post("/api/v4/users", $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("User created successfully.", $json['message']); | $this->assertSame("User created successfully.", $json['message']); | ||||
$this->assertCount(2, $json); | $this->assertCount(2, $json); | ||||
▲ Show 20 Lines • Show All 75 Lines • ▼ Show 20 Lines | class UsersTest extends TestCase | ||||
} | } | ||||
/** | /** | ||||
* Test user update (PUT /api/v4/users/<user-id>) | * Test user update (PUT /api/v4/users/<user-id>) | ||||
*/ | */ | ||||
public function testUpdate(): void | public function testUpdate(): void | ||||
{ | { | ||||
$userA = $this->getTestUser('UsersControllerTest1@userscontroller.com'); | $userA = $this->getTestUser('UsersControllerTest1@userscontroller.com'); | ||||
$userA->setSetting('password_policy', 'min:8,digit'); | |||||
$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'); | ||||
$domain = $this->getTestDomain( | $domain = $this->getTestDomain( | ||||
'userscontroller.com', | 'userscontroller.com', | ||||
['status' => Domain::STATUS_NEW, 'type' => Domain::TYPE_EXTERNAL] | ['status' => Domain::STATUS_NEW, 'type' => Domain::TYPE_EXTERNAL] | ||||
); | ); | ||||
Show All 12 Lines | public function testUpdate(): void | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertSame("User data updated successfully.", $json['message']); | $this->assertSame("User data updated successfully.", $json['message']); | ||||
$this->assertTrue(!empty($json['statusInfo'])); | $this->assertTrue(!empty($json['statusInfo'])); | ||||
$this->assertCount(3, $json); | $this->assertCount(3, $json); | ||||
// Test some invalid data | // Test some invalid data | ||||
$post = ['password' => '12345678', 'currency' => 'invalid']; | $post = ['password' => '1234567', 'currency' => 'invalid']; | ||||
$response = $this->actingAs($userA)->put("/api/v4/users/{$userA->id}", $post); | $response = $this->actingAs($userA)->put("/api/v4/users/{$userA->id}", $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 password confirmation does not match.', $json['errors']['password'][0]); | $this->assertSame("The password confirmation does not match.", $json['errors']['password'][0]); | ||||
$this->assertSame('The currency must be 3 characters.', $json['errors']['currency'][0]); | $this->assertSame("Specified password does not comply with the policy.", $json['errors']['password'][1]); | ||||
$this->assertSame("The currency must be 3 characters.", $json['errors']['currency'][0]); | |||||
// Test full profile update including password | // Test full profile update including password | ||||
$post = [ | $post = [ | ||||
'password' => 'simple', | 'password' => 'simple123', | ||||
'password_confirmation' => 'simple', | 'password_confirmation' => 'simple123', | ||||
'first_name' => 'John2', | 'first_name' => 'John2', | ||||
'last_name' => 'Doe2', | 'last_name' => 'Doe2', | ||||
'organization' => 'TestOrg', | 'organization' => 'TestOrg', | ||||
'phone' => '+123 123 123', | 'phone' => '+123 123 123', | ||||
'external_email' => 'external@gmail.com', | 'external_email' => 'external@gmail.com', | ||||
'billing_address' => 'billing', | 'billing_address' => 'billing', | ||||
'country' => 'CH', | 'country' => 'CH', | ||||
'currency' => 'CHF', | 'currency' => 'CHF', | ||||
▲ Show 20 Lines • Show All 344 Lines • ▼ Show 20 Lines | public function testUserResponse(): void | ||||
$this->assertCount(0, $result['accounts']); | $this->assertCount(0, $result['accounts']); | ||||
$this->assertCount(1, $result['wallets']); | $this->assertCount(1, $result['wallets']); | ||||
$this->assertSame($wallet->id, $result['wallet']['id']); | $this->assertSame($wallet->id, $result['wallet']['id']); | ||||
$this->assertArrayNotHasKey('discount', $result['wallet']); | $this->assertArrayNotHasKey('discount', $result['wallet']); | ||||
$this->assertTrue($result['statusInfo']['enableDomains']); | $this->assertTrue($result['statusInfo']['enableDomains']); | ||||
$this->assertTrue($result['statusInfo']['enableWallets']); | $this->assertTrue($result['statusInfo']['enableWallets']); | ||||
$this->assertTrue($result['statusInfo']['enableUsers']); | $this->assertTrue($result['statusInfo']['enableUsers']); | ||||
$this->assertTrue($result['statusInfo']['enableSettings']); | |||||
// Ned is John's wallet controller | // Ned is John's wallet controller | ||||
$ned = $this->getTestUser('ned@kolab.org'); | $ned = $this->getTestUser('ned@kolab.org'); | ||||
$ned_wallet = $ned->wallets()->first(); | $ned_wallet = $ned->wallets()->first(); | ||||
$result = $this->invokeMethod(new UsersController(), 'userResponse', [$ned]); | $result = $this->invokeMethod(new UsersController(), 'userResponse', [$ned]); | ||||
$this->assertEquals($ned->id, $result['id']); | $this->assertEquals($ned->id, $result['id']); | ||||
$this->assertEquals($ned->email, $result['email']); | $this->assertEquals($ned->email, $result['email']); | ||||
$this->assertTrue(is_array($result['accounts'])); | $this->assertTrue(is_array($result['accounts'])); | ||||
$this->assertTrue(is_array($result['wallets'])); | $this->assertTrue(is_array($result['wallets'])); | ||||
$this->assertCount(1, $result['accounts']); | $this->assertCount(1, $result['accounts']); | ||||
$this->assertCount(1, $result['wallets']); | $this->assertCount(1, $result['wallets']); | ||||
$this->assertSame($wallet->id, $result['wallet']['id']); | $this->assertSame($wallet->id, $result['wallet']['id']); | ||||
$this->assertSame($wallet->id, $result['accounts'][0]['id']); | $this->assertSame($wallet->id, $result['accounts'][0]['id']); | ||||
$this->assertSame($ned_wallet->id, $result['wallets'][0]['id']); | $this->assertSame($ned_wallet->id, $result['wallets'][0]['id']); | ||||
$this->assertSame($provider, $result['wallet']['provider']); | $this->assertSame($provider, $result['wallet']['provider']); | ||||
$this->assertSame($provider, $result['wallets'][0]['provider']); | $this->assertSame($provider, $result['wallets'][0]['provider']); | ||||
$this->assertTrue($result['statusInfo']['enableDomains']); | $this->assertTrue($result['statusInfo']['enableDomains']); | ||||
$this->assertTrue($result['statusInfo']['enableWallets']); | $this->assertTrue($result['statusInfo']['enableWallets']); | ||||
$this->assertTrue($result['statusInfo']['enableUsers']); | $this->assertTrue($result['statusInfo']['enableUsers']); | ||||
$this->assertTrue($result['statusInfo']['enableSettings']); | |||||
// Test discount in a response | // Test discount in a response | ||||
$discount = Discount::where('code', 'TEST')->first(); | $discount = Discount::where('code', 'TEST')->first(); | ||||
$wallet->discount()->associate($discount); | $wallet->discount()->associate($discount); | ||||
$wallet->save(); | $wallet->save(); | ||||
$mod_provider = $provider == 'mollie' ? 'stripe' : 'mollie'; | $mod_provider = $provider == 'mollie' ? 'stripe' : 'mollie'; | ||||
$wallet->setSetting($mod_provider . '_id', 123); | $wallet->setSetting($mod_provider . '_id', 123); | ||||
$user->refresh(); | $user->refresh(); | ||||
Show All 12 Lines | public function testUserResponse(): void | ||||
// Jack is not a John's wallet controller | // Jack is not a John's wallet controller | ||||
$jack = $this->getTestUser('jack@kolab.org'); | $jack = $this->getTestUser('jack@kolab.org'); | ||||
$result = $this->invokeMethod(new UsersController(), 'userResponse', [$jack]); | $result = $this->invokeMethod(new UsersController(), 'userResponse', [$jack]); | ||||
$this->assertFalse($result['statusInfo']['enableDomains']); | $this->assertFalse($result['statusInfo']['enableDomains']); | ||||
$this->assertFalse($result['statusInfo']['enableWallets']); | $this->assertFalse($result['statusInfo']['enableWallets']); | ||||
$this->assertFalse($result['statusInfo']['enableUsers']); | $this->assertFalse($result['statusInfo']['enableUsers']); | ||||
$this->assertFalse($result['statusInfo']['enableSettings']); | |||||
} | } | ||||
/** | /** | ||||
* List of email address validation cases for testValidateEmail() | * List of email address validation cases for testValidateEmail() | ||||
* | * | ||||
* @return array Arguments for testValidateEmail() | * @return array Arguments for testValidateEmail() | ||||
*/ | */ | ||||
public function dataValidateEmail(): array | public function dataValidateEmail(): array | ||||
▲ Show 20 Lines • Show All 217 Lines • Show Last 20 Lines |