2021-11-14 01:52:46 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Api\v1\Controllers;
|
|
|
|
|
2023-08-01 11:28:27 +02:00
|
|
|
use App\Api\v1\Controllers\IconController;
|
2023-03-08 17:43:26 +01:00
|
|
|
use App\Models\TwoFAccount;
|
|
|
|
use App\Models\User;
|
2022-11-22 15:15:52 +01:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2023-08-01 11:28:27 +02:00
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
2021-11-30 17:39:33 +01:00
|
|
|
use Tests\FeatureTestCase;
|
2021-11-14 01:52:46 +01:00
|
|
|
|
2021-11-22 01:09:54 +01:00
|
|
|
/**
|
2023-08-01 11:28:27 +02:00
|
|
|
* IconController test class
|
2021-11-22 01:09:54 +01:00
|
|
|
*/
|
2023-08-01 11:28:27 +02:00
|
|
|
#[CoversClass(IconController::class)]
|
2021-11-30 17:39:33 +01:00
|
|
|
class IconControllerTest extends FeatureTestCase
|
2021-11-14 01:52:46 +01:00
|
|
|
{
|
2023-03-08 17:43:26 +01:00
|
|
|
/**
|
|
|
|
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
|
|
|
|
*/
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
public function setUp() : void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->user = User::factory()->create();
|
|
|
|
}
|
2021-11-14 01:52:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_upload_icon_returns_filename()
|
|
|
|
{
|
|
|
|
$file = UploadedFile::fake()->image('testIcon.jpg');
|
|
|
|
|
2023-03-08 17:43:26 +01:00
|
|
|
$response = $this->actingAs($this->user, 'api-guard')
|
|
|
|
->json('POST', '/api/v1/icons', [
|
|
|
|
'icon' => $file,
|
|
|
|
])
|
2021-11-14 01:52:46 +01:00
|
|
|
->assertCreated()
|
|
|
|
->assertJsonStructure([
|
2022-11-22 15:15:52 +01:00
|
|
|
'filename',
|
2021-11-14 01:52:46 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_upload_with_invalid_data_returns_validation_error()
|
|
|
|
{
|
2023-03-08 17:43:26 +01:00
|
|
|
$response = $this->actingAs($this->user, 'api-guard')
|
|
|
|
->json('POST', '/api/v1/icons', [
|
|
|
|
'icon' => null,
|
|
|
|
])
|
2021-11-14 01:52:46 +01:00
|
|
|
->assertStatus(422);
|
|
|
|
}
|
|
|
|
|
2022-12-09 10:52:17 +01:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_fetch_logo_returns_filename()
|
|
|
|
{
|
2023-03-08 17:43:26 +01:00
|
|
|
$response = $this->actingAs($this->user, 'api-guard')
|
|
|
|
->json('POST', '/api/v1/icons/default', [
|
2023-12-05 16:32:20 +01:00
|
|
|
'service' => 'dropbox',
|
2023-03-08 17:43:26 +01:00
|
|
|
])
|
2022-12-09 10:52:17 +01:00
|
|
|
->assertStatus(201)
|
|
|
|
->assertJsonStructure([
|
|
|
|
'filename',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_fetch_unknown_logo_returns_nothing()
|
|
|
|
{
|
2023-03-08 17:43:26 +01:00
|
|
|
$response = $this->actingAs($this->user, 'api-guard')
|
|
|
|
->json('POST', '/api/v1/icons/default', [
|
|
|
|
'service' => 'unknown_company',
|
|
|
|
])
|
2022-12-09 10:52:17 +01:00
|
|
|
->assertNoContent();
|
|
|
|
}
|
|
|
|
|
2021-11-14 01:52:46 +01:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_delete_icon_returns_success()
|
|
|
|
{
|
2023-03-08 17:43:26 +01:00
|
|
|
$response = $this->actingAs($this->user, 'api-guard')
|
|
|
|
->json('DELETE', '/api/v1/icons/testIcon.jpg')
|
2021-11-14 01:52:46 +01:00
|
|
|
->assertNoContent(204);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_delete_invalid_icon_returns_success()
|
|
|
|
{
|
2023-03-08 17:43:26 +01:00
|
|
|
$response = $this->actingAs($this->user, 'api-guard')
|
|
|
|
->json('DELETE', '/api/v1/icons/null')
|
2021-11-14 01:52:46 +01:00
|
|
|
->assertNoContent(204);
|
|
|
|
}
|
2023-03-08 17:43:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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',
|
|
|
|
]);
|
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|