From a9e3f27c7a6b11c128201feaf1c291b56f0d3fd6 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 5 Jun 2024 14:19:06 +0100 Subject: [PATCH] Bug 37018: Unit tests --- t/Koha/REST/Plugin/Query.t | 51 +++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/t/Koha/REST/Plugin/Query.t b/t/Koha/REST/Plugin/Query.t index 2f816f4f147..9602e7da742 100755 --- a/t/Koha/REST/Plugin/Query.t +++ b/t/Koha/REST/Plugin/Query.t @@ -206,6 +206,16 @@ get '/build_query' => sub { }; }; +get '/dbic_validate_operators' => sub { + my ( $c, $args ) = @_; + + my $query = $c->req->json->{q}; + + $c->dbic_validate_operators({ filtered_params => $query }); + + $c->render( json => { filtered_params => $query }, status => 200 ); +}; + get '/stash_embed' => sub { my $c = shift; @@ -337,7 +347,7 @@ sub to_model { # The tests -use Test::More tests => 8; +use Test::More tests => 9; use Test::Mojo; subtest 'extract_reserved_params() tests' => sub { @@ -634,3 +644,42 @@ subtest 'dbic_extended_attributes_join() tests' => sub { ] ); }; + +subtest 'dbic_validate_operators' => sub { + plan tests => 2; + + my $t = Test::Mojo->new; + + # Valid queries + my $q = {}; + $t->get_ok( '/dbic_validate_operators' => json => { q => $q } )->status_is(200); + + $q = []; + $t->get_ok( '/dbic_validate_operators' => json => { q => $q } )->status_is(200); + + $q = { + firstname => 'Bilbo', + lastname => 'Baggins' + }; + $t->get_ok( '/dbic_validate_operators' => json => { q => $q } )->status_is(200); + + $q = { + firstname => undef, + lastname => 'Baggins' + }; + $t->get_ok( '/dbic_validate_operators' => json => { q => $q } )->status_is(200); + + $q = { lastname => [ 'Gaggins', 'Gamgee' ] }; + $t->get_ok( '/dbic_validate_operators' => json => { q => $q } )->status_is(200); + + $q = { lastname => { '!=' => [ 'Gaggins', 'Gamgee' ] } }; + $t->get_ok( '/dbic_validate_operators' => json => { q => $q } )->status_is(200); + + $q = { status => { '!=', 'completed', -not_like => 'pending%' }}; + $t->get_ok( '/dbic_validate_operators' => json => { q => $q } )->status_is(200); + + # Invalid queries + $q = [ { "-and" => [ [ { "biblio_id" => { "like(sleep(1/100000))or" => "%a%" } } ] ] } ]; + $t->get_ok( '/dbic_validate_operators' => json => { q => $q } )->status_is(400); + +}; -- 2.45.1