Initial commit
This commit is contained in:
34
tests/Feature/Api/AccountControllerTest.php
Normal file
34
tests/Feature/Api/AccountControllerTest.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Http\Controllers\Api\V1\AccountController;
|
||||
use App\Models\User;
|
||||
use App\Models\Wallet;
|
||||
|
||||
use function Pest\Laravel\actingAs;
|
||||
use function Pest\Laravel\getJson;
|
||||
|
||||
test('get account data', function () {
|
||||
$user = User::factory()
|
||||
->has(Wallet::factory()->richChillGuy())
|
||||
->create(['name' => 'John Doe', 'email' => 'test@test.com']);
|
||||
|
||||
actingAs($user);
|
||||
|
||||
getJson(action(AccountController::class))
|
||||
->assertOk()
|
||||
->assertJson([
|
||||
'data' => [
|
||||
'id' => $user->id,
|
||||
'name' => 'John Doe',
|
||||
'email' => $user->email,
|
||||
'balance' => 1_000_000,
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
test('must be authenticated to get account data', function () {
|
||||
getJson(action(AccountController::class))
|
||||
->assertUnauthorized();
|
||||
});
|
34
tests/Feature/Api/LoginControllerTest.php
Normal file
34
tests/Feature/Api/LoginControllerTest.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Http\Controllers\Api\V1\LoginController;
|
||||
use App\Models\User;
|
||||
|
||||
use function Pest\Laravel\postJson;
|
||||
use function PHPUnit\Framework\assertCount;
|
||||
|
||||
test('login should return token', function () {
|
||||
$user = User::factory()->create(['email' => 'test@test.com']);
|
||||
|
||||
postJson(action(LoginController::class), [
|
||||
'email' => 'test@test.com',
|
||||
'password' => 'password',
|
||||
'device_name' => 'Feature test',
|
||||
])
|
||||
->assertCreated()
|
||||
->assertJsonStructure(['data' => ['token']]);
|
||||
|
||||
assertCount(1, $user->refresh()->tokens);
|
||||
});
|
||||
|
||||
test('bad login should return HTTP 400', function () {
|
||||
postJson(action(LoginController::class), [
|
||||
'email' => 'test@test.com',
|
||||
'password' => 'password',
|
||||
'device_name' => 'Feature test',
|
||||
])
|
||||
->assertStatus(400)
|
||||
->assertJsonPath('message', 'Invalid credentials.')
|
||||
->assertJsonPath('code', 'BAD_LOGIN');
|
||||
});
|
66
tests/Feature/Api/SendMoneyControllerTest.php
Normal file
66
tests/Feature/Api/SendMoneyControllerTest.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Http\Controllers\Api\V1\SendMoneyController;
|
||||
use App\Models\User;
|
||||
use App\Models\Wallet;
|
||||
|
||||
use function Pest\Laravel\actingAs;
|
||||
use function Pest\Laravel\assertDatabaseCount;
|
||||
use function Pest\Laravel\assertDatabaseHas;
|
||||
use function Pest\Laravel\postJson;
|
||||
|
||||
test('send money to a friend', function () {
|
||||
$user = User::factory()
|
||||
->has(Wallet::factory()->richChillGuy())
|
||||
->create();
|
||||
|
||||
$recipient = User::factory()
|
||||
->has(Wallet::factory())
|
||||
->create();
|
||||
|
||||
actingAs($user);
|
||||
|
||||
postJson(action(SendMoneyController::class), [
|
||||
'recipient_email' => $recipient->email,
|
||||
'amount' => 100,
|
||||
'reason' => 'Just a chill guy gift',
|
||||
])
|
||||
->assertNoContent(201);
|
||||
|
||||
expect($recipient->refresh()->wallet->balance)->toBe(100);
|
||||
|
||||
assertDatabaseHas('wallet_transfers', [
|
||||
'amount' => 100,
|
||||
'source_id' => $user->wallet->id,
|
||||
'target_id' => $recipient->wallet->id,
|
||||
]);
|
||||
|
||||
assertDatabaseCount('wallet_transactions', 3);
|
||||
});
|
||||
|
||||
test('cannot send money to a friend with insufficient balance', function () {
|
||||
$user = User::factory()
|
||||
->has(Wallet::factory())
|
||||
->create();
|
||||
|
||||
$recipient = User::factory()
|
||||
->has(Wallet::factory())
|
||||
->create();
|
||||
|
||||
actingAs($user);
|
||||
|
||||
postJson(action(SendMoneyController::class), [
|
||||
'recipient_email' => $recipient->email,
|
||||
'amount' => 100,
|
||||
'reason' => 'Just a chill guy gift',
|
||||
])
|
||||
->assertBadRequest()
|
||||
->assertJson([
|
||||
'code' => 'INSUFFICIENT_BALANCE',
|
||||
'message' => 'Insufficient balance in wallet.',
|
||||
]);
|
||||
|
||||
expect($recipient->refresh()->wallet->balance)->toBe(0);
|
||||
});
|
Reference in New Issue
Block a user