Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/Services/OAuth2/ApiScopeGroupService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Models\OAuth2\Factories\ApiScopeGroupFactory;
use Auth\Repositories\IUserRepository;
use models\exceptions\EntityNotFoundException;
use models\exceptions\ValidationException;
use models\utils\IEntity;
use OAuth2\Exceptions\InvalidApiScopeGroup;
use OAuth2\Repositories\IApiScopeRepository;
Expand Down Expand Up @@ -100,7 +101,7 @@ public function update(int $id, array $payload):IEntity
$former_group = $this->repository->getByName($payload['name']);
if(!is_null($former_group) && $former_group->getId() != $id)
{
throw new InvalidApiScopeGroup(sprintf('there is already another api scope group name (%s).', $payload['name']));
throw new ValidationException(sprintf('there is already another api scope group name (%s).', $payload['name']));
}
}

Expand Down Expand Up @@ -144,7 +145,7 @@ public function create(array $payload):IEntity

if(!is_null($former_group))
{
throw new InvalidApiScopeGroup(sprintf('there is already another api scope group name (%s).', $name));
throw new ValidationException(sprintf('there is already another api scope group name (%s).', $name));
}
$group = ApiScopeGroupFactory::build($payload);
$scopes = $payload['scopes'];
Expand Down
67 changes: 67 additions & 0 deletions tests/unit/ApiScopeGroupServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php namespace Tests\unit;

/**
* Copyright 2026 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

use LaravelDoctrine\ORM\Facades\EntityManager;
use models\exceptions\ValidationException;
use Models\OAuth2\ApiScopeGroup;
use OAuth2\Services\IApiScopeGroupService;
use Tests\BrowserKitTestCase;

/**
* Class ApiScopeGroupServiceTest
* A duplicated group name must surface as a ValidationException (HTTP 412 in
* ApiScopeGroupController), not as an unhandled exception (HTTP 500).
* @package Tests\unit
*/
class ApiScopeGroupServiceTest extends BrowserKitTestCase
{
private function persistGroup(string $name): ApiScopeGroup
{
$group = new ApiScopeGroup();
$group->setName($name);
$group->setActive(true);
$group->setDescription('test description');
EntityManager::persist($group);
EntityManager::flush();
return $group;
}

public function testUpdateWithNameUsedByAnotherGroupThrowsValidationException()
{
$suffix = uniqid();
$this->persistGroup("group_a_$suffix");
$group_b = $this->persistGroup("group_b_$suffix");

$this->expectException(ValidationException::class);
$this->expectExceptionMessage("group_a_$suffix");

app(IApiScopeGroupService::class)->update($group_b->getId(), ['name' => "group_a_$suffix"]);
}

public function testCreateWithExistingNameThrowsValidationException()
{
$name = 'group_dup_' . uniqid();
$this->persistGroup($name);

$this->expectException(ValidationException::class);
$this->expectExceptionMessage($name);

app(IApiScopeGroupService::class)->create([
'name' => $name,
'scopes' => '',
'users' => '',
]);
}
}
Loading