add: api tests for allowedlists

pull/9069/head
Luciano Righetti 2023-05-10 15:59:26 +02:00
parent 42d99f25c0
commit f8762b8fc3
6 changed files with 217 additions and 6 deletions

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace App\Test\Fixture;
use Cake\TestSuite\Fixture\TestFixture;
class AllowedlistsFixture extends TestFixture
{
public $connection = 'test';
public const ALLOWED_LIST_1_ID = 1000;
public const ALLOWED_LIST_2_ID = 2000;
public function init(): void
{
$this->records = [
[
'id' => self::ALLOWED_LIST_1_ID,
'name' => '/192.168.0.\d+/',
],
[
'id' => self::ALLOWED_LIST_2_ID,
'name' => '/192.168.1.\d+/',
]
];
parent::init();
}
}

View File

@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Api\Allowedlists\Admin;
use Cake\TestSuite\TestCase;
use App\Test\Fixture\AuthKeysFixture;
use App\Test\Helper\ApiTestTrait;
class AddAllowedlistApiTest extends TestCase
{
use ApiTestTrait;
protected const ENDPOINT = '/admin/allowedlists/add';
protected $fixtures = [
'app.Organisations',
'app.Users',
'app.AuthKeys',
'app.Allowedlists'
];
public function testAdminAddAllowedlist(): void
{
$this->skipOpenApiValidations();
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);
$regex = '/127.0.0.\d+/';
$this->post(
self::ENDPOINT,
[
'name' => $regex,
]
);
$this->assertResponseOk();
$this->assertDbRecordExists('Allowedlists', ['name' => $regex]);
}
public function testAdminAddAllowedlistFailsOnInvalidRegex(): void
{
$this->skipOpenApiValidations();
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);
$regex = 'foobar';
$this->post(
self::ENDPOINT,
[
'name' => $regex,
]
);
$this->assertResponseOk();
$this->assertResponseContains("Allowedlist could not be added");
$this->assertDbRecordNotExists('Allowedlists', ['name' => $regex]);
}
}

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Api\Allowedlists\Admin;
use Cake\TestSuite\TestCase;
use App\Test\Fixture\AuthKeysFixture;
use App\Test\Helper\ApiTestTrait;
use App\Test\Fixture\AllowedlistsFixture;
class DeleteEventBlocklistApiTest extends TestCase
{
use ApiTestTrait;
protected const ENDPOINT = '/admin/allowedlists/delete';
protected $fixtures = [
'app.Organisations',
'app.Users',
'app.AuthKeys',
'app.Allowedlists'
];
public function testAdminDeleteAllowedlistById(): void
{
$this->skipOpenApiValidations();
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);
$url = sprintf('%s/%s', self::ENDPOINT, AllowedlistsFixture::ALLOWED_LIST_2_ID);
$this->delete($url);
$this->assertResponseOk();
$this->assertDbRecordNotExists('Allowedlists', ['id' => AllowedlistsFixture::ALLOWED_LIST_2_ID]);
}
}

View File

@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Api\Allowedlists\Admin;
use Cake\TestSuite\TestCase;
use App\Test\Fixture\AuthKeysFixture;
use App\Test\Helper\ApiTestTrait;
use App\Test\Fixture\AllowedlistsFixture;
class EditAllowedlistApiTest extends TestCase
{
use ApiTestTrait;
protected const ENDPOINT = '/admin/allowedlists/edit';
protected $fixtures = [
'app.Organisations',
'app.Users',
'app.AuthKeys',
'app.Allowedlists'
];
public function testAdminEditAllowedlist(): void
{
$this->skipOpenApiValidations();
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);
$new_regex = "/10.0.0.\d+/";
$url = sprintf('%s/%s', self::ENDPOINT, AllowedlistsFixture::ALLOWED_LIST_2_ID);
$this->post(
$url,
[
'name' => $new_regex
]
);
$this->assertResponseOk();
$this->assertDbRecordExists('Allowedlists', [
'id' => AllowedlistsFixture::ALLOWED_LIST_2_ID,
'name' => $new_regex,
]);
}
}

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Api\Allowedlists\Admin;
use Cake\TestSuite\TestCase;
use App\Test\Fixture\AuthKeysFixture;
use App\Test\Fixture\AllowedlistsFixture;
use App\Test\Helper\ApiTestTrait;
class IndexAllowedlistsApiTest extends TestCase
{
use ApiTestTrait;
protected const ENDPOINT = '/admin/allowedlists/index';
protected $fixtures = [
'app.Organisations',
'app.Users',
'app.AuthKeys',
'app.Allowedlists',
];
public function testIndexAllowed(): void
{
$this->skipOpenApiValidations();
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);
$this->get(self::ENDPOINT);
$this->assertResponseOk();
$this->assertResponseContains(sprintf('"id": %d', AllowedlistsFixture::ALLOWED_LIST_1_ID));
}
}

View File

@ -2,27 +2,27 @@
declare(strict_types=1);
namespace App\Test\TestCase\Api\Users;
namespace App\Test\TestCase\Api\Allowedlists;
use Cake\TestSuite\TestCase;
use App\Test\Fixture\AuthKeysFixture;
use App\Test\Fixture\EventBlocklistsFixture;
use App\Test\Fixture\AllowedlistsFixture;
use App\Test\Helper\ApiTestTrait;
class IndexAllowedlistsApiTest extends TestCase
{
use ApiTestTrait;
protected const ENDPOINT = '/event-blocklists/index';
protected const ENDPOINT = '/allowedlists/index';
protected $fixtures = [
'app.Organisations',
'app.Users',
'app.AuthKeys',
'app.EventBlocklists'
'app.Allowedlists',
];
public function testIndexEventBlocklists(): void
public function testIndexAllowedlist(): void
{
$this->skipOpenApiValidations();
@ -31,6 +31,6 @@ class IndexAllowedlistsApiTest extends TestCase
$this->get(self::ENDPOINT);
$this->assertResponseOk();
$this->assertResponseContains(sprintf('"event_uuid": "%s"', EventBlocklistsFixture::EVENT_BLOCK_LIST_1_EVENT_UUID));
$this->assertResponseContains(sprintf('"id": %d', AllowedlistsFixture::ALLOWED_LIST_1_ID));
}
}