Skip to content

Commit 34cf82d

Browse files
tests: add tests for sort flags #646
1 parent 5e282ec commit 34cf82d

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Tests/APICoreEndpointTestCase.inc

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,4 +810,77 @@ class APICoreEndpointTestCase extends TestCase {
810810
$this->assert_equals($resp->response_id, 'ENDPOINT_SORT_ORDER_FIELD_UNKNOWN_SORT_ORDER');
811811
$this->assert_equals($resp->code, 400);
812812
}
813+
814+
/**
815+
* Ensure the 'sort_flags' common control parameter must be a valid PHP sort constant.
816+
*/
817+
public function test_sort_flags_must_be_php_sort_constant(): void {
818+
# Use a GET request to request a non-string sort order
819+
$json_resp = \RESTAPI\Core\Tools\http_request(
820+
url: 'https://localhost/api/v2/firewall/aliases',
821+
method: 'GET',
822+
data: ['sort_by' => 'name', 'sort_flags' => 12345],
823+
headers: ['Content-Type' => 'application/json'],
824+
username: 'admin',
825+
password: 'pfsense',
826+
validate_certs: false,
827+
);
828+
# Ensure the response threw a ENDPOINT_SORT_FLAGS_FIELD_INVALID_TYPE error
829+
$resp = json_decode($json_resp);
830+
$this->assert_equals($resp->response_id, 'ENDPOINT_SORT_FLAGS_FIELD_INVALID_TYPE');
831+
$this->assert_equals($resp->code, 400);
832+
833+
# Make another request to sort order that is not known
834+
$json_resp = \RESTAPI\Core\Tools\http_request(
835+
url: 'https://localhost/api/v2/firewall/aliases',
836+
method: 'GET',
837+
data: ['sort_by' => 'name', 'sort_flags' => 'unknown_sort_flags'],
838+
headers: ['Content-Type' => 'application/json'],
839+
username: 'admin',
840+
password: 'pfsense',
841+
validate_certs: false,
842+
);
843+
844+
# Ensure the response threw a ENDPOINT_SORT_FLAGS_FIELD_UNKNOWN_SORT_FLAGS error
845+
$resp = json_decode($json_resp);
846+
$this->assert_equals($resp->response_id, 'ENDPOINT_SORT_FLAGS_FIELD_UNKNOWN_SORT_FLAGS');
847+
$this->assert_equals($resp->code, 400);
848+
}
849+
850+
/**
851+
* Ensure clients can specify the 'sort_flags' common control parameter to sort the returned data.
852+
*/
853+
public function test_sort_flags(): void {
854+
# Use a PUT request to create new aliases
855+
\RESTAPI\Core\Tools\http_request(
856+
url: 'https://localhost/api/v2/firewall/aliases',
857+
method: 'PUT',
858+
data: [
859+
['name' => 'alias_a', 'type' => 'host', 'descr' => '127.0.0.22'],
860+
['name' => 'alias_b', 'type' => 'host', 'descr' => '127.0.0.2'],
861+
['name' => 'alias_c', 'type' => 'host', 'descr' => '127.0.0.100'],
862+
['name' => 'alias_d', 'type' => 'host', 'descr' => '127.0.0.10'],
863+
],
864+
headers: ['Content-Type' => 'application/json'],
865+
username: 'admin',
866+
password: 'pfsense',
867+
validate_certs: false,
868+
);
869+
870+
# Ensure we can make a successful GET request to a `many` endpoint with the 'sort_flags' control parameter
871+
$json_resp = \RESTAPI\Core\Tools\http_request(
872+
url: 'https://localhost/api/v2/firewall/aliases',
873+
method: 'GET',
874+
data: ['sort_by' => 'descr', 'sort_flags' => 'SORT_NATURAL'],
875+
headers: ['Content-Type' => 'application/json'],
876+
username: 'admin',
877+
password: 'pfsense',
878+
validate_certs: false,
879+
);
880+
$resp = json_decode($json_resp);
881+
$this->assert_equals($resp->data[0]->name, 'alias_b');
882+
$this->assert_equals($resp->data[1]->name, 'alias_d');
883+
$this->assert_equals($resp->data[2]->name, 'alias_a');
884+
$this->assert_equals($resp->data[3]->name, 'alias_c');
885+
}
813886
}

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Tests/APICoreModelSetTestCase.inc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,4 +435,36 @@ class APICoreModelSetTestCase extends RESTAPI\Core\TestCase {
435435
$host_9,
436436
]);
437437
}
438+
439+
/**
440+
* Checks that ModelSets can be sorted using the requested sort flags
441+
*/
442+
public function test_sort_flags(): void {
443+
# Create test firewall aliases with descriptions set to IP addresses
444+
$alias_a = new FirewallAlias(name: 'alias_a', type: 'host', descr: '127.0.0.22');
445+
$alias_b = new FirewallAlias(name: 'alias_b', type: 'host', descr: '127.0.0.200');
446+
$alias_c = new FirewallAlias(name: 'alias_c', type: 'host', descr: '127.0.0.100');
447+
$alias_a->create();
448+
$alias_b->create();
449+
$alias_c->create();
450+
451+
# Read the firewall aliases and use regular sorting to ensure the order is correct
452+
# This should sort the IP address out of logical order since SORT_REGULAR does not sort numerically
453+
$modelset = FirewallAlias::read_all()->sort(['descr'], order: SORT_ASC, flags: SORT_REGULAR);
454+
$this->assert_equals($modelset->model_objects[0]->name->value, 'alias_c');
455+
$this->assert_equals($modelset->model_objects[1]->name->value, 'alias_b');
456+
$this->assert_equals($modelset->model_objects[2]->name->value, 'alias_a');
457+
458+
# Read the firewall aliases and use natural sorting to ensure the order is correct
459+
# This should sort the IP address in numerical order since SORT_NATURAL does sort numerically
460+
$modelset = FirewallAlias::read_all()->sort(['descr'], order: SORT_ASC, flags: SORT_NATURAL);
461+
$this->assert_equals($modelset->model_objects[0]->name->value, 'alias_a');
462+
$this->assert_equals($modelset->model_objects[1]->name->value, 'alias_c');
463+
$this->assert_equals($modelset->model_objects[2]->name->value, 'alias_b');
464+
465+
# Delete the firewall aliases
466+
$alias_a->delete();
467+
$alias_b->delete();
468+
$alias_c->delete();
469+
}
438470
}

0 commit comments

Comments
 (0)