add: add api test for /galaxies/export and other fixes

pull/9368/head
Luciano Righetti 2023-11-13 12:56:30 +01:00
parent 56dea3e966
commit f040a50b5a
11 changed files with 159 additions and 90 deletions

View File

@ -330,7 +330,7 @@ class GalaxiesController extends AppController
$options = [
'conditions' => [
'GalaxyClusters.galaxy_id' => $galaxyId,
// 'GalaxyClusters.distribution' => $requestData['distribution'],
'GalaxyClusters.distribution' => $requestData['distribution'],
]
];
if(!empty($clusterType)){

View File

@ -954,26 +954,26 @@ class GalaxiesTable extends AppTable
public function convertToMISPGalaxyFormat($galaxy, $clusters)
{
$converted = [];
$converted['name'] = $galaxy['Galaxy']['name'];
$converted['type'] = $galaxy['Galaxy']['type'];
$converted['name'] = $galaxy['name'];
$converted['type'] = $galaxy['type'];
$converted['authors'] = [];
$converted['version'] = 0;
$values = [];
$fieldsToSave = ['description', 'uuid', 'value', 'extends_uuid', 'extends_version'];
foreach ($clusters as $i => $cluster) {
foreach ($fieldsToSave as $field) {
$values[$i][$field] = $cluster['GalaxyCluster'][$field];
$values[$i][$field] = $cluster[$field];
}
$converted['uuid'] = $cluster['GalaxyCluster']['collection_uuid'];
$converted['source'] = $cluster['GalaxyCluster']['source'];
if (!empty($cluster['GalaxyCluster']['authors'])) {
foreach ($cluster['GalaxyCluster']['authors'] as $author) {
$converted['uuid'] = $cluster['collection_uuid'];
$converted['source'] = $cluster['source'];
if (!empty($cluster['authors'])) {
foreach ($cluster['authors'] as $author) {
if (!is_null($author) && $author != 'null') {
$converted['authors'][$author] = $author;
}
}
}
$converted['version'] = $converted['version'] > $cluster['GalaxyCluster']['version'];
$converted['version'] = $converted['version'] > $cluster['version'];
foreach ($cluster['GalaxyCluster']['GalaxyElement'] as $element) {
if (isset($values[$i]['meta'][$element['key']])) {
if (is_array($values[$i]['meta'][$element['key']])) {
@ -992,7 +992,7 @@ class GalaxiesTable extends AppTable
];
if (!empty($relation['Tag'])) {
foreach ($relation['Tag'] as $tag) {
$values[$i]['related'][$j]['tags'][] = $tag['name'];
$values[$i]['related'][$j]['tags'][] = $tag['Tag']['name'];
}
}
}

View File

@ -11,7 +11,7 @@ use Cake\Validation\Validator;
class GalaxyClusterRelationTagsTable extends AppTable
{
public $useTable = 'galaxy_cluster_relation_tags';
public $actsAs = array('AuditLog', 'Containable');
public $actsAs = ['AuditLog', 'Containable'];
public function validationDefault(Validator $validator): Validator
{
@ -29,7 +29,12 @@ class GalaxyClusterRelationTagsTable extends AppTable
$this->addBehavior('AuditLog');
$this->belongsTo('GalaxyClusterRelations');
$this->belongsTo('Tags');
$this->belongsTo(
'Tags',
[
'propertyName' => 'Tag',
]
);
}
public function softDelete($id)
@ -52,19 +57,22 @@ class GalaxyClusterRelationTagsTable extends AppTable
$saveResult = false;
foreach ($tags as $tagName) {
if ($capture) {
$tagId = $this->Tags->captureTag(array('name' => $tagName), $user);
$tagId = $this->Tags->captureTag(['name' => $tagName], $user);
} else {
$tagId = $this->Tags->lookupTagIdFromName($tagName);
}
$existingAssociation = $this->find('all', array(
'recursive' => -1,
'conditions' => array(
'tag_id' => $tagId,
'galaxy_cluster_relation_id' => $galaxyClusterRelationId
)
))->first();
$existingAssociation = $this->find(
'all',
[
'recursive' => -1,
'conditions' => [
'tag_id' => $tagId,
'galaxy_cluster_relation_id' => $galaxyClusterRelationId
]
]
)->first();
if (empty($existingAssociation) && $tagId != -1) {
$saveResult = $this->save($this->newEntity(array('galaxy_cluster_relation_id' => $galaxyClusterRelationId, 'tag_id' => $tagId)));
$saveResult = $this->save($this->newEntity(['galaxy_cluster_relation_id' => $galaxyClusterRelationId, 'tag_id' => $tagId]));
$allSaved = $allSaved && $saveResult;
if (!$saveResult) {
$this->Log->createLogEntry($user, 'attachTags', 'GalaxyClusterRelationTag', 0, __('Could not attach tag %s', $tagName), __('relation (%s)', $galaxyClusterRelationId));
@ -76,6 +84,6 @@ class GalaxyClusterRelationTagsTable extends AppTable
public function detachTag($user, $relationTagId)
{
$this->delete(array('GalaxyClusterRelationTag.relationTagId' => $relationTagId));
$this->delete(['GalaxyClusterRelationTag.relationTagId' => $relationTagId]);
}
}

View File

@ -7,7 +7,6 @@ use App\Model\Table\AppTable;
use Cake\ORM\Locator\LocatorAwareTrait;
use Cake\Utility\Hash;
use Cake\Validation\Validator;
use Exception;
/**
* @property GalaxyClusterRelationTag $GalaxyClusterRelationTag
@ -87,6 +86,7 @@ class GalaxyClusterRelationsTable extends AppTable
'GalaxyClusterRelationTags',
[
'dependent' => true,
'propertyName' => 'Tag',
]
);
}
@ -331,9 +331,10 @@ class GalaxyClusterRelationsTable extends AppTable
if (empty($existingRelation)) {
$errors[] = __('Unkown ID');
} else {
$options = ['conditions' => [
'uuid' => $relation['GalaxyClusterRelation']['galaxy_cluster_uuid']
]
$options = [
'conditions' => [
'uuid' => $relation['GalaxyClusterRelation']['galaxy_cluster_uuid']
]
];
$cluster = $this->SourceCluster->fetchGalaxyClusters($user, $options);
if (empty($cluster)) {
@ -499,11 +500,12 @@ class GalaxyClusterRelationsTable extends AppTable
$existingRelation = $this->find(
'all',
['conditions' => [
'GalaxyClusterRelations.galaxy_cluster_uuid' => $relation['GalaxyClusterRelation']['galaxy_cluster_uuid'],
'GalaxyClusterRelations.referenced_galaxy_cluster_uuid' => $relation['GalaxyClusterRelation']['referenced_galaxy_cluster_uuid'],
'GalaxyClusterRelations.referenced_galaxy_cluster_type' => $relation['GalaxyClusterRelation']['referenced_galaxy_cluster_type'],
]
[
'conditions' => [
'GalaxyClusterRelations.galaxy_cluster_uuid' => $relation['GalaxyClusterRelation']['galaxy_cluster_uuid'],
'GalaxyClusterRelations.referenced_galaxy_cluster_uuid' => $relation['GalaxyClusterRelation']['referenced_galaxy_cluster_uuid'],
'GalaxyClusterRelations.referenced_galaxy_cluster_type' => $relation['GalaxyClusterRelation']['referenced_galaxy_cluster_type'],
]
]
)->first();
if (!empty($existingRelation)) {
@ -523,9 +525,9 @@ class GalaxyClusterRelationsTable extends AppTable
$relation['GalaxyClusterRelation'] = $EventsTable->captureSGForElement($relation['GalaxyClusterRelation'], $user);
}
try {
$galaxyClusterRelationEntity = $this->newEntity($relation['GalaxyClusterRelation']);
$this->saveOrFail($galaxyClusterRelationEntity);
$galaxyClusterRelationEntity = $this->newEntity($relation['GalaxyClusterRelation'], ['associated' => []]);
$result = $this->save($galaxyClusterRelationEntity);
if ($result) {
$results['imported']++;
$modelKey = false;
if (!empty($relation['GalaxyClusterRelation']['GalaxyClusterRelationTag'])) {
@ -539,7 +541,7 @@ class GalaxyClusterRelationsTable extends AppTable
// Since we don't have tag soft-deletion, tags added by users will be kept.
$this->GalaxyClusterRelationTags->attachTags($user, $galaxyClusterRelationEntity->id, $tagNames, $capture = true);
}
} catch (Exception $e) {
} else {
$results['failed']++;
}
}
@ -572,9 +574,10 @@ class GalaxyClusterRelationsTable extends AppTable
*/
private function syncUUIDsAndIDs(array $user, array $relation)
{
$options = ['conditions' => [
'uuid' => $relation['GalaxyClusterRelation']['galaxy_cluster_uuid']
]
$options = [
'conditions' => [
'uuid' => $relation['GalaxyClusterRelation']['galaxy_cluster_uuid']
]
];
$sourceCluster = $this->SourceCluster->fetchGalaxyClusters($user, $options);
if (!empty($sourceCluster)) {
@ -582,9 +585,10 @@ class GalaxyClusterRelationsTable extends AppTable
$relation['GalaxyClusterRelation']['galaxy_cluster_id'] = $sourceCluster['SourceCluster']['id'];
$relation['GalaxyClusterRelation']['galaxy_cluster_uuid'] = $sourceCluster['SourceCluster']['uuid'];
}
$options = ['conditions' => [
'uuid' => $relation['GalaxyClusterRelation']['referenced_galaxy_cluster_uuid']
]
$options = [
'conditions' => [
'uuid' => $relation['GalaxyClusterRelation']['referenced_galaxy_cluster_uuid']
]
];
$targetCluster = $this->TargetCluster->fetchGalaxyClusters($user, $options);
if (!empty($targetCluster)) {

View File

@ -818,7 +818,7 @@ class GalaxyClustersTable extends AppTable
}
}
}
foreach ($cluster['GalaxyElement'] as $i => $element) {
foreach ($cluster['GalaxyCluster']['GalaxyElement'] as $i => $element) {
unset($clusters[$k]['GalaxyElement'][$i]['id']);
unset($clusters[$k]['GalaxyElement'][$i]['galaxy_cluster_id']);
}
@ -1178,7 +1178,9 @@ class GalaxyClustersTable extends AppTable
'GalaxyElements',
'GalaxyClusterRelations' => [
'conditions' => $GalaxyClusterRelationTable->buildConditions($user, false),
'GalaxyClusterRelationTags',
'GalaxyClusterRelationTags' => [
'Tags'
],
'SharingGroup',
],
'Orgc',
@ -1281,7 +1283,7 @@ class GalaxyClustersTable extends AppTable
if ($includeFullClusterRelationship) {
$targetingClusterRelation['TargetingClusterRelation']['GalaxyCluster'] = $targetingClusterRelation['SourceCluster'];
}
$targetingClusterRelations[$k] = $targetingClusterRelation['TargetingClusterRelation'];
$targetingClusterRelations[$k] = $targetingClusterRelation->toArray();
}
}

View File

@ -14,7 +14,12 @@ class GalaxyClusterRelationTagsFixture extends TestFixture
{
$faker = \Faker\Factory::create();
$this->records = [];
$this->records = [
[
'galaxy_cluster_relation_id' => GalaxyClusterRelationsFixture::GALAXY_CLUSTER_RELATION_1_ID,
'tag_id' => TagsFixture::TAG_1_ID,
]
];
parent::init();
}
}

View File

@ -10,11 +10,25 @@ class GalaxyClusterRelationsFixture extends TestFixture
{
public $connection = 'test';
public const GALAXY_CLUSTER_RELATION_1_ID = 1000;
public function init(): void
{
$faker = \Faker\Factory::create();
$this->records = [];
$this->records = [
[
'id' => self::GALAXY_CLUSTER_RELATION_1_ID,
'galaxy_cluster_id' => GalaxyClustersFixture::GALAXY_CLUSTER_1_ID,
'referenced_galaxy_cluster_id' => GalaxyClustersFixture::GALAXY_CLUSTER_1_ID,
'referenced_galaxy_cluster_uuid' => GalaxyClustersFixture::GALAXY_CLUSTER_1_UUID,
'referenced_galaxy_cluster_type' => 'similar',
'galaxy_cluster_uuid' => GalaxyClustersFixture::GALAXY_CLUSTER_1_UUID,
'distribution' => '0',
'sharing_group_id' => null,
'default' => false,
]
];
parent::init();
}
}

View File

@ -4,8 +4,8 @@ declare(strict_types=1);
namespace App\Test\Fixture;
use Cake\TestSuite\Fixture\TestFixture;
use App\Test\Fixture\GalaxiesFixture;
use Cake\TestSuite\Fixture\TestFixture;
class GalaxyClustersFixture extends TestFixture
{
@ -27,7 +27,7 @@ class GalaxyClustersFixture extends TestFixture
'source' => 'test-fixture-source',
'type' => 'test-fixture-type',
'authors' => '["test-fixture-author"]',
'collection_uuid' => '',
'collection_uuid' => GalaxiesFixture::GALAXY_1_UUID,
'value' => 'test-fixture-cluster-1',
'org_id' => OrganisationsFixture::ORGANISATION_A_ID,
'orgc_id' => OrganisationsFixture::ORGANISATION_A_ID,

View File

@ -10,11 +10,27 @@ class TagsFixture extends TestFixture
{
public $connection = 'test';
public const TAG_1_ID = 1;
public function init(): void
{
$faker = \Faker\Factory::create();
$this->records = [];
$this->records = [
[
'id' => self::TAG_1_ID,
'name' => 'test:tag',
'colour' => '#000000',
'exportable' => 1,
'org_id' => OrganisationsFixture::ORGANISATION_A_ID,
'user_id' => UsersFixture::USER_ADMIN_ID,
'hide_tag' => 0,
'numerical_value' => null,
'is_galaxy' => 0,
'is_custom_galaxy' => 0,
'local_only' => 0
]
];
parent::init();
}
}

View File

@ -34,33 +34,38 @@ class ExportGalaxyApiTest extends TestCase
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);
$url = sprintf('%s/%s', self::ENDPOINT, GalaxiesFixture::GALAXY_1_UUID);
$this->post($url, [
"Galaxy" => [
"default" => false,
"custom" => false,
"distribution" => "0",
"format" => "misp-galaxy",
"download" => false
$this->post(
$url,
[
"Galaxy" => [
"default" => false,
"custom" => false,
"distribution" => "0",
"format" => "misp-galaxy",
"download" => false
]
]
]);
$this->markTestSkipped("This is not implemented yet.");
);
// $this->markTestSkipped("This is not implemented yet.");
// $this->assertResponseOk();
// $galaxy = $this->getJsonResponseAsArray();
$this->assertResponseOk();
$galaxy = $this->getJsonResponseAsArray();
// $this->assertEquals(GalaxiesFixture::GALAXY_1_ID, $galaxy['id']);
// $this->assertEquals(GalaxiesFixture::GALAXY_1_NAME, $galaxy['name']);
$this->assertEquals(GalaxiesFixture::GALAXY_1_UUID, $galaxy['uuid']);
$this->assertEquals(GalaxiesFixture::GALAXY_1_NAME, $galaxy['name']);
// # check that the galaxy has the correct clusters
// $this->assertEquals(GalaxyClustersFixture::GALAXY_CLUSTER_1_UUID, $galaxy['GalaxyCluster'][0]['uuid']);
// $this->assertEquals(GalaxiesFixture::GALAXY_1_ID, $galaxy['GalaxyCluster'][0]['galaxy_id']);
# check that the galaxy has the correct clusters exported
$this->assertEquals(GalaxyClustersFixture::GALAXY_CLUSTER_1_UUID, $galaxy['values'][0]['uuid']);
$this->assertEquals('test-fixture-cluster-1', $galaxy['values'][0]['value']);
// # check that the galaxy has the correct elements
// $this->assertEquals(GalaxyClustersFixture::GALAXY_CLUSTER_1_ID, $galaxy['GalaxyCluster'][0]['GalaxyElement'][0]['galaxy_cluster_id']);
// $this->assertEquals('test-fixture-element-key', $galaxy['GalaxyCluster'][0]['GalaxyElement'][0]['key']);
// $this->assertEquals('test-fixture-element-value', $galaxy['GalaxyCluster'][0]['GalaxyElement'][0]['value']);
# check that the galaxy has the correct elements exported
$this->assertEquals('test-fixture-element-value', $galaxy['values'][0]['meta']['test-fixture-element-key']);
# TODO: check that the galaxy has the correct relations
# TODO: check that the galaxy has the correct tags
# check that the galaxy has the correct relations exported
$this->assertEquals(GalaxyClustersFixture::GALAXY_CLUSTER_1_UUID, $galaxy['values'][0]['related'][0]['dest-uuid']);
$this->assertEquals('similar', $galaxy['values'][0]['related'][0]['type']);
# check that the galaxy has the correct relation tags exported
$this->assertEquals('test:tag', $galaxy['values'][0]['related'][0]['tags'][0]);
}
}

View File

@ -26,7 +26,7 @@ class PushGalaxyClusterApiTest extends TestCase
'app.Tags',
];
public function testUpdateGalaxies(): void
public function testPushGalaxyCluster(): void
{
$this->skipOpenApiValidations();
@ -74,31 +74,46 @@ class PushGalaxyClusterApiTest extends TestCase
$this->post(self::ENDPOINT, $jsonGalaxyCluster);
$this->assertResponseOk();
$this->assertDbRecordExists('Galaxies', [
'uuid' => 'c51c59e9-f213-4ad4-9913-09a43d78dff5'
]);
$this->assertDbRecordExists(
'Galaxies',
[
'uuid' => 'c51c59e9-f213-4ad4-9913-09a43d78dff5'
]
);
// # check that the galaxy has the correct clusters
$this->assertDbRecordExists('GalaxyClusters', [
'uuid' => '06251f72-9d60-43b5-a28a-48a2e0452d1e'
]);
$this->assertDbRecordExists(
'GalaxyClusters',
[
'uuid' => '06251f72-9d60-43b5-a28a-48a2e0452d1e'
]
);
# check that the galaxy has the correct elements
$this->assertDbRecordExists('GalaxyElements', [
'key' => 'test-pushed-element-key',
'value' => 'test-pushed-element-value'
]);
$this->assertDbRecordExists(
'GalaxyElements',
[
'key' => 'test-pushed-element-key',
'value' => 'test-pushed-element-value'
]
);
// # check that the galaxy has the correct relations
$this->assertDbRecordExists('GalaxyClusterRelations', [
'referenced_galaxy_cluster_uuid' => '06251f72-9d60-43b5-a28a-48a2e0452d1e',
'referenced_galaxy_cluster_type' => 'self-referenced-cluster-type',
'galaxy_cluster_uuid' => '06251f72-9d60-43b5-a28a-48a2e0452d1e'
]);
$this->assertDbRecordExists(
'GalaxyClusterRelations',
[
'referenced_galaxy_cluster_uuid' => '06251f72-9d60-43b5-a28a-48a2e0452d1e',
'referenced_galaxy_cluster_type' => 'self-referenced-cluster-type',
'galaxy_cluster_uuid' => '06251f72-9d60-43b5-a28a-48a2e0452d1e'
]
);
# check that the galaxy has the correct tags
$this->assertDbRecordExists('Tags', [
'name' => 'foo:bar'
]);
$this->assertDbRecordExists(
'Tags',
[
'name' => 'foo:bar'
]
);
}
}