Skip to content

Commit

Permalink
Create APIAccess
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Pichat committed Feb 16, 2024
1 parent 4b1c346 commit dd1e691
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function getContextParameters(): array
],
'shopId' => $this->shopContext->getId(),
'langId' => $this->languageContext->getId(),
'apiClientId' => $this->apiClientContext->getApiClient()->getId(),
'apiClientId' => $this->apiClientContext->getApiClient() ? $this->apiClientContext->getApiClient()->getId() : null,
],
];
}
Expand Down
4 changes: 3 additions & 1 deletion src/PrestaShopBundle/ApiPlatform/Provider/QueryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use PrestaShop\PrestaShop\Core\CommandBus\CommandBusInterface;
use PrestaShop\PrestaShop\Core\Context\ApiClientContext;
use PrestaShop\PrestaShop\Core\Context\LanguageContext;
use PrestaShop\PrestaShop\Core\Context\ShopContext;
use PrestaShopBundle\ApiPlatform\ContextParametersTrait;
Expand All @@ -49,7 +50,8 @@ public function __construct(
protected readonly CommandBusInterface $queryBus,
protected readonly DomainSerializer $domainSerializer,
protected readonly ShopContext $shopContext,
protected readonly LanguageContext $languageContext
protected readonly LanguageContext $languageContext,
protected readonly ApiClientContext $apiClientContext,
) {
}

Expand Down
69 changes: 69 additions & 0 deletions src/PrestaShopBundle/ApiPlatform/Resources/ApiAccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace PrestaShopBundle\ApiPlatform\Resources;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\Query\GetApiAccessForEditing;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet;
use PrestaShopBundle\ApiPlatform\Provider\QueryProvider;

#[ApiResource(
operations: [
new CQRSGet(
uriTemplate: '/api-access/infos',
openapiContext: [
'summary' => 'Get current API Access details',
'description' => 'Get API Access public details only, sensitive information like secrets is not returned',
],
provider: QueryProvider::class,
CQRSQuery: GetApiAccessForEditing::class,
scopes: [],
CQRSQueryMapping: [
'[_context][apiClientId]' => '[apiAccessId]',
],
),
],
)]
class ApiAccess
{
#[ApiProperty(identifier: true)]
public int $apiAccessId;
public string $apiClientId;

public string $clientName;

public string $description;

public bool $enabled;

public int $lifetime;

public array $scopes;
}
90 changes: 90 additions & 0 deletions tests/Integration/ApiPlatform/EndPoint/ApiAccessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace Tests\Integration\ApiPlatform\EndPoint;

class ApiAccessTest extends ApiTestCase
{
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
self::createApiAccess();
}

/**
* @dataProvider getProtectedEndpoints
*
* @param string $method
* @param string $uri
*/
public function testProtectedEndpoints(string $method, string $uri): void
{
$client = static::createClient();
$response = $client->request($method, $uri);
self::assertResponseStatusCodeSame(401);

$content = $response->getContent(false);
$this->assertNotEmpty($content);
$this->assertEquals('No Authorization header provided', $content);
}

public function getProtectedEndpoints(): iterable
{
yield 'get endpoint' => [
'GET',
'/api/api-access/infos',
];
}

public function testGetInfos()
{
$bearerToken = $this->getBearerToken();
$client = static::createClient();
$response = $client->request('GET', '/api/api-access/infos', [
'auth_bearer' => $bearerToken,
]);
self::assertResponseStatusCodeSame(200);

$decodedResponse = json_decode($response->getContent(), true);
$this->assertNotFalse($decodedResponse);

$this->assertEquals(
[
'apiAccessId' => 1,
'apiClientId' => self::CLIENT_ID,
'clientName' => self::CLIENT_NAME,
'description' => '',
'enabled' => true,
'lifetime' => 10000,
'scopes' => [],
],
$decodedResponse
);
}
}

0 comments on commit dd1e691

Please sign in to comment.