diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7e7dbb6259f6a0be41715d88d548f717e04e0645..849d5dcebb90dc23a7a09a74abf3e56c2312c485 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -2,7 +2,7 @@ image: docker:19.03.12 variables: DOCKER_TLS_CERTDIR: "/certs" - IMAGE_VER: 2.2.0 + IMAGE_VER: 2.3.0 services: - docker:19.03.12-dind diff --git a/lib/CF.pm b/lib/CF.pm index 2e7a0e171e5206662b4e7e8601142816b97e858f..1fd445f629639f816a0ee557988856cdab6f88e3 100644 --- a/lib/CF.pm +++ b/lib/CF.pm @@ -26,6 +26,11 @@ sub startup { ->username($cfg->{db_username}) ->password($cfg->{db_password}) ; + if ($cfg->{test}) { + $pg->search_path(['test']); + $pg->db->query('drop schema if exists test cascade'); + $pg->db->query('create schema test'); + } $pg->migrations->from_dir($self->home . '/sql'); $pg->migrations->migrate(); $self->helper( pg => sub { return $pg; } ); @@ -34,8 +39,13 @@ sub startup { my $schema = CF::Schema->connect({ dsn => $cfg->{db_dsn}, user => $cfg->{db_username}, - password => $cfg->{db_password} + password => $cfg->{db_password}, }); + + if ( $cfg->{test} ) { + $schema->storage->dbh->do("set search_path to test") ; + } + $self->helper( schema => sub { return $schema; } ); # Redis diff --git a/t/events.t b/t/events.t new file mode 100644 index 0000000000000000000000000000000000000000..9a3bde1c35b50721f228b94b6fcc9a4b02fba5dd --- /dev/null +++ b/t/events.t @@ -0,0 +1,81 @@ +use Mojo::Base -strict; + +use Test::More; +use Test::Mojo; + +use constant SECRET => 'TEST'; +use constant USERS => { + nobody => { + "sub" => "00000000-0000-0000-0000-nobody", + "roles" => [], + "name" => "Testováci Nikdo", + "groups" => [], + "preferred_username" => "test.nobody", + }, + organizer => { + "sub" => "00000000-0000-0000-0000-organizer", + "roles" => [ "organizer" ], + "name" => "Testováci Pořadatel", + "groups" => [ "cen:f", ], + "preferred_username" => "test.organizer", + }, +}; + +my $t = Test::Mojo->new('CF', { + test => 1, + test_auth_jwt_secret => SECRET, +}); + +# pokus najit uzivatele +$t->get_ok('/api/sso/subjects?search=andrej.ramaseuski')->status_is(200) + ->json_is('/0/value' => 'andrej.ramaseuski') +; + +# seznam udalosti pro neuautorizovaneho +$t->get_ok('/api/events')->status_is(200); + +# Pokus pridat event neautorizovanym uzivatelem +$t->post_ok('/api/events')->status_is(401); + +# Pokus pridat event neopravnenym uzivatelem +$t->post_ok('/api/events', { + Authorization => auth_header(USERS->{nobody}) +})->status_is(401); + +# Pokus pridat event bez dat +$t->post_ok('/api/events' => { + Authorization => auth_header(USERS->{organizer}) +})->status_is(400); + +# pokus pridat event bez validnich dat +$t->post_ok('/api/events' => { + Authorization => auth_header(USERS->{organizer}) +}, +json => { +})->status_is(400); + +# pokus pridat event +$t->post_ok('/api/events' => { + Authorization => auth_header(USERS->{organizer}) +}, +json => { + "type" => 1, + "is_opened" => 1, + "start" => "2021-07-18 10:00", + "finish" => "2021-07-18 18:00", + "name" => "Schůze KS Pardubický kraj", + "description" => "TEST 8968714465", + "organizer" => "PKS Pardubický kraj" +})->status_is(201); + +$t->app->pg->db->query('drop schema if exists test cascade'); +done_testing(); + +sub auth_header { + my $claims = shift; + return 'Bearer ' . Mojo::JWT->new( + claims => $claims, secret => SECRET + )->encode(); +} + +