diff --git a/graphql/sanctum.graphql b/graphql/sanctum.graphql index 9184f06..2a9d76d 100644 --- a/graphql/sanctum.graphql +++ b/graphql/sanctum.graphql @@ -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 { diff --git a/tests/Integration/GraphQL/Mutations/LoginTest.php b/tests/Integration/GraphQL/Mutations/LoginTest.php index 0aed45e..f64fc1a 100644 --- a/tests/Integration/GraphQL/Mutations/LoginTest.php +++ b/tests/Integration/GraphQL/Mutations/LoginTest.php @@ -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 { @@ -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', @@ -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.'); } }