Skip to content

Use custom Backend domain, refactoring to Executors responsible for calling HTTP endpoints #264

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

Merged
merged 5 commits into from
Dec 10, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions etc/config/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#*** Set the base URL for your Magento instance ***#
MAGENTO_BASE_URL=http://devdocs.magento.com/

#*** Uncomment if you are running Admin Panel on separate domain (used with MAGENTO_BACKEND_NAME) ***#
# MAGENTO_BACKEND_BASE_HOST=http://admin.example.com/

#*** Set the Admin Username and Password for your Magento instance ***#
MAGENTO_BACKEND_NAME=admin
MAGENTO_ADMIN_USERNAME=admin
Expand All @@ -23,8 +26,9 @@ MAGENTO_ADMIN_PASSWORD=123123q
BROWSER=chrome

#*** Uncomment and set host & port if your dev environment needs different value other than MAGENTO_BASE_URL for Rest API Requests ***#
#MAGENTO_RESTAPI_SERVER_HOST=
#MAGENTO_RESTAPI_SERVER_PORT=
#MAGENTO_RESTAPI_SERVER_HOST=restapi.magento.com
#MAGENTO_RESTAPI_SERVER_PORT=8080
#MAGENTO_RESTAPI_SERVER_PROTOCOL=https

#*** Uncomment these properties to set up a dev environment with symlinked projects ***#
#TESTS_BP=
Expand Down
1 change: 1 addition & 0 deletions etc/config/functional.suite.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ modules:
config:
\Magento\FunctionalTestingFramework\Module\MagentoWebDriver:
url: "%MAGENTO_BASE_URL%"
backend_url: "%MAGENTO_BACKEND_BASE_URL%"
backend_name: "%MAGENTO_BACKEND_NAME%"
browser: 'chrome'
restart: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,18 @@
abstract class AbstractExecutor implements CurlInterface
{
/**
* Base url.
* Returns Magento base URL. Used as a fallback for other services (eg. WebApi, Backend)
*
* @var string
*/
protected static $baseUrl = null;

/**
* Resolve base url.
*
* @return void
* Returns base URL for Magento instance
* @return string
*/
protected static function resolveBaseUrl()
public function getBaseUrl(): string
{

if ((getenv('MAGENTO_RESTAPI_SERVER_HOST') !== false)
&& (getenv('MAGENTO_RESTAPI_SERVER_HOST') !== '') ) {
self::$baseUrl = getenv('MAGENTO_RESTAPI_SERVER_HOST');
} else {
self::$baseUrl = getenv('MAGENTO_BASE_URL');
}

if ((getenv('MAGENTO_RESTAPI_SERVER_PORT') !== false)
&& (getenv('MAGENTO_RESTAPI_SERVER_PORT') !== '')) {
self::$baseUrl .= ':' . getenv('MAGENTO_RESTAPI_SERVER_PORT');
}

self::$baseUrl = rtrim(self::$baseUrl, '/') . '/';
return getenv('MAGENTO_BASE_URL');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,11 @@ class AdminExecutor extends AbstractExecutor implements CurlInterface
private $response;

/**
* Should executor remove backend_name from api url
* Flag describes whether the request is to Magento Base URL, removes backend_name from api url
* @var boolean
*/
private $removeBackend;

/**
* Backend url.
*
* @var string
*/
private static $adminUrl;

/**
* Constructor.
* @param boolean $removeBackend
Expand All @@ -58,15 +51,21 @@ class AdminExecutor extends AbstractExecutor implements CurlInterface
*/
public function __construct($removeBackend)
{
if (!isset(parent::$baseUrl)) {
parent::resolveBaseUrl();
}
self::$adminUrl = parent::$baseUrl . getenv('MAGENTO_BACKEND_NAME') . '/';
$this->removeBackend = $removeBackend;
$this->transport = new CurlTransport();
$this->authorize();
}

/**
* Returns base URL for Magento backend instance
* @return string
*/
public function getBaseUrl(): string
{
$backendHost = getenv('MAGENTO_BACKEND_BASE_URL') ?: parent::getBaseUrl();
return $backendHost . getenv('MAGENTO_BACKEND_NAME') . '/';
}

/**
* Authorize admin on backend.
*
Expand All @@ -76,11 +75,11 @@ public function __construct($removeBackend)
private function authorize()
{
// Perform GET to backend url so form_key is set
$this->transport->write(self::$adminUrl, [], CurlInterface::GET);
$this->transport->write($this->getBaseUrl(), [], CurlInterface::GET);
$this->read();

// Authenticate admin user
$authUrl = self::$adminUrl . 'admin/auth/login/';
$authUrl = $this->getBaseUrl() . 'admin/auth/login/';
$data = [
'login[username]' => getenv('MAGENTO_ADMIN_USERNAME'),
'login[password]' => getenv('MAGENTO_ADMIN_PASSWORD'),
Expand Down Expand Up @@ -119,10 +118,10 @@ private function setFormKey()
public function write($url, $data = [], $method = CurlInterface::POST, $headers = [])
{
$url = ltrim($url, "/");
$apiUrl = self::$adminUrl . $url;
$apiUrl = $this->getBaseUrl() . $url;

if ($this->removeBackend) {
$apiUrl = parent::$baseUrl . $url;
$apiUrl = parent::getBaseUrl() . $url;
}

if ($this->formKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ class FrontendExecutor extends AbstractExecutor implements CurlInterface
*/
public function __construct($customerEmail, $customerPassWord)
{
if (!isset(parent::$baseUrl)) {
parent::resolveBaseUrl();
}
$this->transport = new CurlTransport();
$this->customerEmail = $customerEmail;
$this->customerPassword = $customerPassWord;
Expand All @@ -84,11 +81,11 @@ public function __construct($customerEmail, $customerPassWord)
*/
private function authorize()
{
$url = parent::$baseUrl . 'customer/account/login/';
$url = $this->getBaseUrl() . 'customer/account/login/';
$this->transport->write($url);
$this->read();

$url = parent::$baseUrl . 'customer/account/loginPost/';
$url = $this->getBaseUrl() . 'customer/account/loginPost/';
$data = [
'login[username]' => $this->customerEmail,
'login[password]' => $this->customerPassword,
Expand Down Expand Up @@ -146,7 +143,7 @@ public function write($url, $data = [], $method = CurlInterface::POST, $headers
if (isset($data['customer_password'])) {
unset($data['customer_password']);
}
$apiUrl = parent::$baseUrl . $url;
$apiUrl = $this->getBaseUrl() . $url;
if ($this->formKey) {
$data['form_key'] = $this->formKey;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,34 @@ class WebapiExecutor extends AbstractExecutor implements CurlInterface
*/
public function __construct($storeCode = null)
{
if (!isset(parent::$baseUrl)) {
parent::resolveBaseUrl();
}

$this->storeCode = $storeCode;
$this->transport = new CurlTransport();
$this->authorize();
}

/**
* Returns base URL for Magento Web API instance
* @return string
*/
public function getBaseUrl(): string
{
$baseUrl = parent::getBaseUrl();

$webapiHost = getenv('MAGENTO_RESTAPI_SERVER_HOST');
$webapiPort = getenv("MAGENTO_RESTAPI_SERVER_PORT");
$webapiProtocol = getenv("MAGENTO_RESTAPI_SERVER_PROTOCOL");

if ($webapiHost) {
$baseUrl = sprintf('%s://%s/', $webapiProtocol, $webapiHost);
}

if ($webapiPort) {
$baseUrl = rtrim($baseUrl, '/') . ':' . $webapiPort . '/';
}

return $baseUrl;
}

/**
* Returns the authorization token needed for some requests via REST call.
*
Expand Down Expand Up @@ -152,11 +171,11 @@ public function close()
*/
public function getFormattedUrl($resource)
{
$urlResult = parent::$baseUrl . 'rest/';
$urlResult = $this->getBaseUrl() . 'rest/';
if ($this->storeCode != null) {
$urlResult .= $this->storeCode . "/";
}
$urlResult.= trim($resource, "/");
$urlResult .= trim($resource, "/");
return $urlResult;
}
}
16 changes: 13 additions & 3 deletions src/Magento/FunctionalTestingFramework/Util/ModuleResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,17 +396,18 @@ protected function getAdminToken()
{
$login = $_ENV['MAGENTO_ADMIN_USERNAME'] ?? null;
$password = $_ENV['MAGENTO_ADMIN_PASSWORD'] ?? null;
if (!$login || !$password || !isset($_ENV['MAGENTO_BASE_URL'])) {
if (!$login || !$password || !$this->getBackendUrl()) {
$message = "Cannot retrieve API token without credentials and base url, please fill out .env.";
$context = [
"MAGENTO_BASE_URL" => getenv("MAGENTO_BASE_URL"),
"MAGENTO_BACKEND_BASE_URL" => getenv("MAGENTO_BACKEND_BASE_URL"),
"MAGENTO_ADMIN_USERNAME" => getenv("MAGENTO_ADMIN_USERNAME"),
"MAGENTO_ADMIN_PASSWORD" => getenv("MAGENTO_ADMIN_PASSWORD"),
];
throw new TestFrameworkException($message, $context);
}

$url = ConfigSanitizerUtil::sanitizeUrl($_ENV['MAGENTO_BASE_URL']) . $this->adminTokenUrl;
$url = ConfigSanitizerUtil::sanitizeUrl($this->getBackendUrl()) . $this->adminTokenUrl;
$data = [
'username' => $login,
'password' => $password
Expand All @@ -428,7 +429,7 @@ protected function getAdminToken()

if ($responseCode !== 200) {
if ($responseCode == 0) {
$details = "Could not find Magento Instance at given MAGENTO_BASE_URL";
$details = "Could not find Magento Backend Instance at MAGENTO_BACKEND_BASE_URL or MAGENTO_BASE_URL";
} else {
$details = $responseCode . " " . Response::$statusTexts[$responseCode];
}
Expand Down Expand Up @@ -565,4 +566,13 @@ private function getRegisteredModuleList()
}
return [];
}

/**
* Returns custom Backend URL if set, fallback to Magento Base URL
* @return string|null
*/
private function getBackendUrl()
{
return getenv('MAGENTO_BACKEND_BASE_URL') ?: getenv('MAGENTO_BASE_URL');
}
}