2017-06-25 00:49:09 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-06-25 00:49:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services;
|
|
|
|
|
|
|
|
use Mockery as m;
|
2017-06-25 20:37:45 +00:00
|
|
|
use Tests\TestCase;
|
2017-07-01 20:29:49 +00:00
|
|
|
use Illuminate\Foundation\Application;
|
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;
|
|
|
|
use Illuminate\Notifications\ChannelManager;
|
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-06-25 00:49:09 +00:00
|
|
|
use Pterodactyl\Services\Helpers\TemporaryPasswordService;
|
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-07-01 20:29:49 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Foundation\Application
|
|
|
|
*/
|
|
|
|
protected $appMock;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
|
|
|
*/
|
2017-06-25 00:49:09 +00:00
|
|
|
protected $database;
|
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Hashing\Hasher
|
|
|
|
*/
|
2017-06-25 00:49:09 +00:00
|
|
|
protected $hasher;
|
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Notifications\ChannelManager
|
|
|
|
*/
|
|
|
|
protected $notification;
|
2017-06-25 00:49:09 +00:00
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Helpers\TemporaryPasswordService
|
|
|
|
*/
|
2017-06-25 00:49:09 +00:00
|
|
|
protected $passwordService;
|
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
2017-08-27 20:10:51 +00:00
|
|
|
* @var \Pterodactyl\Services\Users\UserCreationService
|
2017-07-01 20:29:49 +00:00
|
|
|
*/
|
2017-06-25 00:49:09 +00:00
|
|
|
protected $service;
|
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
2017-06-25 00:49:09 +00:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->appMock = m::mock(Application::class);
|
|
|
|
$this->database = m::mock(ConnectionInterface::class);
|
2017-06-25 00:49:09 +00:00
|
|
|
$this->hasher = m::mock(Hasher::class);
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->notification = m::mock(ChannelManager::class);
|
2017-06-25 00:49:09 +00:00
|
|
|
$this->passwordService = m::mock(TemporaryPasswordService::class);
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->repository = m::mock(UserRepositoryInterface::class);
|
2017-06-25 00:49:09 +00:00
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
$this->service = new UserCreationService(
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->appMock,
|
|
|
|
$this->notification,
|
2017-06-25 00:49:09 +00:00
|
|
|
$this->database,
|
|
|
|
$this->hasher,
|
|
|
|
$this->passwordService,
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->repository
|
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
|
|
|
{
|
2017-07-01 20:29:49 +00:00
|
|
|
$user = (object) [
|
|
|
|
'name_first' => 'FirstName',
|
|
|
|
'username' => 'user_name',
|
|
|
|
];
|
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');
|
|
|
|
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->hasher->shouldNotReceive('make');
|
2017-09-27 03:16:26 +00:00
|
|
|
$this->passwordService->shouldNotReceive('handle');
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->repository->shouldReceive('create')->with(['password' => 'enc-password'])->once()->andReturn($user);
|
|
|
|
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->appMock->shouldReceive('makeWith')->with(AccountCreated::class, [
|
|
|
|
'user' => [
|
|
|
|
'name' => 'FirstName',
|
|
|
|
'username' => 'user_name',
|
|
|
|
'token' => null,
|
|
|
|
],
|
|
|
|
])->once()->andReturnNull();
|
|
|
|
|
|
|
|
$this->notification->shouldReceive('send')->with($user, null)->once()->andReturnNull();
|
|
|
|
|
2017-08-05 00:11:41 +00:00
|
|
|
$response = $this->service->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);
|
|
|
|
$this->assertEquals($user->username, $response->username);
|
|
|
|
$this->assertEquals($user->name_first, 'FirstName');
|
|
|
|
}
|
2017-06-25 00:49:09 +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
|
|
|
{
|
|
|
|
$user = (object) [
|
|
|
|
'name_first' => 'FirstName',
|
|
|
|
'username' => 'user_name',
|
|
|
|
'email' => 'user@example.com',
|
|
|
|
];
|
2017-06-25 00:49:09 +00:00
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->hasher->shouldNotReceive('make');
|
|
|
|
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->hasher->shouldReceive('make')->once()->andReturn('created-enc-password');
|
2017-09-27 03:16:26 +00:00
|
|
|
$this->passwordService->shouldReceive('handle')
|
2017-08-05 00:11:41 +00:00
|
|
|
->with('user@example.com')
|
|
|
|
->once()
|
|
|
|
->andReturn('random-token');
|
2017-07-01 20:29:49 +00:00
|
|
|
|
|
|
|
$this->repository->shouldReceive('create')->with([
|
|
|
|
'password' => 'created-enc-password',
|
|
|
|
'email' => 'user@example.com',
|
|
|
|
])->once()->andReturn($user);
|
|
|
|
|
|
|
|
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->appMock->shouldReceive('makeWith')->with(AccountCreated::class, [
|
|
|
|
'user' => [
|
|
|
|
'name' => 'FirstName',
|
|
|
|
'username' => 'user_name',
|
|
|
|
'token' => 'random-token',
|
|
|
|
],
|
|
|
|
])->once()->andReturnNull();
|
|
|
|
|
|
|
|
$this->notification->shouldReceive('send')->with($user, null)->once()->andReturnNull();
|
|
|
|
|
2017-08-05 00:11:41 +00:00
|
|
|
$response = $this->service->handle([
|
2017-07-01 20:29:49 +00:00
|
|
|
'email' => 'user@example.com',
|
|
|
|
]);
|
2017-06-25 00:49:09 +00:00
|
|
|
|
|
|
|
$this->assertNotNull($response);
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->assertEquals($user->username, $response->username);
|
|
|
|
$this->assertEquals($user->name_first, 'FirstName');
|
|
|
|
$this->assertEquals($user->email, $response->email);
|
2017-06-25 00:49:09 +00:00
|
|
|
}
|
|
|
|
}
|