Push updates, removes repositories, begins moving functionality to services.

First integration tests included.
This commit is contained in:
Dane Everitt 2017-06-13 23:25:37 -05:00
parent 5c2b9deb09
commit 26e476a794
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
14 changed files with 492 additions and 102 deletions

View file

@ -0,0 +1,22 @@
<?php
namespace Tests;
use Illuminate\Contracts\Console\Kernel;
trait CreatesApplication
{
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
return $app;
}
}

View file

@ -1,16 +0,0 @@
<?php
class ExampleTest extends TestCase
{
/**
* A basic functional test example.
*
* @return void
*/
public function testBasicExample()
{
$this->visit('/')
->see('Laravel 5');
}
}

View file

@ -0,0 +1,156 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Tests\Feature\Services;
use Illuminate\Support\Facades\Notification;
use Pterodactyl\Models\User;
use Pterodactyl\Notifications\AccountCreated;
use Pterodactyl\Services\UserService;
use Tests\TestCase;
class UserServiceTest extends TestCase
{
protected $service;
public function setUp()
{
parent::setUp();
$this->service = $this->app->make(UserService::class);
}
public function testShouldReturnNewUserWithValidData()
{
Notification::fake();
$user = $this->service->create([
'email' => 'test_account@example.com',
'username' => 'test_account',
'password' => 'test_password',
'name_first' => 'Test',
'name_last' => 'Account',
'root_admin' => false,
]);
$this->assertNotNull($user->uuid);
$this->assertNotEquals($user->password, 'test_password');
$this->assertDatabaseHas('users', [
'id' => $user->id,
'uuid' => $user->uuid,
'email' => 'test_account@example.com',
'root_admin' => '0',
]);
Notification::assertSentTo($user, AccountCreated::class, function ($notification) use ($user) {
$this->assertEquals($user->username, $notification->user->username);
$this->assertNull($notification->user->token);
return true;
});
}
public function testShouldReturnNewUserWithPasswordTokenIfNoPasswordProvided()
{
Notification::fake();
$user = $this->service->create([
'email' => 'test_account@example.com',
'username' => 'test_account',
'name_first' => 'Test',
'name_last' => 'Account',
'root_admin' => false,
]);
$this->assertNotNull($user->uuid);
$this->assertNotNull($user->password);
$this->assertDatabaseHas('users', [
'id' => $user->id,
'uuid' => $user->uuid,
'email' => 'test_account@example.com',
'root_admin' => '0',
]);
Notification::assertSentTo($user, AccountCreated::class, function ($notification) use ($user) {
$this->assertEquals($user->username, $notification->user->username);
$this->assertNotNull($notification->user->token);
$this->assertDatabaseHas('password_resets', [
'email' => $user->email,
]);
return true;
});
}
public function testShouldUpdateUserModelInDatabase()
{
$user = factory(User::class)->create();
$response = $this->service->update($user, [
'email' => 'test_change@example.com',
'password' => 'test_password',
]);
$this->assertInstanceOf(User::class, $response);
$this->assertEquals('test_change@example.com', $response->email);
$this->assertNotEquals($response->password, 'test_password');
$this->assertDatabaseHas('users', [
'id' => $user->id,
'email' => 'test_change@example.com',
]);
}
public function testShouldDeleteUserFromDatabase()
{
$user = factory(User::class)->create();
$service = $this->app->make(UserService::class);
$response = $service->delete($user);
$this->assertTrue($response);
$this->assertDatabaseMissing('users', [
'id' => $user->id,
'uuid' => $user->uuid,
]);
}
/**
* @expectedException \Pterodactyl\Exceptions\DisplayException
*/
public function testShouldBlockDeletionOfOwnAccount()
{
$user = factory(User::class)->create();
$this->actingAs($user);
$this->service->delete($user);
}
public function testAlgoForHashingShouldBeRegistered()
{
$this->assertArrayHasKey(UserService::HMAC_ALGO, array_flip(hash_algos()));
}
}

View file

@ -1,25 +1,11 @@
<?php
class TestCase extends Illuminate\Foundation\Testing\TestCase
namespace Tests;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
/**
* The base URL to use while testing the application.
*
* @var string
*/
protected $baseUrl = 'http://localhost';
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__ . '/../bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
return $app;
}
use CreatesApplication, DatabaseTransactions;
}

View file

@ -0,0 +1,61 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Tests\Unit\Services;
use Illuminate\Config\Repository;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Database\Connection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Queue;
use \Mockery as m;
use Pterodactyl\Models\User;
use Pterodactyl\Services\Components\UuidService;
use Pterodactyl\Services\UserService;
use Tests\TestCase;
class UserServiceTest extends TestCase
{
protected $service;
public function setUp()
{
parent::setUp();
$this->config = m::mock(Repository::class);
$this->database = m::mock(Connection::class);
$this->guard = m::mock(Guard::class);
$this->hasher = m::mock(Hasher::class);
$this->uuid = m::mock(UuidService::class);
$this->service = new UserService(
$this->config,
$this->database,
$this->guard,
$this->hasher,
$this->uuid
);;
}
}