2FAuth/tests/Api/v1/Controllers/Auth/UserControllerTest.php

216 lines
6.0 KiB
PHP
Raw Normal View History

2021-11-22 01:09:54 +01:00
<?php
namespace Tests\Api\v1\Controllers\Auth;
2023-08-01 11:28:27 +02:00
use App\Api\v1\Controllers\UserController;
use App\Api\v1\Resources\UserResource;
2021-12-02 13:15:53 +01:00
use App\Models\User;
2023-08-01 11:28:27 +02:00
use PHPUnit\Framework\Attributes\CoversClass;
2021-11-22 01:09:54 +01:00
use Tests\FeatureTestCase;
2022-12-09 10:52:17 +01:00
/**
2023-08-01 11:28:27 +02:00
* UserControllerTest test class
2022-12-09 10:52:17 +01:00
*/
2023-08-01 11:28:27 +02:00
#[CoversClass(UserController::class)]
#[CoversClass(UserResource::class)]
2021-11-22 01:09:54 +01:00
class UserControllerTest extends FeatureTestCase
{
/**
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
2022-11-22 15:15:52 +01:00
*/
2021-11-22 01:09:54 +01:00
protected $user;
private const PREFERENCE_JSON_STRUCTURE = [
'key',
'value',
];
2021-11-22 01:09:54 +01:00
/**
* @test
*/
2022-12-13 12:07:29 +01:00
public function setUp() : void
2021-11-22 01:09:54 +01:00
{
parent::setUp();
2021-12-02 13:15:53 +01:00
$this->user = User::factory()->create();
2021-11-22 01:09:54 +01:00
}
/**
* @test
*/
public function test_show_existing_user_when_authenticated_returns_success()
{
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'api-guard')
2021-11-22 01:09:54 +01:00
->json('GET', '/api/v1/user')
->assertOk()
2023-12-05 16:32:20 +01:00
->assertJsonFragment([
2023-12-20 16:55:58 +01:00
'name' => $this->user->name,
'id' => $this->user->id,
'email' => $this->user->email,
'is_admin' => $this->user->is_admin,
2023-12-05 16:32:20 +01:00
])
->assertJsonStructure([
'preferences',
2021-11-22 01:09:54 +01:00
]);
}
/**
* @test
*/
public function test_allPreferences_returns_consistent_json_structure()
2021-11-22 01:09:54 +01:00
{
$response = $this->actingAs($this->user, 'api-guard')
->json('GET', '/api/v1/user/preferences')
2021-11-22 01:09:54 +01:00
->assertOk()
->assertJsonStructure([
'*' => self::PREFERENCE_JSON_STRUCTURE,
2021-11-22 01:09:54 +01:00
]);
}
/**
* @test
*/
public function test_allPreferences_returns_preferences_with_default_values()
2021-11-22 01:09:54 +01:00
{
$response = $this->actingAs($this->user, 'api-guard')
->json('GET', '/api/v1/user/preferences')
->assertJsonCount(count(config('2fauth.preferences')), $key = null);
foreach (config('2fauth.preferences') as $pref => $value) {
$response->assertJsonFragment([
2023-03-10 22:59:46 +01:00
'key' => $pref,
'value' => $value,
]);
}
}
/**
* @test
*/
public function test_allPreferences_returns_preferences_with_user_values()
{
$userPrefs = [];
foreach (config('2fauth.preferences') as $pref => $value) {
if (is_numeric($value)) {
$userPrefs[$pref] = $value + 1;
2024-04-26 08:01:20 +02:00
} elseif (is_string($value)) {
$userPrefs[$pref] = $value . '_';
2024-04-26 08:01:20 +02:00
} elseif (is_bool($value)) {
$userPrefs[$pref] = ! $value;
}
2021-11-22 01:09:54 +01:00
$this->user['preferences->' . $pref] = $userPrefs[$pref];
}
2024-04-26 08:01:20 +02:00
$this->user->save();
2024-04-26 08:01:20 +02:00
2022-03-31 08:38:35 +02:00
$response = $this->actingAs($this->user, 'api-guard')
->json('GET', '/api/v1/user/preferences')
->assertJsonCount(count(config('2fauth.preferences')), $key = null);
foreach ($userPrefs as $pref => $value) {
$response->assertJsonFragment([
2023-03-10 22:59:46 +01:00
'key' => $pref,
'value' => $value,
]);
}
}
/**
* @test
*/
public function test_showPreference_returns_preference_with_default_value()
{
/**
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
*/
$this->user = User::factory()->create();
$response = $this->actingAs($this->user, 'api-guard')
2023-04-14 17:54:35 +02:00
->json('GET', '/api/v1/user/preferences/showOtpAsDot')
2021-11-22 01:09:54 +01:00
->assertOk()
->assertExactJson([
2023-04-14 17:54:35 +02:00
'key' => 'showOtpAsDot',
'value' => config('2fauth.preferences.showOtpAsDot'),
]);
}
/**
* @test
*/
public function test_showPreference_returns_preference_with_custom_value()
{
2023-04-14 17:54:35 +02:00
$showOtpAsDot = ! config('2fauth.preferences.showOtpAsDot');
$this->user['preferences->showOtpAsDot'] = $showOtpAsDot;
$this->user->save();
$response = $this->actingAs($this->user, 'api-guard')
2023-04-14 17:54:35 +02:00
->json('GET', '/api/v1/user/preferences/showOtpAsDot')
->assertJsonFragment([
2023-04-14 17:54:35 +02:00
'key' => 'showOtpAsDot',
'value' => $showOtpAsDot,
2021-11-22 01:09:54 +01:00
]);
}
/**
* @test
*/
public function test_showPreference_for_missing_preference_returns_not_found()
{
$response = $this->actingAs($this->user, 'api-guard')
->json('GET', '/api/v1/user/preferences/unknown')
->assertNotFound();
}
/**
* @test
*/
public function test_setPreference_returns_updated_preference()
{
/**
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
*/
$this->user = User::factory()->create();
2023-04-14 17:54:35 +02:00
$showOtpAsDot = ! config('2fauth.preferences.showOtpAsDot');
$response = $this->actingAs($this->user, 'api-guard')
2023-04-14 17:54:35 +02:00
->json('PUT', '/api/v1/user/preferences/showOtpAsDot', [
'key' => 'showOtpAsDot',
'value' => $showOtpAsDot,
])
->assertCreated()
->assertExactJson([
2023-04-14 17:54:35 +02:00
'key' => 'showOtpAsDot',
'value' => $showOtpAsDot,
]);
}
/**
* @test
*/
public function test_setPreference_for_missing_preference_returns_not_found()
{
$response = $this->actingAs($this->user, 'api-guard')
->json('PUT', '/api/v1/user/preferences/unknown', [
2023-04-14 17:54:35 +02:00
'key' => 'showOtpAsDot',
'value' => true,
])
->assertNotFound();
}
/**
* @test
*/
public function test_setPreference_with_invalid_data_returns_validation_error()
{
$response = $this->actingAs($this->user, 'api-guard')
2023-04-14 17:54:35 +02:00
->json('PUT', '/api/v1/user/preferences/showOtpAsDot', [
'key' => 'showOtpAsDot',
'value' => null,
])
->assertStatus(422);
}
2022-11-22 15:15:52 +01:00
}