mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-08-09 05:54:34 +02:00
Update & Complete API controllers tests and Unit tests
This commit is contained in:
@ -12,20 +12,20 @@ use Tests\FeatureTestCase;
|
||||
class SettingControllerTest extends FeatureTestCase
|
||||
{
|
||||
/**
|
||||
* @var \App\Models\User
|
||||
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
|
||||
*/
|
||||
protected $user;
|
||||
protected $user, $admin;
|
||||
|
||||
private const SETTING_JSON_STRUCTURE = [
|
||||
'key',
|
||||
'value',
|
||||
];
|
||||
|
||||
private const TWOFAUTH_NATIVE_SETTING = 'showTokenAsDot';
|
||||
private const TWOFAUTH_NATIVE_SETTING = 'checkForUpdate';
|
||||
|
||||
private const TWOFAUTH_NATIVE_SETTING_DEFAULT_VALUE = false;
|
||||
private const TWOFAUTH_NATIVE_SETTING_DEFAULT_VALUE = true;
|
||||
|
||||
private const TWOFAUTH_NATIVE_SETTING_CHANGED_VALUE = true;
|
||||
private const TWOFAUTH_NATIVE_SETTING_CHANGED_VALUE = false;
|
||||
|
||||
private const USER_DEFINED_SETTING = 'mySetting';
|
||||
|
||||
@ -41,6 +41,7 @@ class SettingControllerTest extends FeatureTestCase
|
||||
parent::setUp();
|
||||
|
||||
$this->user = User::factory()->create();
|
||||
$this->admin = User::factory()->administrator()->create();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -48,7 +49,7 @@ class SettingControllerTest extends FeatureTestCase
|
||||
*/
|
||||
public function test_index_returns_setting_collection()
|
||||
{
|
||||
$response = $this->actingAs($this->user, 'api-guard')
|
||||
$response = $this->actingAs($this->admin, 'api-guard')
|
||||
->json('GET', '/api/v1/settings')
|
||||
->assertOk()
|
||||
->assertJsonStructure([
|
||||
@ -59,9 +60,22 @@ class SettingControllerTest extends FeatureTestCase
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function test_show_native_unchanged_setting_returns_consistent_value()
|
||||
public function test_index_is_forbidden_to_users()
|
||||
{
|
||||
$response = $this->actingAs($this->user, 'api-guard')
|
||||
->json('GET', '/api/v1/settings')
|
||||
->assertForbidden()
|
||||
->assertJsonStructure([
|
||||
'message',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function test_show_native_unchanged_setting_returns_consistent_value()
|
||||
{
|
||||
$response = $this->actingAs($this->admin, 'api-guard')
|
||||
->json('GET', '/api/v1/settings/' . self::TWOFAUTH_NATIVE_SETTING)
|
||||
->assertOk()
|
||||
->assertExactJson([
|
||||
@ -77,7 +91,7 @@ class SettingControllerTest extends FeatureTestCase
|
||||
{
|
||||
Settings::set(self::TWOFAUTH_NATIVE_SETTING, self::TWOFAUTH_NATIVE_SETTING_CHANGED_VALUE);
|
||||
|
||||
$response = $this->actingAs($this->user, 'api-guard')
|
||||
$response = $this->actingAs($this->admin, 'api-guard')
|
||||
->json('GET', '/api/v1/settings/' . self::TWOFAUTH_NATIVE_SETTING)
|
||||
->assertOk()
|
||||
->assertExactJson([
|
||||
@ -93,7 +107,7 @@ class SettingControllerTest extends FeatureTestCase
|
||||
{
|
||||
Settings::set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE);
|
||||
|
||||
$response = $this->actingAs($this->user, 'api-guard')
|
||||
$response = $this->actingAs($this->admin, 'api-guard')
|
||||
->json('GET', '/api/v1/settings/' . self::USER_DEFINED_SETTING)
|
||||
->assertOk()
|
||||
->assertExactJson([
|
||||
@ -107,7 +121,7 @@ class SettingControllerTest extends FeatureTestCase
|
||||
*/
|
||||
public function test_show_missing_setting_returns_not_found()
|
||||
{
|
||||
$response = $this->actingAs($this->user, 'api-guard')
|
||||
$response = $this->actingAs($this->admin, 'api-guard')
|
||||
->json('GET', '/api/v1/settings/missing')
|
||||
->assertNotFound();
|
||||
}
|
||||
@ -115,9 +129,22 @@ class SettingControllerTest extends FeatureTestCase
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function test_store_custom_user_setting_returns_success()
|
||||
public function test_show_setting_is_forbidden_to_users()
|
||||
{
|
||||
$response = $this->actingAs($this->user, 'api-guard')
|
||||
->json('GET', '/api/v1/settings/' . self::TWOFAUTH_NATIVE_SETTING)
|
||||
->assertForbidden()
|
||||
->assertJsonStructure([
|
||||
'message',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function test_store_custom_user_setting_returns_success()
|
||||
{
|
||||
$response = $this->actingAs($this->admin, 'api-guard')
|
||||
->json('POST', '/api/v1/settings', [
|
||||
'key' => self::USER_DEFINED_SETTING,
|
||||
'value' => self::USER_DEFINED_SETTING_VALUE,
|
||||
@ -134,7 +161,7 @@ class SettingControllerTest extends FeatureTestCase
|
||||
*/
|
||||
public function test_store_invalid_custom_user_setting_returns_validation_error()
|
||||
{
|
||||
$response = $this->actingAs($this->user, 'api-guard')
|
||||
$response = $this->actingAs($this->admin, 'api-guard')
|
||||
->json('POST', '/api/v1/settings', [
|
||||
'key' => null,
|
||||
'value' => null,
|
||||
@ -149,7 +176,7 @@ class SettingControllerTest extends FeatureTestCase
|
||||
{
|
||||
Settings::set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE);
|
||||
|
||||
$response = $this->actingAs($this->user, 'api-guard')
|
||||
$response = $this->actingAs($this->admin, 'api-guard')
|
||||
->json('POST', '/api/v1/settings', [
|
||||
'key' => self::USER_DEFINED_SETTING,
|
||||
'value' => self::USER_DEFINED_SETTING_VALUE,
|
||||
@ -162,7 +189,7 @@ class SettingControllerTest extends FeatureTestCase
|
||||
*/
|
||||
public function test_update_unchanged_native_setting_returns_updated_setting()
|
||||
{
|
||||
$response = $this->actingAs($this->user, 'api-guard')
|
||||
$response = $this->actingAs($this->admin, 'api-guard')
|
||||
->json('PUT', '/api/v1/settings/' . self::TWOFAUTH_NATIVE_SETTING, [
|
||||
'value' => self::TWOFAUTH_NATIVE_SETTING_CHANGED_VALUE,
|
||||
])
|
||||
@ -180,7 +207,7 @@ class SettingControllerTest extends FeatureTestCase
|
||||
{
|
||||
Settings::set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE);
|
||||
|
||||
$response = $this->actingAs($this->user, 'api-guard')
|
||||
$response = $this->actingAs($this->admin, 'api-guard')
|
||||
->json('PUT', '/api/v1/settings/' . self::USER_DEFINED_SETTING, [
|
||||
'value' => self::USER_DEFINED_SETTING_CHANGED_VALUE,
|
||||
])
|
||||
@ -196,7 +223,7 @@ class SettingControllerTest extends FeatureTestCase
|
||||
*/
|
||||
public function test_update_missing_user_setting_returns_created_setting()
|
||||
{
|
||||
$response = $this->actingAs($this->user, 'api-guard')
|
||||
$response = $this->actingAs($this->admin, 'api-guard')
|
||||
->json('PUT', '/api/v1/settings/' . self::USER_DEFINED_SETTING, [
|
||||
'value' => self::USER_DEFINED_SETTING_CHANGED_VALUE,
|
||||
])
|
||||
@ -214,7 +241,7 @@ class SettingControllerTest extends FeatureTestCase
|
||||
{
|
||||
Settings::set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE);
|
||||
|
||||
$response = $this->actingAs($this->user, 'api-guard')
|
||||
$response = $this->actingAs($this->admin, 'api-guard')
|
||||
->json('DELETE', '/api/v1/settings/' . self::USER_DEFINED_SETTING)
|
||||
->assertNoContent();
|
||||
}
|
||||
@ -224,7 +251,7 @@ class SettingControllerTest extends FeatureTestCase
|
||||
*/
|
||||
public function test_destroy_native_setting_returns_bad_request()
|
||||
{
|
||||
$response = $this->actingAs($this->user, 'api-guard')
|
||||
$response = $this->actingAs($this->admin, 'api-guard')
|
||||
->json('DELETE', '/api/v1/settings/' . self::TWOFAUTH_NATIVE_SETTING)
|
||||
->assertStatus(400)
|
||||
->assertJsonStructure([
|
||||
@ -238,8 +265,23 @@ class SettingControllerTest extends FeatureTestCase
|
||||
*/
|
||||
public function test_destroy_missing_user_setting_returns_not_found()
|
||||
{
|
||||
$response = $this->actingAs($this->user, 'api-guard')
|
||||
$response = $this->actingAs($this->admin, 'api-guard')
|
||||
->json('DELETE', '/api/v1/settings/' . self::USER_DEFINED_SETTING)
|
||||
->assertNotFound();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
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',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user