phpphpunitsymfony4functional-testinglexikjwtauthbundle

Error: Unable to find the controller for path "/login_check". The route is wrongly configured. (LexikJWTAuthentication)


Description

When I'm doing a normal request to my symfony server running on http://localhost:8000/api/admin/login_check it returns the desired jwt token.

However, when I do it with the functional tests (with ./bin/phpunit) I get the following error:

Error: Unable to find the controller for path \"/api/admin/login_check\". The route is wrongly configured.

I also went through the functional test docs.

Bug Reproduced

Don't hesitate to clone or fork this project to test. There is README.md explaining installation steps.

I was also able to reproduce the bug by cloning a working example provided by one of the creators of the lexikjwtauthenticationbundle.

Logs

Test Logs (error)

Occurs when running ./bin/phpunit

[2019-01-31 09:37:49] request.INFO: Matched route "api_admin_login_check". {"route":"api_admin_login_check","route_parameters":{"_route":"api_admin_login_check"},"request_uri":"http://localhost/api/admin/login_check","method":"POST"} []
[2019-01-31 09:37:49] security.INFO: Populated the TokenStorage with an anonymous Token. [] []
[2019-01-31 09:37:49] request.WARNING: Unable to look for the controller as the "_controller" parameter is missing. [] []

Dev Logs (success)

Occurs when doing a curl or postman request

[2019-01-29 21:16:26] request.INFO: Matched route "api_admin_login_check". {"route":"api_admin_login_check","route_parameters":{"_route":"api_admin_login_check"},"request_uri":"https://localhost:8000/api/admin/login_check","method":"POST"} []
[2019-01-29 21:16:27] doctrine.DEBUG: SELECT t0.id AS id_1, t0.email AS email_2, t0.password AS password_3 FROM admin t0 WHERE t0.email = ? LIMIT 1 ["email@test.com"] []
[2019-01-29 21:16:27] security.INFO: User has been authenticated successfully. {"username":null} []

Relevant Code:

Test method:

    public function testLogin(){

        $client = static::createClient();
        $client->request('POST', '/api/admin/login_check', [], [],
            [
                'Content-Type' => 'application/json',
                'Accept' => 'application/json'
            ],
            json_encode([
                'email' => 'email@test.com',
                'password' => 'qwerty123'
            ])
        );

        $this->assertEquals(200, $client->getResponse()->getStatusCode());

    }

Routes:

# Admin Routes
api_admin_login_check:
    path: /api/admin/login_check
    methods:  [POST]

Security:

security:

# more configs here

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        login_admin:
            pattern: ^/api/admin/login
            stateless: true
            anonymous: true
            json_login:
                username_path: email
                provider: app_admin_provider
                check_path: /api/admin/login_check
                success_handler: lexik_jwt_authentication.handler.authentication_success
                failure_handler: lexik_jwt_authentication.handler.authentication_failure

        admin_api:
            pattern: ^/api/admin
            stateless: true
            provider: app_admin_provider
            guard:
                authenticators:
                    - lexik_jwt_authentication.jwt_token_authenticator

    access_control:
        - { path: ^/api/admin/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api/admin, roles: ROLE_ADMIN }
        - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }

Question

Why is there a 404 route not found for /api/admin/login_check route during Functional Testing but works fine with curl and postman?

Github #610


Solution

  • You have wrong name for content-type header in your test request. It should have name CONTENT_TYPE.

    $client->request(
       'POST',
       '/login_check',
       ['_username' => 'lexik', '_password' => 'dummy'],
       [],
       ['CONTENT_TYPE' => 'application/json']
    );
    

    Checking is here: vendor/symfony/security/Http/Firewall/UsernamePasswordJsonAuthenticationListener.php::handle

    if (false === strpos($request->getRequestFormat(), 'json')
       && false === strpos($request->getContentType(), 'json')
    ) {
      return;
    }
    

    It helps in your "bug reproduced" code at least.