Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/UsersTest.php
<?php | <?php | ||||
namespace Tests\Feature\Controller; | namespace Tests\Feature\Controller; | ||||
use App\Discount; | use App\Discount; | ||||
use App\Domain; | use App\Domain; | ||||
use App\Http\Controllers\API\V4\UsersController; | use App\Http\Controllers\API\V4\UsersController; | ||||
use App\Package; | use App\Package; | ||||
use App\Plan; | |||||
use App\Sku; | use App\Sku; | ||||
use App\Tenant; | use App\Tenant; | ||||
use App\User; | use App\User; | ||||
use App\Wallet; | use App\Wallet; | ||||
use Carbon\Carbon; | use Carbon\Carbon; | ||||
use Illuminate\Support\Facades\Queue; | use Illuminate\Support\Facades\Queue; | ||||
use Illuminate\Support\Str; | use Illuminate\Support\Str; | ||||
use Tests\TestCase; | use Tests\TestCase; | ||||
Show All 25 Lines | public function setUp(): void | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$wallet = $user->wallets()->first(); | $wallet = $user->wallets()->first(); | ||||
$wallet->discount()->dissociate(); | $wallet->discount()->dissociate(); | ||||
$wallet->settings()->whereIn('key', ['mollie_id', 'stripe_id'])->delete(); | $wallet->settings()->whereIn('key', ['mollie_id', 'stripe_id'])->delete(); | ||||
$wallet->save(); | $wallet->save(); | ||||
$user->settings()->whereIn('key', ['greylist_enabled', 'guam_enabled'])->delete(); | $user->settings()->whereIn('key', ['greylist_enabled', 'guam_enabled'])->delete(); | ||||
$user->status |= User::STATUS_IMAP_READY | User::STATUS_LDAP_READY; | $user->status |= User::STATUS_IMAP_READY | User::STATUS_LDAP_READY; | ||||
$user->status &= ~User::STATUS_RESTRICTED; | |||||
$user->save(); | $user->save(); | ||||
Plan::withEnvTenantContext()->where('title', 'individual')->update(['mode' => 'email']); | |||||
$user->setSettings(['plan_id' => null]); | |||||
} | } | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
$this->clearBetaEntitlements(); | $this->clearBetaEntitlements(); | ||||
Show All 14 Lines | public function tearDown(): void | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$wallet = $user->wallets()->first(); | $wallet = $user->wallets()->first(); | ||||
$wallet->discount()->dissociate(); | $wallet->discount()->dissociate(); | ||||
$wallet->settings()->whereIn('key', ['mollie_id', 'stripe_id'])->delete(); | $wallet->settings()->whereIn('key', ['mollie_id', 'stripe_id'])->delete(); | ||||
$wallet->save(); | $wallet->save(); | ||||
$user->settings()->whereIn('key', ['greylist_enabled', 'guam_enabled'])->delete(); | $user->settings()->whereIn('key', ['greylist_enabled', 'guam_enabled'])->delete(); | ||||
$user->status |= User::STATUS_IMAP_READY | User::STATUS_LDAP_READY; | $user->status |= User::STATUS_IMAP_READY | User::STATUS_LDAP_READY; | ||||
$user->status &= ~User::STATUS_RESTRICTED; | |||||
$user->save(); | $user->save(); | ||||
Plan::withEnvTenantContext()->where('title', 'individual')->update(['mode' => 'email']); | |||||
$user->setSettings(['plan_id' => null]); | |||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test user deleting (DELETE /api/v4/users/<id>) | * Test user deleting (DELETE /api/v4/users/<id>) | ||||
*/ | */ | ||||
public function testDestroy(): void | public function testDestroy(): void | ||||
▲ Show 20 Lines • Show All 1,277 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
/** | /** | ||||
* Test user data response used in show and info actions | * Test user data response used in show and info actions | ||||
*/ | */ | ||||
public function testUserResponse(): void | public function testUserResponse(): void | ||||
{ | { | ||||
$provider = \config('services.payment_provider') ?: 'mollie'; | $provider = \config('services.payment_provider') ?: 'mollie'; | ||||
$user = $this->getTestUser('john@kolab.org'); | $john = $this->getTestUser('john@kolab.org'); | ||||
$wallet = $user->wallets()->first(); | $wallet = $john->wallets()->first(); | ||||
$wallet->setSettings(['mollie_id' => null, 'stripe_id' => null]); | $wallet->setSettings(['mollie_id' => null, 'stripe_id' => null]); | ||||
$result = $this->invokeMethod(new UsersController(), 'userResponse', [$user]); | $wallet->owner->setSettings(['plan_id' => null]); | ||||
$result = $this->invokeMethod(new UsersController(), 'userResponse', [$john]); | |||||
$this->assertEquals($user->id, $result['id']); | $this->assertEquals($john->id, $result['id']); | ||||
$this->assertEquals($user->email, $result['email']); | $this->assertEquals($john->email, $result['email']); | ||||
$this->assertEquals($user->status, $result['status']); | $this->assertEquals($john->status, $result['status']); | ||||
$this->assertTrue(is_array($result['statusInfo'])); | $this->assertTrue(is_array($result['statusInfo'])); | ||||
$this->assertTrue(is_array($result['settings'])); | $this->assertTrue(is_array($result['settings'])); | ||||
$this->assertSame('US', $result['settings']['country']); | $this->assertSame('US', $result['settings']['country']); | ||||
$this->assertSame('USD', $result['settings']['currency']); | $this->assertSame('USD', $result['settings']['currency']); | ||||
$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(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->assertFalse($result['isLocked']); | |||||
$this->assertTrue($result['statusInfo']['enableDomains']); | $this->assertTrue($result['statusInfo']['enableDomains']); | ||||
$this->assertTrue($result['statusInfo']['enableWallets']); | $this->assertTrue($result['statusInfo']['enableWallets']); | ||||
$this->assertTrue($result['statusInfo']['enableWalletMandates']); | |||||
$this->assertTrue($result['statusInfo']['enableWalletPayments']); | |||||
$this->assertTrue($result['statusInfo']['enableUsers']); | $this->assertTrue($result['statusInfo']['enableUsers']); | ||||
$this->assertTrue($result['statusInfo']['enableSettings']); | $this->assertTrue($result['statusInfo']['enableSettings']); | ||||
// Ned is John's wallet controller | // Ned is John's wallet controller | ||||
$plan = Plan::withEnvTenantContext()->where('title', 'individual')->first(); | |||||
$plan->mode = 'mandate'; | |||||
$plan->save(); | |||||
$wallet->owner->setSettings(['plan_id' => $plan->id]); | |||||
$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->assertFalse($result['isLocked']); | |||||
$this->assertTrue($result['statusInfo']['enableDomains']); | $this->assertTrue($result['statusInfo']['enableDomains']); | ||||
$this->assertTrue($result['statusInfo']['enableWallets']); | $this->assertTrue($result['statusInfo']['enableWallets']); | ||||
$this->assertTrue($result['statusInfo']['enableWalletMandates']); | |||||
$this->assertFalse($result['statusInfo']['enableWalletPayments']); | |||||
$this->assertTrue($result['statusInfo']['enableUsers']); | $this->assertTrue($result['statusInfo']['enableUsers']); | ||||
$this->assertTrue($result['statusInfo']['enableSettings']); | $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(); | $john->refresh(); | ||||
$result = $this->invokeMethod(new UsersController(), 'userResponse', [$user]); | $result = $this->invokeMethod(new UsersController(), 'userResponse', [$john]); | ||||
$this->assertEquals($user->id, $result['id']); | $this->assertEquals($john->id, $result['id']); | ||||
$this->assertSame($discount->id, $result['wallet']['discount_id']); | $this->assertSame($discount->id, $result['wallet']['discount_id']); | ||||
$this->assertSame($discount->discount, $result['wallet']['discount']); | $this->assertSame($discount->discount, $result['wallet']['discount']); | ||||
$this->assertSame($discount->description, $result['wallet']['discount_description']); | $this->assertSame($discount->description, $result['wallet']['discount_description']); | ||||
$this->assertSame($mod_provider, $result['wallet']['provider']); | $this->assertSame($mod_provider, $result['wallet']['provider']); | ||||
$this->assertSame($discount->id, $result['wallets'][0]['discount_id']); | $this->assertSame($discount->id, $result['wallets'][0]['discount_id']); | ||||
$this->assertSame($discount->discount, $result['wallets'][0]['discount']); | $this->assertSame($discount->discount, $result['wallets'][0]['discount']); | ||||
$this->assertSame($discount->description, $result['wallets'][0]['discount_description']); | $this->assertSame($discount->description, $result['wallets'][0]['discount_description']); | ||||
$this->assertSame($mod_provider, $result['wallets'][0]['provider']); | $this->assertSame($mod_provider, $result['wallets'][0]['provider']); | ||||
$this->assertFalse($result['isLocked']); | |||||
// 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']['enableWalletMandates']); | |||||
$this->assertFalse($result['statusInfo']['enableWalletPayments']); | |||||
$this->assertFalse($result['statusInfo']['enableUsers']); | $this->assertFalse($result['statusInfo']['enableUsers']); | ||||
$this->assertFalse($result['statusInfo']['enableSettings']); | $this->assertFalse($result['statusInfo']['enableSettings']); | ||||
$this->assertFalse($result['isLocked']); | |||||
// Test locked user | |||||
$john->restrict(); | |||||
$result = $this->invokeMethod(new UsersController(), 'userResponse', [$john]); | |||||
$this->assertTrue($result['isLocked']); | |||||
} | } | ||||
/** | /** | ||||
* User email address validation. | * User email address validation. | ||||
* | * | ||||
* Note: Technically these include unit tests, but let's keep it here for now. | * Note: Technically these include unit tests, but let's keep it here for now. | ||||
* FIXME: Shall we do a http request for each case? | * FIXME: Shall we do a http request for each case? | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 232 Lines • Show Last 20 Lines |