Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
tleon committed Jan 19, 2024
1 parent a93ad67 commit 53311ac
Show file tree
Hide file tree
Showing 9 changed files with 573 additions and 0 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Integration tests
on:
push:
pull_request:

permissions:
contents: read # to clone the repos and get release assets (shivammathur/setup-php)

jobs:
integration:
permissions:
contents: read # to clone the repos and get release assets (shivammathur/setup-php)
name: Integration tests
runs-on: ubuntu-latest
strategy:
matrix:
php: [ '8.1', '8.2' ]
fail-fast: false
steps:
- name: Checkout module code
uses: actions/checkout@v3
with:
path: ps_apiresources
- uses: actions/checkout@v3
name: Checkout PrestaShop repository
with:
fetch-depth: 0
repository: PrestaShop/PrestaShop
path: prestashop
- name: Run ci.sh script
run: |
cd prestashop
USER_ID=$(id -u) GROUP_ID=$(id -g) PS_INSTALL_AUTO=0 docker-compose build --no-cache && docker-compose up -d --force-recreate
if [ $? -ne 0 ]; then
echo "docker install failed"
exit
fi
bash -l -c 'while [[ "$(curl -L -s -o /dev/null -w %{http_code} 'http://localhost:8001/install-dev/')" != "200" ]]; do echo "waiting for shop install"; sleep 5; done'
USER_ID=$(id -u) GROUP_ID=$(id -g) docker exec prestashop_prestashop-git_1 php bin/console prestashop:module uninstall ps_apiresources
rm -rf modules/ps_apiresources
mkdir -p modules/ps_apiresources
cp -r ../ps_apiresources/ modules/ps_apiresources
USER_ID=$(id -u) GROUP_ID=$(id -g) docker exec prestashop_prestashop-git_1 composer install --no-interaction --working-dir=/var/www/html/modules/ps_apiresources
USER_ID=$(id -u) GROUP_ID=$(id -g) docker exec prestashop_prestashop-git_1 composer create-test-db
USER_ID=$(id -u) GROUP_ID=$(id -g) docker exec prestashop_prestashop-git_1 vendor/bin/phpunit modules/ps_apiresources/tests/* -c modules/ps_apiresources/tests/Integration/phpunit.xml
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
},
"classmap": ["ps_apiresources.php"]
},
"autoload-dev": {
"psr-4": {
"PsApiResourcesTest\\": "tests/"
}
},
"config": {
"preferred-install": "dist",
"classmap-authoritative": true,
Expand Down
107 changes: 107 additions & 0 deletions src/ApiPlatform/Resources/Hook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?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 [email protected] 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 <[email protected]>
* @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 PrestaShop\Module\APIResources\ApiPlatform\Resources;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Put;
use PrestaShop\PrestaShop\Core\Domain\Hook\Command\UpdateHookStatusCommand;
use PrestaShop\PrestaShop\Core\Domain\Hook\Exception\HookNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Hook\Query\GetHook;
use PrestaShop\PrestaShop\Core\Domain\Hook\Query\GetHookStatus;
use PrestaShopBundle\ApiPlatform\Processor\CommandProcessor;
use PrestaShopBundle\ApiPlatform\Provider\QueryProvider;

#[ApiResource(
operations: [
new Get(
uriTemplate: '/hook-status/{id}',
requirements: ['id' => '\d+'],
openapiContext: [
'summary' => 'Get hook status A',
'description' => 'Get hook status B',
'parameters' => [
[
'name' => 'id',
'in' => 'path',
'required' => true,
'schema' => [
'type' => 'string',
],
'description' => 'Id of the hook you are requesting the status from',
],
[
'name' => 'Authorization',
'in' => 'scopes',
'description' => 'hook_read <br> hook_write ',
],
],
],
exceptionToStatus: [HookNotFoundException::class => 404],
provider: QueryProvider::class,
extraProperties: [
'CQRSQuery' => GetHookStatus::class,
'scopes' => ['hook_read'],
]
),
new Put(
uriTemplate: '/hook-status',
processor: CommandProcessor::class,
extraProperties: [
'CQRSCommand' => UpdateHookStatusCommand::class,
'scopes' => ['hook_write'],
]
),
new Get(
uriTemplate: '/hooks/{id}',
requirements: ['id' => '\d+'],
exceptionToStatus: [HookNotFoundException::class => 404],
provider: QueryProvider::class,
extraProperties: [
'CQRSQuery' => GetHook::class,
'scopes' => ['hook_read'],
]
),
],
)]
class Hook
{
#[ApiProperty(identifier: true)]
public int $id;

public bool $active;

public string $name;

public string $title;

public string $description;
}
100 changes: 100 additions & 0 deletions tests/Integration/ApiPlatform/ApiTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?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 [email protected] 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 <[email protected]>
* @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 PsApiResourcesTest\Integration\ApiPlatform;

use ApiPlatform\Symfony\Bundle\Test\Client;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\Command\AddApiAccessCommand;
use Tests\Resources\DatabaseDump;

abstract class ApiTestCase extends \ApiPlatform\Symfony\Bundle\Test\ApiTestCase
{
protected const CLIENT_ID = 'test_client_id';
protected const CLIENT_NAME = 'test_client_name';

protected static ?string $clientSecret = null;

public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
DatabaseDump::restoreTables(['api_access']);
}

public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
DatabaseDump::restoreTables(['api_access']);
self::$clientSecret = null;
}

protected static function createClient(array $kernelOptions = [], array $defaultOptions = []): Client
{
if (!isset($defaultOptions['headers']['accept'])) {
$defaultOptions['headers']['accept'] = ['application/json'];
}

return parent::createClient($kernelOptions, $defaultOptions);
}

protected function getBearerToken(array $scopes = []): string
{
if (null === self::$clientSecret) {
self::createApiAccess($scopes);
}
$client = static::createClient();
$parameters = ['parameters' => [
'client_id' => static::CLIENT_ID,
'client_secret' => static::$clientSecret,
'grant_type' => 'client_credentials',
'scope' => $scopes,
]];
$options = ['extra' => $parameters];
$response = $client->request('POST', '/api/oauth2/token', $options);

return json_decode($response->getContent())->access_token;
}

protected static function createApiAccess(array $scopes = [], int $lifetime = 10000): void
{
$client = static::createClient();
$command = new AddApiAccessCommand(
static::CLIENT_NAME,
static::CLIENT_ID,
true,
'',
$lifetime,
$scopes
);

$container = $client->getContainer();
$commandBus = $container->get('prestashop.core.command_bus');
$createdApiAccess = $commandBus->handle($command);

self::$clientSecret = $createdApiAccess->getSecret();
}
}
124 changes: 124 additions & 0 deletions tests/Integration/ApiPlatform/GetHookStatusTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?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 [email protected] 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 <[email protected]>
* @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 PsApiResourcesTest\Integration\ApiPlatform;

use Tests\Resources\DatabaseDump;

class GetHookStatusTest extends ApiTestCase
{
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
DatabaseDump::restoreTables(['hook']);
}

public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
DatabaseDump::restoreTables(['hook']);
}

public function testGetHookStatus(): void
{
$inactiveHook = new \Hook();
$inactiveHook->name = 'inactiveHook';
$inactiveHook->active = false;
$inactiveHook->add();

$activeHook = new \Hook();
$activeHook->name = 'activeHook';
$activeHook->active = true;
$activeHook->add();

$bearerToken = $this->getBearerToken([
'hook_read',
'hook_write',
]);
$response = static::createClient()->request('GET', '/api/hook-status/' . (int) $inactiveHook->id, ['auth_bearer' => $bearerToken]);
self::assertEquals(json_decode($response->getContent())->active, $inactiveHook->active);
self::assertResponseStatusCodeSame(200);

$response = static::createClient()->request('GET', '/api/hook-status/' . (int) $activeHook->id, ['auth_bearer' => $bearerToken]);
self::assertEquals(json_decode($response->getContent())->active, $activeHook->active);
self::assertResponseStatusCodeSame(200);

static::createClient()->request('GET', '/api/hook-status/' . 9999, ['auth_bearer' => $bearerToken]);
self::assertResponseStatusCodeSame(404);

static::createClient()->request('GET', '/api/hook-status/' . $activeHook->id);
self::assertResponseStatusCodeSame(401);

$inactiveHook->delete();
$activeHook->delete();
}

public function testDisableHook(): void
{
$hook = new \Hook();
$hook->name = 'disableHook';
$hook->active = true;
$hook->add();

$bearerToken = $this->getBearerToken([
'hook_read',
'hook_write',
]);
static::createClient()->request('PUT', '/api/hook-status', [
'auth_bearer' => $bearerToken,
'json' => ['id' => (int) $hook->id, 'active' => false],
]);
self::assertResponseStatusCodeSame(200);

$response = static::createClient()->request('GET', '/api/hook-status/' . (int) $hook->id, ['auth_bearer' => $bearerToken]);
self::assertEquals(json_decode($response->getContent())->active, false);
self::assertResponseStatusCodeSame(200);
}

public function testEnableHook(): void
{
$hook = new \Hook();
$hook->name = 'enableHook';
$hook->active = false;
$hook->add();

$bearerToken = $this->getBearerToken([
'hook_read',
'hook_write',
]);
static::createClient()->request('PUT', '/api/hook-status', [
'auth_bearer' => $bearerToken,
'json' => ['id' => (int) $hook->id, 'active' => true],
]);
self::assertResponseStatusCodeSame(200);

$response = static::createClient()->request('GET', '/api/hook-status/' . (int) $hook->id, ['auth_bearer' => $bearerToken]);
self::assertEquals(json_decode($response->getContent())->active, true);
self::assertResponseStatusCodeSame(200);
}
}
Loading

0 comments on commit 53311ac

Please sign in to comment.