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

234 lines
7.7 KiB
PHP
Raw Normal View History

2021-11-22 01:09:54 +01:00
<?php
namespace Tests\Api\v1\Controllers\Auth;
2021-12-02 13:15:53 +01:00
use App\Models\User;
2021-11-22 01:09:54 +01:00
use Tests\FeatureTestCase;
2022-12-09 10:52:17 +01:00
/**
* @covers \App\Api\v1\Controllers\UserController
* @covers \App\Api\v1\Resources\UserResource
*/
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()
->assertExactJson([
'name' => $this->user->name,
'id' => $this->user->id,
'email' => $this->user->email,
'is_admin' => $this->user->is_admin,
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 = [
2023-03-10 22:59:46 +01:00
'showTokenAsDot' => true,
'closeOtpOnCopy' => true,
'copyOtpOnDisplay' => true,
'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-10 22:59:46 +01:00
$this->user['preferences->showTokenAsDot'] = $userPrefs['showTokenAsDot'];
$this->user['preferences->closeOtpOnCopy'] = $userPrefs['closeOtpOnCopy'];
$this->user['preferences->copyOtpOnDisplay'] = $userPrefs['copyOtpOnDisplay'];
$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'];
$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')
->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')
->json('GET', '/api/v1/user/preferences/showTokenAsDot')
2021-11-22 01:09:54 +01:00
->assertOk()
->assertExactJson([
2023-03-10 22:59:46 +01:00
'key' => 'showTokenAsDot',
'value' => config('2fauth.preferences.showTokenAsDot'),
]);
}
/**
* @test
*/
public function test_showPreference_returns_preference_with_custom_value()
{
2023-03-10 22:59:46 +01:00
$showTokenAsDot = ! config('2fauth.preferences.showTokenAsDot');
$this->user['preferences->showTokenAsDot'] = $showTokenAsDot;
$this->user->save();
$response = $this->actingAs($this->user, 'api-guard')
->json('GET', '/api/v1/user/preferences/showTokenAsDot')
->assertJsonFragment([
2023-03-10 22:59:46 +01:00
'key' => 'showTokenAsDot',
'value' => $showTokenAsDot,
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();
$showTokenAsDot = ! config('2fauth.preferences.showTokenAsDot');
$response = $this->actingAs($this->user, 'api-guard')
->json('PUT', '/api/v1/user/preferences/showTokenAsDot', [
'key' => 'showTokenAsDot',
'value' => $showTokenAsDot,
])
->assertCreated()
->assertExactJson([
2023-03-10 22:59:46 +01:00
'key' => 'showTokenAsDot',
'value' => $showTokenAsDot,
]);
}
/**
* @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', [
'key' => 'showTokenAsDot',
'value' => true,
])
->assertNotFound();
}
/**
* @test
*/
public function test_setPreference_with_invalid_data_returns_validation_error()
{
$response = $this->actingAs($this->user, 'api-guard')
->json('PUT', '/api/v1/user/preferences/showTokenAsDot', [
'key' => 'showTokenAsDot',
'value' => null,
])
->assertStatus(422);
}
2022-11-22 15:15:52 +01:00
}