Skip to content
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

Undefined token in Api Tests #45

Open
dreamsbond opened this issue Mar 6, 2018 · 7 comments
Open

Undefined token in Api Tests #45

dreamsbond opened this issue Mar 6, 2018 · 7 comments
Assignees

Comments

@dreamsbond
Copy link

Hi @neomerx.

I found the OAuthLoginTrait does not properly returning token based on corresponding role of user, says "admin", etc...

@neomerx neomerx self-assigned this Mar 6, 2018
@neomerx
Copy link
Collaborator

neomerx commented Mar 6, 2018

Default application uses Faker to populate the database with some data. The data are originally random though until you change seed script it will have

And here are default methods with those default emails/passwords to have API tokens.

If you modify migrations/seeds you obviously need to make some changes in the login trait. The actual changes are application specific.

@dreamsbond
Copy link
Author

    /**
     * @param string $username
     * @param string $password
     *
     * @return string
     */
    private function requestOAuthToken(string $username, string $password): string
    {
        /** @var ResponseInterface $response */
        $response = $this->post('/token', [
            'grant_type' => 'password',
            'username'   => $username,
            'password'   => $password,
        ]);

        assert($response->getStatusCode() == 200);
        assert(($token = json_decode((string)$response->getBody())) !== false);

        $value = $token->access_token;
        assert(empty($value) === false);

        return $value;
    }

tested out $token was not assigned
in

assert(($token = json_decode((string)$response->getBody())) !== false);

and therefore getAdminOAuthToken failed

@neomerx
Copy link
Collaborator

neomerx commented Mar 12, 2018

What are the inputs (username, password)? Does it work with the default app?

@dreamsbond
Copy link
Author

both default faker seeded username and password and;
self-defined username and password works by

replacing

assert(($token = json_decode((string)$response->getBody())) !== false);

with

        $token = json_decode((string)$response->getBody());
        assert($token !== false);

in OAuthLoginTrait

@neomerx
Copy link
Collaborator

neomerx commented Mar 12, 2018

I took default app, run migration and seed then changed CommentApiTest line 13 as below and added lines 22-23. Token seems to work 🤷‍♂️

image

Can you please clone the default app and commit changes that show the issue?

@dreamsbond
Copy link
Author

default app cloned.

seems does not help.

PHPUnit 6.5.7 by Sebastian Bergmann and contributors.


Undefined variable: token

image

image

image

@neomerx
Copy link
Collaborator

neomerx commented Mar 15, 2018

  • I created a new project composer create-project --prefer-dist limoncello-php/app app_name (on install it runs migrations and seeds).
  • I opened the newly created project in IDE and run tests successfully.
  • opened \Tests\Api\UserApiTest::testIndex, added use for OAuthLoginTrait and $token = $this->requestOAuthToken('[email protected]', UsersSeed::DEFAULT_PASSWORD);
  • put a breakpoint & run debugger
  • 🤷‍♂️
    image

@neomerx neomerx added fixed and removed in progress labels Apr 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants