mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-08-09 13:55:01 +02:00
Update & Complete API controllers tests and Unit tests
This commit is contained in:
@ -12,10 +12,15 @@ use Tests\FeatureTestCase;
|
||||
class UserControllerTest extends FeatureTestCase
|
||||
{
|
||||
/**
|
||||
* @var \App\Models\User
|
||||
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
private const PREFERENCE_JSON_STRUCTURE = [
|
||||
'key',
|
||||
'value',
|
||||
];
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
@ -35,38 +40,196 @@ class UserControllerTest extends FeatureTestCase
|
||||
->json('GET', '/api/v1/user')
|
||||
->assertOk()
|
||||
->assertExactJson([
|
||||
'name' => $this->user->name,
|
||||
'id' => $this->user->id,
|
||||
'email' => $this->user->email,
|
||||
'name' => $this->user->name,
|
||||
'id' => $this->user->id,
|
||||
'email' => $this->user->email,
|
||||
'is_admin' => $this->user->is_admin,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function test_show_existing_user_when_anonymous_returns_success()
|
||||
public function test_allPreferences_returns_consistent_json_structure()
|
||||
{
|
||||
$response = $this->json('GET', '/api/v1/user/name')
|
||||
$response = $this->actingAs($this->user, 'api-guard')
|
||||
->json('GET', '/api/v1/user/preferences')
|
||||
->assertOk()
|
||||
->assertExactJson([
|
||||
'name' => $this->user->name,
|
||||
->assertJsonStructure([
|
||||
'*' => self::PREFERENCE_JSON_STRUCTURE,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function test_show_missing_user_returns_success_with_null_name()
|
||||
public function test_allPreferences_returns_preferences_with_default_values()
|
||||
{
|
||||
User::destroy($this->user->id);
|
||||
$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([
|
||||
'key' => $pref,
|
||||
'value' => $value,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function test_allPreferences_returns_preferences_with_user_values()
|
||||
{
|
||||
$userPrefs = [
|
||||
'showTokenAsDot' => true,
|
||||
'closeOtpOnCopy' => true,
|
||||
'copyOtpOnDisplay' => true,
|
||||
'useBasicQrcodeReader' => true,
|
||||
'displayMode' => 'grid',
|
||||
'showAccountsIcons' => false,
|
||||
'kickUserAfter' => 5,
|
||||
'activeGroup' => 1,
|
||||
'rememberActiveGroup' => false,
|
||||
'defaultGroup' => 1,
|
||||
'defaultCaptureMode' => 'advancedForm',
|
||||
'useDirectCapture' => true,
|
||||
'useWebauthnAsDefault' => true,
|
||||
'useWebauthnOnly' => true,
|
||||
'getOfficialIcons' => false,
|
||||
'theme' => 'dark',
|
||||
'formatPassword' => false,
|
||||
'formatPasswordBy' => 1,
|
||||
'lang' => 'fr',
|
||||
];
|
||||
|
||||
$this->user['preferences->showTokenAsDot'] = $userPrefs['showTokenAsDot'];
|
||||
$this->user['preferences->closeOtpOnCopy'] = $userPrefs['closeOtpOnCopy'];
|
||||
$this->user['preferences->copyOtpOnDisplay'] = $userPrefs['copyOtpOnDisplay'];
|
||||
$this->user['preferences->useBasicQrcodeReader'] = $userPrefs['useBasicQrcodeReader'];
|
||||
$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->useWebauthnAsDefault'] = $userPrefs['useWebauthnAsDefault'];
|
||||
$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();
|
||||
|
||||
$response = $this->actingAs($this->user, 'api-guard')
|
||||
->json('GET', '/api/v1/user')
|
||||
->json('GET', '/api/v1/user/preferences')
|
||||
->assertJsonCount(count(config('2fauth.preferences')), $key = null);
|
||||
|
||||
foreach ($userPrefs as $pref => $value) {
|
||||
$response->assertJsonFragment([
|
||||
'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')
|
||||
->assertOk()
|
||||
->assertExactJson([
|
||||
'name' => $this->user->name,
|
||||
'id' => $this->user->id,
|
||||
'email' => $this->user->email,
|
||||
'key' => 'showTokenAsDot',
|
||||
'value' => config('2fauth.preferences.showTokenAsDot'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function test_showPreference_returns_preference_with_custom_value()
|
||||
{
|
||||
$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([
|
||||
'key' => 'showTokenAsDot',
|
||||
'value' => $showTokenAsDot,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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([
|
||||
'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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user