add: more tests, fix bugs

pull/9143/head
Luciano Righetti 2023-06-20 11:52:27 +02:00
parent 1b0d05b3bb
commit ce28fcc54f
13 changed files with 368 additions and 59 deletions

View File

@ -66,9 +66,9 @@ class SharingGroupsController extends AppController
if (empty($sg['roaming']) && empty($sg['SharingGroupServer'])) { if (empty($sg['roaming']) && empty($sg['SharingGroupServer'])) {
$sharingGroupServerEntity = new SharingGroupServer( $sharingGroupServerEntity = new SharingGroupServer(
[ [
'sharing_group_id' => $id, 'sharing_group_id' => $id,
'server_id' => 0, 'server_id' => 0,
'all_orgs' => 0 'all_orgs' => 0
] ]
); );
$this->SharingGroups->SharingGroupServers->save($sharingGroupServerEntity); $this->SharingGroups->SharingGroupServers->save($sharingGroupServerEntity);
@ -106,9 +106,9 @@ class SharingGroupsController extends AppController
foreach ($sg['Organisation'] as $org) { foreach ($sg['Organisation'] as $org) {
$sharingGroupOrgEntity = new SharingGroupOrg( $sharingGroupOrgEntity = new SharingGroupOrg(
[ [
'sharing_group_id' => $sharingGroupEntity->id, 'sharing_group_id' => $sharingGroupEntity->id,
'org_id' => $org['id'], 'org_id' => $org['id'],
'extend' => $org['extend'] 'extend' => $org['extend']
] ]
); );
$this->SharingGroups->SharingGroupOrgs->save($sharingGroupOrgEntity); $this->SharingGroups->SharingGroupOrgs->save($sharingGroupOrgEntity);
@ -118,9 +118,9 @@ class SharingGroupsController extends AppController
foreach ($sg['Server'] as $server) { foreach ($sg['Server'] as $server) {
$sharingGroupServerEntity = new SharingGroupServer( $sharingGroupServerEntity = new SharingGroupServer(
[ [
'sharing_group_id' => $sharingGroupEntity->id, 'sharing_group_id' => $sharingGroupEntity->id,
'server_id' => $server['id'], 'server_id' => $server['id'],
'all_orgs' => $server['all_orgs'] 'all_orgs' => $server['all_orgs']
] ]
); );
$this->SharingGroups->SharingGroupServers->save($sharingGroupServerEntity); $this->SharingGroups->SharingGroupServers->save($sharingGroupServerEntity);
@ -192,7 +192,7 @@ class SharingGroupsController extends AppController
if ($this->ParamHandler->isRest()) { if ($this->ParamHandler->isRest()) {
if (!empty($this->request->getData('SharingGroup'))) { if (!empty($this->request->getData('SharingGroup'))) {
$data = $this->request->getData('SharingGroup'); $data = $this->request->getData('SharingGroup');
}else{ } else {
$data = $this->request->getData(); $data = $this->request->getData();
} }
$data['uuid'] = $sharingGroup['uuid']; $data['uuid'] = $sharingGroup['uuid'];
@ -392,7 +392,7 @@ class SharingGroupsController extends AppController
$response[$k]['deletable'] = $deletable; $response[$k]['deletable'] = $deletable;
} }
if ($this->ParamHandler->isRest()) { if ($this->ParamHandler->isRest()) {
return $this->RestResponse->viewData(['response' => $response], $this->response->getType()); // 'response' to keep BC return $this->RestResponse->viewData(['response' => $response]); // 'response' to keep BC
} }
$this->set('passive', $passive); $this->set('passive', $passive);
$this->set('sharingGroups', $response); $this->set('sharingGroups', $response);
@ -530,9 +530,9 @@ class SharingGroupsController extends AppController
]; ];
if (!empty($id)) { if (!empty($id)) {
if ($type == 'org') { if ($type == 'org') {
return $this->SharingGroups->SharingGroupOrgs->Organisation->fetchOrg($id); return $this->SharingGroups->SharingGroupOrgs->Organisations->fetchOrg($id);
} else { } else {
return $this->SharingGroups->SharingGroupServers->Server->fetchServer($id); return $this->SharingGroups->SharingGroupServers->Servers->fetchServer($id);
} }
} }
if ($type !== 'org' && $type !== 'server') { if ($type !== 'org' && $type !== 'server') {
@ -541,9 +541,9 @@ class SharingGroupsController extends AppController
foreach ($params[$type] as $param) { foreach ($params[$type] as $param) {
if (!empty($request[$param])) { if (!empty($request[$param])) {
if ($type == 'org') { if ($type == 'org') {
return $this->SharingGroups->SharingGroupOrgs->Organisation->fetchOrg($request[$param]); return $this->SharingGroups->SharingGroupOrgs->Organisations->fetchOrg($request[$param]);
} else { } else {
return $this->SharingGroups->SharingGroupServers->Server->fetchServer($request[$param]); return $this->SharingGroups->SharingGroupServers->Servers->fetchServer($request[$param]);
} }
} }
} }
@ -568,18 +568,18 @@ class SharingGroupsController extends AppController
} }
} }
if (!$addOrg) { if (!$addOrg) {
return $this->RestResponse->saveFailResponse('SharingGroup', $this->action, false, 'Organisation is already in the sharing group.', $this->response->getType()); return $this->RestResponse->saveFailResponse('SharingGroup', 'addOrg', false, 'Organisation is already in the sharing group.');
} }
$sharingGroupOrgEntity = new SharingGroupOrg( $sharingGroupOrgEntity = new SharingGroupOrg(
[ [
'org_id' => $org['id'], 'org_id' => $org['id'],
'sharing_group_id' => $sg['id'], 'sharing_group_id' => $sg['id'],
'extend' => $extend ? 1 : 0 'extend' => $extend ? 1 : 0
] ]
); );
$result = $this->SharingGroups->SharingGroupOrgs->save($sharingGroupOrgEntity); $result = $this->SharingGroups->SharingGroupOrgs->save($sharingGroupOrgEntity);
return $this->__sendQuickSaveResponse($this->action, $result, 'Organisation'); return $this->__sendQuickSaveResponse('addOrg', $result, 'Organisation');
} }
public function removeOrg($sg_id = false, $object_id = false) public function removeOrg($sg_id = false, $object_id = false)
@ -599,10 +599,11 @@ class SharingGroupsController extends AppController
} }
} }
if (false === $removeOrg) { if (false === $removeOrg) {
return $this->RestResponse->saveFailResponse('SharingGroup', $this->action, false, 'Organisation is not in the sharing group.', $this->response->getType()); return $this->RestResponse->saveFailResponse('SharingGroup', 'removeOrg', false, 'Organisation is not in the sharing group.');
} }
$result = $this->SharingGroups->SharingGroupOrgs->delete($removeOrg); $orgEntity = $this->SharingGroups->SharingGroupOrgs->get($removeOrg);
return $this->__sendQuickSaveResponse($this->action, $result, 'Organisation'); $result = $this->SharingGroups->SharingGroupOrgs->delete($orgEntity);
return $this->__sendQuickSaveResponse('removeOrg', $result, 'Organisation');
} }
public function addServer($sg_id = false, $object_id = false, $all = false) public function addServer($sg_id = false, $object_id = false, $all = false)
@ -621,23 +622,23 @@ class SharingGroupsController extends AppController
$addServer = true; $addServer = true;
if (!empty($sg['SharingGroupServer'])) { if (!empty($sg['SharingGroupServer'])) {
foreach ($sg['SharingGroupServer'] as $sgs) { foreach ($sg['SharingGroupServer'] as $sgs) {
if ($sgs['server_id'] == $server['Server']['id']) { if ($sgs['server_id'] == $server['id']) {
$addServer = false; $addServer = false;
} }
} }
} }
if (!$addServer) { if (!$addServer) {
return $this->RestResponse->saveFailResponse('SharingGroup', $this->action, false, 'Server is already in the sharing group.', $this->response->getType()); return $this->RestResponse->saveFailResponse('SharingGroup', 'addServer', false, 'Server is already in the sharing group.');
} }
$sharingGroupServerEntity = new SharingGroupServer( $sharingGroupServerEntity = new SharingGroupServer(
[ [
'server_id' => $server['Server']['id'], 'server_id' => $server['id'],
'sharing_group_id' => $sg['id'], 'sharing_group_id' => $sg['id'],
'all_orgs' => $all ? 1 : 0 'all_orgs' => $all ? 1 : 0
] ]
); );
$result = $this->SharingGroups->SharingGroupServers->save($sharingGroupServerEntity); $result = $this->SharingGroups->SharingGroupServers->save($sharingGroupServerEntity);
return $this->__sendQuickSaveResponse($this->action, $result); return $this->__sendQuickSaveResponse('addServer', $result);
} }
public function removeServer($sg_id = false, $object_id = false) public function removeServer($sg_id = false, $object_id = false)
@ -650,17 +651,18 @@ class SharingGroupsController extends AppController
$removeServer = false; $removeServer = false;
if (!empty($sg['SharingGroupServer'])) { if (!empty($sg['SharingGroupServer'])) {
foreach ($sg['SharingGroupServer'] as $sgs) { foreach ($sg['SharingGroupServer'] as $sgs) {
if ($sgs['server_id'] == $server['Server']['id']) { if ($sgs['server_id'] == $server['id']) {
$removeServer = $server['Server']['id']; $removeServer = $sgs['id'];
break; break;
} }
} }
} }
if (false === $removeServer) { if (false === $removeServer) {
return $this->RestResponse->saveFailResponse('SharingGroup', $this->action, false, 'Server is not in the sharing group.', $this->response->getType()); return $this->RestResponse->saveFailResponse('SharingGroup', 'removeServer', false, 'Server is not in the sharing group.');
} }
$result = $this->SharingGroups->SharingGroupServers->delete($removeServer); $serverEntity = $this->SharingGroups->SharingGroupServers->get($removeServer);
return $this->__sendQuickSaveResponse($this->action, $result); $result = $this->SharingGroups->SharingGroupServers->delete($serverEntity);
return $this->__sendQuickSaveResponse('removeServer', $result);
} }
private function __sendQuickSaveResponse($action, $result, $object_type = 'Server') private function __sendQuickSaveResponse($action, $result, $object_type = 'Server')
@ -670,9 +672,9 @@ class SharingGroupsController extends AppController
$actionType = 'removed from'; $actionType = 'removed from';
} }
if ($result) { if ($result) {
return $this->RestResponse->saveSuccessResponse('SharingGroup', $action, false, $this->response->getType(), $object_type . ' ' . $actionType . ' the sharing group.'); return $this->RestResponse->saveSuccessResponse('SharingGroup', $action, false, 'json', $object_type . ' ' . $actionType . ' the sharing group.');
} else { } else {
return $this->RestResponse->saveFailResponse('SharingGroup', $action, false, $object_type . ' could not be ' . $actionType . ' the sharing group.', $this->response->getType()); return $this->RestResponse->saveFailResponse('SharingGroup', $action, false, $object_type . ' could not be ' . $actionType . ' the sharing group.');
} }
} }

View File

@ -3,12 +3,11 @@
namespace App\Model\Table; namespace App\Model\Table;
use App\Model\Table\AppTable; use App\Model\Table\AppTable;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\Error\Debugger;
use Cake\Event\EventInterface;
use Cake\Datasource\EntityInterface;
use ArrayObject; use ArrayObject;
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
use Cake\Validation\Validation;
use Cake\Validation\Validator;
class OrganisationsTable extends AppTable class OrganisationsTable extends AppTable
{ {
@ -45,17 +44,23 @@ class OrganisationsTable extends AppTable
public function captureOrg($org): ?int public function captureOrg($org): ?int
{ {
if (!empty($org['uuid'])) { if (!empty($org['uuid'])) {
$existingOrg = $this->find()->where([ $existingOrg = $this->find()->where(
[
'uuid' => $org['uuid'] 'uuid' => $org['uuid']
])->first(); ]
)->first();
} else { } else {
return null; return null;
} }
if (empty($existingOrg)) { if (empty($existingOrg)) {
$entityToSave = $this->newEmptyEntity(); $entityToSave = $this->newEmptyEntity();
$this->patchEntity($entityToSave, $org, [ $this->patchEntity(
$entityToSave,
$org,
[
'accessibleFields' => $entityToSave->getAccessibleFieldForNew() 'accessibleFields' => $entityToSave->getAccessibleFieldForNew()
]); ]
);
} else { } else {
$this->patchEntity($existingOrg, $org); $this->patchEntity($existingOrg, $org);
$entityToSave = $existingOrg; $entityToSave = $existingOrg;
@ -67,4 +72,25 @@ class OrganisationsTable extends AppTable
} }
return $savedEntity->id; return $savedEntity->id;
} }
public function fetchOrg($id)
{
if (empty($id)) {
return false;
}
$conditions = ['Organisations.id' => $id];
if (Validation::uuid($id)) {
$conditions = ['Organisations.uuid' => $id];
} elseif (!is_numeric($id)) {
$conditions = ['LOWER(Organisations.name)' => strtolower($id)];
}
$org = $this->find(
'all',
[
'conditions' => $conditions,
'recursive' => -1
]
)->disableHydration()->first();
return (empty($org)) ? false : $org;
}
} }

View File

@ -24,8 +24,8 @@ class ServersTable extends AppTable
$existingServer = $this->find( $existingServer = $this->find(
'all', 'all',
[ [
'recursive' => -1, 'recursive' => -1,
'conditions' => ['url' => $server['url']] 'conditions' => ['url' => $server['url']]
] ]
)->disableHydration()->first(); )->disableHydration()->first();
// unlike with other capture methods, if we find a server that we don't know // unlike with other capture methods, if we find a server that we don't know
@ -35,4 +35,26 @@ class ServersTable extends AppTable
} }
return $existingServer['id']; return $existingServer['id'];
} }
public function fetchServer($id)
{
if (empty($id)) {
return false;
}
$conditions = ['Servers.id' => $id];
if (!is_numeric($id)) {
$conditions = ['OR' => [
'LOWER(Servers.name)' => strtolower($id),
'LOWER(Servers.url)' => strtolower($id)
]];
}
$server = $this->find(
'all',
[
'conditions' => $conditions,
'recursive' => -1
]
)->disableHydration()->first();
return (empty($server)) ? false : $server;
}
} }

View File

@ -178,9 +178,9 @@ class SharingGroupsTable extends AppTable
[ [
'contain' => ['SharingGroupServers' => ['Servers'], 'SharingGroupOrgs' => ['Organisations'], 'Organisations'], 'contain' => ['SharingGroupServers' => ['Servers'], 'SharingGroupOrgs' => ['Organisations'], 'Organisations'],
'conditions' => $conditions, 'conditions' => $conditions,
'order' => 'SharingGroup.name ASC' 'order' => 'SharingGroups.name ASC'
] ]
); )->disableHydration()->toArray();
return $sgs; return $sgs;
} elseif ($scope === 'simplified') { } elseif ($scope === 'simplified') {
$fieldsOrg = ['id', 'name', 'uuid']; $fieldsOrg = ['id', 'name', 'uuid'];
@ -217,7 +217,7 @@ class SharingGroupsTable extends AppTable
'fields' => $fieldsSharingGroup[$permissionTree]['fields'], 'fields' => $fieldsSharingGroup[$permissionTree]['fields'],
'order' => 'SharingGroups.name ASC' 'order' => 'SharingGroups.name ASC'
] ]
)->toArray(); )->disableHydration()->toArray();
return $this->appendOrgsAndServers($sgs, $fieldsOrg, $fieldsServer); return $this->appendOrgsAndServers($sgs, $fieldsOrg, $fieldsServer);
} elseif ($scope === 'distribution_graph') { } elseif ($scope === 'distribution_graph') {
// Specific scope that fetch just necessary information for distribution graph // Specific scope that fetch just necessary information for distribution graph
@ -231,7 +231,7 @@ class SharingGroupsTable extends AppTable
'fields' => ['SharingGroups.id', 'SharingGroups.name', 'SharingGroups.org_id'], 'fields' => ['SharingGroups.id', 'SharingGroups.name', 'SharingGroups.org_id'],
'order' => 'SharingGroups.name ASC' 'order' => 'SharingGroups.name ASC'
] ]
)->toArray(); )->disableHydration()->toArray();
if ($canSeeOrgs) { if ($canSeeOrgs) {
return $this->appendOrgsAndServers($sgs, ['id', 'name'], []); return $this->appendOrgsAndServers($sgs, ['id', 'name'], []);
} }
@ -248,7 +248,7 @@ class SharingGroupsTable extends AppTable
'order' => 'SharingGroups.name ASC', 'order' => 'SharingGroups.name ASC',
'conditions' => $conditions, 'conditions' => $conditions,
] ]
); )->disableHydration()->toArray();
return $sgs; return $sgs;
} elseif ($scope === 'uuid') { } elseif ($scope === 'uuid') {
$sgs = $this->find( $sgs = $this->find(
@ -258,7 +258,7 @@ class SharingGroupsTable extends AppTable
'fields' => ['SharingGroups.id', 'SharingGroups.uuid'], 'fields' => ['SharingGroups.id', 'SharingGroups.uuid'],
'conditions' => $conditions, 'conditions' => $conditions,
] ]
); )->disableHydration()->toArray();
return $sgs; return $sgs;
} }
throw new InvalidArgumentException("Invalid scope $scope"); throw new InvalidArgumentException("Invalid scope $scope");

View File

@ -16,6 +16,9 @@ class OrganisationsFixture extends TestFixture
public const ORGANISATION_B_ID = 2000; public const ORGANISATION_B_ID = 2000;
public const ORGANISATION_B_UUID = '36d22d9a-851e-4838-a655-9999c1d19497'; public const ORGANISATION_B_UUID = '36d22d9a-851e-4838-a655-9999c1d19497';
public const ORGANISATION_C_ID = 3000;
public const ORGANISATION_C_UUID = '73adc87b-be68-43d4-be8a-be5bdfcb59d2';
public function init(): void public function init(): void
{ {
$faker = \Faker\Factory::create(); $faker = \Faker\Factory::create();
@ -46,6 +49,19 @@ class OrganisationsFixture extends TestFixture
'description' => 'ORGANISATION B', 'description' => 'ORGANISATION B',
'date_created' => $faker->dateTime()->getTimestamp(), 'date_created' => $faker->dateTime()->getTimestamp(),
'date_modified' => $faker->dateTime()->getTimestamp() 'date_modified' => $faker->dateTime()->getTimestamp()
],
[
'id' => self::ORGANISATION_C_ID,
'uuid' => self::ORGANISATION_C_UUID,
'name' => 'Organisation C',
'url' => $faker->url,
'nationality' => $faker->countryCode,
'sector' => 'IT',
'type' => '',
'contacts' => '',
'description' => 'ORGANISATION C',
'date_created' => $faker->dateTime()->getTimestamp(),
'date_modified' => $faker->dateTime()->getTimestamp()
] ]
]; ];
parent::init(); parent::init();

View File

@ -16,6 +16,9 @@ class ServersFixture extends TestFixture
public const SERVER_B_ID = 2000; public const SERVER_B_ID = 2000;
public const SERVER_B_NAME = 'Server B'; public const SERVER_B_NAME = 'Server B';
public const SERVER_C_ID = 3000;
public const SERVER_C_NAME = 'Server C';
public function init(): void public function init(): void
{ {
$faker = \Faker\Factory::create(); $faker = \Faker\Factory::create();
@ -68,7 +71,31 @@ class ServersFixture extends TestFixture
'remove_missing_tags' => false, 'remove_missing_tags' => false,
'caching_enabled' => false, 'caching_enabled' => false,
'priority' => 1, 'priority' => 1,
] ],
[
'id' => self::SERVER_C_ID,
'org_id' => OrganisationsFixture::ORGANISATION_C_ID,
'name' => self::SERVER_C_NAME,
'url' => $faker->url,
'authkey' => $faker->sha1(),
'push' => true,
'pull' => true,
'push_sightings' => true,
'push_galaxy_clusters' => true,
'pull_galaxy_clusters' => true,
'organization' => 'Org B',
'remote_org_id' => $faker->numberBetween(1000, 2000),
'publish_without_email' => true,
'unpublish_event' => true,
'self_signed' => true,
'pull_rules' => json_encode([]),
'push_rules' => json_encode([]),
'internal' => false,
'skip_proxy' => false,
'remove_missing_tags' => false,
'caching_enabled' => false,
'priority' => 1,
],
]; ];
parent::init(); parent::init();
} }

View File

@ -19,6 +19,11 @@ class SharingGroupServersFixture extends TestFixture
'sharing_group_id' => SharingGroupsFixture::SHARING_GROUP_A_ID, 'sharing_group_id' => SharingGroupsFixture::SHARING_GROUP_A_ID,
'server_id' => ServersFixture::SERVER_A_ID, 'server_id' => ServersFixture::SERVER_A_ID,
'all_orgs' => false, 'all_orgs' => false,
],
[
'sharing_group_id' => SharingGroupsFixture::SHARING_GROUP_A_ID,
'server_id' => ServersFixture::SERVER_C_ID,
'all_orgs' => false,
] ]
]; ];
parent::init(); parent::init();

View File

@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Api\SharingGroups;
use App\Test\Fixture\AuthKeysFixture;
use App\Test\Fixture\OrganisationsFixture;
use App\Test\Fixture\SharingGroupsFixture;
use App\Test\Helper\ApiTestTrait;
use Cake\TestSuite\TestCase;
class AddSharingGroupOrgApiTest extends TestCase
{
use ApiTestTrait;
protected const ENDPOINT = '/sharing-groups/add-org';
protected $fixtures = [
'app.Organisations',
'app.Users',
'app.AuthKeys',
'app.Servers',
'app.SharingGroups',
'app.SharingGroupOrgs',
'app.SharingGroupServers',
];
public function testAddSharingGroupOrganisation(): void
{
$this->skipOpenApiValidations();
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);
$this->post(
sprintf("%s/%s/%s", self::ENDPOINT, SharingGroupsFixture::SHARING_GROUP_A_ID, OrganisationsFixture::ORGANISATION_C_ID)
);
$this->assertResponseOk();
$this->assertDbRecordExists(
'SharingGroupOrgs',
[
'sharing_group_id' => SharingGroupsFixture::SHARING_GROUP_A_ID,
'org_id' => OrganisationsFixture::ORGANISATION_C_ID
]
);
}
}

View File

@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Api\SharingGroups;
use App\Test\Fixture\AuthKeysFixture;
use App\Test\Fixture\ServersFixture;
use App\Test\Fixture\SharingGroupsFixture;
use App\Test\Helper\ApiTestTrait;
use Cake\TestSuite\TestCase;
class AddSharingGroupServerApiTest extends TestCase
{
use ApiTestTrait;
protected const ENDPOINT = '/sharing-groups/add-server';
protected $fixtures = [
'app.Organisations',
'app.Users',
'app.AuthKeys',
'app.Servers',
'app.SharingGroups',
'app.SharingGroupOrgs',
'app.SharingGroupServers',
];
public function testAddSharingGroupServer(): void
{
$this->skipOpenApiValidations();
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);
$this->post(
sprintf("%s/%s/%s", self::ENDPOINT, SharingGroupsFixture::SHARING_GROUP_A_ID, ServersFixture::SERVER_B_ID)
);
$this->assertResponseOk();
$this->assertDbRecordExists(
'SharingGroupServers',
[
'sharing_group_id' => SharingGroupsFixture::SHARING_GROUP_A_ID,
'server_id' => ServersFixture::SERVER_B_ID
]
);
}
}

View File

@ -4,10 +4,10 @@ declare(strict_types=1);
namespace App\Test\TestCase\Api\SharingGroups; namespace App\Test\TestCase\Api\SharingGroups;
use Cake\TestSuite\TestCase;
use App\Test\Fixture\AuthKeysFixture; use App\Test\Fixture\AuthKeysFixture;
use App\Test\Fixture\SharingGroupsFixture; use App\Test\Fixture\SharingGroupsFixture;
use App\Test\Helper\ApiTestTrait; use App\Test\Helper\ApiTestTrait;
use Cake\TestSuite\TestCase;
class IndexSharingGroupsApiTest extends TestCase class IndexSharingGroupsApiTest extends TestCase
{ {
@ -32,9 +32,14 @@ class IndexSharingGroupsApiTest extends TestCase
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);
$this->get(self::ENDPOINT); $this->get(self::ENDPOINT);
$sharingGroups = $this->getJsonResponseAsArray()['response'];
$this->assertResponseOk(); $this->assertResponseOk();
$this->assertResponseContains(sprintf('"uuid": "%s"', SharingGroupsFixture::SHARING_GROUP_A_UUID));
$this->assertResponseContains(sprintf('"uuid": "%s"', SharingGroupsFixture::SHARING_GROUP_B_UUID)); $this->assertCount(2, $sharingGroups);
$this->assertEquals('Sharing Group A', $sharingGroups[0]['name']);
$this->assertEquals(SharingGroupsFixture::SHARING_GROUP_A_UUID, $sharingGroups[0]['uuid']);
$this->assertEquals('Sharing Group B', $sharingGroups[1]['name']);
$this->assertEquals(SharingGroupsFixture::SHARING_GROUP_B_UUID, $sharingGroups[1]['uuid']);
} }
} }

View File

@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Api\SharingGroups;
use App\Test\Fixture\AuthKeysFixture;
use App\Test\Fixture\OrganisationsFixture;
use App\Test\Fixture\SharingGroupsFixture;
use App\Test\Helper\ApiTestTrait;
use Cake\TestSuite\TestCase;
class RemoveSharingGroupOrgApiTest extends TestCase
{
use ApiTestTrait;
protected const ENDPOINT = '/sharing-groups/remove-org';
protected $fixtures = [
'app.Organisations',
'app.Users',
'app.AuthKeys',
'app.Servers',
'app.SharingGroups',
'app.SharingGroupOrgs',
'app.SharingGroupServers',
];
public function testRemoveSharingGroupOrganisation(): void
{
$this->skipOpenApiValidations();
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);
$this->assertDbRecordExists(
'SharingGroupOrgs',
[
'sharing_group_id' => SharingGroupsFixture::SHARING_GROUP_A_ID,
'org_id' => OrganisationsFixture::ORGANISATION_B_ID
]
);
$this->post(
sprintf("%s/%s/%s", self::ENDPOINT, SharingGroupsFixture::SHARING_GROUP_A_ID, OrganisationsFixture::ORGANISATION_B_ID)
);
$this->assertResponseOk();
$this->assertDbRecordNotExists(
'SharingGroupOrgs',
[
'sharing_group_id' => SharingGroupsFixture::SHARING_GROUP_A_ID,
'org_id' => OrganisationsFixture::ORGANISATION_B_ID
]
);
}
}

View File

@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Api\SharingGroups;
use App\Test\Fixture\AuthKeysFixture;
use App\Test\Fixture\ServersFixture;
use App\Test\Fixture\SharingGroupsFixture;
use App\Test\Helper\ApiTestTrait;
use Cake\TestSuite\TestCase;
class RemoveSharingGroupServerApiTest extends TestCase
{
use ApiTestTrait;
protected const ENDPOINT = '/sharing-groups/remove-server';
protected $fixtures = [
'app.Organisations',
'app.Users',
'app.AuthKeys',
'app.Servers',
'app.SharingGroups',
'app.SharingGroupOrgs',
'app.SharingGroupServers',
];
public function testRemoveSharingGroupServer(): void
{
$this->skipOpenApiValidations();
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);
$this->assertDbRecordExists(
'SharingGroupServers',
[
'sharing_group_id' => SharingGroupsFixture::SHARING_GROUP_A_ID,
'server_id' => ServersFixture::SERVER_C_ID
]
);
$this->post(
sprintf("%s/%s/%s", self::ENDPOINT, SharingGroupsFixture::SHARING_GROUP_A_ID, ServersFixture::SERVER_C_ID)
);
$this->assertResponseOk();
$this->assertDbRecordNotExists(
'SharingGroupServers',
[
'sharing_group_id' => SharingGroupsFixture::SHARING_GROUP_A_ID,
'server_id' => ServersFixture::SERVER_C_ID
]
);
}
}

View File

@ -4,12 +4,12 @@ declare(strict_types=1);
namespace App\Test\TestCase\Api\SharingGroups; namespace App\Test\TestCase\Api\SharingGroups;
use Cake\TestSuite\TestCase;
use App\Test\Fixture\AuthKeysFixture; use App\Test\Fixture\AuthKeysFixture;
use App\Test\Fixture\OrganisationsFixture; use App\Test\Fixture\OrganisationsFixture;
use App\Test\Fixture\ServersFixture; use App\Test\Fixture\ServersFixture;
use App\Test\Fixture\SharingGroupsFixture; use App\Test\Fixture\SharingGroupsFixture;
use App\Test\Helper\ApiTestTrait; use App\Test\Helper\ApiTestTrait;
use Cake\TestSuite\TestCase;
class ViewSharingGroupApiTest extends TestCase class ViewSharingGroupApiTest extends TestCase
{ {
@ -51,8 +51,10 @@ class ViewSharingGroupApiTest extends TestCase
// check that the sharing group contains the correct servers // check that the sharing group contains the correct servers
$this->assertArrayHasKey('SharingGroupServer', $sharingGroup); $this->assertArrayHasKey('SharingGroupServer', $sharingGroup);
$this->assertCount(1, $sharingGroup['SharingGroupServer']); $this->assertCount(2, $sharingGroup['SharingGroupServer']);
$this->assertEquals(ServersFixture::SERVER_A_ID, $sharingGroup['SharingGroupServer'][0]['Server']['id']); $this->assertEquals(ServersFixture::SERVER_A_ID, $sharingGroup['SharingGroupServer'][0]['Server']['id']);
$this->assertEquals(ServersFixture::SERVER_A_NAME, $sharingGroup['SharingGroupServer'][0]['Server']['name']); $this->assertEquals(ServersFixture::SERVER_A_NAME, $sharingGroup['SharingGroupServer'][0]['Server']['name']);
$this->assertEquals(ServersFixture::SERVER_C_ID, $sharingGroup['SharingGroupServer'][1]['Server']['id']);
$this->assertEquals(ServersFixture::SERVER_C_NAME, $sharingGroup['SharingGroupServer'][1]['Server']['name']);
} }
} }