2017-06-25 00:49:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services;
|
|
|
|
|
|
|
|
use Mockery as m;
|
2017-06-25 20:37:45 +00:00
|
|
|
use Tests\TestCase;
|
2018-01-01 18:13:08 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2017-12-31 02:25:04 +00:00
|
|
|
use Tests\Traits\MocksUuids;
|
2017-06-25 20:37:45 +00:00
|
|
|
use Illuminate\Contracts\Hashing\Hasher;
|
2017-07-01 20:29:49 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2018-01-01 18:13:08 +00:00
|
|
|
use Illuminate\Support\Facades\Notification;
|
2018-07-01 01:47:31 +00:00
|
|
|
use Illuminate\Contracts\Auth\PasswordBroker;
|
2017-06-25 00:49:09 +00:00
|
|
|
use Pterodactyl\Notifications\AccountCreated;
|
2017-08-27 20:10:51 +00:00
|
|
|
use Pterodactyl\Services\Users\UserCreationService;
|
2017-07-01 20:29:49 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
2017-06-25 00:49:09 +00:00
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
class UserCreationServiceTest extends TestCase
|
2017-06-25 00:49:09 +00:00
|
|
|
{
|
2017-12-31 02:25:04 +00:00
|
|
|
use MocksUuids;
|
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
/**
|
2018-01-01 18:13:08 +00:00
|
|
|
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
2017-07-01 20:29:49 +00:00
|
|
|
*/
|
2018-01-01 18:13:08 +00:00
|
|
|
private $connection;
|
2017-07-01 20:29:49 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-01 18:13:08 +00:00
|
|
|
* @var \Illuminate\Contracts\Hashing\Hasher|\Mockery\Mock
|
2017-07-01 20:29:49 +00:00
|
|
|
*/
|
2018-01-01 18:13:08 +00:00
|
|
|
private $hasher;
|
2017-06-25 00:49:09 +00:00
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
/**
|
2018-07-01 01:47:31 +00:00
|
|
|
* @var \Illuminate\Contracts\Auth\PasswordBroker|\Mockery\Mock
|
2017-07-01 20:29:49 +00:00
|
|
|
*/
|
2018-07-01 01:47:31 +00:00
|
|
|
private $passwordBroker;
|
2017-06-25 00:49:09 +00:00
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
/**
|
2018-01-01 18:13:08 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock
|
2017-07-01 20:29:49 +00:00
|
|
|
*/
|
2018-01-01 18:13:08 +00:00
|
|
|
private $repository;
|
2017-06-25 00:49:09 +00:00
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
2017-06-25 00:49:09 +00:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2018-01-01 18:13:08 +00:00
|
|
|
Notification::fake();
|
|
|
|
$this->connection = m::mock(ConnectionInterface::class);
|
2017-06-25 00:49:09 +00:00
|
|
|
$this->hasher = m::mock(Hasher::class);
|
2018-07-01 01:47:31 +00:00
|
|
|
$this->passwordBroker = m::mock(PasswordBroker::class);
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->repository = m::mock(UserRepositoryInterface::class);
|
2017-06-25 00:49:09 +00:00
|
|
|
}
|
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
/**
|
|
|
|
* Test that a user is created when a password is passed.
|
|
|
|
*/
|
2017-08-05 00:11:41 +00:00
|
|
|
public function testUserIsCreatedWhenPasswordIsProvided()
|
2017-06-25 00:49:09 +00:00
|
|
|
{
|
2018-01-01 18:13:08 +00:00
|
|
|
$user = factory(User::class)->make();
|
2017-06-25 00:49:09 +00:00
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->hasher->shouldReceive('make')->with('raw-password')->once()->andReturn('enc-password');
|
2018-01-01 18:13:08 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
2017-12-31 02:25:04 +00:00
|
|
|
$this->repository->shouldReceive('create')->with([
|
|
|
|
'password' => 'enc-password',
|
|
|
|
'uuid' => $this->getKnownUuid(),
|
2018-01-01 18:13:08 +00:00
|
|
|
], true, true)->once()->andReturn($user);
|
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
|
|
|
$response = $this->getService()->handle([
|
2017-07-01 20:29:49 +00:00
|
|
|
'password' => 'raw-password',
|
|
|
|
]);
|
2017-06-25 00:49:09 +00:00
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->assertNotNull($response);
|
2018-01-01 18:13:08 +00:00
|
|
|
Notification::assertSentTo($user, AccountCreated::class, function ($notification) use ($user) {
|
|
|
|
$this->assertSame($user, $notification->user);
|
|
|
|
$this->assertNull($notification->token);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
2017-07-01 20:29:49 +00:00
|
|
|
}
|
2017-06-25 00:49:09 +00:00
|
|
|
|
2017-12-31 02:25:04 +00:00
|
|
|
/**
|
|
|
|
* Test that a UUID passed in the submission data is not used when
|
|
|
|
* creating the user.
|
|
|
|
*/
|
|
|
|
public function testUuidPassedInDataIsIgnored()
|
|
|
|
{
|
2018-01-01 18:13:08 +00:00
|
|
|
$user = factory(User::class)->make();
|
2017-12-31 02:25:04 +00:00
|
|
|
|
|
|
|
$this->hasher->shouldReceive('make')->andReturn('enc-password');
|
2018-01-01 18:13:08 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->andReturnNull();
|
2017-12-31 02:25:04 +00:00
|
|
|
$this->repository->shouldReceive('create')->with([
|
|
|
|
'password' => 'enc-password',
|
|
|
|
'uuid' => $this->getKnownUuid(),
|
2018-01-01 18:13:08 +00:00
|
|
|
], true, true)->once()->andReturn($user);
|
|
|
|
$this->connection->shouldReceive('commit')->andReturnNull();
|
2017-12-31 02:25:04 +00:00
|
|
|
|
2018-01-01 18:13:08 +00:00
|
|
|
$response = $this->getService()->handle([
|
2017-12-31 02:25:04 +00:00
|
|
|
'password' => 'raw-password',
|
|
|
|
'uuid' => 'test-uuid',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertNotNull($response);
|
2018-01-01 18:13:08 +00:00
|
|
|
$this->assertInstanceOf(User::class, $response);
|
|
|
|
Notification::assertSentTo($user, AccountCreated::class, function ($notification) use ($user) {
|
|
|
|
$this->assertSame($user, $notification->user);
|
|
|
|
$this->assertNull($notification->token);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
2017-12-31 02:25:04 +00:00
|
|
|
}
|
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
/**
|
|
|
|
* Test that a user is created with a random password when no password is provided.
|
|
|
|
*/
|
2017-08-05 00:11:41 +00:00
|
|
|
public function testUserIsCreatedWhenNoPasswordIsProvided()
|
2017-07-01 20:29:49 +00:00
|
|
|
{
|
2018-01-01 18:13:08 +00:00
|
|
|
$user = factory(User::class)->make();
|
2017-06-25 00:49:09 +00:00
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->hasher->shouldNotReceive('make');
|
2018-01-01 18:13:08 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->hasher->shouldReceive('make')->once()->andReturn('created-enc-password');
|
2018-07-01 01:47:31 +00:00
|
|
|
$this->passwordBroker->shouldReceive('createToken')->with($user)->once()->andReturn('random-token');
|
2017-07-01 20:29:49 +00:00
|
|
|
|
|
|
|
$this->repository->shouldReceive('create')->with([
|
|
|
|
'password' => 'created-enc-password',
|
2018-01-01 18:13:08 +00:00
|
|
|
'email' => $user->email,
|
2017-12-31 02:25:04 +00:00
|
|
|
'uuid' => $this->getKnownUuid(),
|
2018-01-01 18:13:08 +00:00
|
|
|
], true, true)->once()->andReturn($user);
|
2017-07-01 20:29:49 +00:00
|
|
|
|
2018-01-01 18:13:08 +00:00
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
2017-07-01 20:29:49 +00:00
|
|
|
|
2018-01-01 18:13:08 +00:00
|
|
|
$response = $this->getService()->handle([
|
|
|
|
'email' => $user->email,
|
2017-07-01 20:29:49 +00:00
|
|
|
]);
|
2017-06-25 00:49:09 +00:00
|
|
|
|
|
|
|
$this->assertNotNull($response);
|
2018-01-01 18:13:08 +00:00
|
|
|
$this->assertInstanceOf(User::class, $response);
|
|
|
|
Notification::assertSentTo($user, AccountCreated::class, function ($notification) use ($user) {
|
|
|
|
$this->assertSame($user, $notification->user);
|
|
|
|
$this->assertSame('random-token', $notification->token);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a new instance of the service using mocked dependencies.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Services\Users\UserCreationService
|
|
|
|
*/
|
|
|
|
private function getService(): UserCreationService
|
|
|
|
{
|
2018-07-01 01:47:31 +00:00
|
|
|
return new UserCreationService($this->connection, $this->hasher, $this->passwordBroker, $this->repository);
|
2017-06-25 00:49:09 +00:00
|
|
|
}
|
|
|
|
}
|