2021-11-15 00:21:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Api\v1\Controllers;
|
|
|
|
|
2023-08-01 11:28:27 +02:00
|
|
|
use App\Api\v1\Controllers\SettingController;
|
2022-11-22 15:15:52 +01:00
|
|
|
use App\Facades\Settings;
|
2021-12-02 13:15:53 +01:00
|
|
|
use App\Models\User;
|
2024-01-29 08:54:21 +01:00
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
use Illuminate\Support\Facades\Route;
|
|
|
|
use Illuminate\Support\Str;
|
2023-08-01 11:28:27 +02:00
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
2024-06-26 14:29:13 +02:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2021-11-15 00:21:07 +01:00
|
|
|
use Tests\FeatureTestCase;
|
2021-11-22 01:09:54 +01:00
|
|
|
|
|
|
|
/**
|
2023-08-01 11:28:27 +02:00
|
|
|
* SettingController test class
|
2021-11-22 01:09:54 +01:00
|
|
|
*/
|
2023-08-01 11:28:27 +02:00
|
|
|
#[CoversClass(SettingController::class)]
|
2021-11-15 00:21:07 +01:00
|
|
|
class SettingControllerTest extends FeatureTestCase
|
|
|
|
{
|
|
|
|
/**
|
2023-03-08 17:43:26 +01:00
|
|
|
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
|
2022-11-22 15:15:52 +01:00
|
|
|
*/
|
2023-03-10 22:59:46 +01:00
|
|
|
protected $user;
|
|
|
|
|
|
|
|
protected $admin;
|
2021-11-15 00:21:07 +01:00
|
|
|
|
|
|
|
private const SETTING_JSON_STRUCTURE = [
|
|
|
|
'key',
|
2022-11-22 15:15:52 +01:00
|
|
|
'value',
|
2021-11-15 00:21:07 +01:00
|
|
|
];
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-03-08 17:43:26 +01:00
|
|
|
private const TWOFAUTH_NATIVE_SETTING = 'checkForUpdate';
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-03-08 17:43:26 +01:00
|
|
|
private const TWOFAUTH_NATIVE_SETTING_DEFAULT_VALUE = true;
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2023-03-08 17:43:26 +01:00
|
|
|
private const TWOFAUTH_NATIVE_SETTING_CHANGED_VALUE = false;
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-11-15 00:21:07 +01:00
|
|
|
private const USER_DEFINED_SETTING = 'mySetting';
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-11-15 00:21:07 +01:00
|
|
|
private const USER_DEFINED_SETTING_VALUE = 'mySetting';
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-11-15 00:21:07 +01:00
|
|
|
private const USER_DEFINED_SETTING_CHANGED_VALUE = 'mySetting';
|
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
public function setUp() : void
|
2021-11-15 00:21:07 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2023-03-10 22:59:46 +01:00
|
|
|
$this->user = User::factory()->create();
|
2023-03-08 17:43:26 +01:00
|
|
|
$this->admin = User::factory()->administrator()->create();
|
2021-11-15 00:21:07 +01:00
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2024-01-29 08:54:21 +01:00
|
|
|
public function test_all_controller_routes_are_protected_by_admin_middleware()
|
|
|
|
{
|
|
|
|
$routes = Route::getRoutes()->getRoutes();
|
|
|
|
|
|
|
|
$controllerRoutes = Arr::where($routes, function (\Illuminate\Routing\Route $route, int $key) {
|
|
|
|
if (Str::startsWith($route->getActionName(), SettingController::class)) {
|
|
|
|
return $route;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
foreach ($controllerRoutes as $controllerRoute) {
|
|
|
|
$this->assertContains('admin', $controllerRoute->middleware());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2021-11-15 00:21:07 +01:00
|
|
|
public function test_index_returns_setting_collection()
|
|
|
|
{
|
2023-03-08 17:43:26 +01:00
|
|
|
$response = $this->actingAs($this->admin, 'api-guard')
|
2021-11-15 00:21:07 +01:00
|
|
|
->json('GET', '/api/v1/settings')
|
|
|
|
->assertOk()
|
|
|
|
->assertJsonStructure([
|
2022-11-22 15:15:52 +01:00
|
|
|
'*' => self::SETTING_JSON_STRUCTURE,
|
2021-11-15 00:21:07 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2023-03-08 17:43:26 +01:00
|
|
|
public function test_index_is_forbidden_to_users()
|
2021-11-15 00:21:07 +01:00
|
|
|
{
|
2022-03-31 08:38:35 +02:00
|
|
|
$response = $this->actingAs($this->user, 'api-guard')
|
2023-03-08 17:43:26 +01:00
|
|
|
->json('GET', '/api/v1/settings')
|
|
|
|
->assertForbidden()
|
|
|
|
->assertJsonStructure([
|
|
|
|
'message',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2023-03-08 17:43:26 +01:00
|
|
|
public function test_show_native_unchanged_setting_returns_consistent_value()
|
|
|
|
{
|
|
|
|
$response = $this->actingAs($this->admin, 'api-guard')
|
2021-11-15 00:21:07 +01:00
|
|
|
->json('GET', '/api/v1/settings/' . self::TWOFAUTH_NATIVE_SETTING)
|
|
|
|
->assertOk()
|
|
|
|
->assertExactJson([
|
2022-11-22 15:15:52 +01:00
|
|
|
'key' => self::TWOFAUTH_NATIVE_SETTING,
|
2021-11-15 00:21:07 +01:00
|
|
|
'value' => self::TWOFAUTH_NATIVE_SETTING_DEFAULT_VALUE,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2021-11-15 00:21:07 +01:00
|
|
|
public function test_show_native_changed_setting_returns_consistent_value()
|
|
|
|
{
|
2022-07-30 17:51:02 +02:00
|
|
|
Settings::set(self::TWOFAUTH_NATIVE_SETTING, self::TWOFAUTH_NATIVE_SETTING_CHANGED_VALUE);
|
2021-11-15 00:21:07 +01:00
|
|
|
|
2023-03-08 17:43:26 +01:00
|
|
|
$response = $this->actingAs($this->admin, 'api-guard')
|
2021-11-15 00:21:07 +01:00
|
|
|
->json('GET', '/api/v1/settings/' . self::TWOFAUTH_NATIVE_SETTING)
|
|
|
|
->assertOk()
|
|
|
|
->assertExactJson([
|
2022-11-22 15:15:52 +01:00
|
|
|
'key' => self::TWOFAUTH_NATIVE_SETTING,
|
2021-11-15 00:21:07 +01:00
|
|
|
'value' => self::TWOFAUTH_NATIVE_SETTING_CHANGED_VALUE,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2021-11-15 00:21:07 +01:00
|
|
|
public function test_show_custom_user_setting_returns_consistent_value()
|
|
|
|
{
|
2022-07-30 17:51:02 +02:00
|
|
|
Settings::set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE);
|
2021-11-15 00:21:07 +01:00
|
|
|
|
2023-03-08 17:43:26 +01:00
|
|
|
$response = $this->actingAs($this->admin, 'api-guard')
|
2021-11-15 00:21:07 +01:00
|
|
|
->json('GET', '/api/v1/settings/' . self::USER_DEFINED_SETTING)
|
|
|
|
->assertOk()
|
|
|
|
->assertExactJson([
|
2022-11-22 15:15:52 +01:00
|
|
|
'key' => self::USER_DEFINED_SETTING,
|
2021-11-15 00:21:07 +01:00
|
|
|
'value' => self::USER_DEFINED_SETTING_VALUE,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2021-11-15 00:21:07 +01:00
|
|
|
public function test_show_missing_setting_returns_not_found()
|
|
|
|
{
|
2023-03-08 17:43:26 +01:00
|
|
|
$response = $this->actingAs($this->admin, 'api-guard')
|
2021-11-15 00:21:07 +01:00
|
|
|
->json('GET', '/api/v1/settings/missing')
|
|
|
|
->assertNotFound();
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2023-03-08 17:43:26 +01:00
|
|
|
public function test_show_setting_is_forbidden_to_users()
|
2021-11-15 00:21:07 +01:00
|
|
|
{
|
2022-03-31 08:38:35 +02:00
|
|
|
$response = $this->actingAs($this->user, 'api-guard')
|
2023-03-08 17:43:26 +01:00
|
|
|
->json('GET', '/api/v1/settings/' . self::TWOFAUTH_NATIVE_SETTING)
|
|
|
|
->assertForbidden()
|
|
|
|
->assertJsonStructure([
|
|
|
|
'message',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2023-03-08 17:43:26 +01:00
|
|
|
public function test_store_custom_user_setting_returns_success()
|
|
|
|
{
|
|
|
|
$response = $this->actingAs($this->admin, 'api-guard')
|
2021-11-15 00:21:07 +01:00
|
|
|
->json('POST', '/api/v1/settings', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'key' => self::USER_DEFINED_SETTING,
|
2021-11-15 00:21:07 +01:00
|
|
|
'value' => self::USER_DEFINED_SETTING_VALUE,
|
|
|
|
])
|
|
|
|
->assertCreated()
|
|
|
|
->assertExactJson([
|
2022-11-22 15:15:52 +01:00
|
|
|
'key' => self::USER_DEFINED_SETTING,
|
2021-11-15 00:21:07 +01:00
|
|
|
'value' => self::USER_DEFINED_SETTING_VALUE,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2021-11-15 00:21:07 +01:00
|
|
|
public function test_store_invalid_custom_user_setting_returns_validation_error()
|
|
|
|
{
|
2023-03-08 17:43:26 +01:00
|
|
|
$response = $this->actingAs($this->admin, 'api-guard')
|
2021-11-15 00:21:07 +01:00
|
|
|
->json('POST', '/api/v1/settings', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'key' => null,
|
2021-11-15 00:21:07 +01:00
|
|
|
'value' => null,
|
|
|
|
])
|
|
|
|
->assertStatus(422);
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2021-11-15 00:21:07 +01:00
|
|
|
public function test_store_existing_custom_user_setting_returns_validation_error()
|
|
|
|
{
|
2022-07-30 17:51:02 +02:00
|
|
|
Settings::set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE);
|
2021-11-15 00:21:07 +01:00
|
|
|
|
2023-03-08 17:43:26 +01:00
|
|
|
$response = $this->actingAs($this->admin, 'api-guard')
|
2021-11-15 00:21:07 +01:00
|
|
|
->json('POST', '/api/v1/settings', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'key' => self::USER_DEFINED_SETTING,
|
2021-11-15 00:21:07 +01:00
|
|
|
'value' => self::USER_DEFINED_SETTING_VALUE,
|
|
|
|
])
|
|
|
|
->assertStatus(422);
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2021-11-15 00:21:07 +01:00
|
|
|
public function test_update_unchanged_native_setting_returns_updated_setting()
|
|
|
|
{
|
2023-03-08 17:43:26 +01:00
|
|
|
$response = $this->actingAs($this->admin, 'api-guard')
|
2021-11-15 00:21:07 +01:00
|
|
|
->json('PUT', '/api/v1/settings/' . self::TWOFAUTH_NATIVE_SETTING, [
|
|
|
|
'value' => self::TWOFAUTH_NATIVE_SETTING_CHANGED_VALUE,
|
|
|
|
])
|
|
|
|
->assertOk()
|
|
|
|
->assertExactJson([
|
2022-11-22 15:15:52 +01:00
|
|
|
'key' => self::TWOFAUTH_NATIVE_SETTING,
|
2021-11-15 00:21:07 +01:00
|
|
|
'value' => self::TWOFAUTH_NATIVE_SETTING_CHANGED_VALUE,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2021-11-15 00:21:07 +01:00
|
|
|
public function test_update_custom_user_setting_returns_updated_setting()
|
|
|
|
{
|
2022-07-30 17:51:02 +02:00
|
|
|
Settings::set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE);
|
2021-11-15 00:21:07 +01:00
|
|
|
|
2023-03-08 17:43:26 +01:00
|
|
|
$response = $this->actingAs($this->admin, 'api-guard')
|
2021-11-15 00:21:07 +01:00
|
|
|
->json('PUT', '/api/v1/settings/' . self::USER_DEFINED_SETTING, [
|
|
|
|
'value' => self::USER_DEFINED_SETTING_CHANGED_VALUE,
|
|
|
|
])
|
|
|
|
->assertOk()
|
|
|
|
->assertExactJson([
|
2022-11-22 15:15:52 +01:00
|
|
|
'key' => self::USER_DEFINED_SETTING,
|
2021-11-15 00:21:07 +01:00
|
|
|
'value' => self::USER_DEFINED_SETTING_CHANGED_VALUE,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2021-11-15 00:21:07 +01:00
|
|
|
public function test_update_missing_user_setting_returns_created_setting()
|
|
|
|
{
|
2023-03-08 17:43:26 +01:00
|
|
|
$response = $this->actingAs($this->admin, 'api-guard')
|
2021-11-15 00:21:07 +01:00
|
|
|
->json('PUT', '/api/v1/settings/' . self::USER_DEFINED_SETTING, [
|
|
|
|
'value' => self::USER_DEFINED_SETTING_CHANGED_VALUE,
|
|
|
|
])
|
|
|
|
->assertOk()
|
|
|
|
->assertExactJson([
|
2022-11-22 15:15:52 +01:00
|
|
|
'key' => self::USER_DEFINED_SETTING,
|
2021-11-15 00:21:07 +01:00
|
|
|
'value' => self::USER_DEFINED_SETTING_CHANGED_VALUE,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2021-11-15 00:21:07 +01:00
|
|
|
public function test_destroy_user_setting_returns_success()
|
|
|
|
{
|
2022-07-30 17:51:02 +02:00
|
|
|
Settings::set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE);
|
2021-11-15 00:21:07 +01:00
|
|
|
|
2023-03-08 17:43:26 +01:00
|
|
|
$response = $this->actingAs($this->admin, 'api-guard')
|
2021-11-15 00:21:07 +01:00
|
|
|
->json('DELETE', '/api/v1/settings/' . self::USER_DEFINED_SETTING)
|
|
|
|
->assertNoContent();
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2021-11-15 00:21:07 +01:00
|
|
|
public function test_destroy_native_setting_returns_bad_request()
|
|
|
|
{
|
2023-03-08 17:43:26 +01:00
|
|
|
$response = $this->actingAs($this->admin, 'api-guard')
|
2021-11-15 00:21:07 +01:00
|
|
|
->json('DELETE', '/api/v1/settings/' . self::TWOFAUTH_NATIVE_SETTING)
|
|
|
|
->assertStatus(400)
|
|
|
|
->assertJsonStructure([
|
|
|
|
'message',
|
|
|
|
'reason',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2021-11-15 00:21:07 +01:00
|
|
|
public function test_destroy_missing_user_setting_returns_not_found()
|
|
|
|
{
|
2023-03-08 17:43:26 +01:00
|
|
|
$response = $this->actingAs($this->admin, 'api-guard')
|
2021-11-15 00:21:07 +01:00
|
|
|
->json('DELETE', '/api/v1/settings/' . self::USER_DEFINED_SETTING)
|
|
|
|
->assertNotFound();
|
|
|
|
}
|
2023-03-08 17:43:26 +01:00
|
|
|
|
2024-06-26 14:29:13 +02:00
|
|
|
#[Test]
|
2023-03-08 17:43:26 +01:00
|
|
|
public function test_destroy_is_forbidden_to_users()
|
|
|
|
{
|
|
|
|
Settings::set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE);
|
|
|
|
|
|
|
|
$response = $this->actingAs($this->user, 'api-guard')
|
|
|
|
->json('DELETE', '/api/v1/settings/' . self::USER_DEFINED_SETTING)
|
|
|
|
->assertForbidden()
|
|
|
|
->assertJsonStructure([
|
|
|
|
'message',
|
|
|
|
]);
|
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|