2017-08-31 02:11:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Http\Controllers\Base;
|
|
|
|
|
|
|
|
use Mockery as m;
|
2017-12-03 20:00:47 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2017-08-31 02:11:14 +00:00
|
|
|
use Prologue\Alerts\AlertsMessageBag;
|
2017-08-31 02:14:20 +00:00
|
|
|
use Pterodactyl\Services\Users\UserUpdateService;
|
2017-12-03 20:00:47 +00:00
|
|
|
use Tests\Unit\Http\Controllers\ControllerTestCase;
|
2017-08-31 02:11:14 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Base\AccountController;
|
|
|
|
use Pterodactyl\Http\Requests\Base\AccountDataFormRequest;
|
|
|
|
|
2017-12-03 20:00:47 +00:00
|
|
|
class AccountControllerTest extends ControllerTestCase
|
2017-08-31 02:11:14 +00:00
|
|
|
{
|
|
|
|
/**
|
2017-12-03 20:00:47 +00:00
|
|
|
* @var \Prologue\Alerts\AlertsMessageBag|\Mockery\Mock
|
2017-08-31 02:11:14 +00:00
|
|
|
*/
|
|
|
|
protected $alert;
|
|
|
|
|
|
|
|
/**
|
2017-12-03 20:00:47 +00:00
|
|
|
* @var \Pterodactyl\Services\Users\UserUpdateService|\Mockery\Mock
|
2017-08-31 02:11:14 +00:00
|
|
|
*/
|
|
|
|
protected $updateService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->alert = m::mock(AlertsMessageBag::class);
|
|
|
|
$this->updateService = m::mock(UserUpdateService::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the index controller.
|
|
|
|
*/
|
|
|
|
public function testIndexController()
|
|
|
|
{
|
2017-12-03 20:00:47 +00:00
|
|
|
$response = $this->getController()->index();
|
2017-08-31 02:11:14 +00:00
|
|
|
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertIsViewResponse($response);
|
2017-08-31 02:11:14 +00:00
|
|
|
$this->assertViewNameEquals('base.account', $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test controller when password is being updated.
|
|
|
|
*/
|
|
|
|
public function testUpdateControllerForPassword()
|
|
|
|
{
|
2017-12-03 20:00:47 +00:00
|
|
|
$this->setRequestMockClass(AccountDataFormRequest::class);
|
2017-12-17 19:07:38 +00:00
|
|
|
$user = $this->generateRequestUserModel();
|
2017-12-03 20:00:47 +00:00
|
|
|
|
2017-08-31 02:11:14 +00:00
|
|
|
$this->request->shouldReceive('input')->with('do_action')->andReturn('password');
|
|
|
|
$this->request->shouldReceive('input')->with('new_password')->once()->andReturn('test-password');
|
|
|
|
|
2017-12-03 20:00:47 +00:00
|
|
|
$this->updateService->shouldReceive('setUserLevel')->with(User::USER_LEVEL_USER)->once()->andReturnNull();
|
2017-12-17 19:07:38 +00:00
|
|
|
$this->updateService->shouldReceive('handle')->with($user, ['password' => 'test-password'])->once()->andReturn(collect());
|
2017-08-31 02:11:14 +00:00
|
|
|
$this->alert->shouldReceive('success->flash')->once()->andReturnNull();
|
|
|
|
|
2017-12-03 20:00:47 +00:00
|
|
|
$response = $this->getController()->update($this->request);
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertIsRedirectResponse($response);
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->assertRedirectRouteEquals('account', $response);
|
2017-08-31 02:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test controller when email is being updated.
|
|
|
|
*/
|
|
|
|
public function testUpdateControllerForEmail()
|
|
|
|
{
|
2017-12-03 20:00:47 +00:00
|
|
|
$this->setRequestMockClass(AccountDataFormRequest::class);
|
2017-12-17 19:07:38 +00:00
|
|
|
$user = $this->generateRequestUserModel();
|
2017-12-03 20:00:47 +00:00
|
|
|
|
2017-08-31 02:11:14 +00:00
|
|
|
$this->request->shouldReceive('input')->with('do_action')->andReturn('email');
|
|
|
|
$this->request->shouldReceive('input')->with('new_email')->once()->andReturn('test@example.com');
|
|
|
|
|
2017-12-03 20:00:47 +00:00
|
|
|
$this->updateService->shouldReceive('setUserLevel')->with(User::USER_LEVEL_USER)->once()->andReturnNull();
|
2017-12-17 19:07:38 +00:00
|
|
|
$this->updateService->shouldReceive('handle')->with($user, ['email' => 'test@example.com'])->once()->andReturn(collect());
|
2017-08-31 02:11:14 +00:00
|
|
|
$this->alert->shouldReceive('success->flash')->once()->andReturnNull();
|
|
|
|
|
2017-12-03 20:00:47 +00:00
|
|
|
$response = $this->getController()->update($this->request);
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertIsRedirectResponse($response);
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->assertRedirectRouteEquals('account', $response);
|
2017-08-31 02:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test controller when identity is being updated.
|
|
|
|
*/
|
|
|
|
public function testUpdateControllerForIdentity()
|
|
|
|
{
|
2017-12-03 20:00:47 +00:00
|
|
|
$this->setRequestMockClass(AccountDataFormRequest::class);
|
2017-12-17 19:07:38 +00:00
|
|
|
$user = $this->generateRequestUserModel();
|
2017-12-03 20:00:47 +00:00
|
|
|
|
2017-08-31 02:11:14 +00:00
|
|
|
$this->request->shouldReceive('input')->with('do_action')->andReturn('identity');
|
2018-05-04 11:58:39 +00:00
|
|
|
$this->request->shouldReceive('only')->with(['name_first', 'name_last', 'username', 'language'])->once()->andReturn([
|
2017-08-31 02:11:14 +00:00
|
|
|
'test_data' => 'value',
|
|
|
|
]);
|
|
|
|
|
2017-12-03 20:00:47 +00:00
|
|
|
$this->updateService->shouldReceive('setUserLevel')->with(User::USER_LEVEL_USER)->once()->andReturnNull();
|
2017-12-17 19:07:38 +00:00
|
|
|
$this->updateService->shouldReceive('handle')->with($user, ['test_data' => 'value'])->once()->andReturn(collect());
|
2017-08-31 02:11:14 +00:00
|
|
|
$this->alert->shouldReceive('success->flash')->once()->andReturnNull();
|
|
|
|
|
2017-12-03 20:00:47 +00:00
|
|
|
$response = $this->getController()->update($this->request);
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertIsRedirectResponse($response);
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->assertRedirectRouteEquals('account', $response);
|
2017-08-31 02:11:14 +00:00
|
|
|
}
|
2017-12-03 20:00:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of the controller for testing.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Http\Controllers\Base\AccountController
|
|
|
|
*/
|
|
|
|
private function getController(): AccountController
|
|
|
|
{
|
|
|
|
return new AccountController($this->alert, $this->updateService);
|
|
|
|
}
|
2017-08-31 02:11:14 +00:00
|
|
|
}
|