Lighthouse: How to write test for login

Created on 19 Oct 2020  路  5Comments  路  Source: nuwave/lighthouse

Hi
I have some problem for writing login test. I realized that I have to use
Artisan::call('passport:install');
but now I do not how config this data for
$credentials = $this->buildCredentials($args);
please help me, How can I write a standard test for login

It's my test code

`
/** @test */
public function user_can_login_with_true_user_and_password()
{
Artisan::call('passport:install');

    $user = factory(User::class)->create([
        'phone' => '0936987456',
    ]);

    $response = $this->graphQL(/** @lang GraphQL */ '
        mutation Login(
            $username: String!
            $password: String!
        ){
            login(
                input:{
                    username: $username
                    password: $password
                  }
            ){
                tokens{
                  access_token
                  refresh_token
                }
                status
            }
        }
    ', [
        'username' => $user->phone,
        'password' => "123456789",
    ]);

    dd($response);
}

`
Thanks

question

All 5 comments

Hello! Here is the sample from one of my project:

public function testMutationUserLogin(): void
{
    $this->flushHeaders();

    $response = $this->graphQL(/** @lang GraphQL */ '
        mutation {
            login (
                input: {
                    email:"[email protected]",
                    password:"MyPrecious1",
                }
            )
            {
                id
                name
                email
            }
        }
    ');

    $user = $response->json('data.login');
    self::assertEquals($this->user->id, $user['id']);
}

This user was created in setUp method.

Response of login mutation is User model, with id, email, name etc.

Thanks a lot,
Do you use $this->buildCredentials($args) in your login mutation?

Nope, i use Laravel factories and setUp method of tests in combination with DatabaseTransactions trait in root TestCase class.

Thanks, this problem occur when you use buildCredentials($args) in LoginMutation class and passport for authentication. My classes work well in real but I have some problem in test when Lighthouse use passport for authentication

I finally find a answer for this work you must use this code.

`$client = app(ClientRepository::class)->createPasswordGrantClient(null, 'test', 'http://localhost');

    config()->set('lighthouse-graphql-passport.client_id', $client->id);

    config()->set('lighthouse-graphql-passport.client_secret', $client->secret);`
Was this page helpful?
0 / 5 - 0 ratings

Related issues

souljacker picture souljacker  路  3Comments

vine1993 picture vine1993  路  3Comments

Leuloch picture Leuloch  路  3Comments

basudebpalfreelancer picture basudebpalfreelancer  路  4Comments

latheesan-k picture latheesan-k  路  3Comments