Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ci/phpunit/TestBase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php


namespace Hashtopolis;

use Hashtopolis\dba\AbstractModel;
use Hashtopolis\dba\AbstractModelFactory;
use Hashtopolis\dba\models\User;
Expand Down
208 changes: 201 additions & 7 deletions ci/phpunit/dba/AbstractModelFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
<?php

namespace dba;
namespace Hashtopolis\dba;

use Hashtopolis\dba\models\Agent;
use Hashtopolis\dba\models\CrackerBinary;
use Hashtopolis\dba\models\CrackerBinaryType;
use Hashtopolis\dba\models\Hash;
use TestBase;
use Hashtopolis\dba\models\HashType;
use Hashtopolis\dba\models\HealthCheck;
use Hashtopolis\dba\models\HealthCheckAgent;
use Hashtopolis\TestBase;
use Random\RandomException;
use Hashtopolis\dba\models\Hashlist;
use Hashtopolis\dba\OrderFilter;
use Exception;
use Hashtopolis\inc\defines\DHashlistFormat;
use Hashtopolis\inc\utils\AccessUtils;
use Hashtopolis\dba\Factory;
use Hashtopolis\dba\QueryFilter;
use Hashtopolis\dba\models\User;

require_once(dirname(__FILE__) . '/../TestBase.php');
Expand All @@ -25,6 +29,194 @@ public function testGetDBWithTest(): void {
$this->assertNotNull($db);
}

/**
* For non-mapped tables, the mapped table should be the same.
*
* @return void
*/
public function testGetMappedModelTableWithoutMapping(): void {
$agentFactory = Factory::getAgentFactory();
$this->assertEquals($agentFactory->getModelTable(), $agentFactory->getMappedModelTable());
}

/**
* For mapped tables, the mapped table should have the prefix
*
* @return void
*/
public function testGetMappedModelTableWithMapping(): void {
$userFactory = Factory::getUserFactory();
$this->assertEquals("htp_" . $userFactory->getModelTable(), $userFactory->getMappedModelTable());
}

/**
* Test with a normal model where no remapping is needed.
*
* @return void
*/
public function testGetMappedModelKeysWithoutRemapping(): void {
$hashType = new HashType(null, 'placeholder', 0, 0);
$dict = $hashType->getKeyValueDict();
$dict_mapped = AbstractModelFactory::getMappedModelKeys($hashType);
$this->assertEquals(array_keys($dict), $dict_mapped);
}

/**
* Test with a model where remapping is needed on a column
*
* @return void
*/
public function testGetMappedModelKeysWithRemapping(): void {
$healthCheckAgent = new HealthCheckAgent(null, 1, 1, 0, 0, 0, 0, 5, '');
$dict = $healthCheckAgent->getKeyValueDict();
$dict_mapped = AbstractModelFactory::getMappedModelKeys($healthCheckAgent);
$this->assertNotEquals(array_keys($dict), $dict_mapped);
$this->assertContains("htp_end", $dict_mapped);
}

/**
* Test that for a non-mapped key the return value just remains the same as it was before
*
* @return void
*/
public function testGetMappedModelKeyWithoutRemapping(): void {
$hashType = new HashType(null, 'placeholder', 0, 0);
$key_mapped = AbstractModelFactory::getMappedModelKey($hashType, HashType::IS_SALTED);
$this->assertEquals(HashType::IS_SALTED, $key_mapped);
}

/**
* Test that for a aapped key the return value gets mapped
Comment thread
s3inlc marked this conversation as resolved.
Outdated
*
* @return void
*/
public function testGetMappedModelKeyWithRemapping(): void {
$healthCheckAgent = new HealthCheckAgent(null, 1, 1, 0, 0, 0, 0, 5, '');
$key_mapped = AbstractModelFactory::getMappedModelKey($healthCheckAgent, HealthCheckAgent::END);
$this->assertEquals("htp_end", $key_mapped);
}

/**
* Test creating a hash type object and saving it.
*
* @return void
* @throws RandomException
*/
public function testSaveModelSuccessStaticId(): void {
$id = 100000 + random_int(10, 999);
$hashType = new HashType($id, 'placeholder', 0, 0);
$hashType = Factory::getHashTypeFactory()->save($hashType);
$this->registerDatabaseObject(Factory::getHashTypeFactory(), $hashType);
$this->assertEquals($id, $hashType->getId());
}

/**
* Test creating a hash type object without providing an id and let it auto increment.
*
* @return void
*/
public function testSaveModelSuccessNoId(): void {
$hashType = new HashType(null, 'placeholder', 0, 0);
$hashType = Factory::getHashTypeFactory()->save($hashType);
$this->registerDatabaseObject(Factory::getHashTypeFactory(), $hashType);
$this->assertNotEquals(null, $hashType->getId());
}

/**
* Test just updating a model without any change, should remain the same in the database.
*
* @return void
* @throws Exception
*/
public function testUpdateModelSuccessNoChanges(): void {
$hashType = new HashType(null, 'placeholder', 0, 0);
$hashType = $this->createDatabaseObject(Factory::getHashTypeFactory(), $hashType);
$this->assertNotNull($hashType->getId());

Factory::getHashTypeFactory()->update($hashType);
$hashTypeUpdated = Factory::getHashTypeFactory()->get($hashType->getId());
$this->assertEquals($hashType->getKeyValueDict(), $hashTypeUpdated->getKeyValueDict());
}

/**
* Test updating a model with a single change.
*
* @return void
* @throws Exception
*/
public function testUpdateModelSuccessSingleChange(): void {
$hashType = new HashType(null, 'placeholder', 0, 0);
$hashType = $this->createDatabaseObject(Factory::getHashTypeFactory(), $hashType);
$this->assertNotNull($hashType->getId());
$this->assertTrue($hashType instanceof HashType);

$hashType->setDescription('HashType X');
Factory::getHashTypeFactory()->update($hashType);

$hashTypeUpdated = Factory::getHashTypeFactory()->get($hashType->getId());
$this->assertEquals('HashType X', $hashTypeUpdated->getDescription());
}

/**
* Test updating a model with a single change on a column which needs to be mapped to check mapping functionality.
* We have to create some objects first and then be able to create a HealthCheckAgent relation where the column
* 'end' needs to be mapped in the database as it is a reserved keyword. We check that we get the update from a
* re-read from the database
*
* @return void
* @throws Exception
*/
public function testUpdateModelSuccessSingleChangeOnMappedColumn(): void {
$agent = new Agent(null, '', '', 0, '', '', 0, 0, 0, '', '', 0, '', null, 0, '');
$agent = $this->createDatabaseObject(Factory::getAgentFactory(), $agent);

$hashType = new HashType(null, 'placeholder', 0, 0);
$hashType = $this->createDatabaseObject(Factory::getHashTypeFactory(), $hashType);

$crackerBinaryType = new CrackerBinaryType(null, '', 0);
$crackerBinaryType = $this->createDatabaseObject(Factory::getCrackerBinaryTypeFactory(), $crackerBinaryType);

$crackerBinary = new CrackerBinary(null, $crackerBinaryType->getId(), '', '', '');
$crackerBinary = $this->createDatabaseObject(Factory::getCrackerBinaryFactory(), $crackerBinary);

$healthCheck = new HealthCheck(null, 0, 0, 0, $hashType->getId(), $crackerBinary->getId(), 0, '');
$healthCheck = $this->createDatabaseObject(Factory::getHealthCheckFactory(), $healthCheck);

$healthCheckAgent = new HealthCheckAgent(null, $healthCheck->getId(), $agent->getId(), 0, 0, 0, 0, 0, '');
$healthCheckAgent = $this->createDatabaseObject(Factory::getHealthCheckAgentFactory(), $healthCheckAgent);
$this->assertNotNull($healthCheckAgent->getId());
$this->assertTrue($healthCheckAgent instanceof HealthCheckAgent);

$healthCheckAgent->setEnd(9999);
Factory::getHealthCheckAgentFactory()->update($healthCheckAgent);

$healthCheckAgentUpdated = Factory::getHealthCheckAgentFactory()->get($healthCheckAgent->getId());
$this->assertEquals(9999, $healthCheckAgentUpdated->getEnd());
}

/**
* Test updating a model with multiple changed columns.
*
* @return void
* @throws Exception
*/
public function testUpdateModelSuccessMultipleChanges(): void {
$hashType = new HashType(null, 'placeholder', 0, 0);
$hashType = $this->createDatabaseObject(Factory::getHashTypeFactory(), $hashType);
$this->assertNotNull($hashType->getId());
$this->assertTrue($hashType instanceof HashType);

$hashType->setDescription('HashType X');
$hashType->setIsSalted(1);
$hashType->setIsSlowHash(1);
Factory::getHashTypeFactory()->update($hashType);

$hashTypeUpdated = Factory::getHashTypeFactory()->get($hashType->getId());
$this->assertEquals('HashType X', $hashTypeUpdated->getDescription());
$this->assertEquals(1, $hashType->getIsSalted());
Comment thread
s3inlc marked this conversation as resolved.
Outdated
$this->assertEquals(1, $hashType->getIsSlowHash());
}

/**
* Tests both cases to be used on a simple QueryFilter with no result.
* When single is true, null must be returned if no matching entry was found, empty array otherwise
Expand Down Expand Up @@ -96,7 +288,8 @@ public function testTimeseriesFilterNoneCracked(): void {

$qF1 = new QueryFilter(Hash::IS_CRACKED, 1, "=");
$qF2 = new QueryFilter(Hash::TIME_CRACKED, $timeLimit, ">");
$counts = Factory::getHashFactory()->columnTimeseriesFilter([Factory::FILTER => [$qF1, $qF2]], Hash::TIME_CRACKED);
$qF3 = new QueryFilter(Hash::HASHLIST_ID, $hashlist->getId(), "=");
$counts = Factory::getHashFactory()->columnTimeseriesFilter([Factory::FILTER => [$qF1, $qF2, $qF3]], Hash::TIME_CRACKED);

$this->assertSame([], $counts);
}
Expand Down Expand Up @@ -127,7 +320,8 @@ public function testTimeseriesFilter(): void {

$qF1 = new QueryFilter(Hash::IS_CRACKED, 1, "=");
$qF2 = new QueryFilter(Hash::TIME_CRACKED, $timeLimit, ">");
$counts = Factory::getHashFactory()->columnTimeseriesFilter([Factory::FILTER => [$qF1, $qF2]], Hash::TIME_CRACKED);
$qF3 = new QueryFilter(Hash::HASHLIST_ID, $hashlist->getId(), "=");
$counts = Factory::getHashFactory()->columnTimeseriesFilter([Factory::FILTER => [$qF1, $qF2, $qF3]], Hash::TIME_CRACKED);

// build the array on our own to compare
$expected = [];
Expand Down
6 changes: 2 additions & 4 deletions ci/phpunit/inc/UtilTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php

namespace inc;
namespace Hashtopolis\inc;

use Hashtopolis\inc\Util;
use PHPUnit\Framework\TestCase;

require_once(dirname(__FILE__) . '/../TestMocks.php');
require_once(dirname(__FILE__) . '/../../../src/inc/startup/include.php');
require_once(dirname(__FILE__) . '/../TestBase.php');

final class UtilTest extends TestCase {
public function testIsMailConfiguredReturnsFalseWithoutSsmtpConfig(): void {
Expand Down
39 changes: 22 additions & 17 deletions ci/phpunit/inc/apiv2/util/CorsHackMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<?php

namespace Tests\inc\apiv2\util;
namespace Hashtopolis\inc\apiv2\util;

use PHPUnit\Framework\TestCase;

use Exception;
use Hashtopolis\inc\apiv2\error\HttpForbidden;

use Slim\Factory\AppFactory;
use Slim\Psr7\Request;
use Hashtopolis\inc\apiv2\util\CorsHackMiddleware;

class DummyRequest {
private string $http_origin;
Expand All @@ -28,6 +25,7 @@ final class CorsHackMiddlewareTest extends TestCase {
* Tests all possible valid localhost variations with different ports.
*
* @return void
* @throws HttpForbidden
*/
public function testValidLocalhostVariations(): void {
$this->expectNotToPerformAssertions();
Expand Down Expand Up @@ -97,7 +95,8 @@ public function testInvalidLocalhostPort(): void {
$response = $app->getResponseFactory()->createResponse();

$request->setHeaderLine("http://127.0.0.1:4201");
$this->expectException(CorsHackMiddleware::CheckCORS($request, $response));

CorsHackMiddleware::CheckCORS($request, $response);
}

/**
Expand All @@ -118,7 +117,8 @@ public function testEvilDomainForLocalhost(): void {
$response = $app->getResponseFactory()->createResponse();

$request->setHeaderLine("http://evil.com:4200");
$this->expectException(CorsHackMiddleware::CheckCORS($request, $response));

CorsHackMiddleware::CheckCORS($request, $response);
}

/**
Expand All @@ -139,7 +139,8 @@ public function testEvilIPForLocalhost(): void {
$response = $app->getResponseFactory()->createResponse();

$request->setHeaderLine("http://137.137.137.1:4200");
$this->expectException(CorsHackMiddleware::CheckCORS($request, $response));

CorsHackMiddleware::CheckCORS($request, $response);
}

/**
Expand All @@ -160,13 +161,15 @@ public function testInvalidDomainPort(): void {
$response = $app->getResponseFactory()->createResponse();

$request->setHeaderLine("http://hashtopolis-cluster.com:4201");
$this->expectException(CorsHackMiddleware::CheckCORS($request, $response));

CorsHackMiddleware::CheckCORS($request, $response);
}

/**
* Tests a valid domain without port as origin.
*
*/
*
* @throws HttpForbidden
*/
public function testValidDomainWithoutPort(): void {
$this->expectNotToPerformAssertions();

Expand Down Expand Up @@ -202,12 +205,13 @@ public function testValidHttpsDomainWithoutPort(): void {
$request->setHeaderLine("https://hashtopolis-cluster.com");
CorsHackMiddleware::CheckCORS($request, $response);
}

/**
* Tests a valid https-domain with port as origin but configured http-backend-url.
* The http:// or https:// are not part of the CORS checks.
*
*/
*
* @throws HttpForbidden
*/
public function testValidHttpsDomainWithPortWithHttpConfig(): void {
$this->expectNotToPerformAssertions();

Expand All @@ -223,11 +227,12 @@ public function testValidHttpsDomainWithPortWithHttpConfig(): void {
$request->setHeaderLine("https://hashtopolis-cluster.com:8080");
CorsHackMiddleware::CheckCORS($request, $response);
}

/**
* Tests a valid domain with a differnt frontend port as origin.
*
*/
*
* @throws HttpForbidden
*/
public function testValidDomainWithoutDifferentFrontendPort(): void {
$this->expectNotToPerformAssertions();

Expand Down
Loading
Loading