Commit 548347a2 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

add some basic me routes

parent 5b7b0f2b
Pipeline #2422 passed with stages
in 1 minute and 46 seconds
<?php
namespace App\Http\Controllers;
use App\Guards\ApiGuard;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Contracts\Auth\Guard;
use Source\Authorization\Authorizer;
use Source\Exceptions\AuthenticationException;
use Source\UseCases\Users\GetUser\GetUserUseCase;
use Source\UseCases\GroupUser\GetUserGroups\GetUserGroupsUseCase;
use Source\UseCases\Users\GetUser\APIPresenter as GetUserAPIPresenter;
use Source\UseCases\GroupUser\GetUserGroups\APIPresenter as GetUserGroupsAPIPresenter;
class MeController extends ApiController
{
/**
* @var \Illuminate\Contracts\Auth\Guard
*/
protected Guard $guard;
protected string $userId;
/**
* @param \Illuminate\Http\Request $request
* @param \Source\Authorization\Authorizer $authorizer
* @param \App\Guards\ApiGuard $guard
* @throws \Source\Exceptions\AuthenticationException
*/
public function __construct(Request $request, Authorizer $authorizer, ApiGuard $guard)
{
parent::__construct($request, $authorizer);
$this->guard = $guard;
$userId = $this->guard->id();
if (!$userId) {
throw new AuthenticationException();
}
$this->userId = $userId;
}
/**
* @param \Source\UseCases\Users\GetUser\GetUserUseCase $useCase
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function index(GetUserUseCase $useCase): JsonResponse
{
$presenter = new GetUserAPIPresenter();
$useCase->get($this->userId, $presenter);
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\GroupUser\GetUserGroups\GetUserGroupsUseCase $useCase
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function groups(GetUserGroupsUseCase $useCase): JsonResponse
{
$presenter = new GetUserGroupsAPIPresenter();
$useCase->getGroupsForUser($this->userId, $presenter);
return $this->respondWithData($presenter->getViewModel());
}
}
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MeController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\UsersController;
use App\Http\Controllers\GroupsController;
......@@ -48,7 +48,10 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::get('{groupId}/users', [GroupsController::class, 'getUsersForGroup']);
});
Route::get('/user', static function (Request $request) {
return $request->user();
Route::group([
'prefix' => 'me',
], static function () {
Route::get('/', [MeController::class, 'index']);
Route::get('/groups', [MeController::class, 'groups']);
});
});
<?php
namespace Tests\Feature\Api\Me;
use Illuminate\Foundation\Testing\TestResponse;
use Tests\Feature\AuthenticatesWithApplicationTestCase;
class GetCurrentUserApiTest extends AuthenticatesWithApplicationTestCase
{
protected TestResponse $response;
protected function handleTest(): void
{
$this->response = $this->getJson('/me?api_token=' . $this->authToken);
}
/**
* @test
*/
public function it_requires_a_logged_in_user(): void
{
$this->handleTest();
$this->response->assertStatus(401);
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_gets_the_logged_in_user(): void
{
$this->authenticate();
$this->handleTest();
$this->response->assertStatus(200);
$this->response->assertJson(['user' => [
'id' => $this->authUser->getId()
]]);
}
}
<?php
namespace Tests\Feature\Api\Me;
use Source\Entities\Group;
use Source\Gateways\Groups\GroupsRepository;
use Illuminate\Foundation\Testing\TestResponse;
use Source\Gateways\GroupUser\GroupUserRepository;
use Source\Gateways\Groups\InMemoryGroupsRepository;
use Tests\Feature\AuthenticatesWithApplicationTestCase;
use Source\Gateways\GroupUser\InMemoryGroupUserRepository;
class GetCurrentUserGroupsApiTest extends AuthenticatesWithApplicationTestCase
{
protected TestResponse $response;
protected InMemoryGroupUserRepository $repository;
protected InMemoryGroupsRepository $groups;
public function setUp(): void
{
parent::setUp();
$this->groups = $this->app->make(GroupsRepository::class);
$this->repository = $this->app->make(GroupUserRepository::class);
}
protected function handleTest(): void
{
$this->response = $this->getJson('/me/groups?api_token=' . $this->authToken);
}
/**
* @test
*/
public function it_requires_a_logged_in_user(): void
{
$this->handleTest();
$this->response->assertStatus(401);
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_gets_the_logged_in_user_groups(): void
{
$this->authenticate();
$group = $this->groups->create(new Group(0, 'title', 'descriptions'));
$this->repository->addUserToGroup($this->authUser->getId(), $group->getId());
$this->handleTest();
$this->response->assertStatus(200);
$this->response->assertJson(['groups' => [[
'id' => $group->getId()
]]]);
}
}
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