Begin unit tests for repositories
This commit is contained in:
parent
72735c24f7
commit
f451e4dc47
18 changed files with 734 additions and 43 deletions
|
@ -0,0 +1,87 @@
|
|||
<?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\Repositories\Eloquent;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Mockery as m;
|
||||
use Pterodactyl\Models\Allocation;
|
||||
use Pterodactyl\Repositories\Eloquent\AllocationRepository;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AllocationRepositoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
protected $builder;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Repositories\Eloquent\AllocationRepository
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->builder = m::mock(Builder::class);
|
||||
$this->repository = m::mock(AllocationRepository::class)->makePartial();
|
||||
|
||||
$this->repository->shouldReceive('getBuilder')->withNoArgs()->andReturn($this->builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that we are returning the correct model.
|
||||
*/
|
||||
public function testCorrectModelIsAssigned()
|
||||
{
|
||||
$this->assertEquals(Allocation::class, $this->repository->model());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that allocations can be assigned to a server correctly.
|
||||
*/
|
||||
public function testAllocationsAreAssignedToAServer()
|
||||
{
|
||||
$this->builder->shouldReceive('whereIn')->with('id', [1, 2])->once()->andReturnSelf()
|
||||
->shouldReceive('update')->with(['server_id' => 10])->once()->andReturn(true);
|
||||
|
||||
$this->assertTrue($this->repository->assignAllocationsToServer(10, [1, 2]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that allocations with a node relationship are returned.
|
||||
*/
|
||||
public function testAllocationsForANodeAreReturned()
|
||||
{
|
||||
$this->builder->shouldReceive('where')->with('node_id', 1)->once()->andReturnSelf()
|
||||
->shouldReceive('get')->once()->andReturn(factory(Allocation::class)->make());
|
||||
|
||||
$this->assertInstanceOf(Allocation::class, $this->repository->getAllocationsForNode(1));
|
||||
}
|
||||
}
|
65
tests/Unit/Repositories/Eloquent/ApiKeyRepositoryTest.php
Normal file
65
tests/Unit/Repositories/Eloquent/ApiKeyRepositoryTest.php
Normal file
|
@ -0,0 +1,65 @@
|
|||
<?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\Repositories\Eloquent;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Mockery as m;
|
||||
use Pterodactyl\Models\APIKey;
|
||||
use Pterodactyl\Repositories\Eloquent\ApiKeyRepository;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ApiKeyRepositoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
protected $builder;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Repositories\Eloquent\ApiKeyRepository
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->builder = m::mock(Builder::class);
|
||||
$this->repository = m::mock(ApiKeyRepository::class)->makePartial();
|
||||
|
||||
$this->repository->shouldReceive('getBuilder')->withNoArgs()->andReturn($this->builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that we are returning the correct model.
|
||||
*/
|
||||
public function testCorrectModelIsAssigned()
|
||||
{
|
||||
$this->assertEquals(APIKey::class, $this->repository->model());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
<?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\Repositories\Eloquent;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Mockery as m;
|
||||
use Pterodactyl\Models\APIPermission;
|
||||
use Pterodactyl\Repositories\Eloquent\ApiPermissionRepository;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ApiPermissionRepositoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
protected $builder;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Repositories\Eloquent\ApiPermissionRepository
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->builder = m::mock(Builder::class);
|
||||
$this->repository = m::mock(ApiPermissionRepository::class)->makePartial();
|
||||
|
||||
$this->repository->shouldReceive('getBuilder')->withNoArgs()->andReturn($this->builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that we are returning the correct model.
|
||||
*/
|
||||
public function testCorrectModelIsAssigned()
|
||||
{
|
||||
$this->assertEquals(APIPermission::class, $this->repository->model());
|
||||
}
|
||||
}
|
103
tests/Unit/Repositories/Eloquent/DatabaseHostRepositoryTest.php
Normal file
103
tests/Unit/Repositories/Eloquent/DatabaseHostRepositoryTest.php
Normal file
|
@ -0,0 +1,103 @@
|
|||
<?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\Repositories\Eloquent;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Mockery as m;
|
||||
use Pterodactyl\Models\DatabaseHost;
|
||||
use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository;
|
||||
use Tests\TestCase;
|
||||
|
||||
class DatabaseHostRepositoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
protected $builder;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Repositories\Eloquent\DatabaseHostRepository
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->builder = m::mock(Builder::class);
|
||||
$this->repository = m::mock(DatabaseHostRepository::class)->makePartial();
|
||||
|
||||
$this->repository->shouldReceive('getBuilder')->withNoArgs()->andReturn($this->builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that we are returning the correct model.
|
||||
*/
|
||||
public function testCorrectModelIsAssigned()
|
||||
{
|
||||
$this->assertEquals(DatabaseHost::class, $this->repository->model());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test query to reutrn all of the default view data.
|
||||
*/
|
||||
public function testHostWithDefaultViewDataIsReturned()
|
||||
{
|
||||
$this->builder->shouldReceive('withCount')->with('databases')->once()->andReturnSelf()
|
||||
->shouldReceive('with')->with('node')->once()->andReturnSelf()
|
||||
->shouldReceive('get')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$this->assertNull($this->repository->getWithViewDetails());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test query to return host and servers.
|
||||
*/
|
||||
public function testHostIsReturnedWithServers()
|
||||
{
|
||||
$model = factory(DatabaseHost::class)->make();
|
||||
|
||||
$this->builder->shouldReceive('with')->with('databases.server')->once()->andReturnSelf()
|
||||
->shouldReceive('find')->with(1, ['*'])->once()->andReturn($model);
|
||||
|
||||
$this->assertEquals($model, $this->repository->getWithServers(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test exception is found if no host is found when querying for servers.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function testExceptionIsThrownIfNoRecordIsFoundWithServers()
|
||||
{
|
||||
$this->builder->shouldReceive('with')->with('databases.server')->once()->andReturnSelf()
|
||||
->shouldReceive('find')->with(1, ['*'])->once()->andReturnNull();
|
||||
|
||||
$this->repository->getWithServers(1);
|
||||
}
|
||||
}
|
172
tests/Unit/Repositories/Eloquent/DatabaseRepositoryTest.php
Normal file
172
tests/Unit/Repositories/Eloquent/DatabaseRepositoryTest.php
Normal file
|
@ -0,0 +1,172 @@
|
|||
<?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\Repositories\Eloquent;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Mockery as m;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException;
|
||||
use Pterodactyl\Models\Database;
|
||||
use Pterodactyl\Repositories\Eloquent\DatabaseRepository;
|
||||
use Tests\TestCase;
|
||||
|
||||
class DatabaseRepositoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
protected $builder;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Repositories\Eloquent\DatabaseRepository
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->builder = m::mock(Builder::class);
|
||||
$this->repository = m::mock(DatabaseRepository::class)->makePartial()->shouldAllowMockingProtectedMethods();
|
||||
|
||||
$this->repository->shouldReceive('getBuilder')->withNoArgs()->andReturn($this->builder);
|
||||
$this->repository->shouldNotReceive('runStatement');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that we are returning the correct model.
|
||||
*/
|
||||
public function testCorrectModelIsAssigned()
|
||||
{
|
||||
$this->assertEquals(Database::class, $this->repository->model());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a database can be created if it does not already exist.
|
||||
*/
|
||||
public function testDatabaseIsCreatedIfNotExists()
|
||||
{
|
||||
$data = [
|
||||
'server_id' => 1,
|
||||
'database_host_id' => 100,
|
||||
'database' => 'somename',
|
||||
];
|
||||
|
||||
$this->builder->shouldReceive('where')->with([
|
||||
['server_id', '=', array_get($data, 'server_id')],
|
||||
['database_host_id', '=', array_get($data, 'database_host_id')],
|
||||
['database', '=', array_get($data, 'database')],
|
||||
])->once()->andReturnSelf()
|
||||
->shouldReceive('count')->withNoArgs()->once()->andReturn(0);
|
||||
|
||||
$this->repository->shouldReceive('create')->with($data)->once()->andReturn(true);
|
||||
|
||||
$this->assertTrue($this->repository->createIfNotExists($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown if a database already exists with the given name.
|
||||
*/
|
||||
public function testExceptionIsThrownIfDatabaseAlreadyExists()
|
||||
{
|
||||
$this->builder->shouldReceive('where->count')->once()->andReturn(1);
|
||||
$this->repository->shouldNotReceive('create');
|
||||
|
||||
try {
|
||||
$this->repository->createIfNotExists([]);
|
||||
} catch (DisplayException $exception) {
|
||||
$this->assertInstanceOf(DuplicateDatabaseNameException::class, $exception);
|
||||
$this->assertEquals('A database with those details already exists for the specified server.', $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test SQL used to create a database.
|
||||
*/
|
||||
public function testCreateDatabaseStatement()
|
||||
{
|
||||
$query = sprintf('CREATE DATABASE IF NOT EXISTS `%s`', 'test_database');
|
||||
$this->repository->shouldReceive('runStatement')->with($query, 'test')->once()->andReturn(true);
|
||||
|
||||
$this->assertTrue($this->repository->createDatabase('test_database', 'test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test SQL used to create a user.
|
||||
*/
|
||||
public function testCreateUserStatement()
|
||||
{
|
||||
$query = sprintf('CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'', 'test', '%', 'password');
|
||||
$this->repository->shouldReceive('runStatement')->with($query, 'test')->once()->andReturn(true);
|
||||
|
||||
$this->assertTrue($this->repository->createUser('test', '%', 'password', 'test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a user is assigned the correct permissions on a database.
|
||||
*/
|
||||
public function testUserAssignmentToDatabaseStatement()
|
||||
{
|
||||
$query = sprintf('GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX ON `%s`.* TO `%s`@`%s`', 'test_database', 'test', '%');
|
||||
$this->repository->shouldReceive('runStatement')->with($query, 'test')->once()->andReturn(true);
|
||||
|
||||
$this->assertTrue($this->repository->assignUserToDatabase('test_database', 'test', '%', 'test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test SQL for flushing privileges.
|
||||
*/
|
||||
public function testFlushStatement()
|
||||
{
|
||||
$this->repository->shouldReceive('runStatement')->with('FLUSH PRIVILEGES', 'test')->once()->andReturn(true);
|
||||
|
||||
$this->assertTrue($this->repository->flush('test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test SQL to drop a database.
|
||||
*/
|
||||
public function testDropDatabaseStatement()
|
||||
{
|
||||
$query = sprintf('DROP DATABASE IF EXISTS `%s`', 'test_database');
|
||||
$this->repository->shouldReceive('runStatement')->with($query, 'test')->once()->andReturn(true);
|
||||
|
||||
$this->assertTrue($this->repository->dropDatabase('test_database', 'test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test SQL to drop a user.
|
||||
*/
|
||||
public function testDropUserStatement()
|
||||
{
|
||||
$query = sprintf('DROP USER IF EXISTS `%s`@`%s`', 'test', '%');
|
||||
$this->repository->shouldReceive('runStatement')->with($query, 'test')->once()->andReturn(true);
|
||||
|
||||
$this->assertTrue($this->repository->dropUser('test', '%', 'test'));
|
||||
}
|
||||
}
|
115
tests/Unit/Repositories/Eloquent/LocationRepositoryTest.php
Normal file
115
tests/Unit/Repositories/Eloquent/LocationRepositoryTest.php
Normal file
|
@ -0,0 +1,115 @@
|
|||
<?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\Repositories\Eloquent;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Mockery as m;
|
||||
use Pterodactyl\Models\Location;
|
||||
use Pterodactyl\Repositories\Eloquent\LocationRepository;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LocationRepositoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
protected $builder;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Repositories\Eloquent\LocationRepository
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->builder = m::mock(Builder::class);
|
||||
$this->repository = m::mock(LocationRepository::class)->makePartial();
|
||||
|
||||
$this->repository->shouldReceive('getBuilder')->withNoArgs()->andReturn($this->builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that we are returning the correct model.
|
||||
*/
|
||||
public function testCorrectModelIsAssigned()
|
||||
{
|
||||
$this->assertEquals(Location::class, $this->repository->model());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that all locations with associated node and server counts are returned.
|
||||
*/
|
||||
public function testAllLocationsWithDetailsAreReturned()
|
||||
{
|
||||
$this->builder->shouldReceive('withCount')->with('nodes', 'servers')->once()->andReturnSelf()
|
||||
->shouldReceive('get')->with(['*'])->once()->andReturnNull();
|
||||
|
||||
$this->assertNull($this->repository->getAllWithDetails());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that all locations with associated node are returned.
|
||||
*/
|
||||
public function testAllLocationsWithNodes()
|
||||
{
|
||||
$this->builder->shouldReceive('with')->with('nodes')->once()->andReturnSelf()
|
||||
->shouldReceive('get')->with(['*'])->once()->andReturnNull();
|
||||
|
||||
$this->assertNull($this->repository->getAllWithNodes());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a single location with associated node is returned.
|
||||
*/
|
||||
public function testLocationWithNodeIsReturned()
|
||||
{
|
||||
$model = factory(Location::class)->make();
|
||||
|
||||
$this->builder->shouldReceive('with')->with('nodes.servers')->once()->andReturnSelf()
|
||||
->shouldReceive('find')->with(1, ['*'])->once()->andReturn($model);
|
||||
|
||||
$response = $this->repository->getWithNodes(1);
|
||||
$this->assertInstanceOf(Location::class, $response);
|
||||
$this->assertEquals($model, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown when getting location with nodes if no location is found.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function testExceptionIsThrownIfNoLocationIsFoundWithNodes()
|
||||
{
|
||||
$this->builder->shouldReceive('with')->with('nodes.servers')->once()->andReturnSelf()
|
||||
->shouldReceive('find')->with(1, ['*'])->once()->andReturnNull();
|
||||
|
||||
$this->repository->getWithNodes(1);
|
||||
}
|
||||
}
|
|
@ -25,6 +25,8 @@
|
|||
namespace Tests\Unit\Services\Administrative;
|
||||
|
||||
use Mockery as m;
|
||||
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Database\DatabaseManager;
|
||||
use Illuminate\Contracts\Encryption\Encrypter;
|
||||
|
@ -39,6 +41,11 @@ class DatabaseHostServiceTest extends TestCase
|
|||
*/
|
||||
protected $database;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
|
||||
*/
|
||||
protected $databaseRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Extensions\DynamicDatabaseConnection
|
||||
*/
|
||||
|
@ -67,12 +74,14 @@ class DatabaseHostServiceTest extends TestCase
|
|||
parent::setUp();
|
||||
|
||||
$this->database = m::mock(DatabaseManager::class);
|
||||
$this->databaseRepository = m::mock(DatabaseRepositoryInterface::class);
|
||||
$this->dynamic = m::mock(DynamicDatabaseConnection::class);
|
||||
$this->encrypter = m::mock(Encrypter::class);
|
||||
$this->repository = m::mock(DatabaseHostRepositoryInterface::class);
|
||||
|
||||
$this->service = new DatabaseHostService(
|
||||
$this->database,
|
||||
$this->databaseRepository,
|
||||
$this->repository,
|
||||
$this->dynamic,
|
||||
$this->encrypter
|
||||
|
@ -82,7 +91,7 @@ class DatabaseHostServiceTest extends TestCase
|
|||
/**
|
||||
* Test that creating a host returns the correct data.
|
||||
*/
|
||||
public function test_create_host_function()
|
||||
public function testHostIsCreated()
|
||||
{
|
||||
$data = [
|
||||
'password' => 'raw-password',
|
||||
|
@ -130,7 +139,7 @@ class DatabaseHostServiceTest extends TestCase
|
|||
/**
|
||||
* Test that passing a password will store an encrypted version in the DB.
|
||||
*/
|
||||
public function test_update_with_password()
|
||||
public function testHostIsUpdatedWithPasswordProvided()
|
||||
{
|
||||
$finalData = (object) ['password' => 'enc-pass', 'host' => '123.456.78.9'];
|
||||
|
||||
|
@ -158,7 +167,7 @@ class DatabaseHostServiceTest extends TestCase
|
|||
/**
|
||||
* Test that passing no or empty password will skip storing it.
|
||||
*/
|
||||
public function test_update_without_password()
|
||||
public function testHostIsUpdatedWithoutPassword()
|
||||
{
|
||||
$finalData = (object) ['host' => '123.456.78.9'];
|
||||
|
||||
|
@ -182,12 +191,27 @@ class DatabaseHostServiceTest extends TestCase
|
|||
/**
|
||||
* Test that a database host can be deleted.
|
||||
*/
|
||||
public function test_delete_function()
|
||||
public function testHostIsDeleted()
|
||||
{
|
||||
$this->repository->shouldReceive('deleteIfNoDatabases')->with(1)->once()->andReturn(true);
|
||||
$this->databaseRepository->shouldReceive('findCountWhere')->with([['database_host_id', '=', 1]])->once()->andReturn(0);
|
||||
$this->repository->shouldReceive('delete')->with(1)->once()->andReturn(true);
|
||||
|
||||
$response = $this->service->delete(1);
|
||||
|
||||
$this->assertTrue($response, 'Assert that response is true.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test exception is thrown when there are databases attached to a host.
|
||||
*/
|
||||
public function testExceptionIsThrownIfHostHasDatabases()
|
||||
{
|
||||
$this->databaseRepository->shouldReceive('findCountWhere')->with([['database_host_id', '=', 1]])->once()->andReturn(2);
|
||||
|
||||
try {
|
||||
$this->service->delete(1);
|
||||
} catch (DisplayException $exception) {
|
||||
$this->assertEquals(trans('admin/exceptions.databases.delete_has_databases'), $exception->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue