Commit 22f5ae90 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Add API route for getting groups on any user

parent b1add371
Pipeline #2415 passed with stages
in 1 minute and 49 seconds
......@@ -11,6 +11,7 @@ use Source\UseCases\Users\CreateUser\CreateUserUseCase;
use Source\UseCases\Users\DeleteUser\DeleteUserUseCase;
use Source\UseCases\Users\UpdateUser\UpdateUserUseCase;
use Source\UseCases\Users\GetAllUsers\GetAllUsersUseCase;
use Source\UseCases\GroupUser\GetUserGroups\GetUserGroupsUseCase;
use Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupUseCase;
use Source\UseCases\Users\GetUser\APIPresenter as GetUserAPIPresenter;
use Source\UseCases\Users\GetAllUsers\APIPresenter as AllUsersAPIPresenter;
......@@ -18,6 +19,7 @@ use Source\UseCases\Users\CreateUser\APIPresenter as CreateUserAPIPresenter;
use Source\UseCases\Users\DeleteUser\APIPresenter as DeleteUserAPIPresenter;
use Source\UseCases\Users\UpdateUser\APIPresenter as UpdateUserAPIPresenter;
use Source\UseCases\GroupUser\RemoveUserFromGroup\RemoveUserFromGroupUseCase;
use Source\UseCases\GroupUser\GetUserGroups\APIPresenter as GetUserGroupsAPIPresenter;
use Source\UseCases\GroupUser\AddUserToGroup\APIPresenter as AddUserToGroupAPIPresenter;
use Source\UseCases\GroupUser\RemoveUserFromGroup\APIPresenter as RemoveUserFromGroupAPIPresenter;
......@@ -182,4 +184,22 @@ class UsersController extends ApiController
return $this->respondWithMessage($presenter->getViewModel()['message']);
}
/**
* @param \Source\UseCases\GroupUser\GetUserGroups\GetUserGroupsUseCase $useCase
* @param string $userId
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function getGroupsForUser(GetUserGroupsUseCase $useCase, string $userId): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_USERS]);
$presenter = new GetUserGroupsAPIPresenter();
$useCase->getGroupsForUser($userId, $presenter);
return $this->respondWithData($presenter->getViewModel());
}
}
......@@ -21,6 +21,7 @@ use Source\UseCases\Groups\DeleteGroup\DeleteGroupUseCaseServiceProvider;
use Source\UseCases\Groups\UpdateGroup\UpdateGroupUseCaseServiceProvider;
use Source\UseCases\Token\Authenticate\AuthenticateUseCaseServiceProvider;
use Source\UseCases\Groups\GetAllGroups\GetAllGroupsUseCaseServiceProvider;
use Source\UseCases\GroupUser\GetUserGroups\GetUserGroupsUseCaseServiceProvider;
use Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupUseCaseServiceProvider;
use Source\UseCases\GroupUser\RemoveUserFromGroup\RemoveUserFromGroupUseCaseServiceProvider;
use Source\UseCases\Doors\Authenticate\AuthenticateUseCaseServiceProvider as DoorAuthenticateUseCaseServiceProvider;
......@@ -60,6 +61,7 @@ class AppServiceProvider extends ServiceProvider
GetAllGroupsUseCaseServiceProvider::class,
// GroupUser
GetUserGroupsUseCaseServiceProvider::class,
AddUserToGroupUseCaseServiceProvider::class,
RemoveUserFromGroupUseCaseServiceProvider::class,
......
......@@ -30,6 +30,8 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::put('{userId}', [UsersController::class, 'update']);
Route::delete('{userId}', [UsersController::class, 'delete']);
Route::get('{userId}/groups', [UsersController::class, 'getGroupsForUser']);
Route::post('{userId}/group/{groupId}', [UsersController::class, 'addUserToGroup']);
Route::delete('{userId}/group/{groupId}', [UsersController::class, 'removeUserFromGroup']);
});
......
......@@ -3,10 +3,10 @@
namespace Source\UseCases\GroupUser\GetUserGroups;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Foundation\Application;
use Source\Gateways\GroupUser\GroupUserRepository;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
/**
* Service provider must be registered in AppServiceProvider
......
<?php
namespace Tests\Feature\Api\Users;
use Source\Entities\User;
use Source\Entities\Group;
use Source\Gateways\Groups\GroupsRepository;
use Illuminate\Foundation\Testing\TestResponse;
use Source\Gateways\GroupUser\GroupUserRepository;
use Tests\Feature\AuthenticatesWithApplicationTestCase;
class GetGroupsForUserApiTest extends AuthenticatesWithApplicationTestCase
{
protected TestResponse $response;
protected GroupsRepository $groups;
protected GroupUserRepository $repository;
public function setUp(): void
{
parent::setUp();
$this->groups = $this->app->make(GroupsRepository::class);
$this->repository = $this->app->make(GroupUserRepository::class);
}
public function handleTest(string $userId): void
{
$this->response = $this->getJson("/users/$userId/groups?api_token={$this->authToken}");
}
/**
* @test
*/
public function it_denies_unauthorized(): void
{
$this->handleTest('uid');
$this->response->assertStatus(401);
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_protects_the_route(): void
{
$this->authorize(false);
$this->handleTest('uid');
$this->response->assertStatus(403);
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_adds_a_group_to_a_user(): void
{
$this->authenticate();
$user = $this->usersRepository->create(new User(0, '', '', '', '', '', '', ''));
$group = $this->groups->create(new Group(0, '', ''));
$this->repository->addUserToGroup($user->getId(), $group->getId());
$this->handleTest($user->getId());
$this->response->assertStatus(200);
$this->assertCount(1, $this->response->json('groups'));
}
}
......@@ -13,8 +13,8 @@ use Source\Gateways\GroupUser\GroupUserRepository;
use Source\Gateways\Users\InMemoryUsersRepository;
use Source\Gateways\Groups\InMemoryGroupsRepository;
use Source\Gateways\GroupUser\InMemoryGroupUserRepository;
use Source\UseCases\GroupUser\GetUserGroups\ResponseModel;
use Source\UseCases\GroupUser\GetUserGroups\GetUserGroups;
use Source\UseCases\GroupUser\GetUserGroups\ResponseModel;
class UseCaseTest extends TestCase
{
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment