Update & Complete API controllers tests and Unit tests

This commit is contained in:
Bubka
2023-03-08 17:43:26 +01:00
parent 823acde49d
commit 0a8807d87a
18 changed files with 545 additions and 99 deletions

View File

@@ -2,7 +2,8 @@
namespace Tests\Api\v1\Controllers;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use App\Models\TwoFAccount;
use App\Models\User;
use Illuminate\Http\UploadedFile;
use Tests\FeatureTestCase;
@@ -11,7 +12,20 @@ use Tests\FeatureTestCase;
*/
class IconControllerTest extends FeatureTestCase
{
use WithoutMiddleware;
/**
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
*/
protected $user;
/**
*
*/
public function setUp() : void
{
parent::setUp();
$this->user = User::factory()->create();
}
/**
* @test
@@ -20,9 +34,10 @@ class IconControllerTest extends FeatureTestCase
{
$file = UploadedFile::fake()->image('testIcon.jpg');
$response = $this->json('POST', '/api/v1/icons', [
'icon' => $file,
])
$response = $this->actingAs($this->user, 'api-guard')
->json('POST', '/api/v1/icons', [
'icon' => $file,
])
->assertCreated()
->assertJsonStructure([
'filename',
@@ -34,9 +49,10 @@ class IconControllerTest extends FeatureTestCase
*/
public function test_upload_with_invalid_data_returns_validation_error()
{
$response = $this->json('POST', '/api/v1/icons', [
'icon' => null,
])
$response = $this->actingAs($this->user, 'api-guard')
->json('POST', '/api/v1/icons', [
'icon' => null,
])
->assertStatus(422);
}
@@ -45,9 +61,10 @@ class IconControllerTest extends FeatureTestCase
*/
public function test_fetch_logo_returns_filename()
{
$response = $this->json('POST', '/api/v1/icons/default', [
'service' => 'twitter',
])
$response = $this->actingAs($this->user, 'api-guard')
->json('POST', '/api/v1/icons/default', [
'service' => 'twitter',
])
->assertStatus(201)
->assertJsonStructure([
'filename',
@@ -59,9 +76,10 @@ class IconControllerTest extends FeatureTestCase
*/
public function test_fetch_unknown_logo_returns_nothing()
{
$response = $this->json('POST', '/api/v1/icons/default', [
'service' => 'unknown_company',
])
$response = $this->actingAs($this->user, 'api-guard')
->json('POST', '/api/v1/icons/default', [
'service' => 'unknown_company',
])
->assertNoContent();
}
@@ -70,7 +88,8 @@ class IconControllerTest extends FeatureTestCase
*/
public function test_delete_icon_returns_success()
{
$response = $this->json('DELETE', '/api/v1/icons/testIcon.jpg')
$response = $this->actingAs($this->user, 'api-guard')
->json('DELETE', '/api/v1/icons/testIcon.jpg')
->assertNoContent(204);
}
@@ -79,7 +98,27 @@ class IconControllerTest extends FeatureTestCase
*/
public function test_delete_invalid_icon_returns_success()
{
$response = $this->json('DELETE', '/api/v1/icons/null')
$response = $this->actingAs($this->user, 'api-guard')
->json('DELETE', '/api/v1/icons/null')
->assertNoContent(204);
}
/**
* @test
*/
public function test_delete_icon_of_another_user_is_forbidden()
{
$anotherUser = User::factory()->create();
TwoFAccount::factory()->for($anotherUser)->create([
'icon' => 'testIcon.jpg',
]);
$response = $this->actingAs($this->user, 'api-guard')
->json('DELETE', '/api/v1/icons/testIcon.jpg')
->assertForbidden()
->assertJsonStructure([
'message',
]);
}
}