misc_pterodactyl-panel/tests/Unit/Services/Users/UserCreationServiceTest.php

158 lines
5.2 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Unit\Services;
use Mockery as m;
2017-06-25 20:37:45 +00:00
use Tests\TestCase;
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;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Support\Facades\Notification;
use Pterodactyl\Notifications\AccountCreated;
use Pterodactyl\Services\Users\UserCreationService;
use Pterodactyl\Services\Helpers\TemporaryPasswordService;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
class UserCreationServiceTest extends TestCase
{
2017-12-31 02:25:04 +00:00
use MocksUuids;
/**
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
*/
private $connection;
/**
* @var \Illuminate\Contracts\Hashing\Hasher|\Mockery\Mock
*/
private $hasher;
/**
* @var \Pterodactyl\Services\Helpers\TemporaryPasswordService|\Mockery\Mock
*/
private $passwordService;
/**
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock
*/
private $repository;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
Notification::fake();
$this->connection = m::mock(ConnectionInterface::class);
$this->hasher = m::mock(Hasher::class);
$this->passwordService = m::mock(TemporaryPasswordService::class);
$this->repository = m::mock(UserRepositoryInterface::class);
}
/**
* Test that a user is created when a password is passed.
*/
public function testUserIsCreatedWhenPasswordIsProvided()
{
$user = factory(User::class)->make();
$this->hasher->shouldReceive('make')->with('raw-password')->once()->andReturn('enc-password');
$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(),
], true, true)->once()->andReturn($user);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->getService()->handle([
'password' => 'raw-password',
]);
$this->assertNotNull($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
/**
* Test that a UUID passed in the submission data is not used when
* creating the user.
*/
public function testUuidPassedInDataIsIgnored()
{
$user = factory(User::class)->make();
2017-12-31 02:25:04 +00:00
$this->hasher->shouldReceive('make')->andReturn('enc-password');
$this->connection->shouldReceive('beginTransaction')->andReturnNull();
2017-12-31 02:25:04 +00:00
$this->repository->shouldReceive('create')->with([
'password' => 'enc-password',
'uuid' => $this->getKnownUuid(),
], true, true)->once()->andReturn($user);
$this->connection->shouldReceive('commit')->andReturnNull();
2017-12-31 02:25:04 +00:00
$response = $this->getService()->handle([
2017-12-31 02:25:04 +00:00
'password' => 'raw-password',
'uuid' => 'test-uuid',
]);
$this->assertNotNull($response);
$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
}
/**
* Test that a user is created with a random password when no password is provided.
*/
public function testUserIsCreatedWhenNoPasswordIsProvided()
{
$user = factory(User::class)->make();
$this->hasher->shouldNotReceive('make');
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->hasher->shouldReceive('make')->once()->andReturn('created-enc-password');
$this->passwordService->shouldReceive('handle')->with($user->email)->once()->andReturn('random-token');
$this->repository->shouldReceive('create')->with([
'password' => 'created-enc-password',
'email' => $user->email,
2017-12-31 02:25:04 +00:00
'uuid' => $this->getKnownUuid(),
], true, true)->once()->andReturn($user);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->getService()->handle([
'email' => $user->email,
]);
$this->assertNotNull($response);
$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
{
return new UserCreationService($this->connection, $this->hasher, $this->passwordService, $this->repository);
}
}