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
|
|
|
|
{
|
|
|
|
/**
|
2023-03-08 17:43:26 +01:00
|
|
|
* @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;
|
|
|
|
|
2023-03-08 17:43:26 +01:00
|
|
|
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
|
|
|
|
*/
|
2023-03-08 17:43:26 +01:00
|
|
|
public function test_allPreferences_returns_consistent_json_structure()
|
2021-11-22 01:09:54 +01:00
|
|
|
{
|
2023-03-08 17:43:26 +01:00
|
|
|
$response = $this->actingAs($this->user, 'api-guard')
|
|
|
|
->json('GET', '/api/v1/user/preferences')
|
2021-11-22 01:09:54 +01:00
|
|
|
->assertOk()
|
2023-03-08 17:43:26 +01:00
|
|
|
->assertJsonStructure([
|
|
|
|
'*' => self::PREFERENCE_JSON_STRUCTURE,
|
2021-11-22 01:09:54 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-03-08 17:43:26 +01:00
|
|
|
public function test_allPreferences_returns_preferences_with_default_values()
|
2021-11-22 01:09:54 +01:00
|
|
|
{
|
2023-03-08 17:43:26 +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,
|
2023-03-08 17:43:26 +01:00
|
|
|
'value' => $value,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_allPreferences_returns_preferences_with_user_values()
|
|
|
|
{
|
|
|
|
$userPrefs = [
|
2023-04-14 17:54:35 +02:00
|
|
|
'showOtpAsDot' => true,
|
2023-03-10 22:59:46 +01:00
|
|
|
'closeOtpOnCopy' => true,
|
|
|
|
'copyOtpOnDisplay' => true,
|
2023-03-08 17:43:26 +01:00
|
|
|
'useBasicQrcodeReader' => true,
|
2023-03-10 22:59:46 +01:00
|
|
|
'displayMode' => 'grid',
|
|
|
|
'showAccountsIcons' => false,
|
|
|
|
'kickUserAfter' => 5,
|
|
|
|
'activeGroup' => 1,
|
|
|
|
'rememberActiveGroup' => false,
|
|
|
|
'defaultGroup' => 1,
|
|
|
|
'defaultCaptureMode' => 'advancedForm',
|
|
|
|
'useDirectCapture' => true,
|
|
|
|
'useWebauthnOnly' => true,
|
|
|
|
'getOfficialIcons' => false,
|
|
|
|
'theme' => 'dark',
|
|
|
|
'formatPassword' => false,
|
|
|
|
'formatPasswordBy' => 1,
|
|
|
|
'lang' => 'fr',
|
2023-03-08 17:43:26 +01:00
|
|
|
];
|
|
|
|
|
2023-04-14 17:54:35 +02:00
|
|
|
$this->user['preferences->showOtpAsDot'] = $userPrefs['showOtpAsDot'];
|
2023-03-10 22:59:46 +01:00
|
|
|
$this->user['preferences->closeOtpOnCopy'] = $userPrefs['closeOtpOnCopy'];
|
|
|
|
$this->user['preferences->copyOtpOnDisplay'] = $userPrefs['copyOtpOnDisplay'];
|
2023-03-08 17:43:26 +01:00
|
|
|
$this->user['preferences->useBasicQrcodeReader'] = $userPrefs['useBasicQrcodeReader'];
|
2023-03-10 22:59:46 +01:00
|
|
|
$this->user['preferences->displayMode'] = $userPrefs['displayMode'];
|
|
|
|
$this->user['preferences->showAccountsIcons'] = $userPrefs['showAccountsIcons'];
|
|
|
|
$this->user['preferences->kickUserAfter'] = $userPrefs['kickUserAfter'];
|
|
|
|
$this->user['preferences->activeGroup'] = $userPrefs['activeGroup'];
|
|
|
|
$this->user['preferences->rememberActiveGroup'] = $userPrefs['rememberActiveGroup'];
|
|
|
|
$this->user['preferences->defaultGroup'] = $userPrefs['defaultGroup'];
|
|
|
|
$this->user['preferences->defaultCaptureMode'] = $userPrefs['defaultCaptureMode'];
|
|
|
|
$this->user['preferences->useDirectCapture'] = $userPrefs['useDirectCapture'];
|
|
|
|
$this->user['preferences->useWebauthnOnly'] = $userPrefs['useWebauthnOnly'];
|
|
|
|
$this->user['preferences->getOfficialIcons'] = $userPrefs['getOfficialIcons'];
|
|
|
|
$this->user['preferences->theme'] = $userPrefs['theme'];
|
|
|
|
$this->user['preferences->formatPassword'] = $userPrefs['formatPassword'];
|
|
|
|
$this->user['preferences->formatPasswordBy'] = $userPrefs['formatPasswordBy'];
|
|
|
|
$this->user['preferences->lang'] = $userPrefs['lang'];
|
2023-03-08 17:43:26 +01:00
|
|
|
$this->user->save();
|
2021-11-22 01:09:54 +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/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,
|
2023-03-08 17:43:26 +01:00
|
|
|
'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'),
|
2023-03-08 17:43:26 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
2023-03-08 17:43:26 +01:00
|
|
|
$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')
|
2023-03-08 17:43:26 +01:00
|
|
|
->assertJsonFragment([
|
2023-04-14 17:54:35 +02:00
|
|
|
'key' => 'showOtpAsDot',
|
|
|
|
'value' => $showOtpAsDot,
|
2021-11-22 01:09:54 +01:00
|
|
|
]);
|
|
|
|
}
|
2023-03-08 17:43:26 +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');
|
2023-03-08 17:43:26 +01:00
|
|
|
|
|
|
|
$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,
|
2023-03-08 17:43:26 +01:00
|
|
|
])
|
|
|
|
->assertCreated()
|
|
|
|
->assertExactJson([
|
2023-04-14 17:54:35 +02:00
|
|
|
'key' => 'showOtpAsDot',
|
|
|
|
'value' => $showOtpAsDot,
|
2023-03-08 17:43:26 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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',
|
2023-03-08 17:43:26 +01:00
|
|
|
'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',
|
2023-03-08 17:43:26 +01:00
|
|
|
'value' => null,
|
|
|
|
])
|
|
|
|
->assertStatus(422);
|
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|