Skip to content

Commit

Permalink
Merge e41d7cb into 4ea7945
Browse files Browse the repository at this point in the history
  • Loading branch information
wimski authored Apr 21, 2021
2 parents 4ea7945 + e41d7cb commit ef275c7
Show file tree
Hide file tree
Showing 7 changed files with 442 additions and 18 deletions.
28 changes: 25 additions & 3 deletions src/GraphQL/Mutations/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Support\Arr;
use Laravel\Sanctum\Contracts\HasApiTokens;

class Register
Expand All @@ -21,15 +23,18 @@ class Register

protected AuthManager $authManager;
protected Config $config;
protected Hasher $hash;
protected EmailVerificationServiceInterface $emailVerificationService;

public function __construct(
AuthManager $authManager,
Config $config,
Hasher $hash,
EmailVerificationServiceInterface $emailVerificationService
) {
$this->authManager = $authManager;
$this->config = $config;
$this->hash = $hash;
$this->emailVerificationService = $emailVerificationService;
}

Expand All @@ -45,18 +50,18 @@ public function __invoke($_, array $args): array
$userProvider = $this->createUserProvider();

$user = $userProvider->createModel();
$user->fill($args);
$user->fill($this->getPropertiesFromArgs($args));
$user->save();

if ($user instanceof MustVerifyEmail) {
if ($args['verification_url']) {
if (isset($args['verification_url'])) {
$this->emailVerificationService->setVerificationUrl($args['verification_url']['url']);
}

$user->sendEmailVerificationNotification();

return [
'tokens' => [],
'token' => null,
'status' => RegisterStatus::MUST_VERIFY_EMAIL(),
];
}
Expand All @@ -71,6 +76,23 @@ public function __invoke($_, array $args): array
];
}

/**
* @param array<string, mixed> $args
* @return array<string, string>
*/
protected function getPropertiesFromArgs(array $args): array
{
$properties = Arr::except($args, [
'directive',
'password_confirmation',
'verification_url',
]);

$properties['password'] = $this->hash->make($properties['password']);

return $properties;
}

protected function getAuthManager(): AuthManager
{
return $this->authManager;
Expand Down
2 changes: 2 additions & 0 deletions tests/Integration/AbstractIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DanielDeWit\LighthouseSanctum\Tests\Integration;

use DanielDeWit\LighthouseSanctum\Providers\LighthouseSanctumServiceProvider;
use DanielDeWit\LighthouseSanctum\Tests\stubs\Users\UserHasApiTokens;
use DanielDeWit\LighthouseSanctum\Tests\Traits\AssertsGraphQLErrorMessage;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\RefreshDatabase;
Expand Down Expand Up @@ -46,6 +47,7 @@ protected function defineDatabaseMigrations(): void
*/
protected function defineEnvironment($app): void
{
$app['config']->set('auth.providers.users.model', UserHasApiTokens::class);
$app['config']->set('lighthouse.schema.register', $this->getStubsPath('schema.graphql'));
$app['config']->set('lighthouse.guard', 'sanctum');
}
Expand Down
15 changes: 0 additions & 15 deletions tests/Integration/GraphQL/Mutations/LogoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,12 @@

namespace DanielDeWit\LighthouseSanctum\Tests\Integration\GraphQL\Mutations;

use DanielDeWit\LighthouseSanctum\GraphQL\Mutations\Logout;
use DanielDeWit\LighthouseSanctum\Tests\Integration\AbstractIntegrationTest;
use DanielDeWit\LighthouseSanctum\Tests\stubs\Users\UserHasApiTokens;
use Illuminate\Contracts\Auth\Factory as AuthFactory;
use Illuminate\Contracts\Translation\Translator;
use Laravel\Sanctum\Sanctum;

class LogoutTest extends AbstractIntegrationTest
{
protected Logout $mutation;

protected function setUp(): void
{
parent::setUp();

$this->mutation = new Logout(
$this->app->make(AuthFactory::class),
$this->app->make(Translator::class),
);
}

/**
* @test
*/
Expand Down
85 changes: 85 additions & 0 deletions tests/Integration/GraphQL/Mutations/RegisterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace DanielDeWit\LighthouseSanctum\Tests\Integration\GraphQL\Mutations;

use DanielDeWit\LighthouseSanctum\Enums\RegisterStatus;
use DanielDeWit\LighthouseSanctum\Tests\Integration\AbstractIntegrationTest;
use DanielDeWit\LighthouseSanctum\Tests\stubs\Users\UserMustVerifyEmail;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Support\Facades\Notification;
use Illuminate\Testing\TestResponse;

class RegisterTest extends AbstractIntegrationTest
{
/**
* @test
*/
public function it_registers_a_user(): void
{
$response = $this->makeRequest()->assertJsonStructure([
'data' => [
'register' => [
'token',
'status',
],
],
]);

static::assertTrue(RegisterStatus::SUCCESS()->is($response->json('data.register.status')));

$this->assertDatabaseHas('users', [
'name' => 'Foo Bar',
'email' => 'foo@bar.com',
]);
}

/**
* @test
*/
public function it_sends_an_email_verification_notification(): void
{
Notification::fake();

$this->app['config']->set('auth.providers.users.model', UserMustVerifyEmail::class);

$response = $this->makeRequest()->assertJsonStructure([
'data' => [
'register' => [
'token',
'status',
],
],
]);

static::assertNull($response->json('data.register.token'));
static::assertTrue(RegisterStatus::MUST_VERIFY_EMAIL()->is($response->json('data.register.status')));

$this->assertDatabaseHas('users', [
'name' => 'Foo Bar',
'email' => 'foo@bar.com',
]);

$user = UserMustVerifyEmail::first();

Notification::assertSentTo($user, VerifyEmail::class);
}

protected function makeRequest(): TestResponse
{
return $this->graphQL(/** @lang GraphQL */'
mutation {
register(input: {
name: "Foo Bar",
email: "foo@bar.com",
password: "supersecret",
password_confirmation: "supersecret",
}) {
token
status
}
}
');
}
}
50 changes: 50 additions & 0 deletions tests/Traits/MocksUserProvider.php
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);
}
Loading

0 comments on commit ef275c7

Please sign in to comment.