Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Access Info endpoint #35397

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"prestashop/gsitemap": "^4",
"prestashop/pagesnotfound": "^2",
"prestashop/productcomments": "^6.0",
"prestashop/ps_apiresources": "dev-fix/protected-tests",
"prestashop/ps_apiresources": "dev-product-context",
"prestashop/ps_banner": "^2",
"prestashop/ps_bestsellers": "^1.0",
"prestashop/ps_brandlist": "^1.0",
Expand Down Expand Up @@ -253,7 +253,7 @@
"repositories": {
"ps_apiresources": {
"type": "vcs",
"url": "https://github.com/M0rgan01/ps_apiresources.git"
"url": "https://github.com/jolelievre/ps_apiresources.git"
}
},
"minimum-stability": "dev",
Expand Down
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 11 additions & 6 deletions src/PrestaShopBundle/ApiPlatform/ContextParametersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,31 @@

namespace PrestaShopBundle\ApiPlatform;

use PrestaShop\PrestaShop\Core\Context\ApiClientContext;
use PrestaShop\PrestaShop\Core\Context\LanguageContext;
use PrestaShop\PrestaShop\Core\Context\ShopContext;

trait ContextParametersTrait
{
protected readonly ShopContext $shopContext;
protected readonly LanguageContext $languageContext;
protected readonly ApiClientContext $apiClientContext;

protected function getContextParameters(): array
{
$shopConstraint = $this->shopContext->getShopConstraint();

return [
'shopConstraint' => [
'shopId' => $shopConstraint->getShopId()?->getValue(),
'shopGroupId' => $shopConstraint->getShopGroupId()?->getValue(),
'isStrict' => $shopConstraint->isStrict(),
'_context' => [
'shopConstraint' => [
'shopId' => $shopConstraint->getShopId()?->getValue(),
'shopGroupId' => $shopConstraint->getShopGroupId()?->getValue(),
'isStrict' => $shopConstraint->isStrict(),
],
'shopId' => $this->shopContext->getId(),
'langId' => $this->languageContext->getId(),
'apiClientId' => $this->apiClientContext->getApiClient()?->getId(),
],
'shopId' => $this->shopContext->getId(),
'langId' => $this->languageContext->getId(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
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 $commandBus,
protected readonly DomainSerializer $domainSerializer,
protected readonly ShopContext $shopContext,
protected readonly LanguageContext $languageContext
protected readonly LanguageContext $languageContext,
protected readonly ApiClientContext $apiClientContext,
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use PrestaShop\PrestaShop\Core\Context\ApiClientContext;
use PrestaShop\PrestaShop\Core\Context\LanguageContext;
use PrestaShop\PrestaShop\Core\Context\ShopContext;
use PrestaShop\PrestaShop\Core\Exception\TypeException;
Expand All @@ -49,13 +50,15 @@ class QueryListProvider implements ProviderInterface
{
use QueryResultSerializerTrait;
use ContextParametersTrait;

public const DEFAULT_PAGINATED_ITEM_LIMIT = 50;

public function __construct(
protected readonly DomainSerializer $domainSerializer,
protected readonly ContainerInterface $container,
protected readonly ShopContext $shopContext,
protected readonly LanguageContext $languageContext
protected readonly LanguageContext $languageContext,
protected readonly ApiClientContext $apiClientContext,
) {
}

Expand Down
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
64 changes: 64 additions & 0 deletions src/PrestaShopBundle/ApiPlatform/Resources/ApiClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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\ApiClient\Query\GetApiClientForEditing;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet;

#[ApiResource(
operations: [
new CQRSGet(
uriTemplate: '/api-client/infos',
CQRSQuery: GetApiClientForEditing::class,
scopes: [],
CQRSQueryMapping: [
'[_context][apiClientId]' => '[apiClientId]',
],
),
],
)]
class ApiClient
{
#[ApiProperty(identifier: true)]
public int $apiClientId;

public string $clientId;

public string $clientName;

public string $description;

public bool $enabled;

public int $lifetime;

public array $scopes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?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 ApiClientEndpointTest extends ApiTestCase
{
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
self::createApiClient();
}

/**
* @dataProvider getProtectedEndpoints
*
* @param string $method
* @param string $uri
*/
public function testProtectedEndpoints(string $method, string $uri): void
{
$response = static::createClient()->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-client/infos',
];
}

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

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

$this->assertEquals(
[
'apiClientId' => 1,
'clientId' => self::CLIENT_ID,
'clientName' => self::CLIENT_NAME,
'description' => '',
'enabled' => true,
'lifetime' => 10000,
'scopes' => [],
],
$decodedResponse
);
}
}
3 changes: 2 additions & 1 deletion tests/Resources/ApiPlatform/Resources/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class ApiTest
public array $descriptions;

public const QUERY_MAPPING = [
'[langId]' => '[displayLanguageId]',
'[_context][shopConstraint]' => '[shopConstraint]',
'[_context][langId]' => '[displayLanguageId]',
'[basicInformation][localizedNames]' => '[names]',
'[basicInformation][localizedDescriptions]' => '[descriptions]',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use PrestaShop\Module\APIResources\ApiPlatform\Resources\FoundProduct as FoundProductDto;
use PrestaShop\Module\APIResources\ApiPlatform\Resources\Hook;
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 PrestaShop\PrestaShop\Core\Domain\Hook\Query\GetHook;
Expand Down Expand Up @@ -66,6 +67,8 @@ class QueryProviderTest extends TestCase

private ShopContext $shopContext;

private ApiClientContext $apiClientContext;

/**
* Set up dependencies for HookStatusProvider
*/
Expand Down Expand Up @@ -93,11 +96,13 @@ public function setUp(): void
$this->shopContext = $this->createMock(ShopContext::class);
$this->shopContext->method('getShopConstraint')->willReturn(ShopConstraint::shop(1));
$this->shopContext->method('getId')->willReturn(1);
$this->apiClientContext = $this->createMock(ApiClientContext::class);
$this->apiClientContext->method('getApiClient')->willReturn(null);
}

public function testProvideHookStatus(): void
{
$hookStatusProvider = new QueryProvider($this->queryBus, $this->serializer, $this->shopContext, $this->languageContext);
$hookStatusProvider = new QueryProvider($this->queryBus, $this->serializer, $this->shopContext, $this->languageContext, $this->apiClientContext);
$get = new Get();
$get = $get
->withExtraProperties(['CQRSQuery' => GetHookStatus::class])
Expand All @@ -112,7 +117,7 @@ public function testProvideHookStatus(): void

public function testProvideHook(): void
{
$hookStatusProvider = new QueryProvider($this->queryBus, $this->serializer, $this->shopContext, $this->languageContext);
$hookStatusProvider = new QueryProvider($this->queryBus, $this->serializer, $this->shopContext, $this->languageContext, $this->apiClientContext);
$get = new Get();
$get = $get
->withExtraProperties(['CQRSQuery' => GetHook::class])
Expand All @@ -133,22 +138,22 @@ public function testProvideHook(): void

public function testSearchProduct(): void
{
$searchProductProvider = new QueryProvider($this->queryBus, $this->serializer, $this->shopContext, $this->languageContext);
$searchProductProvider = new QueryProvider($this->queryBus, $this->serializer, $this->shopContext, $this->languageContext, $this->apiClientContext);
$get = new GetCollection();
$get = $get
->withExtraProperties(['CQRSQuery' => SearchProducts::class])
->withClass(FoundProductDto::class);
$searchProducts = $searchProductProvider->provide($get, ['phrase' => 'mug', 'resultsLimit' => 10, 'isoCode' => 'EUR']);
self::assertCount(1, $searchProducts);

$searchProductProvider = new QueryProvider($this->queryBus, $this->serializer, $this->shopContext, $this->languageContext);
$searchProductProvider = new QueryProvider($this->queryBus, $this->serializer, $this->shopContext, $this->languageContext, $this->apiClientContext);
$searchProducts = $searchProductProvider->provide($get, ['phrase' => 'search with order id', 'resultsLimit' => 10, 'isoCode' => 'EUR'], ['filters' => ['orderId' => 1]]);
self::assertCount(0, $searchProducts);
}

public function testProvideNoQueryThrowsException(): void
{
$hookStatusProvider = new QueryProvider($this->queryBus, $this->serializer, $this->shopContext, $this->languageContext);
$hookStatusProvider = new QueryProvider($this->queryBus, $this->serializer, $this->shopContext, $this->languageContext, $this->apiClientContext);
$get = new Get();

$this->expectException(CQRSQueryNotFoundException::class);
Expand Down