0

I have a function in an API call that checks $this->session->isLoggedIn().

The session-object is injected via the consturtor.

Now I have to write an api-functional test. But I just can't login the customer and isLoggedIn() will always return false in my service. My test-code currently looks like this:

    $historyId = 1;
    $download = 0;

    $serviceInfo = [
        'rest' => [
            'resourcePath' => '/V1/preorder-history/view/' . $historyId . '/' . $download,
            'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
        ]
    ];

    $loginInfo = [
        'rest' => [
            'resourcePath' => '/V1/customers/me',
            'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
            'token' => $this->token,
        ],
    ];

    $requestData = [
        'historyId' => $historyId,
        'download' => $download,
        'token' => $this->token
    ];

    $customer = $this->_webApiCall($loginInfo, $requestData);

    /** @var $session \Magento\Customer\Model\Session */
    $session = Bootstrap::getObjectManager()->get(\Magento\Customer\Model\Session::class);
    $session->loginById($customer['id']);
    $session->setCustomerId($customer['id']);

    $item = $this->_webApiCall($serviceInfo, $requestData);

EDIT:

within my test, the session object obviously returns that the customer is logged in. So I assume this is not the same object that is injected in my service which I want to test.

EDIT2:

spl_object_hash confirms this theory.

1 Answer 1

1

The API functional tests are black box tests, you cannot change the application state within the tests. Use the API login method instead.

This is a method that you can use in your test:

private function apiLogin($username, $password)
{
    $serviceInfo = [
        'rest' => [
            'resourcePath' => "/V1/integration/customer/token",
            'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
        ],
    ];
    $requestData = ['username' => $username, 'password' => $password];
    $this->token = $this->_webApiCall($serviceInfo, $requestData);
}

Then use 'token' => $this->token, in your subsequent API calls, as you already do.

5
  • "Use the API login method instead" could you elaborate that? Jul 27, 2017 at 8:59
  • 1
    @PhilippSander you can see an example here: github.com/magento/magento2/blob/develop/dev/tests/… I'll update my answer with details Jul 27, 2017 at 9:51
  • thanks for your update, but that's exactly what I do right now. sorry that I didn't post the code. Jul 27, 2017 at 9:57
  • Then what are these lines supposed to do? $session = Bootstrap::getObjectManager()->get(\Magento\Customer\Model\Session::class); $session->loginById($customer['id']); $session->setCustomerId($customer['id']); - can you remove them and update the question with the whole relevant code? Jul 27, 2017 at 10:01
  • they are an attempt to solve my problem. deleteing them will still result in session->isLoggedIn() returning false in my service. Jul 27, 2017 at 10:13

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.