Skip to content

Commit

Permalink
Add tests for login input validation
Browse files Browse the repository at this point in the history
  • Loading branch information
wimski committed Apr 23, 2021
1 parent 254c927 commit 30abd11
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 11 deletions.
4 changes: 2 additions & 2 deletions graphql/sanctum.graphql
Expand Up @@ -9,8 +9,8 @@ type AccessToken {
}

input LoginInput {
email: String! @rules(apply: ["required", "email"])
password: String! @rules(apply: ["required", "string"])
email: String! @rules(apply: ["email"])
password: String!
}

type LogoutResponse {
Expand Down
107 changes: 98 additions & 9 deletions tests/Integration/GraphQL/Mutations/LoginTest.php
Expand Up @@ -7,7 +7,6 @@
use DanielDeWit\LighthouseSanctum\Tests\Integration\AbstractIntegrationTest;
use DanielDeWit\LighthouseSanctum\Tests\stubs\Users\UserHasApiTokens;
use Illuminate\Support\Facades\Hash;
use Illuminate\Testing\TestResponse;

class LoginTest extends AbstractIntegrationTest
{
Expand All @@ -21,7 +20,16 @@ public function it_logs_a_user_in(): void
'password' => Hash::make('supersecret'),
]);

$this->makeRequest()->assertJsonStructure([
$this->graphQL(/** @lang GraphQL */'
mutation {
login(input: {
email: "foo@bar.com",
password: "supersecret"
}) {
token
}
}
')->assertJsonStructure([
'data' => [
'login' => [
'token',
Expand All @@ -35,22 +43,103 @@ public function it_logs_a_user_in(): void
*/
public function it_returns_an_error_if_the_credentials_are_incorrect(): void
{
$this
->makeRequest()
->assertGraphQLErrorMessage('The provided credentials are incorrect.');
$this->graphQL(/** @lang GraphQL */'
mutation {
login(input: {
email: "foo@bar.com",
password: "supersecret"
}) {
token
}
}
')->assertGraphQLErrorMessage('The provided credentials are incorrect.');
}

protected function makeRequest(): TestResponse
/**
* @test
*/
public function it_returns_an_error_if_the_email_field_is_missing(): void
{
return $this->graphQL(/** @lang GraphQL */'
$this->graphQL(/** @lang GraphQL */'
mutation {
login(input: {
email: "foo@bar.com",
password: "supersecret"
}) {
token
}
}
');
')->assertGraphQLErrorMessage('Field LoginInput.email of required type String! was not provided.');
}

/**
* @test
*/
public function it_returns_an_error_if_the_email_field_is_not_a_string(): void
{
$this->graphQL(/** @lang GraphQL */'
mutation {
login(input: {
email: 12345
password: "supersecret"
}) {
token
}
}
')->assertGraphQLErrorMessage('Field "login" argument "input" requires type String!, found 12345.');
}

/**
* @test
*/
public function it_returns_an_error_if_the_email_field_is_not_an_email(): void
{
$this->graphQL(/** @lang GraphQL */'
mutation {
login(input: {
email: "foobar"
password: "supersecret"
}) {
token
}
}
')
->assertGraphQLErrorMessage('Validation failed for the field [login].')
->assertGraphQLValidationError(
'input.email',
'The input.email must be a valid email address.',
);
}

/**
* @test
*/
public function it_returns_an_error_if_the_password_field_is_missing(): void
{
$this->graphQL(/** @lang GraphQL */'
mutation {
login(input: {
email: "foo@bar.com"
}) {
token
}
}
')->assertGraphQLErrorMessage('Field LoginInput.password of required type String! was not provided.');
}

/**
* @test
*/
public function it_returns_an_error_if_the_password_field_is_not_a_string(): void
{
$this->graphQL(/** @lang GraphQL */'
mutation {
login(input: {
email: "foobar"
password: 12345
}) {
token
}
}
')->assertGraphQLErrorMessage('Field "login" argument "input" requires type String!, found 12345.');
}
}

0 comments on commit 30abd11

Please sign in to comment.