Commit 509c85b1 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

got group adding done

parent 06cdb4d0
Pipeline #2225 passed with stages
in 1 minute and 52 seconds
......@@ -11,11 +11,15 @@ 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\AddUserToGroup\AddUserToGroupUseCase;
use Source\UseCases\Users\GetUser\APIPresenter as GetUserAPIPresenter;
use Source\UseCases\Users\GetAllUsers\APIPresenter as AllUsersAPIPresenter;
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\AddUserToGroup\APIPresenter as AddUserToGroupAPIPresenter;
use Source\UseCases\GroupUser\RemoveUserFromGroup\APIPresenter as RemoveUserFromGroupAPIPresenter;
class UsersController extends ApiController
{
......@@ -139,4 +143,42 @@ class UsersController extends ApiController
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupUseCase $useCase
* @param string $userId
* @param string $groupId
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function addUserToGroup(AddUserToGroupUseCase $useCase, string $userId, string $groupId): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_USERS]);
$presenter = new AddUserToGroupAPIPresenter();
$useCase->addUserToGroup($userId, $groupId, $presenter);
return $this->respondWithMessage($presenter->getViewModel()['message']);
}
/**
* @param \Source\UseCases\GroupUser\RemoveUserFromGroup\RemoveUserFromGroupUseCase $useCase
* @param string $userId
* @param string $groupId
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function removeUserFromGroup(RemoveUserFromGroupUseCase $useCase, string $userId, string $groupId): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_USERS]);
$presenter = new RemoveUserFromGroupAPIPresenter();
$useCase->removeUserFromGroup($userId, $groupId, $presenter);
return $this->respondWithMessage($presenter->getViewModel()['message']);
}
}
......@@ -21,6 +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\AddUserToGroup\AddUserToGroupUseCaseServiceProvider;
use Source\UseCases\GroupUser\RemoveUserFromGroup\RemoveUserFromGroupUseCaseServiceProvider;
use Source\UseCases\Doors\Authenticate\AuthenticateUseCaseServiceProvider as DoorAuthenticateUseCaseServiceProvider;
use Source\UseCases\Users\Authenticate\AuthenticateUseCaseServiceProvider as UserAuthenticateUseCaseServiceProvider;
......@@ -57,6 +59,10 @@ class AppServiceProvider extends ServiceProvider
UpdateGroupUseCaseServiceProvider::class,
GetAllGroupsUseCaseServiceProvider::class,
// GroupUser
AddUserToGroupUseCaseServiceProvider::class,
RemoveUserFromGroupUseCaseServiceProvider::class,
// Doors
AuthenticateUseCaseServiceProvider::class,
DoorAuthenticateUseCaseServiceProvider::class,
......
......@@ -29,6 +29,9 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::get('{userId}', [UsersController::class, 'get']);
Route::put('{userId}', [UsersController::class, 'update']);
Route::delete('{userId}', [UsersController::class, 'delete']);
Route::post('{userId}/group/{groupId}', [UsersController::class, 'addUserToGroup']);
Route::delete('{userId}/group/{groupId}', [UsersController::class, 'removeUserFromGroup']);
});
Route::group([
......
......@@ -4,12 +4,12 @@
namespace Source\Gateways\GroupUser;
use Illuminate\Support\ServiceProvider;
use Source\Gateways\Users\UsersRepository;
use Source\Gateways\Groups\GroupsRepository;
use Source\Gateways\Users\LocalUsersRepository;
use Illuminate\Contracts\Foundation\Application;
use Source\Gateways\Groups\LocalGroupsRepository;
use Source\Gateways\Users\InMemoryUsersRepository;
use Illuminate\Contracts\Support\DeferrableProvider;
use Source\Gateways\Groups\InMemoryGroupsRepository;
/**
* Service provider must be registered in AppServiceProvider
......@@ -33,8 +33,8 @@ class GroupUserRepositoryServiceProvider extends ServiceProvider implements Defe
if (env('APP_ENV') === 'testing') {
return new InMemoryGroupUserRepository(
new InMemoryUsersRepository(),
new InMemoryGroupsRepository()
$app->make(UsersRepository::class),
$app->make(GroupsRepository::class)
);
}
......
<?php
namespace Source\UseCases\GroupUser\AddUserToGroup;
use Source\UseCases\BasePresenter;
class APIPresenter extends BasePresenter implements Presenter
{
protected array $viewModel = [];
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['message'] = $responseModel->getMessage();
}
/** @inheritDoc */
public function getViewModel(): array
{
return $this->viewModel;
}
}
<?php
namespace Source\UseCases\GroupUser\AddUserToGroup;
use Source\Gateways\GroupUser\GroupUserRepository;
class AddUserToGroup implements AddUserToGroupUseCase
{
/**
* @var \Source\Gateways\GroupUser\GroupUserRepository
*/
protected GroupUserRepository $repository;
public function __construct(GroupUserRepository $repository)
{
$this->repository = $repository;
}
/**
* @inheritDoc
*/
public function addUserToGroup(string $userId, string $groupId, Presenter $presenter): void
{
$this->repository->addUserToGroup($userId, $groupId);
$responseModel = new ResponseModel('Success');
$presenter->present($responseModel);
}
}
<?php
namespace Source\UseCases\GroupUser\AddUserToGroup;
interface AddUserToGroupUseCase
{
/**
* @param string $userId
* @param string $groupId
* @param \Source\UseCases\GroupUser\AddUserToGroup\Presenter $presenter
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function addUserToGroup(string $userId, string $groupId, Presenter $presenter): void;
}
<?php
namespace Source\UseCases\GroupUser\AddUserToGroup;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Foundation\Application;
use Source\Gateways\GroupUser\GroupUserRepository;
use Illuminate\Contracts\Support\DeferrableProvider;
/**
* Service provider must be registered in AppServiceProvider
*/
class AddUserToGroupUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(AddUserToGroupUseCase::class, static function (Application $app) {
return new AddUserToGroup($app->make(GroupUserRepository::class));
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
}
/**
* @return array
*/
public function provides()
{
return [AddUserToGroupUseCase::class];
}
}
<?php
namespace Source\UseCases\GroupUser\AddUserToGroup;
interface Presenter
{
/**
* @param ResponseModel $responseModel
* @return void
*/
public function present(ResponseModel $responseModel): void;
/**
* @return array
*/
public function getViewModel(): array;
}
<?php
namespace Source\UseCases\GroupUser\AddUserToGroup;
class ResponseModel
{
/**
* @var string
*/
protected string $message;
public function __construct(string $message)
{
$this->message = $message;
}
public function getMessage(): string
{
return $this->message;
}
}
<?php
namespace Source\UseCases\GroupUser\RemoveUserFromGroup;
use Source\UseCases\BasePresenter;
class APIPresenter extends BasePresenter implements Presenter
{
protected array $viewModel = [];
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['message'] = $responseModel->getMessage();
}
/** @inheritDoc */
public function getViewModel(): array
{
return $this->viewModel;
}
}
<?php
namespace Source\UseCases\GroupUser\RemoveUserFromGroup;
interface Presenter
{
/**
* @param ResponseModel $responseModel
* @return void
*/
public function present(ResponseModel $responseModel): void;
/**
* @return array
*/
public function getViewModel(): array;
}
<?php
namespace Source\UseCases\GroupUser\RemoveUserFromGroup;
use Source\Gateways\GroupUser\GroupUserRepository;
class RemoveUserFromGroup implements RemoveUserFromGroupUseCase
{
/**
* @var \Source\Gateways\GroupUser\GroupUserRepository
*/
protected GroupUserRepository $repository;
public function __construct(GroupUserRepository $repository)
{
$this->repository = $repository;
}
/**
* @inheritDoc
*/
public function removeUserFromGroup(string $userId, string $groupId, Presenter $presenter): void
{
$this->repository->removeUserFromGroup($userId, $groupId);
$responseModel = new ResponseModel('Success');
$presenter->present($responseModel);
}
}
<?php
namespace Source\UseCases\GroupUser\RemoveUserFromGroup;
interface RemoveUserFromGroupUseCase
{
/**
* @param string $userId
* @param string $groupId
* @param \Source\UseCases\GroupUser\RemoveUserFromGroup\Presenter $presenter
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function removeUserFromGroup(string $userId, string $groupId, Presenter $presenter): void;
}
<?php
namespace Source\UseCases\GroupUser\RemoveUserFromGroup;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Foundation\Application;
use Source\Gateways\GroupUser\GroupUserRepository;
use Illuminate\Contracts\Support\DeferrableProvider;
/**
* Service provider must be registered in AppServiceProvider
*/
class RemoveUserFromGroupUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(RemoveUserFromGroupUseCase::class, static function (Application $app) {
return new RemoveUserFromGroup($app->make(GroupUserRepository::class));
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
}
/**
* @return array
*/
public function provides()
{
return [RemoveUserFromGroupUseCase::class];
}
}
<?php
namespace Source\UseCases\GroupUser\RemoveUserFromGroup;
class ResponseModel
{
/**
* @var string
*/
protected string $message;
public function __construct(string $message)
{
$this->message = $message;
}
public function getMessage(): string
{
return $this->message;
}
}
<?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 AddUserToGroupApiTest 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, string $groupId): void
{
$this->response = $this->postJson(
"/users/$userId/group/$groupId",
['api_token' => $this->authToken]
);
}
/**
* @test
*/
public function it_denies_unauthorized(): void
{
$this->handleTest('uid', 'gid');
$this->response->assertStatus(401);
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_protects_the_route(): void
{
$this->authorize(false);
$this->handleTest('uid', 'gid');
$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->handleTest($user->getId(), $group->getId());
$this->response->assertStatus(200);
$this->response->assertJson(['message' => 'Success']);
$this->assertEquals($group->getId(), $this->repository->getGroupsForUser($user->getId())[0]->getId());
}
}
......@@ -17,13 +17,10 @@ class CreateUserApiTest extends AuthenticatesWithApplicationTestCase
*/
protected function handleTest(array $data): void
{
$this->response = $this->postJson(
'/users',
array_merge(
['api_token' => $this->authToken],
$data
)
);
$this->response = $this->postJson('/users', array_merge(
['api_token' => $this->authToken],
$data
));
}
/**
......@@ -57,46 +54,46 @@ class CreateUserApiTest extends AuthenticatesWithApplicationTestCase
// Missing one of the required
[
[
'last_name' => $user->getLastName(),
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
'email' => $user->getEmail(),
'doorcode' => $user->getDoorcode(),
'email' => $user->getEmail(),
'doorcode' => $user->getDoorcode(),
],
['first_name' => ['The first name field is required.']],
],
[
[
'first_name' => $user->getFirstName(),
'first_name' => $user->getFirstName(),
'display_name' => $user->getDisplayName(),
'email' => $user->getEmail(),
'doorcode' => $user->getDoorcode(),
'email' => $user->getEmail(),
'doorcode' => $user->getDoorcode(),
],
['last_name' => ['The last name field is required.']],
],
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'email' => $user->getEmail(),
'doorcode' => $user->getDoorcode(),
'last_name' => $user->getLastName(),
'email' => $user->getEmail(),
'doorcode' => $user->getDoorcode(),
],
['display_name' => ['The display name field is required.']],
],
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),
'display_name' => $user->getDisplayName(),
'doorcode' => $user->getDoorcode(),
'doorcode' => $user->getDoorcode(),
],
['email' => ['The email field is required.']],
],
[
[
'first_name' => $user->getFirstName(),
'last_name' => $user->getLastName(),