2017-08-05 00:11:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Users;
|
|
|
|
|
2017-08-05 22:26:30 +00:00
|
|
|
use Mockery as m;
|
|
|
|
use Tests\TestCase;
|
2017-12-03 20:00:47 +00:00
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Illuminate\Support\Collection;
|
2017-08-05 00:11:41 +00:00
|
|
|
use Illuminate\Contracts\Hashing\Hasher;
|
2017-08-27 20:10:51 +00:00
|
|
|
use Pterodactyl\Services\Users\UserUpdateService;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
2017-08-05 00:11:41 +00:00
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
class UserUpdateServiceTest extends TestCase
|
2017-08-05 00:11:41 +00:00
|
|
|
{
|
|
|
|
/**
|
2017-12-03 20:00:47 +00:00
|
|
|
* @var \Illuminate\Contracts\Hashing\Hasher|\Mockery\Mock
|
2017-08-05 00:11:41 +00:00
|
|
|
*/
|
2017-12-03 20:00:47 +00:00
|
|
|
private $hasher;
|
2017-08-05 00:11:41 +00:00
|
|
|
|
|
|
|
/**
|
2020-09-13 18:38:42 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\UserRepository|\Mockery\Mock
|
2017-08-05 00:11:41 +00:00
|
|
|
*/
|
2017-12-03 20:00:47 +00:00
|
|
|
private $repository;
|
2017-08-05 00:11:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2017-08-05 00:11:41 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->hasher = m::mock(Hasher::class);
|
|
|
|
$this->repository = m::mock(UserRepositoryInterface::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-08 03:13:40 +00:00
|
|
|
* Test that the handle function does not attempt to hash a password if no
|
|
|
|
* password is provided or the password is null.
|
|
|
|
*
|
|
|
|
* @dataProvider badPasswordDataProvider
|
2017-08-05 00:11:41 +00:00
|
|
|
*/
|
2018-02-08 03:13:40 +00:00
|
|
|
public function testUpdateUserWithoutTouchingHasherIfNoPasswordPassed(array $data)
|
2017-08-05 00:11:41 +00:00
|
|
|
{
|
2017-12-03 20:00:47 +00:00
|
|
|
$user = factory(User::class)->make();
|
|
|
|
$this->repository->shouldReceive('update')->with($user->id, ['test-data' => 'value'])->once()->andReturnNull();
|
2017-08-05 00:11:41 +00:00
|
|
|
|
2018-02-08 03:13:40 +00:00
|
|
|
$response = $this->getService()->handle($user, $data);
|
2017-12-03 20:00:47 +00:00
|
|
|
$this->assertInstanceOf(Collection::class, $response);
|
|
|
|
$this->assertTrue($response->has('model'));
|
|
|
|
$this->assertTrue($response->has('exceptions'));
|
2017-08-05 00:11:41 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 03:13:40 +00:00
|
|
|
/**
|
|
|
|
* Provide a test data set with passwords that should not be hashed.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function badPasswordDataProvider(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[['test-data' => 'value']],
|
|
|
|
[['test-data' => 'value', 'password' => null]],
|
|
|
|
[['test-data' => 'value', 'password' => '']],
|
|
|
|
[['test-data' => 'value', 'password' => 0]],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-08-05 00:11:41 +00:00
|
|
|
/**
|
|
|
|
* Test that the handle function hashes a password if passed in the data array.
|
|
|
|
*/
|
|
|
|
public function testUpdateUserAndHashPasswordIfProvided()
|
|
|
|
{
|
2017-12-03 20:00:47 +00:00
|
|
|
$user = factory(User::class)->make();
|
2017-08-05 00:11:41 +00:00
|
|
|
$this->hasher->shouldReceive('make')->with('raw_pass')->once()->andReturn('enc_pass');
|
2017-12-03 20:00:47 +00:00
|
|
|
$this->repository->shouldReceive('update')->with($user->id, ['password' => 'enc_pass'])->once()->andReturnNull();
|
|
|
|
|
|
|
|
$response = $this->getService()->handle($user, ['password' => 'raw_pass']);
|
|
|
|
$this->assertInstanceOf(Collection::class, $response);
|
|
|
|
$this->assertTrue($response->has('model'));
|
|
|
|
$this->assertTrue($response->has('exceptions'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an admin can revoke a user's administrative status.
|
|
|
|
*/
|
|
|
|
public function testAdministrativeUserRevokingAdminStatus()
|
|
|
|
{
|
|
|
|
$user = factory(User::class)->make(['root_admin' => true]);
|
|
|
|
$service = $this->getService();
|
|
|
|
$service->setUserLevel(User::USER_LEVEL_ADMIN);
|
|
|
|
|
|
|
|
$this->repository->shouldReceive('update')->with($user->id, ['root_admin' => false])->once()->andReturnNull();
|
|
|
|
|
|
|
|
$response = $service->handle($user, ['root_admin' => false]);
|
|
|
|
$this->assertInstanceOf(Collection::class, $response);
|
|
|
|
$this->assertTrue($response->has('model'));
|
|
|
|
$this->assertTrue($response->has('exceptions'));
|
|
|
|
}
|
2017-08-05 00:11:41 +00:00
|
|
|
|
2017-12-03 20:00:47 +00:00
|
|
|
/**
|
|
|
|
* Test that a normal user is unable to set an administrative status for themselves.
|
|
|
|
*/
|
|
|
|
public function testNormalUserShouldNotRevokeAdminStatus()
|
|
|
|
{
|
|
|
|
$user = factory(User::class)->make(['root_admin' => false]);
|
|
|
|
$service = $this->getService();
|
|
|
|
$service->setUserLevel(User::USER_LEVEL_USER);
|
|
|
|
|
|
|
|
$this->repository->shouldReceive('update')->with($user->id, [])->once()->andReturnNull();
|
|
|
|
|
|
|
|
$response = $service->handle($user, ['root_admin' => true]);
|
|
|
|
$this->assertInstanceOf(Collection::class, $response);
|
|
|
|
$this->assertTrue($response->has('model'));
|
|
|
|
$this->assertTrue($response->has('exceptions'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of the service for testing.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Services\Users\UserUpdateService
|
|
|
|
*/
|
|
|
|
private function getService(): UserUpdateService
|
|
|
|
{
|
2020-09-13 18:38:42 +00:00
|
|
|
return new UserUpdateService($this->hasher, $this->repository);
|
2017-08-05 00:11:41 +00:00
|
|
|
}
|
|
|
|
}
|