-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
254 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DanielDeWit\LighthouseSanctum\Tests\Integration\GraphQL\Mutations; | ||
|
||
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 | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function it_logs_a_user_in(): void | ||
{ | ||
UserHasApiTokens::factory()->create([ | ||
'email' => 'foo@bar.com', | ||
'password' => Hash::make('supersecret'), | ||
]); | ||
|
||
$this->makeRequest()->assertJsonStructure([ | ||
'data' => [ | ||
'login' => [ | ||
'token', | ||
], | ||
], | ||
]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_returns_an_error_if_the_credentials_are_incorrect(): void | ||
{ | ||
$response = $this->makeRequest(); | ||
|
||
$this->assertGraphQLErrorMessage($response, 'The provided credentials are incorrect.'); | ||
} | ||
|
||
protected function makeRequest(): TestResponse | ||
{ | ||
return $this->graphQL(/** @lang GraphQL */' | ||
mutation { | ||
login(input: { | ||
email: "foo@bar.com", | ||
password: "supersecret" | ||
}) { | ||
token | ||
} | ||
} | ||
'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DanielDeWit\LighthouseSanctum\Tests\Traits; | ||
|
||
use Illuminate\Auth\AuthManager; | ||
use Illuminate\Contracts\Auth\UserProvider; | ||
use Illuminate\Contracts\Config\Repository as Config; | ||
use Illuminate\Foundation\Auth\User; | ||
use Mockery; | ||
use Mockery\MockInterface; | ||
|
||
trait MocksUserProvider | ||
{ | ||
/** | ||
* @return AuthManager|MockInterface | ||
*/ | ||
protected function mockAuthManager(?UserProvider $userProvider) | ||
{ | ||
/** @var AuthManager|MockInterface $authManager */ | ||
$authManager = Mockery::mock(AuthManager::class) | ||
->shouldReceive('createUserProvider') | ||
->with('sanctum-provider') | ||
->andReturn($userProvider) | ||
->getMock(); | ||
|
||
return $authManager; | ||
} | ||
|
||
/** | ||
* @return Config|MockInterface | ||
*/ | ||
protected function mockConfig() | ||
{ | ||
/** @var Config|MockInterface $config */ | ||
$config = Mockery::mock(Config::class) | ||
->shouldReceive('get') | ||
->with('lighthouse-sanctum.provider') | ||
->andReturn('sanctum-provider') | ||
->getMock(); | ||
|
||
return $config; | ||
} | ||
|
||
/** | ||
* @return UserProvider|MockInterface | ||
*/ | ||
abstract protected function mockUserProvider(?User $user); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DanielDeWit\LighthouseSanctum\Tests\Unit\GraphQL\Mutations; | ||
|
||
use DanielDeWit\LighthouseSanctum\Exceptions\HasApiTokensException; | ||
use DanielDeWit\LighthouseSanctum\GraphQL\Mutations\Login; | ||
use DanielDeWit\LighthouseSanctum\Tests\stubs\Users\UserHasApiTokens; | ||
use DanielDeWit\LighthouseSanctum\Tests\Traits\MocksUserProvider; | ||
use DanielDeWit\LighthouseSanctum\Tests\Unit\AbstractUnitTest; | ||
use Illuminate\Auth\AuthenticationException; | ||
use Illuminate\Contracts\Auth\UserProvider; | ||
use Illuminate\Foundation\Auth\User; | ||
use Laravel\Sanctum\NewAccessToken; | ||
use Mockery; | ||
use Mockery\MockInterface; | ||
use RuntimeException; | ||
|
||
class LoginTest extends AbstractUnitTest | ||
{ | ||
use MocksUserProvider; | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_logs_a_user_in(): void | ||
{ | ||
$token = Mockery::mock(NewAccessToken::class); | ||
$token->plainTextToken = '1234567890'; | ||
|
||
/** @var UserHasApiTokens|MockInterface $user */ | ||
$user = Mockery::mock(UserHasApiTokens::class) | ||
->shouldReceive('createToken') | ||
->with('default') | ||
->andReturn($token) | ||
->getMock(); | ||
|
||
$userProvider = $this->mockUserProvider($user); | ||
|
||
$mutation = new Login( | ||
$this->mockAuthManager($userProvider), | ||
$this->mockConfig(), | ||
); | ||
|
||
$result = $mutation(null, [ | ||
'email' => 'foo@bar.com', | ||
'password' => 'supersecret', | ||
]); | ||
|
||
static::assertIsArray($result); | ||
static::assertCount(1, $result); | ||
static::assertSame('1234567890', $result['token']); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_throws_an_exception_if_the_user_provider_is_not_found(): void | ||
{ | ||
static::expectException(RuntimeException::class); | ||
static::expectExceptionMessage('User provider not found.'); | ||
|
||
$mutation = new Login( | ||
$this->mockAuthManager(null), | ||
$this->mockConfig(), | ||
); | ||
|
||
$mutation(null, [ | ||
'email' => 'foo@bar.com', | ||
'password' => 'supersecret', | ||
]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_throws_an_exception_if_the_credentials_are_incorrect(): void | ||
{ | ||
static::expectException(AuthenticationException::class); | ||
static::expectExceptionMessage('The provided credentials are incorrect.'); | ||
|
||
$userProvider = $this->mockUserProvider(null); | ||
|
||
$mutation = new Login( | ||
$this->mockAuthManager($userProvider), | ||
$this->mockConfig(), | ||
); | ||
|
||
$mutation(null, [ | ||
'email' => 'foo@bar.com', | ||
'password' => 'supersecret', | ||
]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_throws_an_exception_if_the_user_does_not_have_the_has_api_tokens_trait(): void | ||
{ | ||
$user = Mockery::mock(User::class); | ||
|
||
static::expectException(HasApiTokensException::class); | ||
static::expectExceptionMessage('"' . get_class($user) . '" must implement "Laravel\Sanctum\Contracts\HasApiTokens".'); | ||
|
||
$userProvider = $this->mockUserProvider($user); | ||
|
||
$mutation = new Login( | ||
$this->mockAuthManager($userProvider), | ||
$this->mockConfig(), | ||
); | ||
|
||
$mutation(null, [ | ||
'email' => 'foo@bar.com', | ||
'password' => 'supersecret', | ||
]); | ||
} | ||
|
||
/** | ||
* @return UserProvider|MockInterface | ||
*/ | ||
protected function mockUserProvider(?User $user) | ||
{ | ||
/** @var UserProvider|MockInterface $userProvider */ | ||
$userProvider = Mockery::mock(UserProvider::class) | ||
->shouldReceive('retrieveByCredentials') | ||
->with([ | ||
'email' => 'foo@bar.com', | ||
'password' => 'supersecret', | ||
]) | ||
->andReturn($user) | ||
->getMock(); | ||
|
||
return $userProvider; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.