Skip to content

Added feature tests for DebitCardController #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 169 additions & 17 deletions tests/Feature/DebitCardControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Tests\Feature;

use App\Models\DebitCard;
use App\Models\DebitCardTransaction;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Testing\Fluent\AssertableJson;
use Laravel\Passport\Passport;
use Tests\TestCase;

Expand All @@ -20,54 +23,203 @@ protected function setUp(): void
Passport::actingAs($this->user);
}

public function testCustomerCanSeeAListOfDebitCards()
public function testCustomerCanSeeAListOfDebitCards(): void
{
// get /debit-cards
$userDebitCards = DebitCard::factory()
->active()
->count(2)
->create([
'user_id' => $this->user->id,
]);

$response = $this->get('api/debit-cards');

$response->assertStatus(200);
$response->assertJsonCount(count($userDebitCards));

$response->assertJson(fn (AssertableJson $json) =>
$json->first(
fn ($json) =>
$json
->whereType('id', 'integer')
->whereType('number', 'string')
->whereType('type', 'string')
->whereType('expiration_date', 'string')
->whereType('is_active', 'boolean')
));
}

public function testCustomerCannotSeeAListOfDebitCardsOfOtherCustomers()
public function testCustomerCannotSeeAListOfDebitCardsOfOtherCustomers(): void
{
// get /debit-cards
$anotherUser = User::factory()->create();
DebitCard::factory()
->active()
->count(2)
->create([
'user_id' => $anotherUser->id,
]);

$response = $this->get('api/debit-cards');

$response->assertStatus(200);
$response->assertJsonCount(0);
}

public function testCustomerCanCreateADebitCard()
public function testCustomerCanCreateADebitCard(): void
{
// post /debit-cards
$response = $this->postJson('api/debit-cards', [
'type' => 'testCard'
]);

$response->assertStatus(201);

$response->assertJson(fn (AssertableJson $json) =>
$json
->whereType('id', 'integer')
->whereType('number', 'integer')
->whereType('type', 'string')
->whereType('expiration_date', 'string')
->whereType('is_active', 'boolean')
);

$this->assertDatabaseHas('debit_cards', [
'type' => 'testCard'
]);
}

public function testCustomerCanSeeASingleDebitCardDetails()
public function testCustomerCanSeeASingleDebitCardDetails(): void
{
// get api/debit-cards/{debitCard}
$userDebitCards = DebitCard::factory()
->active()
->count(2)
->create([
'user_id' => $this->user->id,
]);

$response = $this->get('api/debit-cards/' . $userDebitCards->first()->id);

$response->assertStatus(200);
$response->assertJson(fn (AssertableJson $json) =>
$json
->whereType('id', 'integer')
->whereType('number', 'string')
->whereType('type', 'string')
->whereType('expiration_date', 'string')
->whereType('is_active', 'boolean')
);

$response->assertJson(fn ($json) => $json->where('number', $userDebitCards->first()->number)->etc());
}

public function testCustomerCannotSeeASingleDebitCardDetails()
public function testCustomerCannotSeeASingleDebitCardDetails(): void
{
// get api/debit-cards/{debitCard}
$anotherUser = User::factory()->create();
DebitCard::factory()
->active()
->count(2)
->create([
'user_id' => $anotherUser->id,
]);

$response = $this->get('api/debit-cards/' . $anotherUser->first()->id);

$response->assertStatus(403);
}

public function testCustomerCanActivateADebitCard()
public function testCustomerCanActivateADebitCard(): void
{
// put api/debit-cards/{debitCard}
// First create a disabled debit card and make sure it was disabled correctly
$userDebitCard = DebitCard::factory()
->active()
->create([
'user_id' => $this->user->id,
'disabled_at' => date('Y-m-d H:i:s')
]);

$response = $this->get('api/debit-cards/' . $userDebitCard->first()->id);

$response->assertStatus(200);
$response->assertJson(
fn ($json) => $json->where('is_active', false)->etc()
);

// Second set card to active and check result
$response = $this->putJson('api/debit-cards/' . $userDebitCard->first()->id, [
'is_active' => true
]);

$response->assertJson(
fn ($json) => $json
->where('is_active', true)
->where('number', $userDebitCard->first()->number)
->etc()
);
}

public function testCustomerCanDeactivateADebitCard()
public function testCustomerCanDeactivateADebitCard(): void
{
// put api/debit-cards/{debitCard}
$userDebitCard = DebitCard::factory()
->active()
->create([
'user_id' => $this->user->id
]);

$response = $this->putJson('api/debit-cards/' . $userDebitCard->first()->id, [
'is_active' => false
]);

$response->assertJson(
fn ($json) => $json
->where('is_active', false)
->where('number', $userDebitCard->first()->number)
->etc()
);
}

public function testCustomerCannotUpdateADebitCardWithWrongValidation()
{
// put api/debit-cards/{debitCard}
$userDebitCard = DebitCard::factory()
->active()
->create([
'user_id' => $this->user->id
]);

$response = $this->putJson('api/debit-cards/' . $userDebitCard->first()->id, [
'number' => false
]);

$response->assertStatus(422);
}

public function testCustomerCanDeleteADebitCard()
{
// delete api/debit-cards/{debitCard}
$userDebitCard = DebitCard::factory()
->active()
->create([
'user_id' => $this->user->id
]);

$this->deleteJson('api/debit-cards/' . $userDebitCard->id);

$this->assertSoftDeleted('debit_cards', [
'id' => $userDebitCard->id,
'user_id' => $userDebitCard->user->id,
]);
}

public function testCustomerCannotDeleteADebitCardWithTransaction()
{
// delete api/debit-cards/{debitCard}
$userDebitCard = DebitCard::factory()->active()->create([
'user_id' => $this->user->id,
]);

DebitCardTransaction::factory()->create([
'debit_card_id' => $userDebitCard->id
]);

$response = $this->deleteJson('api/debit-cards/' . $userDebitCard->id);

$response->assertStatus(403);
}

// Extra bonus for extra tests :)
Expand Down