Skip to content

Commit

Permalink
Added Behat Tests for WebService
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Oct 26, 2021
1 parent 4b1d302 commit 135ce1d
Show file tree
Hide file tree
Showing 11 changed files with 658 additions and 112 deletions.
33 changes: 32 additions & 1 deletion classes/webservice/WebserviceKey.php
Expand Up @@ -114,6 +114,9 @@ public function deleteAssociations()
return Db::getInstance()->delete('webservice_permission', 'id_webservice_account = ' . (int) $this->id);
}

/**
* @param string $auth_key
*/
public static function getPermissionForAccount($auth_key)
{
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
Expand All @@ -132,6 +135,9 @@ public static function getPermissionForAccount($auth_key)
return $permissions;
}

/**
* @param string $auth_key
*/
public static function isKeyActive($auth_key)
{
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
Expand All @@ -140,6 +146,9 @@ public static function isKeyActive($auth_key)
WHERE `key` = "' . pSQL($auth_key) . '"');
}

/**
* @param string $auth_key
*/
public static function getClassFromKey($auth_key)
{
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
Expand All @@ -148,14 +157,36 @@ public static function getClassFromKey($auth_key)
WHERE `key` = "' . pSQL($auth_key) . '"');
}

/**
* @param string $auth_key
*
* @return int
*/
public static function getIdFromKey(string $auth_key)
{
$sql = sprintf(
'SELECT id_webservice_account FROM `%swebservice_account` WHERE `key` = "%s"',
_DB_PREFIX_,
pSQL($auth_key)
);

return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
}

/**
* @param int $id_account
* @param array $permissions_to_set
*
* @return bool
*/
public static function setPermissionForAccount($id_account, $permissions_to_set)
{
$ok = true;
$sql = 'DELETE FROM `' . _DB_PREFIX_ . 'webservice_permission` WHERE `id_webservice_account` = ' . (int) $id_account;
if (!Db::getInstance()->execute($sql)) {
$ok = false;
}
if (isset($permissions_to_set)) {
if (is_array($permissions_to_set)) {
$permissions = [];
$resources = WebserviceRequest::getResources();
$methods = ['GET', 'PUT', 'POST', 'DELETE', 'HEAD'];
Expand Down
6 changes: 6 additions & 0 deletions classes/webservice/WebserviceRequest.php
Expand Up @@ -244,6 +244,11 @@ public static function getInstance()
return self::$_instance;
}

public static function resetStaticCache(): void
{
static::$_instance = null;
}

protected function getOutputObject($type)
{
// set header param in header or as get param
Expand Down Expand Up @@ -1409,6 +1414,7 @@ public function executeEntityGetAndHead()
if (!$return) {
return false;
} else {
$this->_outputEnabled = true;
$this->objects = $return;
}
}
Expand Down
Expand Up @@ -33,89 +33,62 @@
use PrestaShop\PrestaShop\Core\Domain\Webservice\Command\AddWebserviceKeyCommand;
use PrestaShop\PrestaShop\Core\Domain\Webservice\Command\EditWebserviceKeyCommand;
use PrestaShop\PrestaShop\Core\Domain\Webservice\Exception\DuplicateWebserviceKeyException;
use PrestaShop\PrestaShop\Core\Domain\Webservice\ValueObject\WebserviceKeyId;
use Tests\Integration\Behaviour\Features\Context\SharedStorage;
use Tests\Integration\Behaviour\Features\Context\Util\PrimitiveUtils;
use WebserviceKey;

class WebserviceKeyFeatureContext extends AbstractDomainFeatureContext
class WebserviceKeyFeatureContext extends CommonDomainFeatureContext
{
/**
* @Given I specify following properties for new webservice key :reference:
* @Given I add a new webservice key with specified properties:
*/
public function specifyPropertiesForWebserviceKey(string $reference, TableNode $node): void
public function addNewWebserviceKey(TableNode $node): void
{
$data = $node->getRowsHash();

$data['is_enabled'] = (bool) $data['is_enabled'];
$data['shop_association'] = [
SharedStorage::getStorage()->get($data['shop_association']),
];

SharedStorage::getStorage()->set(sprintf('%s_properties', $reference), $data);
}

/**
* @Then /^I specify "(View|Add|Modify|Delete|Fast view)" permission for "([^"]*)" resources for new webservice key "(.*)"$/
*/
public function specifyResourcePermissions(string $permission, string $resources, string $reference): void
{
$propertiesKey = sprintf('%s_properties', $reference);

$data = SharedStorage::getStorage()->get($propertiesKey);

$permissionsMap = [
'View' => 'GET',
'Add' => 'POST',
'Modify' => 'PUT',
'Delete' => 'DELETE',
'Fast view' => 'HEAD',
];

$data['permissions'][$permissionsMap[$permission]] = PrimitiveUtils::castStringArrayIntoArray($resources);

SharedStorage::getStorage()->set($propertiesKey, $data);
}

/**
* @When I add webservice key :reference with specified properties
*/
public function addWebserviceKeyFromSpecifiedProperties(string $reference): void
{
$propertiesKey = sprintf('%s_properties', $reference);

$data = SharedStorage::getStorage()->get($propertiesKey);
$data['permissions'] = [];
foreach ($data as $key => $value) {
if (substr($key, 0, 11) !== 'permission_') {
continue;
}
$data['permissions'][substr($key, 11)] = PrimitiveUtils::castStringArrayIntoArray($value);
}

$command = new AddWebserviceKeyCommand(
$data['key'],
$data['description'],
$data['is_enabled'],
(bool) $data['is_enabled'],
$data['permissions'],
$data['shop_association']
);

try {
/** @var WebserviceKeyId $webserviceKeyId */
$webserviceKeyId = $this->getCommandBus()->handle($command);

SharedStorage::getStorage()->set($reference, new WebserviceKey($webserviceKeyId->getValue()));
$this->getCommandBus()->handle($command);
} catch (Exception $e) {
$this->setLastException($e);
}

SharedStorage::getStorage()->clear($propertiesKey);
}

/**
* @When I edit webservice key :reference with specified properties:
*/
public function editWebserviceKeyFromSpecifiedProperties(string $reference, TableNode $node): void
{
$webserviceKey = SharedStorage::getStorage()->get($reference);
$webserviceKeyId = (int) WebserviceKey::getIdFromKey($reference);

$data = $node->getRowsHash();
$data['permissions'] = [];
foreach ($data as $key => $value) {
if (substr($key, 0, 11) !== 'permission_') {
continue;
}
$data['permissions'][substr($key, 11)] = PrimitiveUtils::castStringArrayIntoArray($value);
}

$command = new EditWebserviceKeyCommand($webserviceKey->id);
$command = new EditWebserviceKeyCommand($webserviceKeyId);
if (isset($data['key'])) {
$command->setKey($data['key']);
}
Expand All @@ -125,11 +98,12 @@ public function editWebserviceKeyFromSpecifiedProperties(string $reference, Tabl
if (isset($data['is_enabled'])) {
$command->setStatus((bool) $data['is_enabled']);
}
if (!empty($data['permissions'])) {
$command->setPermissions($data['permissions']);
}

try {
$this->getCommandBus()->handle($command);

SharedStorage::getStorage()->set($reference, new WebserviceKey($webserviceKey->id));
} catch (Exception $e) {
$this->setLastException($e);
}
Expand Down
Expand Up @@ -37,7 +37,6 @@
use PrestaShop\PrestaShop\Core\Domain\SqlManagement\ValueObject\DatabaseTableField;
use PrestaShop\PrestaShop\Core\Domain\SqlManagement\ValueObject\SqlRequestId;
use RuntimeException;
use Symfony\Component\HttpFoundation\Request;
use Tests\Integration\Behaviour\Features\Context\Domain\AbstractDomainFeatureContext;

/**
Expand Down
@@ -0,0 +1,89 @@
<?php

namespace Tests\Integration\Behaviour\Features\Context\Util;

/**
* Allows to mock the php://input stream
* Without this stream, we can't write on the php://input stream because it's readyonly
*
* @example
* stream_wrapper_unregister('php');
* stream_register_wrapper('php', StreamWrapperPHP::class);
* file_put_contents('php://input', 'xml=' . $xmlVariable);
*/
class StreamWrapperPHP
{
/**
* @var int
*/
protected $index = 0;
protected $length = null;
protected $data = '';

public $context;

public function __construct()
{
if (file_exists($this->buffer_filename())) {
$this->data = file_get_contents($this->buffer_filename());
}
$this->index = 0;
$this->length = strlen($this->data);
}

protected function buffer_filename(): string
{
return sys_get_temp_dir() . '/php_input.txt';
}

public function stream_open($path, $mode, $options, &$opened_path): bool
{
return true;
}

public function stream_close()
{
}

public function stream_stat(): array
{
return [];
}

public function stream_flush(): bool
{
return true;
}

public function stream_read(int $count): string
{
if (is_null($this->length) === true) {
$this->length = strlen($this->data);
}
$length = min($count, $this->length - $this->index);
$data = substr($this->data, $this->index);
$this->index = $this->index + $length;

return $data;
}

public function stream_eof()
{
return $this->index >= $this->length;
}

public function stream_write($data)
{
return file_put_contents($this->buffer_filename(), $data);
}

public function unlink()
{
if (file_exists($this->buffer_filename())) {
unlink($this->buffer_filename());
}
$this->data = '';
$this->index = 0;
$this->length = 0;
}
}

0 comments on commit 135ce1d

Please sign in to comment.