Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/Reseller/WalletsTest.php
Show First 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | class WalletsTest extends TestCase | ||||
*/ | */ | ||||
public function testOneOff(): void | public function testOneOff(): void | ||||
{ | { | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$admin = $this->getTestUser('jeroen@jeroen.jeroen'); | $admin = $this->getTestUser('jeroen@jeroen.jeroen'); | ||||
$reseller1 = $this->getTestUser('reseller@kolabnow.com'); | $reseller1 = $this->getTestUser('reseller@kolabnow.com'); | ||||
$reseller2 = $this->getTestUser('reseller@reseller.com'); | $reseller2 = $this->getTestUser('reseller@reseller.com'); | ||||
$wallet = $user->wallets()->first(); | $wallet = $user->wallets()->first(); | ||||
$reseller1_wallet = $reseller1->wallets()->first(); | |||||
$balance = $wallet->balance; | $balance = $wallet->balance; | ||||
$reseller1_balance = $reseller1_wallet->balance; | |||||
Transaction::where('object_id', $wallet->id) | Transaction::where('object_id', $wallet->id) | ||||
->whereIn('type', [Transaction::WALLET_AWARD, Transaction::WALLET_PENALTY]) | ->whereIn('type', [Transaction::WALLET_AWARD, Transaction::WALLET_PENALTY]) | ||||
->delete(); | ->delete(); | ||||
Transaction::where('object_id', $reseller1_wallet->id)->delete(); | |||||
// Non-admin user | // Non-admin user | ||||
$response = $this->actingAs($user)->post("api/v4/wallets/{$wallet->id}/one-off", []); | $response = $this->actingAs($user)->post("api/v4/wallets/{$wallet->id}/one-off", []); | ||||
$response->assertStatus(403); | $response->assertStatus(403); | ||||
// Admin user | // Admin user | ||||
$response = $this->actingAs($admin)->post("api/v4/wallets/{$wallet->id}/one-off", []); | $response = $this->actingAs($admin)->post("api/v4/wallets/{$wallet->id}/one-off", []); | ||||
$response->assertStatus(403); | $response->assertStatus(403); | ||||
Show All 10 Lines | public function testOneOff(): void | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('error', $json['status']); | $this->assertSame('error', $json['status']); | ||||
$this->assertSame('The amount must be a number.', $json['errors']['amount'][0]); | $this->assertSame('The amount must be a number.', $json['errors']['amount'][0]); | ||||
$this->assertSame('The description field is required.', $json['errors']['description'][0]); | $this->assertSame('The description field is required.', $json['errors']['description'][0]); | ||||
$this->assertCount(2, $json); | $this->assertCount(2, $json); | ||||
$this->assertCount(2, $json['errors']); | $this->assertCount(2, $json['errors']); | ||||
// Admin user - a valid bonus | // A valid bonus | ||||
$post = ['amount' => '50', 'description' => 'A bonus']; | $post = ['amount' => '50', 'description' => 'A bonus']; | ||||
$response = $this->actingAs($reseller1)->post("api/v4/wallets/{$wallet->id}/one-off", $post); | $response = $this->actingAs($reseller1)->post("api/v4/wallets/{$wallet->id}/one-off", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertSame('The bonus has been added to the wallet successfully.', $json['message']); | $this->assertSame('The bonus has been added to the wallet successfully.', $json['message']); | ||||
$this->assertSame($balance += 5000, $json['balance']); | $this->assertSame($balance += 5000, $json['balance']); | ||||
$this->assertSame($balance, $wallet->fresh()->balance); | $this->assertSame($balance, $wallet->fresh()->balance); | ||||
$this->assertSame($reseller1_balance -= 5000, $reseller1_wallet->fresh()->balance); | |||||
$transaction = Transaction::where('object_id', $wallet->id) | $transaction = Transaction::where('object_id', $wallet->id) | ||||
->where('type', Transaction::WALLET_AWARD)->first(); | ->where('type', Transaction::WALLET_AWARD)->first(); | ||||
$this->assertSame($post['description'], $transaction->description); | $this->assertSame($post['description'], $transaction->description); | ||||
$this->assertSame(5000, $transaction->amount); | $this->assertSame(5000, $transaction->amount); | ||||
$this->assertSame($reseller1->email, $transaction->user_email); | $this->assertSame($reseller1->email, $transaction->user_email); | ||||
// Admin user - a valid penalty | $transaction = Transaction::where('object_id', $reseller1_wallet->id) | ||||
->where('type', Transaction::WALLET_DEBIT)->first(); | |||||
$this->assertSame("Awarded user {$user->email}", $transaction->description); | |||||
$this->assertSame(-5000, $transaction->amount); | |||||
$this->assertSame($reseller1->email, $transaction->user_email); | |||||
// A valid penalty | |||||
$post = ['amount' => '-40', 'description' => 'A penalty']; | $post = ['amount' => '-40', 'description' => 'A penalty']; | ||||
$response = $this->actingAs($reseller1)->post("api/v4/wallets/{$wallet->id}/one-off", $post); | $response = $this->actingAs($reseller1)->post("api/v4/wallets/{$wallet->id}/one-off", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertSame('The penalty has been added to the wallet successfully.', $json['message']); | $this->assertSame('The penalty has been added to the wallet successfully.', $json['message']); | ||||
$this->assertSame($balance -= 4000, $json['balance']); | $this->assertSame($balance -= 4000, $json['balance']); | ||||
$this->assertSame($balance, $wallet->fresh()->balance); | $this->assertSame($balance, $wallet->fresh()->balance); | ||||
$this->assertSame($reseller1_balance += 4000, $reseller1_wallet->fresh()->balance); | |||||
$transaction = Transaction::where('object_id', $wallet->id) | $transaction = Transaction::where('object_id', $wallet->id) | ||||
->where('type', Transaction::WALLET_PENALTY)->first(); | ->where('type', Transaction::WALLET_PENALTY)->first(); | ||||
$this->assertSame($post['description'], $transaction->description); | $this->assertSame($post['description'], $transaction->description); | ||||
$this->assertSame(-4000, $transaction->amount); | $this->assertSame(-4000, $transaction->amount); | ||||
$this->assertSame($reseller1->email, $transaction->user_email); | $this->assertSame($reseller1->email, $transaction->user_email); | ||||
$transaction = Transaction::where('object_id', $reseller1_wallet->id) | |||||
->where('type', Transaction::WALLET_CREDIT)->first(); | |||||
$this->assertSame("Penalized user {$user->email}", $transaction->description); | |||||
$this->assertSame(4000, $transaction->amount); | |||||
$this->assertSame($reseller1->email, $transaction->user_email); | |||||
// Reseller from a different tenant | // Reseller from a different tenant | ||||
\config(['app.tenant_id' => 2]); | \config(['app.tenant_id' => 2]); | ||||
$response = $this->actingAs($reseller2)->post("api/v4/wallets/{$wallet->id}/one-off", []); | $response = $this->actingAs($reseller2)->post("api/v4/wallets/{$wallet->id}/one-off", []); | ||||
$response->assertStatus(404); | $response->assertStatus(404); | ||||
} | } | ||||
/** | /** | ||||
* Test fetching wallet transactions (GET /api/v4/wallets/:id/transactions) | * Test fetching wallet transactions (GET /api/v4/wallets/:id/transactions) | ||||
▲ Show 20 Lines • Show All 119 Lines • Show Last 20 Lines |