add: api tests for add sg with orgs and servers

pull/9143/head
Luciano Righetti 2023-06-19 16:44:13 +02:00
parent 12d43c9d50
commit 1b0d05b3bb
4 changed files with 132 additions and 25 deletions

View File

@ -3,6 +3,7 @@
namespace App\Model\Table;
use App\Model\Table\AppTable;
use Cake\Core\Configure;
class ServersTable extends AppTable
{
@ -11,4 +12,27 @@ class ServersTable extends AppTable
parent::initialize($config);
$this->addBehavior('AuditLog');
}
public function captureServer($server, $user)
{
if (isset($server[0])) {
$server = $server[0];
}
if ($server['url'] == Configure::read('MISP.baseurl')) {
return 0;
}
$existingServer = $this->find(
'all',
[
'recursive' => -1,
'conditions' => ['url' => $server['url']]
]
)->disableHydration()->first();
// unlike with other capture methods, if we find a server that we don't know
// we don't want to save it.
if (empty($existingServer)) {
return false;
}
return $existingServer['id'];
}
}

View File

@ -303,7 +303,7 @@ class SharingGroupsTable extends AppTable
]
)->where(
function (QueryExpression $exp, Query $q) use ($orgsToFetch) {
return $exp->in('id', array_keys($orgsToFetch));
return $exp->in('id', array_keys($orgsToFetch));
}
)->disableHydration()->toArray();
$orgsById = array_column(array_column($orgs, 'Organisation'), null, 'id');
@ -319,7 +319,7 @@ class SharingGroupsTable extends AppTable
]
)->where(
function (QueryExpression $exp, Query $q) use ($serverToFetch) {
return $exp->in('id', array_keys($serverToFetch));
return $exp->in('id', array_keys($serverToFetch));
}
)->disableHydration()->toArray();;
@ -868,9 +868,9 @@ class SharingGroupsTable extends AppTable
{
$sharingGroupOrgEntity = new SharingGroupOrg(
[
'sharing_group_id' => $sg_id,
'org_id' => $user['org_id'],
'extend' => false
'sharing_group_id' => $sg_id,
'org_id' => $user['org_id'],
'extend' => false
]
);
$this->SharingGroupOrgs->save($sharingGroupOrgEntity);
@ -902,11 +902,11 @@ class SharingGroupsTable extends AppTable
if (isset($org['Organisation'][0])) {
$org['Organisation'] = $org['Organisation'][0];
}
$sg['SharingGroupOrg'][$k]['org_id'] = $this->Organisation->captureOrg($org['Organisation'], $user, $force);
$sg['SharingGroupOrg'][$k]['org_id'] = $this->Organisations->captureOrg($org['Organisation'], $user, $force);
if ($sg['SharingGroupOrg'][$k]['org_id'] == $user['org_id']) {
$creatorOrgFound = true;
}
unset($sg['SharingGroupOrg'][$k]['Organisation']);
unset($sg['SharingGroupOrg'][$k]['Organisations']);
if ($force) {
// we are editing not creating here
$temp = $this->SharingGroupOrgs->find(
@ -921,13 +921,11 @@ class SharingGroupsTable extends AppTable
)->disableHydration()->first();
if (empty($temp)) {
$sharingGroupOrgEntity = new SharingGroupOrg(
[
[
'sharing_group_id' => $sg_id,
'org_id' => $sg['SharingGroupOrg'][$k]['org_id'],
'extend' => $org['extend']
]
]
);
$this->SharingGroupOrgs->save($sharingGroupOrgEntity);
} else {
@ -938,13 +936,11 @@ class SharingGroupsTable extends AppTable
}
} else {
$sharingGroupOrgEntity = new SharingGroupOrg(
[
[
'sharing_group_id' => $sg_id,
'org_id' => $sg['SharingGroupOrg'][$k]['org_id'],
'extend' => $org['extend']
]
]
);
$this->SharingGroupOrgs->save($sharingGroupOrgEntity);
}
@ -972,7 +968,7 @@ class SharingGroupsTable extends AppTable
if (!isset($server['all_orgs'])) {
$sg['SharingGroupServer'][$k]['all_orgs'] = 0;
}
$sg['SharingGroupServer'][$k]['server_id'] = $this->SharingGroupServers->Server->captureServer($server, $user, $force);
$sg['SharingGroupServer'][$k]['server_id'] = $this->SharingGroupServers->Servers->captureServer($server, $user, $force);
if ($sg['SharingGroupServer'][$k]['server_id'] == 0 && !empty($sg['SharingGroupServer'][$k]['all_orgs'])) {
$creatorOrgFound = true;
}
@ -994,9 +990,9 @@ class SharingGroupsTable extends AppTable
if (empty($temp)) {
$sharingGroupServerEntity = new SharingGroupServer(
[
'sharing_group_id' => $sg_id,
'server_id' => $sg['SharingGroupServer'][$k]['server_id'],
'all_orgs' => empty($server['all_orgs']) ? 0 : $server['all_orgs']
'sharing_group_id' => $sg_id,
'server_id' => $sg['SharingGroupServer'][$k]['server_id'],
'all_orgs' => empty($server['all_orgs']) ? 0 : $server['all_orgs']
]
);
$this->SharingGroupServers->save($sharingGroupServerEntity);
@ -1009,9 +1005,9 @@ class SharingGroupsTable extends AppTable
} else {
$sharingGroupServerEntity = new SharingGroupServer(
[
'sharing_group_id' => $sg_id,
'server_id' => $sg['SharingGroupServer'][$k]['server_id'],
'all_orgs' => empty($server['all_orgs']) ? 0 : $server['all_orgs']
'sharing_group_id' => $sg_id,
'server_id' => $sg['SharingGroupServer'][$k]['server_id'],
'all_orgs' => empty($server['all_orgs']) ? 0 : $server['all_orgs']
]
);
$this->SharingGroupServers->save($sharingGroupServerEntity);

View File

@ -4,8 +4,8 @@ declare(strict_types=1);
namespace App\Test\Fixture;
use Cake\TestSuite\Fixture\TestFixture;
use Authentication\PasswordHasher\DefaultPasswordHasher;
use Cake\TestSuite\Fixture\TestFixture;
class UsersFixture extends TestFixture
{
@ -13,22 +13,22 @@ class UsersFixture extends TestFixture
// Admin user
public const USER_ADMIN_ID = 1000;
public const USER_ADMIN_EMAIL = 'admin@admin.test';
public const USER_ADMIN_EMAIL = 'admin@test.test';
public const USER_ADMIN_PASSWORD = 'AdminPassword';
// Sync user
public const USER_SYNC_ID = 2000;
public const USER_SYNC_EMAIL = 'sync@admin.test';
public const USER_SYNC_EMAIL = 'sync@test.test';
public const USER_SYNC_PASSWORD = 'SyncPassword';
// Org Admin user
public const USER_ORG_ADMIN_ID = 3000;
public const USER_ORG_ADMIN_EMAIL = 'org_admin@admin.test';
public const USER_ORG_ADMIN_EMAIL = 'org_admin@test.test';
public const USER_ORG_ADMIN_PASSWORD = 'OrgAdminPassword';
// Regular User user
public const USER_REGULAR_USER_ID = 4000;
public const USER_REGULAR_USER_EMAIL = 'user@admin.test';
public const USER_REGULAR_USER_EMAIL = 'user@test.test';
public const USER_REGULAR_USER_PASSWORD = 'UserPassword';
// Default Roles IDs

View File

@ -4,10 +4,10 @@ declare(strict_types=1);
namespace App\Test\TestCase\Api\SharingGroups;
use Cake\TestSuite\TestCase;
use App\Test\Fixture\AuthKeysFixture;
use App\Test\Fixture\OrganisationsFixture;
use App\Test\Helper\ApiTestTrait;
use Cake\TestSuite\TestCase;
class AddSharingGroupApiTest extends TestCase
{
@ -25,7 +25,7 @@ class AddSharingGroupApiTest extends TestCase
'app.SharingGroupServers',
];
public function testSharingGroup(): void
public function testAddSharingGroup(): void
{
$this->skipOpenApiValidations();
@ -53,4 +53,91 @@ class AddSharingGroupApiTest extends TestCase
]
);
}
public function testAddSharingGroupOrganisation(): void
{
$this->skipOpenApiValidations();
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);
$faker = \Faker\Factory::create();
$uuid = $faker->uuid();
$this->post(
self::ENDPOINT,
[
'uuid' => $uuid,
'name' => 'Test Sharing Group with Organisation',
'org_id' => OrganisationsFixture::ORGANISATION_A_ID,
'SharingGroupOrg' => [
[
'uuid' => OrganisationsFixture::ORGANISATION_B_UUID,
'extend' => false
]
]
]
);
$this->assertResponseOk();
$this->assertDbRecordExists(
'SharingGroups',
[
'uuid' => $uuid,
'org_id' => OrganisationsFixture::ORGANISATION_A_ID
]
);
$this->assertDbRecordExists(
'SharingGroupOrgs',
[
'org_id' => OrganisationsFixture::ORGANISATION_B_ID
]
);
}
public function testAddSharingGroupServer(): void
{
$this->skipOpenApiValidations();
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);
$faker = \Faker\Factory::create();
$uuid = $faker->uuid();
$server = $this->getRecordFromDb('Servers', ['id' => OrganisationsFixture::ORGANISATION_B_ID]);
$this->post(
self::ENDPOINT,
[
'uuid' => $uuid,
'name' => 'Test Sharing Group with Server',
'org_id' => OrganisationsFixture::ORGANISATION_A_ID,
'SharingGroupServer' => [
[
'server_id' => $server['id'],
'url' => $server['url'],
'all_orgs' => false,
]
]
]
);
$this->assertResponseOk();
$sharingGroup = $this->getJsonResponseAsArray();
$this->assertDbRecordExists(
'SharingGroups',
[
'uuid' => $uuid,
'org_id' => OrganisationsFixture::ORGANISATION_A_ID
]
);
$this->assertDbRecordExists(
'SharingGroupServers',
[
'server_id' => $server['id'],
'sharing_group_id' => $sharingGroup['id']
]
);
}
}