5

I installed MFTF stand alone and have a magento instance. Just following the getting started document before I write tests of my own. When generated/run the test it gives error - Could not retrieve API token from Magento Instance (302 Found).

Any help is really appreciated

In ModuleResolver.php line 444:

Could not retrieve API token from Magento Instance (302 Found)

generate:tests [-c|--config CONFIG] [-f|--force] [-i|--time TIME] [-t|--tests TESTS] [-d|--debug] [-r|--remove] [--] [...]

3 Answers 3

3

probably you have wrong credentials to Magento instance.

Look in ModuleResolver where API token is retrieving from Magento:

    /**
     * Get the API token for admin.
     *
     * @return string|boolean
     */
    protected function getAdminToken()
    {
        $login = $_ENV['MAGENTO_ADMIN_USERNAME'] ?? null;
        $password = $_ENV['MAGENTO_ADMIN_PASSWORD'] ?? null;
        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($this->getBackendUrl()) . $this->adminTokenUrl;
        $data = [
            'username' => $login,
            'password' => $password
        ];
        $headers = [
            'Content-Type: application/json',
        ];

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $response = curl_exec($ch);
        $responseCode = curl_getinfo($ch)['http_code'];

        if ($responseCode !== 200) {
            if ($responseCode == 0) {
                $details = "Could not find Magento Backend Instance at MAGENTO_BACKEND_BASE_URL or MAGENTO_BASE_URL";
            } else {
                $details = $responseCode . " " . Response::$statusTexts[$responseCode];
            }

            $message = "Could not retrieve API token from Magento Instance ({$details})";
            $context = [
                "tokenUrl" => $url,
                "responseCode" => $responseCode,
                "MAGENTO_ADMIN_USERNAME" => getenv("MAGENTO_ADMIN_USERNAME"),
                "MAGENTO_ADMIN_PASSWORD" => getenv("MAGENTO_ADMIN_PASSWORD"),
            ];
            throw new TestFrameworkException($message, $context);
        }

        return json_decode($response);
    }

My advice is to check .env file in ROOT/dev/test/acceptance/.env

3
  • In .env file, default admin name or admin registration name?
    – Kite Hany
    Jul 16, 2019 at 8:19
  • MAGENTO_BASE_URL= URL for frontend MAGENTO_BACKEND_NAME= URL name for backend(default after magento installation is admin_xxxxxxx. You can check in MAGE_ROOT/app/etc/env.php) MAGENTO_ADMIN_USERNAME= admin login MAGENTO_ADMIN_PASSWORD= admin password Jul 24, 2019 at 5:16
  • in my case there is no .env file what should I do now?
    – Asad Khan
    Apr 28, 2020 at 10:11
2

If you got this problem You should change info of file dot-env related with your instance such as MAGENTO_BASE_URL MAGENTO_ADMIN_USERNAME MAGENTO_ADMIN_PASSWORD
Make sure set write permissions recurssive to folder

magento_root\dev\tests

0

I've got the same message when I forgot to make

php bin/magento setup:upgrade

after I added some modules and tried to generate MFTF tests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.