Skip to content

Commit

Permalink
GetMe
Browse files Browse the repository at this point in the history
  • Loading branch information
KoukiFactori committed Feb 8, 2023
1 parent 068b61a commit 75e0283
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Put;
use App\Controller\GetAvatarController;
use App\Controller\GetMeController;
use App\Repository\UserRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
Expand All @@ -24,7 +26,7 @@
],
controller: GetAvatarController::class,
openapiContext: [
'summary' => 'Retrieve a User avatar',
'summary' => 'Retrieves a User avatar',
'responses' => [
'200' => [
'description' => 'User avatar',
Expand All @@ -40,6 +42,25 @@
],
]
)]
#[GetCollection(
controller: GetMeController::class,
paginationEnabled: false,
security: 'is_granted("ROLE_USER")',
normalizationContext: ['groups' => ['get_User', 'get_Me']],
uriTemplate: '/me',
openapiContext: [
'summary' => 'Retrieves the connected user',
'description' => 'Retrieves the current connected user',
'responses' => [
'200' => [
'description' => 'User!!!!',
],
'400' => [
'description' => 'Throw an error when not connected',
],
],
]
)]
#[Put(denormalizationContext: ['groups' => ['set_User']], security: "is_granted('ROLE_USER') && object == user")]
#[Patch(denormalizationContext: ['groups' => ['set_User']], security: "is_granted('ROLE_USER') && object == user")]
class User implements UserInterface, PasswordAuthenticatedUserInterface
Expand Down Expand Up @@ -76,7 +97,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
private $avatar = null;

#[ORM\Column(length: 100)]
#[Groups(['set_User'])]
#[Groups(['get_Me', 'set_User'])]
private ?string $mail = null;

public function getId(): ?int
Expand Down
52 changes: 52 additions & 0 deletions tests/Api/User/UserGetMeCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Tests\Api\User;

use App\Entity\User;
use App\Factory\UserFactory;
use App\Tests\Support\ApiTester;
use Codeception\Util\HttpCode;

class UserGetMeCest
{
protected static function expectedProperties(): array
{
return [
'id' => 'integer',
'login' => 'string',
'firstname' => 'string',
'lastname' => 'string',
'mail' => 'string:email',
];
}

public function anonymousMeIsUnauthorized(ApiTester $I): void
{
// 1. 'Arrange'
UserFactory::createOne();

// 2. 'Act'
$I->sendGet('/api/me');

// 3. 'Assert'
$I->seeResponseCodeIs(HttpCode::UNAUTHORIZED);
}

public function authenticatedUserOnMeGetData(ApiTester $I): void
{
// 1. 'Arrange'
/** @var $user User */
$user = UserFactory::createOne()->object();
UserFactory::createOne();
$I->amLoggedInAs($user);

// 2. 'Act'
$I->sendGet('/api/me');

// 3. 'Assert'
$I->seeResponseCodeIsSuccessful();
$I->seeResponseIsJson();
$I->seeResponseIsAnEntity(User::class, '/api/me');
$I->seeResponseIsAnItem(self::expectedProperties(), ['login' => $user->getLogin()]);
}
}

0 comments on commit 75e0283

Please sign in to comment.