Commit 9ed6e3f8 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Get api endpoint done for getting a groups users with it's tests

parent 1e919b34
Pipeline #2419 passed with stages
in 1 minute and 50 seconds
......@@ -10,11 +10,13 @@ use Source\UseCases\Groups\CreateGroup\CreateGroupUseCase;
use Source\UseCases\Groups\DeleteGroup\DeleteGroupUseCase;
use Source\UseCases\Groups\UpdateGroup\UpdateGroupUseCase;
use Source\UseCases\Groups\GetAllGroups\GetAllGroupsUseCase;
use Source\UseCases\GroupUser\GetGroupUsers\GetGroupUsersUseCase;
use Source\UseCases\Groups\GetGroup\APIPresenter as GetGroupAPIPresenter;
use Source\UseCases\Groups\GetAllGroups\APIPresenter as AllGroupsAPIPresenter;
use Source\UseCases\Groups\CreateGroup\APIPresenter as CreateGroupAPIPresenter;
use Source\UseCases\Groups\DeleteGroup\APIPresenter as DeleteGroupAPIPresenter;
use Source\UseCases\Groups\UpdateGroup\APIPresenter as UpdateGroupAPIPresenter;
use Source\UseCases\GroupUser\GetGroupUsers\APIPresenter as GetGroupUsersAPIPresenter;
class GroupsController extends ApiController
{
......@@ -126,4 +128,22 @@ class GroupsController extends ApiController
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\GroupUser\GetGroupUsers\GetGroupUsersUseCase $useCase
* @param string $groupId
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function getUsersForGroup(GetGroupUsersUseCase $useCase, string $groupId): JsonResponse
{
$this->authorizer->protectOne([Permissions::MANAGE_GROUPS, Permissions::MANAGE_USERS]);
$presenter = new GetGroupUsersAPIPresenter();
$useCase->getUsersForGroup($groupId, $presenter);
return $this->respondWithData($presenter->getViewModel());
}
}
......@@ -21,8 +21,8 @@ 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\GetGroupUsers\GetGroupUsersUseCaseServiceProvider;
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;
......
......@@ -44,6 +44,8 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::get('{groupId}', [GroupsController::class, 'get']);
Route::put('{groupId}', [GroupsController::class, 'update']);
Route::delete('{groupId}', [GroupsController::class, 'delete']);
Route::get('{groupId}/users', [GroupsController::class, 'getUsersForGroup']);
});
Route::get('/user', static function (Request $request) {
......
......@@ -3,10 +3,10 @@
namespace Source\UseCases\GroupUser\GetGroupUsers;
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
......
......@@ -2,7 +2,6 @@
namespace Source\UseCases\Users\GetAllUsers;
use Carbon\Carbon;
use Source\Entities\User;
use Source\UseCases\BasePresenter;
......
<?php
namespace Tests\Feature\Api\Groups;
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 GetUsersForGroupApiTest 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 $groupId): void
{
$this->response = $this->getJson("/groups/$groupId/users?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_gets_users_for_a_group(): 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($group->getId());
$this->response->assertStatus(200);
$this->assertCount(1, $this->response->json('users'));
}
}
......@@ -58,7 +58,7 @@ class GetGroupsForUserApiTest extends AuthenticatesWithApplicationTestCase
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_adds_a_group_to_a_user(): void
public function it_gets_groups_for_a_user(): void
{
$this->authenticate();
......
......@@ -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\GetGroupUsers\ResponseModel;
use Source\UseCases\GroupUser\GetGroupUsers\GetGroupUsers;
use Source\UseCases\GroupUser\GetGroupUsers\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