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

Middleware @group(middleware: ["auth:api"]) not working on tests #397

Closed
necrojan opened this issue Oct 17, 2018 · 3 comments
Closed

Middleware @group(middleware: ["auth:api"]) not working on tests #397

necrojan opened this issue Oct 17, 2018 · 3 comments

Comments

@necrojan
Copy link

Describe the bug
Middleware works fine when using insomia.
and also on running this first test.

    public function can_view_list_of_genders()
    {
        $user = factory(User::class)->create();
        factory(Gender::class)->create([
            'name'        => 'Example gender name',
            'description' => 'Example gender description'
        ]);

        $response = $this->actingAs($user, 'api')
            ->sendQuery('genders');

        $response->assertJson([
            'data' => [
                'genders' => [
                    [
                        'name'        => 'Example gender name',
                        'description' => 'Example gender description'
                    ]
                ]
            ]
        ]);
    }

but this test should fail, when used dd() it still shows that it was accessible even if not logged in.

    public function guests_cannot_view_list_of_genders()
    {
        factory(Gender::class, 3)->create();

        $response = $this->sendQuery('genders');

        dd($response->getContent());
    }

Expected behavior
It should not display the gender results, should've been an error or data null results

Schema

type Query @group(middleware: ["auth:api"]) {
    genders: [Gender!] ! @all
}

Environment

Lighthouse Version: 2.3
Laravel Version: 5.7
PHP Version: 7.2

Additional context

https://github.com/najaram/hqtest

@spawnia
Copy link
Collaborator

spawnia commented Oct 17, 2018

You bypass the middleware by directly executing your query https://github.com/najaram/graphql-auth-error/blob/master/tests/Feature/GraphQL/GraphQLTestCase.php#L21

Use something like this instead:

    /**
     * Execute a query as if it was sent as a request to the server.
     *
     * @param string $query
     *
     * @return array
     */
    protected function queryViaHttp(string $query): array
    {
        return $this->postJson(
            'graphql',
            ['query' => $query]
        )->json();
    }

Also, i would love if you can take a look at #395 and see if it works for you.

@spawnia spawnia closed this as completed Oct 17, 2018
@necrojan
Copy link
Author

uhhm i've tried to referenced the branch but can't seem to make it work mll-lab:middleware-in-field-resolution can you please guide me through to figure out the branch.

I would really appreciate it. thanks!

@spawnia
Copy link
Collaborator

spawnia commented Oct 17, 2018

Put this in your composer.json

    "repositories": [
        {
            "url": "https://github.com/mll-lab/lighthouse.git",
            "type": "git"
        }
    ],
    "require": {
		...
        "nuwave/lighthouse": "dev-middleware-in-field-resolution",

Thanks for trying it out, looking forward to some feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants