From 9f908c3f7d41dc48b3dea3c91a64a3bda1a6d7d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Jaussoin?= Date: Thu, 13 Jun 2024 15:23:25 +0200 Subject: [PATCH] Fix FLEXIAPI-185 Return null if the account dictionary is empty in the API --- CHANGELOG.md | 1 + flexiapi/app/Account.php | 5 ++++- flexiapi/tests/Feature/ApiAccountTest.php | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a0f0cc..2446fc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ v1.5 ---- +- Fix FLEXIAPI-185 Return null if the account dictionary is empty in the API - Fix FLEXIAPI-184 Append phone_change_code and email_change_code to the admin /accounts/ endpoint if they are available - Fix FLEXIAPI-183 Complete the account hooks on the dictionnary actions - Fix FLEXIAPI-181 Replace APP_ADMINS_MANAGE_MULTI_DOMAINS with APP_SUPER_ADMINS_SIP_DOMAINS diff --git a/flexiapi/app/Account.php b/flexiapi/app/Account.php index ccb2494..6188c29 100644 --- a/flexiapi/app/Account.php +++ b/flexiapi/app/Account.php @@ -22,6 +22,7 @@ namespace App; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Collection; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Http\Request; use Illuminate\Support\Str; @@ -140,8 +141,10 @@ class Account extends Authenticatable return $this->hasMany(AccountDictionaryEntry::class); } - public function getDictionaryAttribute() + public function getDictionaryAttribute(): ?Collection { + if ($this->dictionaryEntries->isEmpty()) return null; + return $this->dictionaryEntries->keyBy('key')->map(function ($entry) { return $entry->value; }); diff --git a/flexiapi/tests/Feature/ApiAccountTest.php b/flexiapi/tests/Feature/ApiAccountTest.php index 840b2cb..02ee9ca 100644 --- a/flexiapi/tests/Feature/ApiAccountTest.php +++ b/flexiapi/tests/Feature/ApiAccountTest.php @@ -423,6 +423,23 @@ class ApiAccountTest extends TestCase $entryNewKey => $entryNewValue ] ]); + + // Clear + + $this->keyAuthenticated($admin) + ->json('PUT', $this->route . '/' . $accountId, [ + 'username' => 'john3', + 'password' => 'bar', + 'algorithm' => 'SHA-256', + 'dictionary' => [] + ]) + ->assertJson(['dictionary' => null]) + ->assertStatus(200); + + $this->keyAuthenticated($admin) + ->json('GET', $this->route . '/' . $accountId) + ->assertSee(['"dictionary":null'], false) + ->assertStatus(200); } public function testActivated()