Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/Admin/WalletsTest.php
Show First 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | class WalletsTest extends TestCase | ||||
* Test awarding/penalizing a wallet (POST /api/v4/wallets/:id/one-off) | * Test awarding/penalizing a wallet (POST /api/v4/wallets/:id/one-off) | ||||
*/ | */ | ||||
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'); | ||||
$wallet = $user->wallets()->first(); | $wallet = $user->wallets()->first(); | ||||
$balance = $wallet->balance; | $balance = $wallet->balance; | ||||
$reseller = $this->getTestUser('reseller@kolabnow.com'); | |||||
$wallet = $user->wallets()->first(); | |||||
$reseller_wallet = $reseller->wallets()->first(); | |||||
$reseller_balance = $reseller_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(); | ||||
// 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); | ||||
Show All 17 Lines | public function testOneOff(): void | ||||
$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($reseller_balance, $reseller_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($admin->email, $transaction->user_email); | $this->assertSame($admin->email, $transaction->user_email); | ||||
// Admin user - a valid penalty | // Admin user - a valid penalty | ||||
$post = ['amount' => '-40', 'description' => 'A penalty']; | $post = ['amount' => '-40', 'description' => 'A penalty']; | ||||
$response = $this->actingAs($admin)->post("api/v4/wallets/{$wallet->id}/one-off", $post); | $response = $this->actingAs($admin)->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($reseller_balance, $reseller_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($admin->email, $transaction->user_email); | $this->assertSame($admin->email, $transaction->user_email); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 90 Lines • Show Last 20 Lines |